python标准库 #69

string

  • string.capwords(word)

给word首个字母大写

s = 'The quick brown fox jumped over the lazy dog'
print string.capwords(s)

>>>The Quick Brown Fox Jumped Over The Lazy Dog
  • string.maketrans() s.translate()

文本替换跟replace差不多

s = 'The quick brown fox jumped over the lazy dog'
leet = string.maketrans('abcdefghijk','12312322222')   
print s.translate(leet)

>>> T22 qu233 2rown 2ox 2ump21 ov2r t22 l1zy 1o2
  • string.Template() [ t.substitute() t.safe_substitute() ]

替换的收获分为正常替换和安全替换,正常替换的收获如果参数不够会抛出异常, KeyError ,安全替换则是根据元文档直接写入

value = {'var': 'foo'}
t = string.Template('''
	variable             :$var
	Escape               :$$
	variable in text     :${var}iable
	''')
print t.substitute(value)

>>>	variable             :foo
	Escape               :$
	variable in text     :fooiable

textwrap

用于文本梅花

  • textwrap.fill(words,with=50)限定每行有50个字符
  • textwrap.dedent(words)去除文本的每行缩进
sample_text = '''
	the textwrap module can be used to format text for  output in 
	situations where pretty-printing is desired. it offers programmatic functionalite
	similar to the paragraph wrapping or filling features found in many text editors.
'''
print textwrap.fill(sample_text,width=79)
print textwrap.dedent(sample_text)


>>>       the textwrap module can be used to format text for  output in
situations where pretty-printing is desired. it offers programmatic
functionalite         similar to the paragraph wrapping or filling features
found in many text editors.

the textwrap module can be used to format text for  output in 
situations where pretty-printing is desired. it offers programmatic functionalite
similar to the paragraph wrapping or filling features found in many text editors.

re

正则表达式 regex

pattern = 'this'
text = 'does this text mathch the pattern'
match = re.search(pattern, text)
print match.start(), match.end()
print match.string, '  ', match.re.pattern
regexs = [re.compile(p) for p in ['this', 'that']]
for regex in regexs:
    print regex.pattern,
    if regex.search(text):
        print 'match'
    else:
        print 'no match'

text = 'ababbabba'
pattern = 'ab(a)'
for match in re.findall(pattern, text):
    print match   # match 是一个字符串

for match in re.finditer(pattern, text):
    print match.start(), match.end()   # 返回的是match 迭代器

print re.search(pattern, text).group()   #
print re.search(pattern, text).group(0)  # 表示获取的整个文本
print re.search(pattern, text).group(1)  # 表示第几个分组

>>>
5 9
does this text mathch the pattern    this
this match
that no match
a
0 3
aba
aba
a

difflib

text1 = '''Lorem ipsum dolor sit amet.consecteture
dddddd
dfdasfd'''

text2 = '''Lorem iisum dolor sit amet
dddddd
dfdasfd'''


d = difflib.Differ()
diff = d.compare(text1.splitlines(), text2.splitlines())
print '\n'.join(diff)
print '------'
print '\n'.join(list(diff))

>>>
- Lorem ipsum dolor sit amet.consecteture
?        ^                  -------------

+ Lorem iisum dolor sit amet
?        ^

  dddddd
  dfdasfd
------
[]

collections

Counter, defaultdict, deque, namedtuple, OrderedDict # 有序的字典
text = 'faasfasdfsad'

a = Counter(text)
a['h'] = 1
print a
a.update('fdafds')
print a
print list(a.elements())  # 生成所有的元素
print a.most_common(2)   # 返回最常用的2个元素

# 支持算术操作  + - * /
b = Counter('fdasf')
print a + b


def calld():
    return 'nnnn'


d = defaultdict(calld, foo='bar')
print d
print d['dd']
print d
# 双端队列是线程安全的
d = deque('fdafds')
print d
print len(d)
print d[0]


candle = deque(xrange(10))
candle.rotate(2)   # 用于对联的旋转。即将后面的移动到前面


def burn(direction, nextSource):
    while True:
        try:
            next = nextSource()
        except Exception as e:
            break
        else:
            print '%8s:%s' % (direction, next)
            # time.sleep(0.1)
    print "%8s done " % direction
    return


left = Thread(target=burn, args=('left', candle.popleft))
right = Thread(target=burn, args=('right', candle.pop))

left.start()
right.start()

# 第一个参数名为新类的名字,另外一个可以是字符串或者以可迭代的组
# Person = namedtuple('Person', ('name', 'age', 'gender'))
# 避免关键字冲突和命名重复like class
Person = namedtuple('Person', 'name age gender')
print Person.__doc__

bob = Person(name='bobd', age=30, gender='male')
print bob[0], bob.name

>>>
Counter({'a': 4, 's': 3, 'f': 3, 'd': 2, 'h': 1})
Counter({'a': 5, 'f': 5, 's': 4, 'd': 4, 'h': 1})
['a', 'a', 'a', 'a', 'a', 'h', 's', 's', 's', 's', 'd', 'd', 'd', 'd', 'f', 'f', 'f', 'f', 'f']
[('a', 5), ('f', 5)]
Counter({'f': 7, 'a': 6, 's': 5, 'd': 5, 'h': 1})
defaultdict(<function calld at 0x00000000029B1EB8>, {'foo': 'bar'})
nnnn
defaultdict(<function calld at 0x00000000029B1EB8>, {'dd': 'nnnn', 'foo': 'bar'})
deque(['f', 'd', 'a', 'f', 'd', 's'])
6
f
    left:8
    left:9
    left:0
    left:1
    left:2   right:7

    left:3   right:6

    left:4   right:5

    left done Person(name, age, gender)   right done 


bobd bobd

bisect

l = []
for i in range(1, 15):
    r = random.randint(1, 100)
    position = bisect.bisect(l, r)   # 查看当前的数字会在列表排在那个位置,但实际并没有真正的擦入
    bisect.insort(l, r)
    print '%3d  %3d ' % (r, position), l
    bisect.insort

# new pos content
# ---------------
#  81    0  [81]
#  92    1  [81, 92]
#  80    0  [80, 81, 92]
#  17    0  [17, 80, 81, 92]
#  13    0  [13, 17, 80, 81, 92]
#   6    0  [6, 13, 17, 80, 81, 92]
#  41    3  [6, 13, 17, 41, 80, 81, 92]
#  66    4  [6, 13, 17, 41, 66, 80, 81, 92]
#  59    4  [6, 13, 17, 41, 59, 66, 80, 81, 92]
#   8    1  [6, 8, 13, 17, 41, 59, 66, 80, 81, 92]
#  25    4  [6, 8, 13, 17, 25, 41, 59, 66, 80, 81, 92]
#   9    2  [6, 8, 9, 13, 17, 25, 41, 59, 66, 80, 81, 92]
#  83   11  [6, 8, 9, 13, 17, 25, 41, 59, 66, 80, 81, 83, 92]
#  99   13  [6, 8, 9, 13, 17, 25, 41, 59, 66, 80, 81, 83, 92, 99]

queue


q = Queue.Queue()    # FIFO 先进先出模式

for i in range(5):
    q.put(i)

while not q.empty():
    print q.get(),
print

q = Queue.LifoQueue()  # LIFO 后进入先出模式

for i in range(5):
    q.put(i)

while not q.empty():
    print q.get(),
print


class Job(object):
    def __init__(self, priority, decription):
        self.priority = priority
        self.decription = decription
        print 'new job:', decription

    def __cmp__(self, other):
        return cmp(self.priority, other.priority)


q = Queue.PriorityQueue()  # 优先级别队列,级别越低先出来

q.put(Job(3, 'Mid-level job'))
q.put(Job(10, 'Low-level job'))
q.put(Job(1, 'high-level job'))


def process_job(q):
    while True:
        next_job = q.get()
        print 'processs job', next_job.decription
        q.task_done()  # 表示在完成一个工作后,向队列发送已经完成的信号,如果没了,那么就停止进程阻塞


workers = [threading.Thread(target=process_job, args=[q, ]),
           threading.Thread(target=process_job, args=[q, ])]

for w in workers:
    w.setDaemon(True)  #  主线程产生子进程,如果主线程结束,会把子线程给杀死,如果没有设置,那么子线程还存在
    w.start()

q.join()   # 意味这等到队列为空执行别的操作   貌似是将当前主现成阻塞等待task_done全部完成没有队列位置才放开线程

time.sleep(2)
print '11111'
 
# 0 1 2 3 4
# 4 3 2 1 0
# new job: Mid-level job
# new job: Low-level job
# new job: high-level job
# processs job high-level job
# processs job Mid-level job
# processs job Low-level job
# 11111
# [Finished in 2.1s]

struct

values = (1, 'ab', 2.7)
s = struct.Struct('I 2s f')
packed_data = s.pack(*values)

print values
print s.format
print s.size
print binascii.hexlify(packed_data)


data = s.unpack(binascii.unhexlify('0100000061620000cdcc2c40'))
print data


# (1, 'ab', 2.7)
# I 2s f
# 12
# 0100000061620000cdcc2c40
# (1, 'ab', 2.700000047683716)

weakref

对象弱引用,如果对象被销毁内存急需内存的收获可以删除该弱引用的对象

class ExpensiveObject(object):

    def __del__(self):
        print 'delete %s' % self


def callback(reference):
    print 'callback', reference


obj = ExpensiveObject()
r = weakref.ref(obj, callback)   # 可以增加回调函数用于告知对象销毁

print obj
print r
print r()
print '111'
del obj
print '222'
print r()

functools

def myfunc(a, b=2):
    "doc for myfunc"
    print 'call', (a, b)
    return


def show_detail(name, f):
    print name
    print f

    try:
        print f.__name__
    except:
        print 'no name'
    print f.__doc__


def show_details(name, f, is_partial=False):
    print name
    print f
    if not is_partial:
        print f.__name__
    if is_partial:
        print f.func
        print f.args
        print f.keywords
    return


myfunc('a', 3)
show_details('myfunc', myfunc)
p1 = functools.partial(myfunc, b=4)    # partial 对象没有  __name__ 和 __doc__对象
print '=================================='                          # 可以使用functools.update_wrapper()将原函数的内容复制进去
show_details('partial with name default ', p1, True)
p1('passs')
p1('overrid', b=5)

print '=================================='
show_detail('myfunc', myfunc)
p1 = functools.partial(myfunc, b=4)
show_detail('raw wrapper', p1)
print '********************************'
print functools.WRAPPER_ASSIGNMENTS
print functools.WRAPPER_UPDATES
print '********************************'
functools.update_wrapper(p1, myfunc)
show_detail('raw wrapper', p1)

print '============='


def fun(f):
    @functools.wraps(f)
    def decorst(a='1', b='2'):
        print f.__name__
        print a, b
        return f
    return decorst


@fun
def dec(a, b):
    myfunc(a, b)


dec(2, 3)


# call ('a', 3)
# myfunc
# <function myfunc at 0x0000000002A989E8>
# myfunc
# ==================================
# partial with name default 
# <functools.partial object at 0x0000000002AA4278>
# <function myfunc at 0x0000000002A989E8>
# ()
# {'b': 4}
# call ('passs', 4)
# call ('overrid', 5)
# ==================================
# myfunc
# <function myfunc at 0x0000000002A989E8>
# myfunc
# doc for myfunc
# raw wrapper
# <functools.partial object at 0x0000000002AA42C8>
# no name
# partial(func, *args, **keywords) - new function with partial application
#     of the given arguments and keywords.

# ********************************
# ('__module__', '__name__', '__doc__')
# ('__dict__',)
# ********************************
# raw wrapper
# <functools.partial object at 0x0000000002AA42C8>
# myfunc
# doc for myfunc
# =============
# dec
# 2 3

from itertools import *


for i in chain([1, 2, 3], ['a', 'b', 'c']):   # 处理多个序列而不必构造一个大的列表
    print i,
print

for i in izip([1, 2, 3], ['a', 'b', 'c']):    # 返回迭代器而不是返回列表
    print i,
print

for i in islice(count(), 5):  # 返回一个迭代器
    print i,
print

for i in islice(count(), 5, 10):
    print i,
print

for i in islice(count(), 0, 100, 10):
    print i,
print

r = islice(count(), 5)
i1, i2 = tee(r)

print list(i1)
print list(i2)


# 1 2 3 a b c
# (1, 'a') (2, 'b') (3, 'c')
# 0 1 2 3 4
# 5 6 7 8 9
# 0 10 20 30 40 50 60 70 80 90
# [0, 1, 2, 3, 4]
# [0, 1, 2, 3, 4]

contextlib

# 传统编写上下文
class WithContext(object):
    def __init__(self, context):
        print context

    def dosomething(self):
        print 'do something'

    def __del__(self):
        print 'del'


class Context(object):
    def __init__(self):
        print 'context __init__'

    def dosomething(self):
        print 'do something'

    def __enter__(self):
        print 'enter'
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        print 'context exit'


with Context() as c:
    print c.dosomething()

print '============='

@contextlib.contextmanager
def make_context():
    print 'enterign'
    try:
        yield {}
    except RuntimeError , err:
        print err
    finally:
        print 'exiting'

with make_context() as value:
    print value


# context __init__
# enter
# do something
# None
# context exit
# =============
# enterign
# {}
# exiting

import time

print time.time()    # 1477448222.86
later = time.time() + 15
print time.ctime(later)   # 格式化时间   # Wed Oct 26 10:17:17 2016

for i in range(5):
    h = hashlib.sha1()
    print time.ctime(), time.time(), time.clock()

# Wed Oct 26 10:17:03 2016 1477448223.3 0.438315857686
# Wed Oct 26 10:17:03 2016 1477448223.74 0.874749253736
# Wed Oct 26 10:17:04 2016 1477448224.17 1.31385075364
# Wed Oct 26 10:17:04 2016 1477448224.61 1.74753680793
# Wed Oct 26 10:17:05 2016 1477448225.05 2.18923808103

print type(time.time())   # 得到的是一个浮点数  # <type 'float'>
print type(time.ctime())  # 得到的是一个字符串  # <type 'str'>

def show_struct(s):
    print s.tm_year
    print s.tm_mon
    print s.tm_mday
    print s.tm_hour
    print s.tm_min
    print s.tm_sec
    print s.tm_wday
    print s.tm_yday
    print s.tm_isdst

print time.asctime()    # Wed Oct 26 10:31:38 2016
print time.ctime()      # Wed Oct 26 10:31:38 2016
print time.gmtime()     # utc time.struct_time(tm_year=2016, tm_mon=10, tm_mday=26, tm_hour=2, tm_min=20, tm_sec=18, tm_wday=2, tm_yday=300, tm_isdst=0)
print time.localtime()  # 本地时间 time.struct_time(tm_year=2016, tm_mon=10, tm_mday=26, tm_hour=10, tm_min=20, tm_sec=18, tm_wday=2, tm_yday=300, tm_isdst=0)
print time.mktime(time.localtime())  # 1477448503.0
print '-----------------------------'

a = time.ctime()
print a   # Wed Oct 26 10:38:55 2016
print time.strptime(a)  # time.struct_time(tm_year=2016, tm_mon=10, tm_mday=26, tm_hour=10, tm_min=38, tm_sec=55, tm_wday=2, tm_yday=300, tm_isdst=-1)
print time.strftime("%a %b %d %H:%M:%S %Y", time.strptime(a))   # Wed Oct 26 10:38:55 2016
print time.strptime('Wed Oct 26 10:31:38 2016')                 # time.struct_time(tm_year=2016, tm_mon=10, tm_mday=26, tm_hour=10, tm_min=31, tm_sec=38, tm_wday=2, tm_yday=300, tm_isdst=-1)

print time.mktime(time.strptime('Wed Oct 26 10:31:38 2016') )  #1477449098.0

datetime

t = datetime.time(1, 2, 3)
print t   # 01:02:03   # <type 'datetime.time'>
print t.hour   # 1

today = datetime.date.today()
print today  # 2016-10-26
print today.ctime()   # Wed Oct 26 00:00:00 2016
print today.timetuple()  # time.struct_time(tm_year=2016, tm_mon=10, tm_mday=26, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=300, tm_isdst=-1)
print today.replace(year=10)   # 0010-10-26  可进行相关替换
print datetime.datetime.today().ctime()  # Wed Oct 26 10:53:02 2016
print datetime.datetime.today()
print datetime.timedelta(hours=1)   # 参数后面需要加s
print datetime.timedelta(hours=1).total_seconds()  # 3600.0
print datetime.datetime.strptime('Wed Oct 26 00:00:00 2016', "%a %b %d %H:%M:%S %Y")# 2016-10-26 00:00:00

random

for i in xrange(5):
    print random.random(),  # 生成 0,1之间的随机数
print

for i in xrange(5):
    print random.uniform(1, 100),  # 生成指定数值区间的数目
print


random.seed(10)
print "Random number with seed 10 : ", random.random()
print "Random number with seed 10 : ", random.random()
# 生成同一个随机数
random.seed(10)
print "Random number with seed 10 : ", random.random()
print "Random number with seed 10 : ", random.random()
# 生成同一个随机数
random.seed(10)
print "Random number with seed 10 : ", random.random()

state = random.getstate()  # 获得随机状态
random.setstate(state)  # 设置随机状态

random.seed()
print random.randint(-5, 5)
print random.choice(['12312', 'adfadsf'])

list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
random.shuffle(list)  #  给一个序列随机洗牌   改变原来的序列
print list

list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
slice = random.sample(list, 5)  # 从list中随机获取5个元素,作为一个片断返回   不改变原来的序列
print slice

print random.Random()      # 生成一个随机对象,可以拥有random的所有函数使用的使用方法
print random.SystemRandom(12).random()

# 0.328842693603 0.348691790654 0.41475079426 0.130556890269 0.851020432626
# 12.911633401 1.63257413485 48.8556069495 78.7545414688 9.48420727765
# Random number with seed 10 :  0.57140259469
# Random number with seed 10 :  0.428889054675
# Random number with seed 10 :  0.57140259469
# Random number with seed 10 :  0.428889054675
# Random number with seed 10 :  0.57140259469
# -1
# 12312
# [9, 5, 6, 1, 10, 2, 7, 8, 3, 4]
# [2, 1, 10, 6, 5]
# <random.Random object at 0x00000000030181E8>
# 0.655502074592

os.path

for path in ['/one/two/three',
             '/one/two/three/',
             '/',
             '.',
             '']:
    print path, ':', os.path.split(path)

# /one/two/three : ('/one/two', 'three')
# /one/two/three/ : ('/one/two/three', '')
# / : ('/', '')
# . : ('', '.')
#  : ('', '')
for path in ['/one/two/three',
             '/one/two/three/',
             '/',
             '.',
             '']:
    print path, ':', os.path.basename(path)
# /one/two/three : three
# /one/two/three/ :
# / :
# . : .
#  :
for path in ['/one/two/three',
             '/one/two/three/',
             '/',
             '.',
             '']:
    print path, ':', os.path.dirname(path)
# /one/two/three : /one/two
# /one/two/three/ : /one/two/three
# / : /
# . :
#  :
for path in ['filename.txt',
             'filename',
             '/path/to/filename.txt',
             '/',
             '',
             'my-archive.tar.gz',
             'no-extension.',
             ]:
    print path, ':', os.path.splitext(path)

# filename.txt : ('filename', '.txt')
# filename : ('filename', '')
# /path/to/filename.txt : ('/path/to/filename', '.txt')
# / : ('/', '')
#  : ('', '')
# my-archive.tar.gz : ('my-archive.tar', '.gz')
# no-extension. : ('no-extension', '.')


print os.path.commonprefix(['/one/two/three/four',
                            '/one/two/threefold',
                            '/one/two/three'])
# /one/two/three 返回一个公共的文件前缀

path = ['one', 'two', 'three']
print os.path.join(*path)   # one\two\three  需要用*号最为前缀。 接受2个参数 (x , *x)
print os.path.join('/aaa', '/ffd', '/fdf')   # /fdf 会丢掉前面的 以os.sep开头的字母
print os.path.expanduser('~\ddd')  # C:\Users\zhoulei/ddd   将~转换成当前用户名
print os.path.normpath('one//two//three')  # one\two\three 清除多余分隔符
print os.path.abspath('.')  # C:\learnplace\python_standard_lib  返回绝对路径
print time.ctime(os.path.getatime(__file__))  # Wed Oct 26 14:14:35 2016  访问时间
print time.ctime(os.path.getmtime(__file__))  # Wed Oct 26 15:29:48 2016 修改时间
print time.ctime(os.path.getctime(__file__))  # Wed Oct 26 14:14:34 2016 创建时间
print os.path.getsize(__file__)  # 2534  获取当前文件的数据量
print __file__  # C:\learnplace\python_standard_lib\test22.py
print os.path.isabs(__file__)  # True  绝对路径
print os.path.isdir(__file__)  # Flase  是一个文件夹
print os.path.isfile(__file__)  # True  是一个文件
print os.path.islink(__file__)  # FALSE
print os.path.ismount(__file__)  # FALSE
print os.path.exists(__file__)  # True
print os.path.lexists(__file__)  # True   link exist
print os.extsep  # .
print os.sep  # /
print os.pardir  # ..
print os.curdir  # .


# os.path.walk()

import glob

# *星号 表示匹配0个或者多个
for name in glob.glob('c:\learnplace\*\*'):
    print name

print '========================='
# c:\learnplace\python_standard_lib\test1.py
# c:\learnplace\python_standard_lib\test10.py
# c:\learnplace\python_standard_lib\test11.py
# c:\learnplace\python_standard_lib\test12.py
# c:\learnplace\python_webdevelop_testdriver\geckodriver.log
# c:\learnplace\python_webdevelop_testdriver\superlists

# ? 匹配单个字
for name in glob.glob(r'c:\learnplace\*\test?.py'):
    print name


print '========================='
# c:\learnplace\python_standard_lib\test1.py
# c:\learnplace\python_standard_lib\test2.py
# c:\learnplace\python_standard_lib\test3.py
# c:\learnplace\python_standard_lib\test4.py
# c:\learnplace\python_standard_lib\test5.py

# [a-z] [1-9] 字符区间

for name in glob.glob(r'c:\learnplace\*\test[1-2].py'):
    print name
# c:\learnplace\python_standard_lib\test1.py
# c:\learnplace\python_standard_lib\test2.py

tempfile

linecache

print linecache.getline("c:\learnplace\python_webdevelop_testdriver\geckodriver.log", 522)  # 超过后会返回一个空串
#  
print __file__
# C:\learnplace\python_standard_lib\test24.py
print linecache.__file__
# C:\Python27\lib\linecache.pyc
module_line = linecache.getline(linecache.__file__[:-1], 13)
print module_line

print tempfile.gettempdir()  # c:\users\zhoulei\appdata\local\temp
print tempfile.gettempprefix()  # tmp

shutil

# 复制文件
shutil.copyfile(src, dst)  # src ,dst 均为文件
shutil.copy(src, dst)  # 复制文件 src为源文件,dst为目标文件夹
shutil.copy2(src, dst)  # 强复制。复制访问信息,创建时间等  src为源文件,dst为目标文件夹
shutil.copymode(src, dst)  # linux中的权限也复制  src ,dst 均文文件   0444
shutil.copystat  # 复制文件的关联权限和时间

shutil.copytree()  # 复制一个目录下的所有文件到一个不存在的文件夹下
shutil.rmtree()  # 删除一个目录树
shutil.move()  # 跟linux 到mv一样
posted @ 2016-10-24 10:07  紫金龙骑  阅读(217)  评论(0编辑  收藏  举报
UV PV