python技巧
2016-03-14 12:33
162 查看
带任意参数的函数
元组实现:
使用Glob()查找文件
类似于增强版的 listdir()函数:
查多个文件类型:
调试
inspect模块用于调试
生成唯一字符串使用md5()函数可以达到目的,其实有一个uuid()函数用于它
为了减少重复的情况,可以用如下的函数。
字符串存储在数据库或者文本中,格式化的存入,取出
a(lp2
I1
aS'two'
p3
aaS'apple'
p4
a.
['hello', 42, [1, 'two'], 'apple']
这是个原生的Python序列化方法。也可以用JSON实现
压缩字符
python可以压缩字符,不涉及任何档案文件
有个atexit的模块,它可以让你在脚本运行完后立马执行一些代码。
假如想在脚本执行结束时测量一些基准数据,比如运行了多长时间:
当你使用atexit.register()时,你的代码都将执行,不论脚本因为什么原因停止运行。
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"> p1aI42
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()时,你的代码都将执行,不论脚本因为什么原因停止运行。
相关文章推荐
- 利用python-flask搭建个人博客
- Python程序基本架构
- Python简介
- Python--函数,自定义函数
- Python--控制语句
- Python爬虫--捕捉网易首页
- Python--for循环
- 【python】编程语言入门经典100例--39
- 基于 Python 和 Scikit-Learn 的机器学习介绍
- Python3学习笔记--基础知识
- python3之list列表
- python安装ez_install 和sklearn
- Python学习 --- 列表
- python threading
- 【零基础入门学习Python笔记014】字符串:各种奇葩的内置方法
- python ORM 模块peewee(二): 数据库使用的基本流程
- Python学习
- python实现二分查找
- python的私有变量 是类变量吗?
- 《与小卡特一起学Python》 Code2