Python

分类下相关文章

python orm orator - php laravel eloquent 的复制品

orator - n. 演讲者 这是一个类似 laravel eloquent 的 python orm 库。 https://github.com/sdispater/orator 使用方法与 eloquent 几乎一致,非常清爽。相比之前用过的 peewee,SQLAlchemy,Django ORM,这个库更简洁明了。 唯一的缺点是,这个项目近期非常不活跃,而且作者对于 pull request 置之不理。 hello world 示例 #!/usr/bin/env python # -*- coding: utf-8 -*- from orator import DatabaseM ...

阅读全文...

中午用 Python telnetlib 写了个水木社区的自动挂机涨积分的脚本

核心代码就这么几行,从 Github 上抄的。。。 使用的是 Python 内置的 telnetlib 这个库。 需要注意的是 expect 里的规则是正则,所以判断登录之后修改密码的逻辑一定要将中括号转义。 while result[0] != 5: tn.write("\n") result = tn.expect([ u"酸甜苦辣板".encode("GBK"), u"本日十大衷心祝福".encode("GBK"), u"本日十大热门话题".encod ...

阅读全文...

Python 计时用装饰器

def time_it(method): def timed(*args, **kw): start_time = time.time() ...

阅读全文...

Python 中 uuid1 与 uuid4 的区别

uuid1 是与机器信息相关的一个 uuid,包含了时间信息,以及机器信息. 分布式存储时使用。 uuid4 则是一个纯随机数,与机器无关, 相重的几率很小。通常生成用户id用这个。 ...

阅读全文...

Python Logging

One of the differences between a great programmer and a bad programmer is that a great programmer adds logging and tools that make it easy to debug the program when things fail. -- Henrik Warne 同一服务的不同功能的日志如何记录 根据 Logging Cookbook 里的解释,可以使用 logging.getLogger("app_name") 创建一个父 logger,然后 ...

阅读全文...

python 的两种除法

Python 除法运算符 '/' 与 '//' 的区别 >>> 10 / 3 3 >>> 10 // 3 3 >>> 10 / 3.0 3.3333333333333335 >>> 10 // 3.0 3.0 >>> type(10 // 3.0) <type 'float'> 也就是当除数和被除数都是整数的时候,返回结果都是整数。 当除数和被除数至少有一个是浮点数的时候,返回结果都是浮点数。但是, // 的结果的小数部分永远是 0. `Floor division'' is what Py ...

阅读全文...

SQLAlchemy

SQLAlchemy is the Python SQL toolkit and Object Relational Mapper that gives application developers the full power and flexibility of SQL. alchemy 是啥意思 ['ælkɪmɪ] n. 点金术;魔力 最佳教程 官方文档 (推荐下载 PDF 到 kindle 里慢慢看) Essential SQLAlchemy (太旧了,还是看官方文档吧) 使用 SQLAlchemy 之类的 ORM 相对 raw sql 的好处 ORM 可以将数据库存储的数据 ...

阅读全文...

Python Testing

是否可自动化测试是评判程序好坏的唯一标准 注意 需要区分 mock 不同对象的方法, 例如: mock method (被测试类已被实例化) -> MagicMock mock method argument -> MagicMock mock class (被测试类尚未被实例化, 但是需要 mock 其实例化后的指定方法) -> with patch Mock 与 MagicMock 的区别 MagicMock 更灵活一些,官方推荐默认使用 MagicMock. 什么时候使用 patch decorator 由于 module 和 class 是全局的,所以执行完一 ...

阅读全文...

urllib2 使用 http 代理

需要验证的问题 如何使用代理 设置代理之后是否对 urllib2 有全局影响 测试程序 api.py # -*- coding: utf-8 -*- import urllib2 def get_rsp(url): response = urllib2.urlopen(url) status_code = response.code content = response.read(300) print "status code: %s" % status_code print "content: %s" ...

阅读全文...

gevent

We can become blind by seeing each day as a similar one. -- Paolo Coelho 教程 最佳教程 patch_all 不是一个好主意 gevent.monkey.patch_all() 给所有能打上 patch 的模块打上 gevent.monkey.patch_socket() 只给 socket 模块打上 我觉得最佳的方式是,只针对你需要的模块打 patch, 否则容易造成 gevent 的滥用。 gevent tutorial 称 monkeypatching 为 dark corners of Gevent. 当然 ...

阅读全文...

Python 包管理

PyPI PyPI - the Python Package Index 参考文档 第一次提交参考了 How to submit a package to PyPI — Peter Downs 但是感觉这个文档有点旧了,很多地方会报 warning。 官方的流程在这里 Packaging and Distributing Projects — Python Packaging User Guide documentation 官方文档的问题是,更像是个字典,没有流程。这里有一个不错的流程文档 Minimal Structure — Python Packaging Tutorial 本地调试 ...

阅读全文...