analitics

Pages

Saturday, August 1, 2009

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 !

Saturday, July 4, 2009

resize images to create thumbnails

First you have many images.
If you want to create thumbnail for each one this is python script:

import glob 
import PIL
from PIL import Image
for infile in glob.glob("*.jpg"):
 img = Image.open(infile)
 dim_percent=(100/float(img.size[0]))
 dim_size=int((float(img.size[1])*float(dim_percent)))
 img = img.resize((100,dim_size),PIL.Image.ANTIALIAS)
 if infile[0:2] != "trumb_":
  img.save("trumb_" + infile, "JPEG")

Sunday, June 21, 2009

PyGTK and GdkPixbuf

What is GdkPixbuf?
* An object holding information about images in memory.
Constructor
GdkPixbuf (GdkColorspace colorspace, bool has_alpha, int bits_per_sample, int width, int height);

Example:

>>> import pygtk
>>> import pygtk
>>> pixbuf = gtk.gdk.Pixbuf( gtk.gdk.COLORSPACE_RGB, False,8, 200, 100)
>>> pixbuf.save('/home/net/cc/a.jpg','jpeg', {'quality':'100'})

The output is :

PyGTK 2.15.2 - unstable

As you know, PyGTK is a set of Python wrappers.
Now PyGTK easily create programs with a graphical user interface using the Python programming language.
The new PyGTK version 2.15.2 and is on PyGTK 2.15.2 source.
From http://pygtk.org
"What's new since 2.15.0?
- (Add HSV support to gtk.gdk.Color objects)
- Add floating-point support to gtk.gdk.Color (Paul)
- Retire hand-written ChangeLog; autocreate from Git history (Paul)
- Fix conditional in docs/Makefile.am (Frederic Peters)
- Document that gtk.gdk.GC coordinates are not related to allocation (Paul)
- Make pygtk_boxed_unref_shared() also handle Py_None (Paul)
- Make gtk.MenuItem.set_submenu accept None (Paul)
- Don't run 'fixxref.py' if documentation is not built (Björn Lindqvist)
- Apply libtool 2.2 compatibility patch (Gian)
- Plug reference leak on main signal watch source (Paul)
- Add extra warning against accidental misuse of tree model columns (Paul)
- Wrap gtk.Border attributes and constructor (Mariano Suárez-Alvarez)
- Make gtk.gdk.Event.time accept 'long' in assignments (Paul)
- Wrap gtk.RcStyle attributes (Paul)

PyGTK requires GTK+ >= 2.8.0 and Python >= 2.3.5 to build."

Tuesday, March 24, 2009

Python - random passwords

This program generates passwords on the size of 10 characters.
By using a function called "random_password" that creates a password of size 10 characters.
This function called "write_file" creates a file with automatic redial function "random_password. It contains a table of 10 X 10 random passwords.

from random import *
import string
chars = string.ascii_letters + string.digits
def random_password():
a = "".join(choice(chars) for x in range(randint(10, 10)))
b = a + ' | '
return b
def write_file():
f = open('random_pass.txt', 'wr+')
for c in range(10):
rand=''
for r in range(10):
rand = rand + random_password()
randpass=rand + random_password() + '\n'
f.write(str(randpass))
f.close()
write_file()

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)