A "Best of the Best Practices" (BOBP) guide to developing in Python.
- "Build tools for others that you want to be built for you." - Kenneth Reitz
- "Simplicity is alway better than functionality." - Pieter Hintjens
| #!/usr/bin/env/python | |
| # | |
| # More of a reference of using jinaj2 without actual template files. | |
| # This is great for a simple output transformation to standard out. | |
| # | |
| # Of course you will need to "sudo pip install jinja2" first! | |
| # | |
| # I like to refer to the following to remember how to use jinja2 :) | |
| # http://jinja.pocoo.org/docs/templates/ | |
| # |
| #/usr/bin/python | |
| #-*- coding: utf-8 -*- | |
| import re | |
| import os | |
| import subprocess | |
| def shell(cmd): |
| #!/bin/env python | |
| #-*- coding: utf-8 -*- | |
| """ 发布特定分支的 Nginx conf, 这里发布以 autodeploy 开头的分支. | |
| 此脚本跑在 Jenkins 任务下, 此任务通过 Jenkins 的 Gerrit Trigger | |
| 插件监控 Nginx conf (Gerrit 项目) 的 Ref Updated 和 Change Merged 事件, | |
| 而且分支要匹配 **/autodeploy*, 满足条件触发此任务. | |
| 1. 先计算出最近一次修改的以 autodeploy 开头的分支, |
| #!/bin/env python | |
| """ argparse example. | |
| url: https://mkaz.com/2014/07/26/python-argparse-cookbook/ | |
| """ | |
| import argparse | |
| #!/bin/env python | |
| import tornado.ioloop | |
| import tornado.web | |
| import tornado.httpserver | |
| def _Exception(func): | |
| def _decorator(*args, **kwargs): |
| #!/usr/bin/env python | |
| # -*- coding: utf-8 -*- | |
| """ Nginx 配置文件发布. | |
| 支持使用 lvs http check 发布和 reload 发布两种方式. | |
| """ |
| test 是一个目录: | |
| test | |
| ├── __init__.py | |
| └── echo.py | |
| extend_path 会扫描 sys.path 下面的每一个目录, 如果目录下存在 __name__ 这个子目录, | |
| 而且子目录下存在 __init__.py 文件, 那么就把子目录加入 __path__ ; | |
| 而且 它会扫描 sys.path 的目录下是否有 __name__.pkg 文件, 如果有会把里面的路径都加到 __path__(不做检查). |
| #!/bin/env python | |
| # -*- coding: utf-8 -*- | |
| """ partial 实例. | |
| 看上面的源代码, 可以看出 partial 接受一个函数和若干参数, 返回一个新的函数,新的函数已经包含了之前的函数和参数, | |
| 而新函数执行时引入的新参数会和之前的参数相加, 真正执行的函数还是之前的函数. | |
| 所以, partial 的作用是动态绑定参数, 第一次用 partial 生成新函数, 第二次继续绑定参数并执行. |
| #!/usr/bin/python | |
| #-*- coding: utf-8 -*- | |
| """ | |
| output: | |
| execute now1(): | |
| 2013-12-25 | |
| call now2(): | |
| 2013-12-25 |