analitics

Pages

Wednesday, May 19, 2010

The beauty of Python: Simple functions - part 1

Validation of a condition and return result in 'Yes' or 'No'.

>>> def valid(x,y):
...     return ('Yes' if x==y else 'No') 
... 
>>> valid(2,3)
'No'
>>> valid(2,2)
'Yes'


Some usefull functions from string module .

>>> import string 
>>> var_template_string=string.Template(" The $subj $vb $something")
>>> var_template_string.substitute(subj="PC", vb="working", something="now")
' The PC working now'
>>> some_string_dictionary={'subj':'car', 'vb':'is', 'something':'blue'}
>>> var_template_string.substitute(some_string_dictionary)
' The car is blue'
>>> some_string_dictionary


Some example with re module and html tag

>>> import re
>>> t='<p>'
>>> some_tag = re.compile(r'<(.*?)(\s|>)')
>>> m = some_tag.match(t)
>>> print m
<_sre.SRE_Match object at 0xb7f79da0>
>>> dir(m)
['__copy__', '__deepcopy__', 'end', 'expand', 'group', 'groupdict', 'groups', 'span', 'start']
>>> print m.start()
0
>>> print m.groups()
('p', '>')
>>> print m.group()
<p>
>>> print m.span()
(0, 3)

The re module has many usefull functions.
This is just some examples to show the simplicity of python language.

Monday, March 1, 2010

Python and OpenGL - first steps

First you need to install python .
Second you need to install pyopengl module.
This is easy if we use some linux distributions.
On Fedora we can use command yum.
On Debian and Ubuntu we can use apt-get.
At last we need some basicaly examples.
We can get some examples from:PyOpenGL-Demo
But this is not all we will need more.
To go on next step we will need to read OpenGL specifications.
Try to use search on internet to see more about OpenGL.