python技巧

带任意参数的函数

  1 def function(arg1="",arg2=""):                                             
  2     print "arg1:{0}".format(arg1)
  3     print "arg2:{0}".format(arg2)
  4 
  5 function("Hello", "World")
  6 #prints args1:Hello
  7 #prints args2:World
  8 
  9 function()
 10 #prints args1:
 11 #prints args2:

元组实现:

  1 def foo(*args):#just use "*" to collect all remaining argument into tuple  
  2     numargs = len(args)
  3     print "Number of arguments:{0}".format(numargs)
  4     for i, x in enumerate(args):
  5         print "Argument {0} is :{1}".format(i,x)
  6 
  7 foo()
  8 #Number of arguments:0
  9 
 10 foo("hello")
 11 #Number of arguments:1
 12 #Argument 0 is :hello
 13 
 14 foo("hello", "world", "again")
 15 #Number of arguments:3
 16 #Argument 0 is :hello
 17 #Argument 1 is :world
 18 #Argument 2 is :again

使用Glob()查找文件

类似于增强版的 listdir()函数:

  1 import glob                                                                
  2 
  3 #get all files
  4 files = glob.glob('*.py')
  5 print files
  6 
  7 #Output
  8 #['1.py','2.py','3.py']

查多个文件类型:

  1 import intertools as it,glob                                               
  2 
  3 def multiple_file_types(*patterns):
  4     return it.chain.from_iterable(glob.glob(pattern) for pattern in patterns)
  5 
  6 for filename in multiple_file_types("*.txt", "*.py"):
  7     print filename
 #output
#1.py
#2.py
#test.txt
如果想的到每个文件的绝对路径,可以在返回值上调用realpath()函数:

  1 import intertools as it,glob,os                                               
  2 
  3 def multiple_file_types(*patterns):
  4     return it.chain.from_iterable(glob.glob(pattern) for pattern in patterns)
  5
  6 for filename in multiple_file_types("*.txt", "*.py"):
  7    realpath=os.path.realpath(filename)
  8    print realpath
 #output
#C:\xxx\1.py
#C:\xxx\2.py
#C:\xxx\test.txt

调试

inspect模块用于调试

  1 import logging,inspect
  2 logging.basicConfig(level=logging.INFO,
  3         format='%(asctime)s %(levelname)-8s %(filename)s:%(lineno)-4d:         %(message)s',
  4         datefmt='%m-%d %H:%M',
  5         )
  6 logging.debug('A debug message')
  7 logging.info('Some information')
  8 logging.warning('A shot across the bow')
  9                                                                            
 10 def test():
 11     frame,filename,line_number,function_name,lines,index=\
 12             inspect.getouterframes(inspect.currentframe())[1]
 13     print (frame,filename,line_number,function_name,lines,index)
 14 
 15 test()
#output
03-14 15:00 INFO     5.py:7   :Some information
03-14 15:00 WARNING  5.py:8   :A shot across the bow
(<frame object at 0x7fd0e56117b0>, '5.py', 15, '<module>', ['test()\n'], 0)

生成唯一ID

生成唯一字符串使用md5()函数可以达到目的,其实有一个uuid()函数用于它

  1 import uuid                                                                
  2 
  3 result = uuid.uuid1()
  4 print result

#output --->various attempts
0a8ff230-e9b3-11e5-82db-b8ac6f2e8f72
0b8e2fc6-e9b3-11e5-a391-b8ac6f2e8f72
0cc3db0c-e9b3-11e5-a11a-b8ac6f2e8f72
即使字符串是唯一的,但他们后面的几个字符看起来相似,这是因为生成的字符串与电脑的MAC地址相联系。
为了减少重复的情况,可以用如下的函数。

  1 import hmac,hashlib                                                        
  2 
  3 key='1'
  4 data='a'
  5 print hmac.new(key,data,hashlib.sha256).hexdigest()
  6 
  7 m=hashlib.sha1()
  8 m.update("The quick brown fox jumps over the lazy dog")
  9 print m.hexdigest()

#output
 c6e693d0b35805080632bc2469e1154a8d1072a86557778c27a01329630f8917
2fd4e1c67a2d28fced849ee1bb76e7391b93eb12
序列化

字符串存储在数据库或者文本中,格式化的存入,取出

  1 import pickle
  2 
  3 variable=['hello',42,[1,'two'],'apple']
  4 
  5 #searialize conten
  6 file=open('serial.txt','w')
  7 serialized_obj=pickle.dumps(variable)
  8 file.write(serialized_obj)
  9 file.close()
 10 
 11 #unserialize to produce original content
 12 target=open('serial.txt', 'r')                                             
 13 myObj=pickle.load(target)
 14 
 15 print serialized_obj
 16 print myObj

#output
 (lp0
S'hello'<pre name="code" class="python"> p1
aI42
a(lp2
I1
aS'two'
p3
aaS'apple'
p4
a.
['hello', 42, [1, 'two'], 'apple']

 这是个原生的Python序列化方法。也可以用JSON实现 

  1 import json
  2 
  3 variable=['hello',42,[1,'two'],'apple']
  4 print "Original {0}-{1}".format(variable,type(variable))
  5 
  6 #encoding
  7 encode=json.dumps(variable)
  8 print "Encode {0}-{1}".format(encode,type(encode))
  9 
 10 #deccoding
 11 decoded=json.loads(encode)                                                 
 12 print "Decoded {0}-{1}".format(decoded,type(decoded))

#ouput
Original ['hello', 42, [1, 'two'], 'apple']-<type 'list'>
Encode ["hello", 42, [1, "two"], "apple"]-<type 'str'>
Decoded [u'hello', 42, [1, u'two'], u'apple']-<type 'list'> 
这样更紧凑,而且最重要的是这样与JavaScript和许多其他语言兼容。然而对于复杂的对象其中一些信息可能丢失

压缩字符

python可以压缩字符,不涉及任何档案文件

  1 #!/usr/bin/python
  2 import zlib
  3 string="qwdsaiiiiiiiiiiii\
  4 iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiuewqyrpiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii\
  5 iiiifdssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssvccccccccccccccccccccccccccccccccccccce\
  6 qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\
  7 wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww\
  8 eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"
  9 
 10 print "Original Size:{0}".format(len(string))                                                                                            
 11 
 12 compressed=zlib.compress(string)
 13 print "Compressed Size:{0}".format(len(compressed))
 14 
 15 decompressed=zlib.decompress(compressed)
 16 print "Decompressed Size:{0}".format(len(decompressed))

#output
Original Size:534
Compressed Size:44
Decompressed Size:534 
注册Shutdown函数

有个atexit的模块,它可以让你在脚本运行完后立马执行一些代码。

假如想在脚本执行结束时测量一些基准数据,比如运行了多长时间:

  1 import atexit,time,math                                                    
  2 def microtime(get_as_float = False):
  3     if get_as_float:
  4         return time.time()
  5     else:
  6         return '%f%d'%math.modf(time.time())
  7 
  8 start_time=microtime(False)
  9 atexit.register(start_time)
 10 
 11 def shutdown():
 12     global start_time
 13     print "Execution took:{0} seconds".format(start_time)
 14 
 15 atexit.register(shutdown)

#output
Execution took:0.7384021457940357 seconds
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
  File "/usr/lib/python2.7/atexit.py", line 24, in _run_exitfuncs
    func(*targs, **kargs)
TypeError: 'str' object is not callable
Error in sys.exitfunc:
Traceback (most recent call last):
  File "/usr/lib/python2.7/atexit.py", line 24, in _run_exitfuncs
    func(*targs, **kargs)
TypeError: 'str' object is not callable 
大眼看起来很简单。只需要将代码添加到脚本的最底层,他将在脚本结束前运行。但是如果脚本中有一个致命错误或者脚本被用户终止,它可能就不运行了。

当你使用atexit.register()时,你的代码都将执行,不论脚本因为什么原因停止运行。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值