项目当中用到 https://github.com/Cirru/sepal.py
贴一点笔记.

PyPI

社区模块方案选用 pip, 在 PyPI 上查询模块, 入门教程:
http://peterdowns.com/posts/first-time-with-pypi.html

python setup.py register -r pypitest
python setup.py sdist upload -r pypitest
python setup.py register -r pypi
python setup.py sdist upload -r pypi

尾递归

Python 不支持尾递归优化, 社区有提供优化的脚本(不过实际项目使用有问题):
http://calebmadrigal.com/tail-call-optimization-in-python/

刷新 REPL 引用模块

同样模仿 Clojure 可以在 REPL 当中测试函数, 那么刷新模块像是这样:

import sys

if 'myModule' in sys.modules:  
    del sys.modules["myModule"]

http://stackoverflow.com/a/3194343/883571

AST

AST 的文档比较丰富的, 不过也比较庞杂, 实现起来估计也会累
考虑到要实现的 AST 的量, 我考虑暂停试验算了.
http://eli.thegreenplace.net/2009/11/28/python-internals-working-with-python-asts/
https://pypi.python.org/pypi/astdump/3.3
http://greentreesnakes.readthedocs.org/en/latest/tofrom.html
https://docs.python.org/2/library/ast.html

import ast
a = ast.literal_eval("[1,2,3,4]") //evaluate an expression safely.
import ast
source = '2 + 2'
node = ast.parse(source, mode='eval')
ast.dump(node)

http://stackoverflow.com/a/13350121/883571

还可以用 codegen.to_source 生成代码:

import ast
import codegen

ast.parse('print(1 + 2)') # return AST
ast.dump(ast.parse('print(1 + 2)')) # return readable AST
codegen.to_source.dump(ast.parse('print(1 + 2)')) # generate code

AST 当中用到一些 keyword arguments:
http://stackoverflow.com/a/1419160/883571

import

Python 模块引用一句 Module Search Path 查找, 可以从 sys.path 查看
https://docs.python.org/2/tutorial/modules.html#the-module-search-path

package 的目录会有 __init__.py 文件, 引入需要暴露的模块
比如这样是把 sepal.py 文件的 transform 函数暴露出去

from sepal import transform

读取文件

with open ("data.txt", "r") as myfile:
    data=myfile.read().replace('n', '')

http://stackoverflow.com/a/8369345/883571

Test

安装 nosetests 来进行测试
http://pythontesting.net/framework/nose/nose-introduction/
http://pythontesting.net/framework/unittest/unittest-introduction/

Dependency

install_requires 字段用于声明依赖
http://www.scotttorborg.com/python-packaging/dependencies.html

本文固定链接: http://www.js-code.com/python/python_49365.html