1. use Glob() to find files

Many python functions have a long and descriptly name, but maybe you dont know the namespace of glob() functions unless you understand it.
It’s like a more powerful version of the listdir() function. it can search files use pattern matching.

1
2
3
4
import glob

files = glob.glob('*.py')
print files

you can also search files by many file types:

1
2
3
4
5
6
7
8
import itertools as it
import glob

def multiple_file_types(*patterns):
return it.chain.from_iterable(glob.glob(pattern) for pattern in patterns)

for filename in multiple_file_types('*.txt','*.py')
print filename

If you want get the real path of every file. you can called the realpath() function in return value:

1
2
3
4
5
6
7
8
9
10
import itertools as it
import glob
import os

def multiple_file_types(*patterns):
return it.chain.from_iterable(glob.glob(pattern) for pattern in patterns)

for filename in multiple_file_types('*.txt','*.py'):
realpath = os.path.realpath(filename)
print realpath
  1. Testing

Some examples use inspect model, this model is userful for testing. It has more function than here.

Some useful example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import logging
import inpect

logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)-8s %(lineno)-4d %(message)s', datefmt='%m-%d %H:%M',)

logging.debug("A debug message")
logging.info('Some information')
logging.warming('A shot across the bow')

def test():
frame,filename,line_number,function_name,lines,index = inspect.getouterframes(inspect.currentframe())[1]
print(frame,filename,line_number,function_name,lines,index)

test()
  1. Create a unique ID
1
2
3
improt uuid
result = uuid.uuid()
print result

but the result have many character are some with each running.
update:

1
2
3
4
5
6
7
8
9
import hmac, hashlib
key = '1'
data = 'a'

print hmac.new(key, data, hashlib.sha256).hexdigest()

m = hashlib.sha1()
m.update('The quick brown fox jumps over the lazy dog')
print m.hexdigest()
  1. Serialization

Do you ever need to store a complex variable in a database or text file?
You don’t need to think of an unusual way to format an array or object as a string, since Python already provides this functionality.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import pickle

variable = ['hello', 22, [1, 'two'], 'dog']

#serizlize content
file = open('serial.txt', 'w')
serialized_obj = pickle.dumps(variable)
file.write(serialized_obj)
file.close()

#unserizlize to content
target = open('serial.txt', 'r')
myObj = pickle.load(target)

print myObj

This is a native method, can also be use JSON to encode and decode.

1
2
3
4
5
6
7
8
9
import json

variable = ['hello', 22, [1, 'two'], 'dog']

#encoding
encode = json.dumps(variable)

#deconding
decoded = json.loads(encode)

This is more compact, and is compatible with javascript or other language.
For complex objects, however, some information may be lost.

  1. Compression character

Python can compres long character dont refer to any documents.

1
2
3
4
5
6
7
8
9
10
11
12
13
import zlib

string = """ther sections do not refer to any specific file but give step by step instructions on how to access features and what some of the feature highlights are
Illustration 3-1: At the outset, a transaction involves shipments of one commodity, but without any commercial explanation, the goods being sold are described as a different commodity in later documentation.
"""

print "Original Size: (0)".format(len(string))

compressed = zlib.compress(string)
print "Compressed Size: (0)".format(len(compressed))

decompressed = zlib.decompress(compressed)
print "Decompressed Size: (0)".format(len(decompressed))
  1. Register ShutDown function

A model name is atexit, it can be running some coding after script.
For example if you want measure some benchmark datasets after running script, like running time.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import atexit
import time
import math

def microtime(get_as_float=False):
if get_as_float:
return time.time()
alse:
return "%f %d"%math.modf(time.time())

start_time = microtime(False)
atexit.registar(start_time)

def shutdowm():
global start_time
print "Execution took: {0} seconds.".format(start_time)

atexit.register(shutdown)

whatever reason cause the script shutdown. the atexit.register() function all with running.