analitics

Pages

Monday, March 14, 2011

IronPython released on 12 and updated on 13 march.

What is it?
IronPython is an implementation of the Python programming language and is running under .NET and Silverlight.

How does it work?
When you run a .NET program written in a language such as C# or VB.NET, your code gets compiled to Intermediate Language (IL) code.
IronPython contains the code and classes for IronPython itself, including the IronPython interpreter. When you run the IronPython interpreter, you are running the IL code for IronPython itself.
More simplistic, the classes you create in your Python code are created dynamically at runtime.

What's New?
This release numerous bug fixes, also includes a IronPython Tools for Visual Studio, support for extension methods and more...
You can see more on official site.

Saturday, March 12, 2011

Tutorials from PyCon 2011

PyCon 2011 is held from March 9th through the 17th, 2011 in Atlanta, Georgia.
It is the annual Python community conference.
More on this page:Pycon 2011 tutorials

Wednesday, February 23, 2011

Just a simple python weather script.

Sometimes we need simple solutions. An example is displaying data on a computer screen using conky. under Linux.
Another example is the display of data without using the browser.
Whether you use Windows or Linux python scripts come to help. Here's a simple example written in python that can display weather data.

import urllib
from xml.dom import minidom

wurl = 'http://xml.weather.yahoo.com/forecastrss?p=%s'
wser = 'http://xml.weather.yahoo.com/ns/rss/1.0'

def weather_for_zip(zip_code):
    url = wurl % zip_code +'&u=c'
    dom = minidom.parse(urllib.urlopen(url))
    forecasts = []
    for node in dom.getElementsByTagNameNS(wser, 'forecast'):
        forecasts.append({
            'date': node.getAttribute('date'),
            'low': node.getAttribute('low'),
            'high': node.getAttribute('high'),
            'condition': node.getAttribute('text')
        })
    ycondition = dom.getElementsByTagNameNS(wser, 'condition')[0]
    return {
        'current_condition': ycondition.getAttribute('text'),
        'current_temp': ycondition.getAttribute('temp'),
        'forecasts': forecasts ,
        'title': dom.getElementsByTagName('title')[0].firstChild.data
    }
def main():
    a=weather_for_zip("ROXX0003")
    print '=================================='
    print '|',a['title'],'|'
    print '=================================='
    print '|current condition=',a['current_condition']
    print '|current temp     =',a['current_temp']
    print '=================================='
    print '|  today     =',a['forecasts'][0]['date']
    print '|  hight     =',a['forecasts'][0]['high']
    print '|  low       =',a['forecasts'][0]['low']
    print '|  condition =',a['forecasts'][0]['condition']
    print '=================================='
    print '|  tomorrow  =',a['forecasts'][1]['date']
    print '|  hight     =',a['forecasts'][1]['high']
    print '|  low       =',a['forecasts'][1]['low']
    print '|  condition =',a['forecasts'][1]['condition']
    print '=================================='

main()
Here is the result of script execution:

>>> 
==================================
| Yahoo! Weather - Bucharest, RO |
==================================
|current condition= Light Snow
|current temp     = -3
==================================
|  today     = 23 Feb 2011
|  hight     = 0
|  low       = -5
|  condition = Light Snow
==================================
|  tomorrow  = 24 Feb 2011
|  hight     = 0
|  low       = -4
|  condition = Mostly Cloudy
==================================
>>> 

Thursday, February 3, 2011

Read feed from sites.

Is a simple example for reading some feed.
I use two functions , first read url and secondary extract data.
This is the code source:

from xml.dom import minidom as dom
import urllib

def fetchPage(url):
    a = urllib.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')[10].firstChild.wholeText
            d = i.getElementsByTagName('Rate')[26].firstChild.wholeText
            print "EURO  ",e
            print "DOLAR ",d

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

    extract(page)
Result is :

DATA  2011-02-03
EURO   4.2609
DOLAR  3.0921
This is all...

Python - calendar

Two simple example about calendar python module.
Show calendar 04 - 2011 :

>>> import calendar
>>> tc = calendar.TextCalendar(calendar.SUNDAY)
>>> print tc.formatmonth(2011,04)
     April 2011
Su Mo Tu We Th Fr Sa
                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 29 30

>>>
Show calendar in HTML format :
>>> import calendar
>>> hc = calendar.HTMLCalendar(calendar.SUNDAY)
>>> print hc.formatmonth(2011,04)
;
The result is a HTML table with the calendar.
This is all .

Tuesday, August 3, 2010

Random module - another example

Random module is a module used in everyday programming.
Even if it is not used in the final software, he helps us to test various types of random data.
We present you a simple example - play with cards.
We chose a list of cards of one color for not having a large number of elements.
We exemplify the use of random module by analogy with a game of cards.
That it means: shuffle cards , select just one from cards and a choice selection of a defined number of cards.
Below you see the code used as an example.

>>> import random
>>> choice=random.choice
>>> shuffle=random.shuffle
>>> for i in range (9):
...     print choice(cards)
... 
Q
K
10
Q
2
2
10
K
6
>>> shuffle(cards)
>>> cards
[4, 7, 10, 'J', 3, 'Q', 6, 2, 'K', 9, 'A', 8, 5]
>>> sample=random.sample
>>> sample(cards,5)
[8, 'J', 2, 'Q', 10]
>>> sample(cards,5)
['A', 9, 4, 'K', 'Q']
>>> 
The random module docs