analitics

Pages

Friday, February 27, 2009

Python and xml

Python is an excellent choice for writing programs that process XML data.
XML's is a simplified subset of the Standard Generalized Markup Language (SGML).
He set of tools helps developers in creating web pages.
This is one simple example which I use with "conky":

from xml.dom import minidom as dom
import urllib2
def fetchPage(url):
a = urllib2.urlopen(url)
return ''.join(a.readlines())
def extract(page):
a = dom.parseString(page)
item2 = a.getElementsByTagName('SendingDate')[0].firstChild.wholeText
print "DATA ",item2
item = a.getElementsByTagName('Cube')
for i in item:
if i.hasChildNodes() == True:
e = i.getElementsByTagName('Rate')[8].firstChild.wholeText
d = i.getElementsByTagName('Rate')[18].firstChild.wholeText
print "EURO ",e
print "DOLAR ",d

if __name__=='__main__':
page = fetchPage("http://www.bnro.ro/nbrfxrates.xml")
extract(page)

First step ...and some examples

So the first step to use python is how to load module.
This is simple one:

import sys
import math
from math import sin
dir(math)

['__doc__', '__file__', '__name__', 'acos', 'asin', 'atan', 'atan2', 'ceil', 'cos', 'cosh', 'degrees', 'e', 'exp', 'fabs', 'floor', 'fmod', 'frexp', 'hypot', 'ldexp', 'log', 'log10', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh']

Next is two examples with calendar module.

import calendar
time_cal=calendar.TextCalendar(calendar.FRIDAY)
print time_cal.formatmonth(2009,02)
February 2009
Fr Sa Su Mo Tu We Th
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28

If i use this into web page this is the solution:

import calendar
html_cal=calendar.HTMLCalendar(calendar.MONDAY)
print html_cal.formatmonth(2009,02)










February 2009
MonTueWedThuFriSatSun






1
2345678
9101112131415
16171819202122
232425262728

Thursday, February 19, 2009

About Python


Python is like a knife to a developer.


Python is a remarkably powerful dynamic programming language.
Python runs on Windows, Linux/Unix, Mac OS X, OS/2, Amiga, Palm Handhelds, and Nokia mobile phones. Python has also been ported to the Java and .NET virtual machines.


Python is distributed under an OSI-approved open source license that makes it free to use, even for commercial products.