01 2013 档案

摘要:python内部表示字符串用unicode,与外界交互时用str获取unicode对象的方法xx.decode('utf8') #无论填什么,也会产生unicode对象unicode('xx') 阅读全文
posted @ 2013-01-30 16:37 践道者 阅读(283) 评论(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 践道者 阅读(1197) 评论(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 践道者 阅读(405) 评论(0) 推荐(0) 编辑
摘要:pkillpkill 和killall 应用方法差不多,也是直接杀死运行中的程序;如果您想杀掉单个进程,请用kill 来杀掉。示例:pkill SynManager.py 阅读全文
posted @ 2013-01-21 11:26 践道者 阅读(291) 评论(0) 推荐(0) 编辑
摘要:chown -R python.python /xxxx 阅读全文
posted @ 2013-01-18 16:16 践道者 阅读(212) 评论(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 践道者 阅读(189) 评论(0) 推荐(0) 编辑
摘要:在使用python脚本和底层C++对象进行交互的过程中发生了一个问题:由于底层C++对象的创建和删除决定权由底层决定,当底层决定删除这些对象而上层仍然在“强引用”这些对象的时候,就会导致“如果上层这些“强引用”不删除,则底层C++对象将一直留在内存里”的现象。什么情况下会出现这种情况呢?比如:场景管理器所管理的模型对象,当玩家很久没看到他们的时候,底层对象管理员会决定将这些对象删除掉以释放内存。但是,由于这些对象已经被上层python脚本所“强引用”,这个时候,就会出现这种浪费内存的现象了。如何解决这个问题呢?python提供了弱引用技术,请看下看。和许多其它的高级语言一样,Python使用了 阅读全文
posted @ 2013-01-16 14:24 践道者 阅读(461) 评论(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 践道者 阅读(708) 评论(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 践道者 阅读(217) 评论(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 践道者 阅读(213) 评论(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 践道者 阅读(364) 评论(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 践道者 阅读(251) 评论(0) 推荐(0) 编辑
摘要:$ ps auxUSER PID %CPU %MEM VSZ RSS TT STAT STARTED TIME COMMANDroot 11 100.0 0.0 0 16 ?? RL 4Dec09 98403:03.48 [idle: cpuroot 12 100.0 0.0 0 16 ?? RL 4Dec09 98430:12.35 [idle: cpust00 94067 0.5 0.2 57172 19600 p2- S 2:36AM 5:04.20 python2.6root 0 0.0 0.0 0 0 ?? DLs 4Dec09 0:35.03 [swapper]root 1 0.0 阅读全文
posted @ 2013-01-07 10:21 践道者 阅读(8009) 评论(0) 推荐(1) 编辑
摘要: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 践道者 阅读(153) 评论(0) 推荐(0) 编辑
摘要:1 #coding=utf8 2 3 import threading 4 import time 5 import logging 6 7 logging.basicConfig( 8 level=logging.DEBUG, 9 format='[%(levelname)s]-(%(threadName)s)-%(message)s',10 )11 12 def worker():13 logging.debug('starting')14 #print 'I am worker:'15 time.sleep(2)16 logging.... 阅读全文
posted @ 2013-01-04 14:29 践道者 阅读(1142) 评论(0) 推荐(0) 编辑
摘要:做了一年多的API数据同步操作,今天才发现可以用更方便灵活高效的方式去进行数据同步,就是用生产者、消费者模式处理,把网络提取数据模块当作生产者,生产者生产产品后放到数据缓冲区,数据处理模块作为消费者,监控缓冲区数据状态信息,这样就达到解耦的目的。整理思路,写文档、画图去了~~ 阅读全文
posted @ 2013-01-03 20:20 践道者 阅读(488) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示