
python 周边
文章平均质量分 70
拖拉机拖拉机
这个作者很懒,什么都没留下…
展开
-
Jinja2 example for generating a local file using a template
原文链接:https://pythonadventures.wordpress.com/archives/Here I want to show you how to generate an HTML file (a local file) using a template with theJinja2 template engine.Python source (proba.转载 2015-10-15 13:42:13 · 1175 阅读 · 0 评论 -
Python 总结
1.如何实现自己的iter2.如何实现自己的range3.如何实现一个dict4.如何实现一个list5.如何实现类似ruby的open clas6.如何实现原创 2015-05-01 20:30:42 · 677 阅读 · 0 评论 -
Python __missing__
According to the python documentation:If a subclass of dict defines a method __missing__(), if the key key is not present, the d[key] operation calls that method with the key key as argument. The原创 2015-04-16 10:55:00 · 3750 阅读 · 0 评论 -
Python的Descriptor在Django中的使用
这篇通过Django源码中的cached_property来看下Python中一个很重要的概念——Descriptor(描述器)的使用。想必通过实际代码来看能让人对其用法更有体会。什么是Descriptor?Descriptor是Python中定义的一个协议,协议的内容是只要你定义的这个类(对象)具有: __get__, __set__, __delete__ 方法中的任意一个转载 2015-04-15 21:09:19 · 1505 阅读 · 0 评论 -
在 Python 中实现 Ruby 的 Open Class 和特异方法
原文抄袭自 https://blog.tonyseek.com/post/open-class-in-python/Ruby 中的 OpenClass 是个非常方便的特性,我们可以扩充一个已有的类,往里面添加方法。甚至还能脱离类,向实例中添加该实例独有的方法,称为“特异方法”。这种做法的前提是语言具有足够的动态特性,能够运行时更改语言结构,所谓“ 元编程 ”(Meta-Prog转载 2015-04-15 10:05:00 · 1066 阅读 · 0 评论 -
How to Get a List of Class Attributes in Python
The other day, I was trying to figure out if there was an easy way to grab a class’s defined attributes (AKA “instance variables”). The reason was that we were using the attributes we created to match转载 2015-04-06 17:36:59 · 861 阅读 · 0 评论 -
saltstack grains
The grain interface is some information about the underlying system,it is available for all the Salt module and compountssalt 'some host's hostname' grains.ls # to list all the grains in the salt mi原创 2015-04-19 00:25:21 · 968 阅读 · 0 评论 -
saltstack cheatsheet summary (key && compound matcher)
Salt key management Listing all Salt key registration requests1.salt-key -LAccepting all Salt key requests:1.salt-key -AAccepting a single Salt key request, where minion_id is the name of th原创 2015-04-18 23:44:52 · 889 阅读 · 0 评论 -
Python Built-in Functions
Difference Between __init__ and __call____init__ would be treated as Constructor where as __call__ methods can be called with objects any number of times. Both __init__ and __call__ functions do tak原创 2015-04-03 14:47:03 · 786 阅读 · 0 评论 -
python的cls,self,classmethod,staticmethod
python类里会出现这三个单词,self和cls都可以用别的单词代替,类的方法有三种,一是通过def定义的 普通的一般的,需要至少传递一个参数,一般用self,这样的方法必须通过一个类的实例去访问,类似于c++中通过对象去访问;二是在def前面加上@classmethod,这种类方法的一个特点就是可以通过类名去调用,但是也必须传递一个参数,一般用cls表示class,表示可以通过类直接调原创 2013-06-25 11:39:01 · 28797 阅读 · 0 评论 -
Python属性和方法
转载出处:http://abyssly.com/2013/08/19/python_attributes_and_methods/关于此文解释了Python新式对象的属性访问机制:函数是如何成为方法的?descriptor和property是如何工作的?如何确定方法解析顺序(method resolution order)?新式对象是从Python2转载 2015-05-04 09:30:58 · 4566 阅读 · 0 评论 -
Python的垃圾回收机制深入分析
无聊的五一,蛋疼的五一。看书吧!转载,原文链接:http://www.jb51.net/article/52229.htm一、概述:Python的GC模块主要运用了“引用计数”(reference counting)来跟踪和回收垃圾。在引用计数的基础上,还可以通过“标记-清除”(mark and sweep)解决容器对象可能产生的循环引用的问题。通过“分代回收”(gener转载 2015-05-01 15:03:42 · 729 阅读 · 0 评论 -
Class method differences in Python: bound, unbound and static
原文出处:http://stackoverflow.com/questions/114214/class-method-differences-in-python-bound-unbound-and-staticWhat is the difference between the following class methods?Is it that one is static转载 2015-05-04 09:39:10 · 726 阅读 · 0 评论 -
Abstract Classes and Factory Design Pattern in Python
Abstract Classes are one of the most useful and important concepts in Object Oriented Programming. I’ll attempt to illustrate their usefulness, and their usage in Python 2.7 with the following (seem转载 2015-07-14 11:15:23 · 746 阅读 · 0 评论 -
Python multiprocessing.Pool: when to use apply, apply_async or map?
来自stackoverflowQ:I have not seen clear examples with use-cases for Pool.apply, Pool.apply_async and Pool.map. I am mainly using Pool.map; what are the advantages of others?A:Back转载 2015-07-13 13:53:15 · 7477 阅读 · 0 评论 -
Multiprocessing vs Threading Python
Q:I am trying to understand the advantages of multiprocessing over threading. I know thatmultiprocessing gets around the Global Interpreter Lock, but what other advantages are there, and can threa转载 2015-07-13 14:58:15 · 1087 阅读 · 0 评论 -
Python thread pool similar to the multiprocessing Pool?
Q:Is there a Pool class for worker threads, similar to the multiprocessing module's Pool class?I like for example the easy way to parallelize a map functiondef long_running_func(p):转载 2015-07-13 14:39:52 · 1239 阅读 · 0 评论 -
Dead simple example of using Multiprocessing Queue, Pool and Locking
Q:I tried to read the documentation at http://docs.python.org/dev/library/multiprocessing.html but I'm still struggling with multiprocessing Queue, Pool and Locking. And for now I was able to转载 2015-07-13 14:17:42 · 526 阅读 · 0 评论 -
strategy pattern in Python
1.function based#!/usr/bin/env pythonimport typesdef method_property(name): def getter(self): return getattr(self, name) def setter(self, value): setattr(self, name, type转载 2015-07-13 11:14:47 · 701 阅读 · 0 评论 -
coverage.py的覆盖率统计原理
原文出处:http://blog.theerrorlog.com/how-coverage-py-produce-coverage-statistics.html在Erlang下,用rebar可以在运行单元测试的同时调用cover模块,从而得 到单元测试的覆盖率,这是很方便的一个功能。最近发现Python下的nose也可 以通过--with-coverage选项达到类似的效果,转载 2015-07-08 10:18:23 · 6438 阅读 · 1 评论 -
Prototype pattern (Python recipe)
原文链接:http://code.activestate.com/recipes/86651-prototype-pattern/http://dongweiming.github.io/python-pototype.htmlfrom copy import deepcopyclass Prototype: def __init__(self): self转载 2015-07-08 14:50:33 · 881 阅读 · 0 评论 -
difference between Python shallow copy and deep copy
A:Shallow copies duplicate as little as possible. A shallow copy of a collection is a copy of the collection structure, not the elements. With a shallow copy, two collections now share the individ转载 2015-05-01 13:07:56 · 842 阅读 · 0 评论 -
那些年,我踩过的坑,而后人绝不要再踩
1.(pyenv)xluren@xluren:~/python/project$ ../pyenv/bin/pip install flask Downloading/unpacking flask Cannot fetch index base URL https://pypi.python.org/si原创 2014-11-06 20:34:57 · 2802 阅读 · 1 评论 -
Python time datetime 模块梳理
time是Python底层的模块,datetime是基于time的实现time相关的数据类型 float ,原创 2014-10-19 19:32:07 · 1057 阅读 · 0 评论 -
flask-cache‘s key_prefix
For example like this, is it necessary to use key_prefix?@cache.cached(timeout=50, key_prefix='all_comments')def get_all_comments(): comments = do_serious_dbio() return [x.author for x in c转载 2014-08-04 11:40:58 · 3224 阅读 · 0 评论 -
python 字符串替换
#!/usr/bin/pythonimport stringfrom_str = "abcdefg"to_str = "1234567"trantab = string.maketrans(from_str, to_str)str = "this is string example....wow!!!";print str.translate(trantab);print s原创 2014-07-09 14:51:53 · 5697 阅读 · 0 评论 -
python urllib* 获取网页信息
直接上代码吧,从简单到复杂,同时涉及到cookie的使用原创 2014-07-08 22:14:25 · 2286 阅读 · 0 评论 -
python list 排序
文章转载,原博主总结的非常好出处转载 2014-07-04 22:30:01 · 709 阅读 · 0 评论 -
python decorator
#!/usr/bin/python import timeimport functools def timeit_0(func): start = time.clock() func() end =time.clock() print 'used:', end - startdef timeit_1(func): def wrapper():原创 2014-07-03 11:05:14 · 563 阅读 · 0 评论 -
python fnmatch
import fnmatchimport osimport redef fnmatch_filter_demo(path,pattern): for path,dir,filelist in os.walk(path): for name in fnmatch.filter(filelist,pattern): print os.path.jo原创 2014-07-03 10:34:54 · 4616 阅读 · 0 评论 -
python 函数形式
1.func(x,y)2.func(x="hello",y="world") #带默认参数3.func(*x)#tuple4原创 2014-07-01 20:44:24 · 584 阅读 · 0 评论 -
sqlalchemy整理(二)
连接数据库,以及数据库操作合集原创 2014-07-15 09:24:21 · 736 阅读 · 0 评论 -
python ConfigParser 模块学习
因为要用到配置文件,所以自己查了下python有这么个模块ConfigParser.然后就学了下,超级的简单[mysqld]datadir=/search/mysqlsocket=/var/lib/mysql/mysql.sockuser=mysql# Default to using old password format for compatibility with mys原创 2014-03-01 20:18:32 · 1159 阅读 · 0 评论 -
flask cache
flask cache简单的使用有两个装饰器原创 2014-08-05 14:09:39 · 4495 阅读 · 0 评论 -
sqlalchemy整理(一)
http://blog.youkuaiyun.com/xluren/article/details/9170431继续之前的原创 2014-07-14 21:27:36 · 786 阅读 · 0 评论 -
sqlalchemy整理(四)
接之前的一二三三中是原创 2014-07-15 10:33:17 · 889 阅读 · 0 评论 -
修改Python ConfigParser option 大小写的问题
ConfigParser option 无论大小写,都会转换成小写,看了源代码发现了一行这样的代码原创 2014-10-20 11:43:44 · 10327 阅读 · 1 评论 -
Python gevent vs thread vs sequence
Core Solo原创 2014-11-04 12:03:32 · 1553 阅读 · 0 评论 -
Python re 模块整理
整理下Python re模块ji原创 2014-10-17 10:44:12 · 1400 阅读 · 0 评论 -
Python 动态引入模块
[xluren@test import_test]$ lltotal 8-rw-rw-r--. 1 xluren xluren 79 Nov 1 19:19 bar.py-rw-rw-r--. 1 xluren xluren 57 Nov 1 19:18 foo.py[xluren@test import_test]$ python bar.py 012345678原创 2014-11-01 19:26:08 · 1254 阅读 · 0 评论