limbo

Results 16 issues of limbo

Body with **Markdown** support

两种实现方式:代码见[gist](https://gist.github.com/d4bb226c89f25725c47ccc85355b7656) `contextlib`这个库小而精, 可以实现很多功能,如回滚: ``` python In [20]: @contextlib.contextmanager ....: def list_transaction(lis): ....: working = list(lis) ....: yield working ....: lis[:] = working ....: In [21]: item = [1,2,3] In [22]:...

将字节数据直接写入文件的缓冲区 In python2.x: ``` >>>import sys >>>sys.stdout.write(b"hello") hello ``` In python3.x: ``` >>> import sys >>> sys.stdout.write(b'Hello\n') Traceback (most recent call last): File "", line 1, in TypeError: must be...

enhancement

如在某个路径下列出`.py`文件,可通过如下方法: 1. 正则 2. startswith& endswith 3. glob 模块 4. fnmatch模块 如下: ``` pyfiles = [name for name in os.listdir('somedir') if name.endswith('.py')] import glob pyfiles = glob.glob('somedir/*.py') from fnmatch import...

eg: ``` >>> import os >>> path = '/Users/beazley/Data/data.csv' >>> # 获取路径的最后一个组件 >>> os.path.basename(path) 'data.csv' >>> # 获取目录名称 >>> os.path.dirname(path) '/Users/beazley/Data' >>> # 组合路径 >>> os.path.join('tmp', 'data', os.path.basename(path)) 'tmp/data/data.csv' >>>...

Python 2.x `readinto`说不要用它,在《Python 核心编程》中也提到: > file.readinto(buf, size): 从文件读取size个字节到buf缓冲区(已不支持) 但是在《Python Cookbook》中却多次使用它,如[5.9 读取二进制数据到可变缓冲区中](http://python3-cookbook.readthedocs.org/zh_CN/latest/c05/p09_read_binary_data_into_mutable_buffer.html), 让我还纳闷啊。 但是使用过程中确实感觉到这个家伙的强大。 我需要赶紧在电脑上装python 3.x 看下这个家伙与python2.x的区别在哪里,是否废弃了,还是改进了。