analitics

Pages

Saturday, August 8, 2009

Permutation of chars with python

It is a simple example of using python language to permutation of chars.
It uses a list generator and "yield":

#!/usr/bin/env python
def permut(lst):
remove = lambda lst0, index: lst0[:index] + lst0[index+1:]
if lst:
for index, x in enumerate(lst):
for y in permut(remove(lst, index)):
yield (x,)+y
print y

else: yield ()
lst='catalin'
for p in permut(['c','a','t','a','l','i','n']):
print p

Example can be modified so output will be a file.

Saturday, August 1, 2009

Google and Python Stuff

I want share this scripts.
Is just two scripts and i think is beautifull.
First is a youtube script :
import gdata.youtube
import gdata.youtube.service

client_yt = gdata.youtube.service.YouTubeService()
query = gdata.youtube.service.YouTubeVideoQuery()
query.vs = "%s" % ('News')
feed = client_yt.YouTubeQuery(query)

for entry in feed.entry:
print entry.title.text
for link in entry.link:
if link.rel == 'alternate':
print link.href

Second is a picassa script :
import gdata.photos
import gdata.photos.service
import urllib
p_client = gdata.photos.service.PhotosService()

query_parameters = map(urllib.quote, ['Sexy','Bucharest']);
feed = p_client.GetFeed("/data/feed/api/all?q=%s%s&max-results=10" % ('Sexy','Girl'))

for entry in feed.entry:
print entry.title.text
for link in entry.link:
if link.rel == 'alternate':
print link.href

I hope you like this little scripts.

Sqlite and Python

These script explore the Python Sqlite modules available for database administration.
Sometime you need to use extraction, processing, storage and presentation of your data.
This is a short example :
#!/usr/bin/python

import sqlite3

def select_db():
print "Select database"

co=sqlite3.connect("cata2.db")
cursor=co.execute("CREATE TABLE report (nr INT,user VARCHAR(20),descriere VARCHAR(100));")
cursor=co.execute("INSERT INTO report (nr,user,descriere) VALUES (1,'root','root administration');")
cursor=co.execute("SELECT * FROM report;")

print cursor.fetchall()

This python script will be create cata.db file on your folder.
The table of database is 'report' and add next values "1,'root','root administration'".
If you want create new database , use :
co=sqlite3.connect("new_database.db")
cursor=co.execute("CREATE TABLE tabela (some_value_integer INT,some_value_chars VARCHAR(20));")

If you want show it , use this :
cursor=co.execute("SELECT * FROM new_database;")

Thank you !