liuyongjians

导航

 

2014年3月21日

摘要: 看官方文档无非就是说str是友好的,但不正式,repr比较官方。别的咋就不从而知了! 直接上代码: 1 In [68]: import datetime 2 3 In [69]: today = datetime.datetime.now() 4 5 In [70]: str(today) 6 Out[70]: '2014-03-22 17:44:48.975809' 7 8 In [71]: repr(today) 9 Out[71]: 'datetime.datetime(2014, 3, 22, 17, 44, 48, 975809)'10 11 In [ 阅读全文
posted @ 2014-03-21 17:49 liuyongjians 阅读(653) 评论(0) 推荐(0) 编辑
 

2014年3月13日

摘要: 工作中遇到如下问题>>> str(u'我')Traceback (most recent call last): File "", line 1, in UnicodeEncodeError: 'ascii' codec can't encode character u'\u6211' in position 0: ordinal not in range(128)解决方式如下>>> str(u'我'.encode('utf-8'))'\xe6 阅读全文
posted @ 2014-03-13 15:58 liuyongjians 阅读(11029) 评论(0) 推荐(0) 编辑
 

2014年3月7日

摘要: 转自 http://www.zlovezl.cn/articles/__init__-and__new__-in-python/Python中的__init__和__new__03-03 20:31259__init__ 方法是什么?使用Python写过面向对象的代码的同学,可能对__init__方法已经非常熟悉了,__init__ 方法通常用在初始化一个类实例的时候。例如:# -*- coding: utf-8 -*-class Person(object): """Silly Person""" def __init__(self 阅读全文
posted @ 2014-03-07 13:37 liuyongjians 阅读(293) 评论(0) 推荐(0) 编辑
 

2014年1月23日

摘要: 网上无意中找来的检测你的代码是否符合python规范https://github.com/imlyj/autopep8可以用它来检测一下你的代码是否符合! 阅读全文
posted @ 2014-01-23 11:14 liuyongjians 阅读(149) 评论(0) 推荐(0) 编辑
 

2013年11月18日

摘要: 今天看到一小段代码,然后发现一个貌似很常见的api,因为不怎么用,所以查了一下。结果,越查越迷糊str.lstrip([chars])Return a copy of the string with leading characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is 阅读全文
posted @ 2013-11-18 23:10 liuyongjians 阅读(1101) 评论(0) 推荐(0) 编辑
 

2013年11月5日

摘要: python的迭代器,只需要实现__iter__跟next()方法就成。例如: def __iter__(self): return self def next(self): result = self.get() if result is StopIteration: raise result return result当迭代到最后无值的时候,会抛出StopIteration异常,不过好些自己的实现函数会去预先处理,不抛此异常例如:it = iter(lst)try: while True: ... 阅读全文
posted @ 2013-11-05 11:47 liuyongjians 阅读(153) 评论(0) 推荐(0) 编辑
 

2013年10月10日

摘要: 这两天看gevent相关内容,突然涉及到callback相关的问题,就觉得有必要深究一下其中的机制原理。网上找了好久,发现一篇http://www.zhihu.com/question/207485141 用户的输入设备发送信息给 device driver。2 Device driver 将信息发给某些 manager 程序。比如说,大多数鼠标和键盘动作都会传给 window manager。3 Window manager 会把这些动作翻译成 event,通过 IPC 机制传给 app。4 App 的 UI framework 会把这些通过 IPC 接受到的 event 放到 event. 阅读全文
posted @ 2013-10-10 16:11 liuyongjians 阅读(170) 评论(0) 推荐(0) 编辑
 

2013年9月2日

摘要: 用bottle的一个项目中,uwsgi作为bottle跟nignx通信协议,取代fastcgi。uwsig配置脚本如下: 0.0.0:9999 128 true /yourpythonprojectpathr/log/uwsgi.pid 9 /yourpythonprojectpathr/ .. application true true true true 512 /yourpythonprojectpathr/log/uwsgi.lognignx放置前面,做反向代理。server { listen 80; ... 阅读全文
posted @ 2013-09-02 17:15 liuyongjians 阅读(979) 评论(0) 推荐(0) 编辑
 

2013年8月30日

摘要: gunicorn起动此项目时。 报错: File "/usr/local/python2.7/lib/python2.7/site-packages/gunicorn/workers/workertmp.py", line 12, in PLATFORM = platform.system()AttributeError: 'module' object has no attribute 'system' 然后看源码,platform是python自带的包,没有问题。最后经人指点,发现项目工程目录中有一个包名是Platform,去掉platf 阅读全文
posted @ 2013-08-30 18:38 liuyongjians 阅读(6686) 评论(0) 推荐(0) 编辑
 

2013年8月9日

摘要: 需求:把xls里所有sheet的内容以每行一条记录写入txt文件内!import xlrddef read_xls(src_file,des_file): data = xlrd.open_workbook(src_file) file = open(des_file,'aw+') for sheet_num in xrange(len(data.sheets())): mysheet = data.sheets()[sheet_num] for row in xrange(0, mysheet.nrows): tmp = ... 阅读全文
posted @ 2013-08-09 11:24 liuyongjians 阅读(223) 评论(0) 推荐(0) 编辑