forked from ghosert/VimProject
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbriefStandardLibrary.py
More file actions
executable file
·204 lines (166 loc) · 7.62 KB
/
Copy pathbriefStandardLibrary.py
File metadata and controls
executable file
·204 lines (166 loc) · 7.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
""" This is a brief standard library test case for python """
if __name__ == "__main__":
# Operating System Interface
import os
# os.system('command') to execute the native command, return 0 means success while 1 mean failure.
os.system('dir jiaweizhang111')
print os.getcwd()
os.chdir('.')
print os.getcwd()
# According to daily file and dictionary management task, the shutil a higher level interface that is eaiser to use.
import shutil
shutil.copyfile('data', 'data1')
shutil.move('data1', 'data')
# glob module for making lists from directory wildcard searches.
import glob
print glob.glob('*.py')
# standard error, standard out, standard in and terminate program
import sys
sys.stderr.write('stderr')
print
sys.stdout.write('stdout')
print
# print sys.stdin.readline() # input a line from keyboard.
# print sys.stdin.readlines() # input mutipule lines from keyboard, ctrl-z to break.
# sys.exit(0) # exit program with successful status.
# String pattern matching
import re
print re.findall('\\bf[a-z]*', 'which afoot foot or hand fell fastest')
# equals to because of \b here, afoot will not be shown.
print re.findall(r'\bf[a-z]*', 'which afoot foot or hand fell fastest')
print re.sub(r'(\b[a-z]+) \1', r'\1', 'cat in the the hat')
print 'one too three'.replace('too', 'two')
# mathematics
import math
print math.sin(math.pi / 2)
print math.log(1024, 2)
import random
print random.choice(['apple', 'pear', 'banana'])
print random.sample(range(100), 10)
print random.random()
print random.randrange(6)
# internet access
import urllib2
for line in urllib2.urlopen('http://www.baidu.com'):
if 'style=height:60px' in line:
print line
# comment the codes below since it needs a mailserver running on localhost, 'sudo apt-get install sendmail' if you want to run a mail server locally.
# import smtplib
# server = smtplib.SMTP('localhost')
# server.sendmail(...)
# server.quit()
# download file.
# import urllib
# urllib.urlretrieve('http://haoetv.com/haoting/wma08/318.wma', 'C:\\318.wma')
# Date time
from datetime import date
now = date.today()
print now.strftime("%m-%d-%y. %d %b %Y is a %A on the %d day of %B.") # same to: import time && print time.strftime('%d%b%y')
birthday = date(1980, 11, 14)
age = now - birthday
print age.days
# archiving and compression data, other module including zlib, gzip, bz2, zipfile, tarfile
import zlib
s = 'The datas will be archived and compressed.'
print 'The length of data before compression: {0}'.format(len(s))
t = zlib.compress(s)
print 'The length of data after compression: {0}'.format(len(t))
s = zlib.decompress(t)
print 'The length of data after decompression: {0}'.format(len(s))
# performance measurement, other module to test performace including profile and pstats
import timeit
print timeit.Timer('t = a; a = b; b = t', 'a = 1; b = 2').timeit()
print timeit.Timer('a, b = b, a', 'a = 1; b = 2').timeit()
# unit test, doctest and unittest module for unit test.
def average(values):
"""Computing average value.
>>> print average([1, 2, 3])
2.0
"""
return sum(values, 0.0) / len(values)
import doctest
print doctest.testmod() # automatically validate the embedded tests, the case in doc string above can be cut and pasted from interpreter.
import unittest
class TestStatisticalFunctions(unittest.TestCase):
def test_average(self):
self.assertEqual(average([20, 30, 70]), 40.0)
self.assertEqual(round(average([1, 5, 7]), 1), 4.3)
self.assertRaises(ZeroDivisionError, average, [])
self.assertRaises(TypeError, average, 20, 30, 70)
# unittest.main() # Calling from the command line invokes all tests
# output formatting
print '\noutput formatting test'
import repr
print repr.repr(set('helloworldworldhelloyouareabigmanforallofus'))
import pprint
t = [[[['black', 'cyan'], 'white', ['green', 'red']], [['magenta', 'yellow'], 'blue']]]
pprint.pprint(t, width=30)
import textwrap
doc = """The wrap() method is just like fill() except that it returns
a list of strings instead of one big string with newlines to separate
the wrapped lines."""
print doc
print textwrap.fill(doc, width=40)
import locale
# locale.setlocale(locale.LC_ALL, 'English_United States.1252')
conv = locale.localeconv() # get a mapping of conventions
x = 1234567.8
print locale.format("%d", x, grouping=True)
# print locale.format("%s%.*f", (conv['currency_symbol'], conv['frac_digits'], x), grouping=True)
# templating
from string import Template
template = Template('${village}folk send $$10 to $cause.')
print template.substitute(village='Nottingham', cause='the ditch fund') # the parameter of template.substitue() support both "dict object" and the form of "(xxx = yyy, jjj = kkk)"
t = Template('Return the $item to $owner.')
d = dict(item='unladen swallow') # equals to d = {'item':'unladen swallow'}
# print t.substitute(d) # throw a exception saying: KeyError: 'owner'
print t.safe_substitute(d) # because of the line above, so we use safe_substitute instead of substitue. This will remain $owner in the string instead of a KeyError exception.
# use the new delimiter instead of $ above
class NewDelimiter(Template):
delimiter = '%'
print NewDelimiter('%jiawei is a good man.').substitute(jiawei='JIAWEI')
print
# multi-thread
import threading, zipfile
class AsyncZip(threading.Thread):
def __init__(self, infile, outfile):
threading.Thread.__init__(self)
self.infile = infile
self.outfile = outfile
def run(self):
f = zipfile.ZipFile(self.outfile, 'w', zipfile.ZIP_DEFLATED)
f.write(self.infile)
f.close()
print 'Finished background zip of: ', self.infile
background = AsyncZip('data', 'myarchive.zip')
background.start()
print 'The main program continues to run in foreground.'
background.join() # Wait for the background task to finish
print 'Main program waited until background was done.'
print
# loggin system
import logging
logging.debug('Debugging information')
logging.info('Informational message')
logging.warning('Warning:config file %s not found', 'server.conf')
logging.error('Error occurred')
logging.critical('Critical error -- shutting down')
# by default, debug and info above is suppressed, and all the logging message output is sent to standard error.
# add item to sorted list
import bisect
li = [1, 2, 3, 4, 5, 6]
bisect.insort(li, 0)
print li
print
# heappq make sure the smallest value put into the zero value.
from heapq import heapify, heappop, heappush
data = [1, 3, 5, 7, 9, 2, 4, 6, 8, 0]
print 'list before re-range: {0}'.format(data)
heapify(data) # rearrange the list into heap order
print 'list after re-range: {0}'.format(data)
heappush(data, -5) # add a new entry
print 'list after add new value -5: {0}'.format(data)
print 'fetch the three smallest entries: {0}'.format([heappop(data) for i in range(3)])
def fff():
return 'i'
print [fff() for i in range(3)]