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
Testing
Some examples use inspect model, this model is userful for testing. It has more function than here.
m = hashlib.sha1() m.update('The quick brown fox jumps over the lazy dog') print m.hexdigest()
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.
#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.
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. """
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())