
Python
文章平均质量分 70
lianliange85
这个作者很懒,什么都没留下…
展开
-
python 面试( interview )
总结面试中关于python 的问题,包括 python 数据结构,python 第三方库, python 算法,python 性能:1. 闭包(closure):we have a closure in Python when a nested function references a value in its enclosing scope.The criteria原创 2016-11-25 17:08:24 · 1413 阅读 · 0 评论 -
python exec/eval/execfile
eval(str [,globals [,locals ]])函数将字符串str当成有效Python表达式来求值,并返回计算结果。同样地, exec语句将字符串str当成有效Python代码来执行.提供给exec的代码的名称空间和exec语句的名称空间相同.最后,execfile(filename [,globals [,locals ]])函数可以用来执行一个文件,看下面的例子:转载 2014-04-18 19:52:26 · 733 阅读 · 0 评论 -
python 修饰器用法2
I presented a couple of examples on how to use function decorators in Python. Those examples were illustrative, yet fairly restricted. First of all, they assumed that the function being decorated only转载 2014-01-14 13:55:57 · 825 阅读 · 0 评论 -
python函数修饰器
原文:Function Decoratorsaortiz | 31 January, 2009 09:50The Python programming language has an interesting syntactic feature called a decorator. Let's use an example in order to explain how and w转载 2014-01-08 16:30:03 · 920 阅读 · 0 评论 -
hashlib — Secure hashes and message digests
hashlib:http://docs.python.org/2/library/hashlib.html?highlight=md5MD5:http://docs.python.org/2/library/md5.html?highlight=md5This module implements a common interface to many different secure has转载 2014-01-10 10:07:15 · 806 阅读 · 0 评论 -
python __name__=='__main__'用法
当你打开一个.py文件时,经常会在代码的最下面看到if __name__ == '__main__':,现在就来介 绍一下它的作用. 模块是对象,并且所有的模块都有一个内置属性 __name__。一个模块的 __name__ 的值取决于您如何应用模块。如果 import 一个模块,那么模块__name__ 的值通常为模块文件名,不带路径或者文件扩展名。但是您也可以像一个标准的转载 2013-12-25 14:32:39 · 741 阅读 · 0 评论 -
python logging 日志记录
许多应用程序中都会有日志模块,用于记录系统在运行过程中的一些关键信息,以便于对系统的运行状况进行跟踪。在.NET平台中,有非常著名的第三方 开源日志组件log4net,c++中,有人们熟悉的log4cpp,而在python中,我们不需要第三方的日志组件,因为它已经为我们提供了简单易 用、且功能强大的日志模块:logging。logging模块支持将日志信息保存到不同的目标域中,如:保存到日志文件中转载 2013-12-25 18:11:20 · 720 阅读 · 0 评论 -
python 单元测试
测试代码位置:你可以将测试用例定义与被测试代码置于同一个模块中(例如“widget.py”),但是将测试代码放置在单独的模块中(如“widgettests.py”)会有一些优势:测试模块可以从命令行单独执行测试代码可以方便地从发布代码中分离少了在缺乏充足理由的情况下为适应被测试代码而更改测试代码的诱惑相对于被测试代码,测试代码不应该被频繁的修改被测试代码可以更方法的进行重构转载 2013-12-25 14:19:35 · 560 阅读 · 0 评论 -
python else 用法
if-else语句:和各种语言相同的用法,在条件语句中,与if语句搭配使用的else语句。如果if语句的条件表达式的结果布尔值为假,那么程序将执行else语句后的代码。它的语法是大家最为熟知的:if expression: expr_true_suiteelse: expr_false_suiteelif(else-if)语句:elif是pyth转载 2013-12-26 09:09:17 · 746 阅读 · 0 评论 -
Python:time, strftime和strptime
最常用的time.time()返回的是一个浮点数,单位为秒。但strftime处理的类型是time.struct_time,实际上是一个tuple。strptime和localtime都会返回这个类型。>>> import time>>> t = time.time()>>> t1202872416.4920001>>> type(t)>>> t = time.lo转载 2013-12-23 16:49:35 · 849 阅读 · 0 评论 -
python uuid
uuid是一种唯一标识,在许多领域作为标识用途。python的uuid模块就是用来生成它的。闲话不说,python提供的生成uuid的方法一共有4种,分别是:1.从硬件地址和时间生成2.从md5算法生成3.随机生成4.从SHA-1算法生成他们在uuid模块里对应uuid1, uuid3, uuid4, uuid5这几个方法,注意没有uuid2。下面是示例:转载 2014-05-16 17:30:16 · 874 阅读 · 0 评论 -
python 目录操作
判断是否为目录在Python中可以使用os.path.isdir()函数判断某一路径是否为目录。其函数原型如下所示:os.path.isdir(path)其参数 path为 要进行判断的路径。如果是则返回TRUE,否则返回FALSE。判断是否为文件在Python中可以使用os.path.isfile()函数判断某一路径是否为文件。其函数原型如下所示。os.path.is转载 2014-05-16 17:46:35 · 629 阅读 · 0 评论 -
python lambda
Small anonymous functions can be created with the lambda keyword. This function returns the sum of its two arguments:lambda a,b: a+b. Lambda functions can be used wherever function objects a转载 2014-10-12 21:51:06 · 602 阅读 · 0 评论 -
python os.path
os.path模块basename('文件路径') 去掉目录路径,返回fname文件名 1 import os 2 os.path.basename('/Volumes/1.mp4') #输出('1.mp4')dirname('文件路径') 去掉文件名,返回目录路径 1 import os 2 os.path.dirname('/Volumes/1.mp4'转载 2014-05-26 18:00:06 · 598 阅读 · 0 评论 -
python 正则表达式
官网:https://docs.python.org/2/library/re.html#re.MatchObject转载 2014-08-25 18:47:32 · 469 阅读 · 0 评论 -
python time 和datetime类型转换,字符串型变量转成日期型变量
为了从字符串中提取时间,并进行比较,因此有了这个问题,如何将字符串转换成datetime类型 1.字符串与time类型的转换>>> import time>>> timestr = "time2009-12-14">>> t = time.strptime(timestr, "time%Y-%m-%d")>>> print t(2009, 12, 14, 0,转载 2013-12-23 16:52:25 · 1734 阅读 · 0 评论 -
python list排序
1.先说一下iterable,中文意思是迭代器。Python的帮助文档中对iterable的解释是:iteralbe指的是能够一次返回它的一个成员的对象。iterable主要包括3类:第一类是所有的序列类型,比如list(列表)、str(字符串)、tuple(元组)。第二类是一些非序列类型,比如dict(字典)、file(文件)。第三类是你定义的任何包含__iter__()或__getitem__转载 2014-07-28 15:55:10 · 817 阅读 · 0 评论 -
浏览器文件下载及文件名编码问题
Content-disposition 是 MIME 协议的扩展,MIME 协议指示 MIME 用户代理如何显示附加的文件。当 Internet Explorer 接收到头时,它会激活文件下载对话框,它的文件名框自动填充了头中指定的文件名。(请注意,这是设计导致的;无法使用此功能将文档保存到用户的计算机上,而不向用户询问保存位置。)Content-Disposition就是当用户想把请求所得的转载 2014-05-26 17:45:50 · 2266 阅读 · 0 评论 -
ctypes
A foreign function library for Pythonhttps://docs.python.org/3.2/library/ctypes.html?highlight=ctypes#ctypes.c_char_p转载 2014-04-18 19:55:28 · 564 阅读 · 0 评论 -
python *args和**kwargs
#-*-coding:utf-8-*- def foo(*args, **kwargs): print 'args = ', args print 'kwargs = ', kwargs print '---------------------------------------'if __name__ == '__main__': foo(1,2,3,4)转载 2014-01-14 11:16:58 · 693 阅读 · 0 评论 -
Python dictionary items() Method
PYTHON Programing:http://www.tutorialspoint.com/python/dictionary_items.htmDescriptionThe method items() returns a list of dict's (key, value) tuple pairsSyntaxFollowing is转载 2013-11-21 10:39:43 · 905 阅读 · 0 评论 -
python bool值
写习惯了C#的代码,在想要将一个字符串'False'转换为bool型的时候,很自然的写了如下的Python代码:看到上面的结果了没?是True。突然记起Python中除了''、""、0、()、[]、{}、None为False之外,其他的都是True。也就是说上面的'False'就是一个不为空的字符串,所以结果就为True了。为了深入了解下Python的bool类型,就看了下转载 2013-12-22 13:30:08 · 1400 阅读 · 0 评论 -
python json simplejson
python安装:easy_install simplejson导入模块:import simplejson as json 几个主要函数:dump,dumps,load,loads 带s跟不带s的区别是 带s的是对 字符串的处理,而不带 s的是对文件对像的处理。json化python字典数据:json.dumps(['foo', {'bar': ('baz转载 2013-12-09 16:06:58 · 1017 阅读 · 0 评论 -
Python斐波拉契生成器与迭代器
迭代器:class Fib: ① def __init__(self, max): ② self.max = max def __iter__(self): ③ self.a =转载 2013-12-05 09:51:44 · 809 阅读 · 0 评论 -
python 字符编码
http://blog.youkuaiyun.com/wanghai__/article/details/62277211. 字符编码简介1.1. ASCIIASCII (American Standard Code for Information Interchange),是一种单字节的编码。计算机世界里一开始只有英文,而单字节可以表示256个不同的字符,可以表示所有的英文字符和转载 2013-11-27 13:30:38 · 605 阅读 · 0 评论 -
python __import__
http://blog.youkuaiyun.com/wanghai__/article/details/6924352下面先看个小例子[wanghai01@tc-crm-rd03 test]$ cat a.pydef func1(): print 'in a.func1'[wanghai01@tc-crm-rd03 test]$ cat c.py pac转载 2013-11-27 13:14:32 · 671 阅读 · 0 评论 -
python Django命令
运行 django-admin.py startproject mysite 命令在当前目录创建一个 mysite 目录。django-admin.py这个文件在C:\Python27\Lib\site-packages\django\bin文件夹里,可以把该目录添加到系统Path里面。Django内置一个轻量级的Web服务器。进入 mysite 目录的话,现在进入其中,并转载 2013-11-25 10:03:10 · 728 阅读 · 0 评论 -
Python 正则表达式
^ 匹配字符串开始位置。$ 匹配字符串结束位置。\b 匹配一个单词边界。\d 匹配一个数字。\D 匹配一个任意的非数字字符。x? 匹配可选的x字符。换句话说,就是0个或者1个x字符。x* 匹配0个或更多的x。x+ 匹配1个或者更多x。x{n,m} 匹配n到m个x,至少n个,不能超过m个。(a|b|c) 匹配单独的任意一个a或者b或者c。(x) 这是一个组,它会记忆它匹配到的字符串。你可以用re.s转载 2013-11-13 10:42:53 · 703 阅读 · 0 评论 -
Python 调试工具
译文:http://blog.jobbole.com/51062/原文:http://blog.ionelmc.ro/2013/06/05/python-debugging-tools/转载 2013-11-08 08:59:18 · 693 阅读 · 0 评论 -
python中列表,元组,字符串如何互相转换
转载:http://blog.youkuaiyun.com/sruru/article/details/7803208python中有三个内建函数:列表,元组和字符串,他们之间的互相转换使用三个函数,str(),tuple()和list(),具体示例如下所示:>>> s = "xxxxx">>> list(s)['x', 'x', 'x', 'x', 'x']>>>转载 2013-11-21 10:18:29 · 887 阅读 · 0 评论 -
Python String find() Method
http://www.tutorialspoint.com/python/string_find.htmDescriptionThe method find() determines if string str occurs in string, or in a substring of string if starting indexbeg and ending inde转载 2013-11-21 14:44:31 · 1235 阅读 · 0 评论 -
树的循环遍历与递归拼接
def Loadtree(id): root={"title": "main","key":0,"children": ""} root["children"]=bindchildnode(0) return rootdef bindchildnode(did): nodechild=[] treeList=Functionmodule.objects.filter(parent原创 2013-11-19 21:29:23 · 1872 阅读 · 0 评论 -
Django class-based views
Class-based viewsA view is a callable which takes a request and returns a response. This can be more than just a function, and Django provides an example of some classes which can be used as views转载 2013-10-28 15:32:06 · 862 阅读 · 0 评论 -
Python与QueryDirets directory items(), keys(), values()
>>> dict = { 1 : 2, 'a' : 'b', 'hello' : 'world' } >>> dict.values() ['b', 2, 'world'] >>> dict.keys() ['a', 1, 'hello'] >>> dict.items() [('a', 'b'), (1, 2), ('hello', 'world')] >>>原创 2013-11-27 13:36:12 · 922 阅读 · 0 评论 -
python 获取系统时间
取得时间相关的信息的话,要用到python time模块,python time模块里面有很多非常好用的功能,你可以去官方文档了解下,要取的当前时间的话,要取得当前时间的时间戳,时间戳好像是1970年到现在时间相隔的时间。你可以试下下面的方式来取得当前时间的时间戳:import timeprint time.time()输出的结果是:1279578704.6725271转载 2013-11-28 09:08:15 · 650 阅读 · 0 评论 -
python 变量名及赋值操作的本质
python 中的变量名有些类似于C++中的指针的概念.python 中的赋值操作改变的并不是内存中变量的值, 而是变量名指向的变量.>>> var = 3>>> tmp = var>>> var3>>> tmp3>>> id(var)10417272>>> id(tmp)10417272>>> id(3)10417272转载 2013-12-09 15:15:49 · 3264 阅读 · 0 评论 -
Python Gossip: import、import as、from import
引入模块,使用import引入模块,會以被匯入的模組名稱在當前模組命名一個同名的名稱,被匯入模組中的变量,其實是以被引入的模块為名稱空間。例如,若有個some.py:name = 'Justin'若你匯入some模組,要存取some模組中的name變數,則必須使用some.name。事實上,你可以使用del刪除與被匯入模組同名的變數:import someprint(s转载 2013-12-09 15:33:15 · 1034 阅读 · 0 评论 -
python import、from import
1, module 的实质python 中的module 本质上就是一个相对独立的python文件. 当需要重用某些功能时, 将此功能所在的模块import到需要的模块即可.2, import 的实质import的过程实际上包括两个主要部分, 1) 类似于shell 的 "source" 命令, 具体说即相当于将被import的mo转载 2013-12-09 15:13:08 · 1121 阅读 · 0 评论 -
Python中的getattr()函数详解
Python’s getattr function is used to fetch an attribute from an object, using a string object instead of an identifier to identify the attribute. In other words, the following two statements are equ转载 2013-11-30 09:31:15 · 829 阅读 · 0 评论 -
高级迭代器的字母算数详解
import reimport itertoolsdef solve(puzzle): words = re.findall('[A-Z]+', puzzle.upper()) unique_characters = set(''.join(words)) assert len(unique_characters) <= 10, 'Too many letters'原创 2013-12-06 14:40:32 · 767 阅读 · 0 评论