Powered by Ajaxy
Digg StumbleUpon LinkedIn YouTube Flickr Facebook Twitter RSS Reset

Python: Sorting a tuple

T = ('cc', 'aa', 'dd', 'bb')
tmpListe = list(T)
tmpListe.sort()
print tmpListe
T=tuple(tmpListe)
print T

Python: Key action: Right key

from Tkinter import *
class KeyEvents(Frame):
    def __init__(self, parent=None):
        Frame.__init__( self )
        self.master.title("Keyboa

Python: communicate small messages between machines with Socket Datagrams

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Accept UDP datagrams, on the given port, from any sender
s.bind(("", 8081))
pr

Python:unzip files

import zipfile
doczip = zipfile.ZipFile('c:/dcc.zip')
doczip.printdir()
for fileName in doczip.fileNamelist():
    print '%s: %r' % (fileName, docz

Python: Return the arctangent of x in radians

import math
print math.atan(-1./-1.)
print math.atan2(-1., -1.)

Python: check if a file has any lines that end with digits

__author__="sako"
__date__ ="$6 oct. 2012 13:05:19$"
import re
def fileEndWith(filename):
    end_with_digit = re.compile(r'd$', re.MULTILINE)

Python: Read POP3 E-mail messages

import poplib
import getpass
serveur = poplib.POP3('mail.example.com')
#User Authentication
serveur.user(getpass.getuser())
serveur.pass_(getpass.g

Python: How to create Power function

def power(x, n):
    if n == 0:
        return 1
    else:
        return x * power(x, n-1)
print power(2,8) # 256

Python: Insert records into MySQL database

import MySQLdb 
myBD = MySQLdb.connect(host="127.0.0.1", 
                       port=3306,user='myusernae',passwd='12345', db="Sales"))
bd_cursor

Python: Replace slice items

li = ['java', 'cpp', 2012, "1234", "python"]
print li                  #['java', 'cpp', 2012, '1234', 'python']
li[2:-1] = ["C#", "PHP"]
print li

Python: How to Add a button to frame

from Tkinter import *                   
class SimpleFrame(Frame):
    def __init__(self, parent=None):
        Frame.__init__(self, parent)

Python: Reading from a File

#open the file
text_file = open("fac.txt", "w")
# read from it
print text_file .read()
#close it
text_file .close()

Python: How to Connect to MySQL Server

import MySQLdb           
myBD = MySQLdb.connect(host="127.0.0.1", 
                       port=3306,user='username',passwd='12345')
bd_cursor = my

Python: How to connect to SQLite Datebase using SQLAlchemy

import sqlalchemy
engine = sqlalchemy.create_engine('sqlite:///Employee.db')
# Execute a query
for res in engine.execute('SELECT * FROM Clients'):

Python: Splitting files Paths

import os, time
path= 'c:/test.txt'
print os.path.basename(path)       #test.txt
print os.path.dirname(path)        # C:
print os.path.split(p