python
xianpeng2020
这个作者很懒,什么都没留下…
展开
-
python descriptor override
.4.2.3 Invoking Descriptors In general, a descriptor is an object attribute with ``binding behavior'', one whose attribute access has been overridden by methods in the descriptor protocol: __get__(),转载 2014-09-11 16:43:38 · 780 阅读 · 0 评论 -
python timeit
a=[1,2,3,4,5] In [71]: %timeit for i in a[::-1]: print i //100 loops, best of 3: 2.3 ms per loop In [117]: %timeit for i in reversed(a):print i >>> timeit.timeit('for x i原创 2014-03-15 13:45:40 · 589 阅读 · 0 评论 -
python 判断控制符 unicode str
unicodedata.category(ch)=='Cc'转载 2014-04-05 18:18:22 · 760 阅读 · 0 评论 -
ruby 一切都是对象
ruby中一切都是对象 ,变量名 都是引用, 函数是值原创 2014-04-06 19:44:22 · 666 阅读 · 0 评论 -
python args list
def func(default=[]): default.append('1') print default func() func() each time default changes because default args of python function evalutaed only when execusion of definition p转载 2014-03-25 23:06:35 · 909 阅读 · 0 评论 -
python bound unbound method
assign function to class make it a unbound method of that class eg: CLASSA.method1 = func1原创 2014-03-24 10:38:53 · 1100 阅读 · 0 评论 -
Flask
@app.route('/login', methods=['GET', 'POST']) def login(): if request.method == 'POST': do_the_login() else: show_the_login_form()转载 2014-05-03 10:46:13 · 582 阅读 · 0 评论 -
python immutable tuple list
The reasons for having immutable types apply to tuples: copy efficiency: rather than copying an immutable object, you can alias it (bind a variable to a reference) comparison efficiency: when yo转载 2014-03-21 00:15:47 · 559 阅读 · 0 评论 -
range xrange iterator generator
range creates a list, so if you do range(1, 10000000) it creates a list in memory with 10000000elements. xrange is a generator, so it is a sequence object is a that evaluates lazily. 所转载 2014-03-20 23:38:26 · 585 阅读 · 0 评论 -
flatten list in python
semms like generator efficient Python [edit]Recursive Function flatten is recursive and preserves its argument: >>> def flatten(lst): return sum( ([x] if not isinstance(x, list) els转载 2014-03-19 11:51:44 · 1181 阅读 · 0 评论 -
python closure
patch module A' method meth1 to module B's, if meth1 access other method in module B, new meth can still access other method because meth1 forms a closure in module A, and also meth1 is p原创 2014-03-25 09:30:46 · 613 阅读 · 0 评论 -
Is Python call-by-value or call-by-reference? Binding Names to Objects
Is Python call-by-value or call-by-reference? Neither. Posted on Nov 13, 2012 by Jeff Knupp One aspect of Python programming that trips up those coming from languages like C or Java is how argumen转载 2014-03-25 23:17:58 · 691 阅读 · 0 评论 -
python +=
In [25]: a=[] In [26]: b=[] In [27]: id(a) Out[27]: 51344568 In [29]: a+=b In [31]: id(a) ## id 没变, a +=不会创建新list Out[31]: 51344568 In [32]: a=a+b原创 2014-03-25 23:27:28 · 461 阅读 · 0 评论 -
python 描述符 类方法 闭包
# -*- coding:utf-8 -*- # class FunWrap(object): u"""包装一个函数""" def __init__(self, func): self.func = func def __get__(self, obj, typ = None): return szhinstancemethod(typ, obj, self) def _原创 2014-09-10 22:41:43 · 674 阅读 · 0 评论 -
python func defaults args are evaluated only once,(in definition time)
In [7]: def r(): ...: return random.randint(1,100) ...:In [12]: def d(a, b=r()): ....: print b,'-------------' ....: In [13]: d(9) 75 ------------- In [14]: d(9) 75 -------------转载 2014-04-26 11:20:02 · 624 阅读 · 0 评论 -
python list存储的是reference id
ids of integer constants are not guaranteed to be equal, and indeed, for integers as large as 257, they aren't. (try id(257) == id(258 - 1)) Everything in python is stored as reference.转载 2014-04-26 10:45:32 · 1026 阅读 · 0 评论 -
python 闭包
def a(): l=[] def set_l(): l.append(1) def get_l(): print l return set_l, get_l s,g = a() In [4]: g() [] In [5]: s() In [6]: s() In [7]: g()原创 2014-04-26 10:24:48 · 499 阅读 · 0 评论 -
python实现二叉树
class _Tree(object): def __init__(self, value, nodeClass): self.value = nodeClass(value) self.leftNode = None self.rightNode = None def setLeftNode(self, node):转载 2014-04-25 16:13:54 · 555 阅读 · 0 评论 -
python copy * shallow
需要在程序中使用二维数组,网上找到一种这样的用法: 1 2 3 4 5 6 #创建一个宽度为3,高度为4的数组 #[[0,0,0], # [0,0,0], # [0,0,0], # [0,0,0]] myList = [[0] * 3] * 4转载 2014-03-27 00:24:12 · 476 阅读 · 0 评论 -
python default arg 和 *args 不能同时用
python 函数 定义过程中参数的设置有一个问题 def func1(原创 2014-04-06 19:02:19 · 965 阅读 · 0 评论 -
redis sort 使用外部key排序
>>> conn.rpush('sort-input', 23, 15, 110, 7) #A4 #A>>> conn.sort('sort-input') #B['转载 2014-04-06 21:45:48 · 1246 阅读 · 0 评论 -
redis python操作 string integer
打开Python解释器: >>> import redis >>> r = redis.Redis(host='localhost', port=6379, db=0) #如果设置了密码,就加上password=密码 >>> r.set('foo', 'bar') #或者写成 r['foo'] = 'bar' True >>> r.get('foo') 'bar'转载 2014-04-06 20:57:03 · 517 阅读 · 0 评论 -
匹配汉字英文字符
正则表达式 匹配中文,英文字母和数字及_的写法!同时控制长度 2012-12-07 18:23 7639人阅读 评论(0) 收藏 举报 匹配中文:[\u4e00-\u9fa5] 英文字母:[a-zA-Z] 数字:[0-9] 匹配中文,英文字母和数字及_: ^[\u4e00-\u9fa5_a-zA-Z0-9]+$ 同时判断输入长度: [\u4e00转载 2014-04-16 20:35:21 · 762 阅读 · 0 评论 -
python leak variable
Here are two ways to count the numbers from 1 to 1000000 in Python. First, the classical way: sum([i for i in xrange(1000000)]) Runs 0.8s on an old Linux box. It uses quite a bit of memory. Secon转载 2014-03-20 23:52:16 · 620 阅读 · 1 评论 -
PYTHON condition Monitor
from collections import deque from threading import Thread,Condition QUEUE = deque() def an_item_is_available(): return bool(QUEUE) def get_an_available_item(): return QUEUE.popleft() def转载 2014-03-12 11:44:46 · 664 阅读 · 0 评论 -
Python thread join
join([timeout]) Wait until the thread terminates. This blocks the calling thread until the thread whose join() method is called terminates – either normally or through an unhandled exception – or u转载 2014-03-12 12:01:30 · 893 阅读 · 0 评论 -
python uml code graph
UML tools for Python: PyUML is an open source Eclipse plug-in offering round trip engineering betwee UML and Python.UML support is limited to class diagrams and the code-generation capabilities转载 2014-03-07 00:21:47 · 1599 阅读 · 0 评论 -
python magic method
A Guide to Python's Magic Methods Copying Sometimes, particularly when dealing with mutable objects, you want to be able to copy an object and make changes without affecting what you copie转载 2014-03-01 21:39:26 · 654 阅读 · 0 评论 -
python corrector
http://norvig.com/spell-correct.html import re, collections def words(text): return re.findall('[a-z]+', text.lower()) def train(features): model = collections.defaultdict(lambda: 1)转载 2014-02-22 13:04:04 · 651 阅读 · 0 评论 -
python decorator
>>> def useful(a, b): return a**2 + b**2 >>> useful.__name__ 'useful' >>> from inspect import getargspec >>> getargspec(useful) (['a', 'b'], None, None, None) >>> @addspam ... def useful(a, b): return转载 2014-01-17 11:00:37 · 487 阅读 · 0 评论 -
virtualenv
VirtualEnv 是什么 VirtualEnv用于在一台机器上创建多个独立的python运行环境,VirtualEnvWrapper为前者提供了一些便利的命令行上的封装。 - 隔离项目之间的第三方包依赖,如A项目依赖django1.2.5,B项目依赖django1.3。 - 为部署应用提供方便,把开发环境的虚拟环境打包到生产环境即可,不需要在服务器上再折腾一翻。转载 2013-08-03 10:57:20 · 635 阅读 · 0 评论 -
install virtualenv
sudo easy_install virtualenv --------------------------- sudo easy_install virtualenvwrapper ----------- Open your bash script ~/.bashrc and add the following two lines: expo转载 2014-01-16 10:02:21 · 515 阅读 · 0 评论 -
django online user
Django: Using Caching to Track Online Users Recently I wanted a simple solution to track whether a user isonline on a given Django site. The definition of "online" on asite is kind of ambiguous, so转载 2014-01-01 14:57:27 · 746 阅读 · 0 评论 -
python thread wait until specific thread complete
#!python3.3 import queue # For Python 2.x use 'import Queue as queue' import threading, time, random def func(id, result_queue): print("Thread", id) time.sleep(random.random() * 5) res转载 2014-03-12 11:50:38 · 774 阅读 · 0 评论 -
python with contextmanager
with file('test.py','r') as f : print f.readline() with的作用,类似try...finally...,提供一种上下文机制,要应用with语句的类,其内部必须提供两个内置函数__enter__以及__exit__。前者在主体代码执行前执行,后则在主体代码执行后执行。as后面的变量,是在转载 2014-03-08 13:04:57 · 1350 阅读 · 0 评论 -
list comprehesion two syntax in python 特殊语法
You can totally do that, it's just an ordering issue: [ unicode(x.strip()) if x is not None else '' for x in row ] Note that this actually uses a different language construct, a conditional expr转载 2014-03-09 22:38:59 · 567 阅读 · 0 评论 -
python yield send
def psychologist(): print 'Please tell me your problems' while True: answer = (yield) if answer is not None: if answer.endswith('?'): print ("Don't ask yourself too much questions") el转载 2014-03-31 10:47:54 · 646 阅读 · 0 评论 -
python dict reg key
Dictionary lookup with regular expression as key I just remembered that I was meant to post that thing here, too. Problem is the following: you want to retrieve all values of a python dictionary转载 2014-03-30 00:06:50 · 711 阅读 · 0 评论 -
python wraps decorator
frominspectimportgetmembers, getargspec fromfunctoolsimportwraps defwraps_decorator(f): @wraps(f) defwraps_wrapper(*args,**kwargs): returnf(*args,**kwargs) returnwraps_wra转载 2014-03-29 23:38:14 · 933 阅读 · 0 评论 -
How To Use Linux epoll with Python
How To Use Linux epoll with Python转载 2014-03-29 17:33:28 · 464 阅读 · 0 评论