随笔分类 -  python

摘要:#!/usr/bin/env python#! coding: utf-8import timeimport tornado.httpserverimport tornado.ioloopimport tornado.webclass fine(tornado.web.RequestHandler): def get(self): self.write("fine leon")app = tornado.web.Application([ (r"/fine", fine),]) if __name__ == '__main__': htt 阅读全文
posted @ 2013-03-18 17:11 践道者 阅读(262) 评论(0) 推荐(0)
摘要:一、循环{% for item in items%} {{ item }}{% end %} 阅读全文
posted @ 2013-03-18 16:54 践道者 阅读(243) 评论(0) 推荐(0)
摘要:import geventdef a(): print 'a starting....' gevent.sleep(1) print 'a ending'def b(): print 'b starting....' gevent.sleep(1) print 'b ending'gevent.joinall([ gevent.spawn(a), gevent.spawn(b)])结果显示,用gevent并不会因为gevent.sleep而造成阻塞,但用内置的time.sleep则会阻塞函数的执行。 阅读全文
posted @ 2013-03-07 09:39 践道者 阅读(407) 评论(0) 推荐(0)
摘要:def secondsToTime(secs): hours = float(secs / (60 * 60)) divisorForMinutes = secs % (60 * 60) minutes = float(divisorForMinutes / 60) divisorForSeconds = divisorForMinutes % 60 seconds = math.ceil(divisorForSeconds) obj = { 'h':hours, 'm':minutes, 's':seconds } return ... 阅读全文
posted @ 2013-02-28 20:52 践道者 阅读(265) 评论(0) 推荐(0)
摘要:while Verbox == False: print 'False'else: print 'True' 阅读全文
posted @ 2013-02-18 14:31 践道者 阅读(4809) 评论(0) 推荐(0)
摘要:解释器options:1.1 –d 提供调试输出1.2 –O 生成优化的字节码(生成.pyo文件)1.3 –S 不导入site模块以在启动时查找python路径1.4 –v 冗余输出(导入语句详细追踪)1.5 –m mod 将一个模块以脚本形式运行1.6 –Q opt 除法选项(参阅文档)1.7 –c cmd 运行以命令行字符串心事提交的python脚本-d 提供调度输出,其意思是如果代码里有__debug__的时候才会有效,如,if __debug__: print 'debug'-O生成优化的字节码,生成pyo后缀文件,此选项会关闭所有__debug__调试信息,但不能关闭 阅读全文
posted @ 2013-02-18 10:36 践道者 阅读(5086) 评论(0) 推荐(0)
摘要:import timeimport threading#当还剩下一百个产品时,则不进行消费,待生产者生产#当生产了一千个产品时,则不进行生产,待消费者消费product = 0 #产品初始化时为0lock = threading.Condition()class Producer(threading.Thread): def __init__(self, lock): self._lock = lock threading.Thread.__init__(self) def run(self): global product ... 阅读全文
posted @ 2013-02-17 16:05 践道者 阅读(2884) 评论(0) 推荐(0)
摘要:_VERBOSE = Falseif __debug__: class _Verbose(object): def __init__(self, verbose=None): if verbose is None: verbose = _VERBOSE self.__verbose = verbose def _note(self, format, *args): if self.__verbose: format = format %... 阅读全文
posted @ 2013-02-17 09:07 践道者 阅读(319) 评论(0) 推荐(0)
摘要:在研究python内置类库Queue源代码时发现full方法连续使用两个逻辑运算符,方法如下: def full(self): self.mutex.acquire() n = 0 < self.maxsize == self._qsize() self.mutex.release() return n经实践,n = 0 < self.maxsize == self._qsize()的意思详细分解即为,if 0 < self.maxsize && self.maxsize == self._qsize(): return Truereturn False可以一 阅读全文
posted @ 2013-02-16 15:25 践道者 阅读(2707) 评论(0) 推荐(0)
摘要:python内部表示字符串用unicode,与外界交互时用str获取unicode对象的方法xx.decode('utf8') #无论填什么,也会产生unicode对象unicode('xx') 阅读全文
posted @ 2013-01-30 16:37 践道者 阅读(293) 评论(0) 推荐(0)
摘要:from cStringIO import StringIOoutput = StringIO()output.write("This goes into the buffer.")print >>output, 'And so does this.' #连接字符串print output.getvalue()output.close()input = StringIO('Inital value for read buffer')print input.read()input.write(' leon-test' 阅读全文
posted @ 2013-01-22 10:04 践道者 阅读(1201) 评论(0) 推荐(0)
摘要:import randomfor i in xrange(5): print '%.4f' % random.random(),printfor i in xrange(5): print '%04.3f' % random.uniform(1, 100),printdef uniform(min, max): return min + (max - min) * random.random()for i in xrange(5): print '%04.3f' % uniform(1, 100),print 初始化随机数种子:random.se 阅读全文
posted @ 2013-01-22 00:11 践道者 阅读(414) 评论(0) 推荐(0)
摘要:#coding=utf8import Queueq = Queue.LifoQueue()for i in range(5): q.put(i)while not q.empty(): print q.get()print #coding=utf8import Queue, threadingq = Queue.PriorityQueue()class Job(object): def __init__(self, priority, description): self.priority = priority self.description... 阅读全文
posted @ 2013-01-18 01:27 践道者 阅读(192) 评论(0) 推荐(0)
摘要:在使用python脚本和底层C++对象进行交互的过程中发生了一个问题:由于底层C++对象的创建和删除决定权由底层决定,当底层决定删除这些对象而上层仍然在“强引用”这些对象的时候,就会导致“如果上层这些“强引用”不删除,则底层C++对象将一直留在内存里”的现象。什么情况下会出现这种情况呢?比如:场景管理器所管理的模型对象,当玩家很久没看到他们的时候,底层对象管理员会决定将这些对象删除掉以释放内存。但是,由于这些对象已经被上层python脚本所“强引用”,这个时候,就会出现这种浪费内存的现象了。如何解决这个问题呢?python提供了弱引用技术,请看下看。和许多其它的高级语言一样,Python使用了 阅读全文
posted @ 2013-01-16 14:24 践道者 阅读(472) 评论(0) 推荐(0)
摘要:def ma(cls): print 'method a'def mb(cls): print 'method b'method_dict = { 'ma':ma, 'mb':mb}class DynamicMethod(type): def __new__(cls, name, bases, dct): if name[:3] == 'Abc': dct.update(method_dict) return type.__new__(cls, name, bases, dct) def __init__(c... 阅读全文
posted @ 2013-01-15 17:00 践道者 阅读(712) 评论(0) 推荐(0)
摘要:1. 是否了解动态语言的鸭子模型?2. 是否了解可变参数与关键字参数?3. 对函数式编程有初步了解。4. 是否知道列表生成式?5. 是否知道lambda/decorator/slots?6. 为什么要把缺省参数设为immutable?7. 是否知道Mixin?8. 是否知道WSGI接口?9. 是否知道异步框架如gevent/tornado?10. 是否深入了解过Python的GC和GIL?11.是否了解python对象的查找机制? 阅读全文
posted @ 2013-01-15 15:57 践道者 阅读(223) 评论(0) 推荐(0)
摘要:__dict__:实例化类时将实例属性分配到__dict____slots__:阻止实例分配属性到__dict__class Base(object): __slots__ = ['y', 'x'] def __init__(self): self.y = 'aa' self.x = 'xx'b = Base()print b.__dict__ #抛错print b.yb.x = 30print b.x 阅读全文
posted @ 2013-01-15 15:17 践道者 阅读(217) 评论(0) 推荐(0)
摘要:1 class AttriubteTest(object): 2 def __init__(self): 3 self.a = 0.01 4 self.b = 1.22 5 6 def __getattribute__(self, item): 7 if item == 'a': 8 return self.a #抛RuntimeError: maximum recursion depth exceeded in cmp异常 9 else:10 return ob... 阅读全文
posted @ 2013-01-15 10:35 践道者 阅读(372) 评论(0) 推荐(0)
摘要:_builtin_types = [ type, object, bool, complex, dict, float, int, list, slice, str, tuple, set, frozenset, Exception, type(None), types.BuiltinFunctionType, types.GeneratorType, types.MethodType, types.CodeType, types.FrameType, types.TracebackType, types.ModuleType, types.FunctionType... 阅读全文
posted @ 2013-01-15 09:33 践道者 阅读(258) 评论(0) 推荐(0)
摘要:class F(object): _b = ['d','a','b','c'] def __init__(self, **kwargs): for i in kwargs.iteritems(): if i[0] in self._b: self.__dict__[i[0]] = i[1] else: raise AttributeError("error")f = F(c=33, a=1,b=2, d=445)print f.aprint f.bprint f.d... 阅读全文
posted @ 2013-01-04 16:16 践道者 阅读(161) 评论(0) 推荐(0)