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

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: Using Classes

class Person:
    def _ _init_ _(self, name, age, salary=0, job=None):
        self.name = name
        self.age  = age
        self.salary  = sala

Python: Encrypt message using sha Module

import sha
hash = sha.new()
hash.update("java, perl, python, and C#")
print repr(hash.digest())
print hash.hexdigest()

Python: Replace Substrings

import re
text = "Learn Java 7.0 and Python"
# literal replace
print re.sub("Java", "Perl", text)
# collapse all non-letter sequences to a single d

Python: Using temporary file

import tempfile
tempfile = tempfile.mktemp()
# get temp file path
print "tempfile", "=>", tempfile
file = open(tempfile, "w+b")
# write to temporar

Python: Replace Substrings

import re 
text = "you're at home here.."
# literal replace
print re.sub("'re", "'ren'", text)
# replace with regular expression
print re.sub("[^W]

Python: copie an entire directory tree

import shutil
import os
# move all text files from current directory to Backup directory
for file in os.listdir("."):
    if os.path.splitext(file)

Python: Using string Methods

__author__="sako"
__date__ ="$6 oct. 2003 12:04:24$"
text = "Learning Python OOP"
print "upper", "=>", text.upper() # to upper
print "lower", "=>",

Python-grouped_words: How to enclose every word in braces

__author__="sako"
__date__ ="$6 oct. 2012 12:24:54$"
import re
def grouped_words():
    grouped_words_word = re.compile('(w+)')
    astring="Welcom

Python: Replace Substrings

import re
text = "Learn Java 7.0 and Python"
# literal replace
print re.sub("Java", "Perl", text)
# collapse all non-letter sequences to a single d

Python: Find the variable Reference Count

import sys
variable = 22012
print sys.getrefcount(0)
print sys.getrefcount(variable)
print sys.getrefcount(None)

Python: Find Built-in Modules using sys module

import sys
def find_method(module):
    print module, "=>",
    if module in sys.builtin_module_names:
        print ""
    else:
        module =

Python: Find Built-in Modules using sys module

import sys
def find_method(module):
    print module, "=>",
    if module in sys.builtin_module_names:
        print ""
    else:
        module =

Python: Loop Over a Text File

import sys
import fileinput
for line in fileinput.input("c:/test.txt"):
    sys.stdout.write("=> ")
    sys.stdout.write(line)

Python-File: print all lines containing digits

import re 
digs = re.compile(r'd+')
for line in open('test.txt'):
    if digs.search(line): 
	print line,