Showing posts with label Others. Show all posts
Showing posts with label Others. Show all posts

Friday, July 1, 2011

Creating an Excel file or a spreadsheet using Python's xlwt (xlutils) module

A spreadsheet is used to simulates a paper accounting worksheet. It displays multiple cells usually in a two-dimensional matrix or grid consisting of rows and columns. We have so many reporting software s that will simply create spreadsheets. But the problem is, these reporting software s may not support on all systems and also take lot of CPU time. Then arise an idea to use some method to create spreadsheet from my python code. Then I found about the module xlutils in python.

XLUTILS module contain packages for reading from(xlrd) and writing to(xlwt) excel files. Since I only need the package to write to excel file I'm installing the xlwt package.

Installation
We can install any python package to our repository using pip, pip is an easy way to install python packages.
To install pip
omal@zbeanz:~$sudo apt-get install python-pip
After installing pip you can install python packages by
omal@zbeanz:~$sudo pip install xlwt
This will install xlwt package to python modules

 Now in our python code,
to import the package functionality to the our program.
We can define the title, header, number, date, and normal style using xlwt.easyxf(). For example title style is given below
We can define as many styles we want. We can also define how the border should appear. For example,
After defining the styles we can now define the workbook. Workbook is actually what we view in our spreadsheet. To define workbook,
A spreadsheet may contain more than one sheet, so to add sheets,
Now we can start adding data to the sheet we have created.  To write data to the 'sheet1' use the reference ws and ws.write(row, column, 'Value or text',style). For example:
To change the width and height of a cell,
 We can add complete data to the spreadsheet by writing and changing the height and width according to our need. After completing this we can save the workbook to a buffer. So First define the buffer and then save the workbook to the  buffer. This doesn't complete the process.
Now we have to encode the data in the buffer and write it to a file say 'test.xls'. To encode the data in the buffer import base64 module. Now open the file test.xls to view our excel file we have created.


I have not mentioned the last few steps (from encoding) so that you can try it out.

Thursday, November 11, 2010

How to develop a Game using python?

          Everyone like to play games. If we start to play a computer games sometimes we don't quit the game until we finish it. Game can be developed in  any language. I am using python for developing a game. Usual python language can be used for developing textual games. For developing graphical games we need a special module in python  called pygame.

       As the first phase of developing the graphical game we have to make a game plan and then import the pygame module and necessary modules needed for our game. 
 




Then set up the pygame using pygame.init(). If we want to use sleep function or clock then we have to initiate them also.


Next step is to set up our output window. For this we are using pygame.display.set_mode().  We can also provide a caption for our window using pygame.display.set_caption()


Now set up the color variables. Each color can be created by a tuple with three elements. The three elements are red, blue and green each starting from 0 to 255. To create a black color we have to provide tuple as (0, 0, 0) and for white (255, 255, 255). And thus we can create any color.


If we want to add a text, then first define the font. Then provide the text. TextRect will help to select a rectangle space where our text will be printed. 


We can fill a background color using

Then we can define all the geometrical figures needed for our game.


Five arguments for drawing a line. First is window surface, second color, third coordinate (x1, y1), fourth coordinate (x2, y2) and finally the width of the line. Like this we can also draw circles, rectangles, ellipse etc.

The keyword blit is used to print the text inside a rectangle which we had already created. pygame.display.update() function is needed to print every operations that have been done. Without this function nothing will be printed. This is because when we create a new object it take a small delay in drawing each object to the screen. So the update function helps to print at the end of definitions. If our game has a game loop - an infinite loop, then this update function will be inside this loop. A user can provide request using pygame.event.get(). On each loop each event is processed and produce a output according to the event.




Saturday, October 16, 2010

AVL TREE -The Self Balancing Binary Tree

        AVL Tree is a self balancing binary tree. When an insertion or deletion occurs, it re-balances the tree by checking the height of two child subtrees. Balancing factor of a node is equal to height of the left subtree minus height of the right subtree. If the balancing factor is equal to -1, 0 or 1 then the tree is considered balanced else the tree is rebalanced by calling one or more tree rotations. A simple example for AVL tree is given below.

          Insert a node with data 3 (node 3), which is inserted to the tree as a simple binary tree. Then insert node 2 which is inserted to the left of node 3. Here the function balance() is called  and height of the subtrees are checked. Since the height is not > 1 or < -1 tree rotations are not done here. next the node 1 is inserted. Now the tree becomes unbalanced and rotations are called.

        Now insert node 4 and 5. The balancing factor gives a difference of -2 that is right side the node is not balanced. So again a single rotation is applied.

        Inserting node 6 makes the balanced tree unbalanced. Here node 2 is unbalanced. So a single rotation is called which makes node 4 as the child of parent of node 2, if any element present in the left of node 4, then it is passed to the right child of node 2 and node 2 is passed to the left of node 4. Next node 7 is inserted. At this point the root is balanced. But when we go through each node checking whether it is balanced or not we found that the node 5 is not balanced and single rotation is applied here. Then inserting node 16 then node 15 which makes the tree again unbalanced.
 
        Now the tree is unbalanced and to become balanced we have to apply double rotation. First rotating child and grandchild of the unbalanced node 7 and then rotating the node 7and the new child (node 15) which makes the tree again balanced.
        Insert node 14 which comes to the right of node 7 and the tree becomes unbalanced at node 6. Again a double rotation is called, first rotation is with the child(node 7) and grandchild(node 15) of node 6. Then rotation is with node 6 and with the new child(node 7)

You can find my codes: http://github.com/omalbastin/avl_tree

Monday, September 20, 2010

Ruby- Basics

RUBY

Ruby is a programming language which is somewhat same as Python more complex than python. We can write a program and save it with an extension of 'filename.rb' and can run this program using 'ruby filename.rb'.

To write to the screen puts, print and printf are used.
PUTS- Write on the screen with a return at the end.
PRINT-Write on the screen without a return at the end.
PRINTF- I cant understand the main difference for printf from puts and print. i think printf is mainly used to print a floating point variables, strings etc.

To Read from the console we use gets
STDIN.gets-Standard input to the console is taken by the gets.
For example,
---------------------------------------
puts "What is your name?"
$name = STDIN.gets
puts "Hi "+$name
---------------------------------------

Functions

  We can define a method using def,
--------------------------------
def method_name(values)
    statements
end
-------------------------------
A method must end with 'end'. Indention has no importance here. end shows the ending of that method.

Uppercase- To make a string to upper case we can use upcase.
"hello".upcase => HELLO

To Return a value from a method 'return' is used. If return is not provided Ruby will return the contents of the last expression.
We can assign optional argument values and extra arguments to a method. Extra arguments are gathered into the last variable if preceded with a "*"
for example
--------------------------------
def test(a=1,b=2,*c)
   puts "#{a},#{b}"
   c.each{|x| print "#{x} "}
end
test 3,4,5,6,7,8,9
--------------------------------
which produces
-------------------------------
3,4
5,6,7,8,9
-------------------------------
If # is provided inside double quotes, #{variable} will evaluate the variable.

Sunday, August 29, 2010

Python- Programing Language

 Python is a object oriented programing language in which the underlying codes are in C. It also supports functional programming. It is dynamic typed language, i.e., it doesn't require any type declarations. It is considerably slow compared to C.

When programming using python don't compare it with C or CPP. here any thing can be assigned to a variable.

Eg:
a=1       then a integer value 1 is assigned to a.
a=1,      then a tuple (1,) is assigned to a.
a=[1,2]  then a list [1,2] is assigned to a.
a='hello' then a string 'hello' is assigned to a.

List in python
 A list is a collecton of any type. There is a function called range(), which have three parameters- start, stop and step.It will give a list according to these parameters. for eg:
>>>range(10)
[0,1,2,3,4,5,6,7,8,9]
Here it take the value start=0, stop=10 and step=1

>>>range(4,10,2)
[4,6,8]
Here start=4, stop=10, step=2

Hash or associative array/directory
It will have  keys and a value corresponding to each key. eg:
>>>a={'key1':2,'key2':3,'key3',5}
by calling a.keys() will provide a list of keys and a.values() will provide a list of values
>>>a.keys()
['key1','key2','key3']
>>>a.values()
[2,3,5]
>>> a.items()
[('key1',2),('key2',3),('key3',5)]

Functions in python

Some Basic things and commends In Linux

loopback (lo): when two or more programs want to communicate each other, then loopback is used. It use inet address: 127.0.0.1

ctrl+d : When a string or a file is reading, to show the end of the file ctrl+d is used

Ctrl+c: Te terminate a process or to kill a process.

ctrl+z: It i used to temporarily suspend a process. To continue the process fg <no. of the process> is used. fg stands for foreground the suspended process.

Create a file in the terminal(vim must be installed) using vim <filename.extension>. To change the settings of vim editor,
To set the column no. esc :set nu
To delete the first n lines esc :1,nd
To undo esc :u
To copy the first n lines:esc :1,ny