很多公司都大量使用了python,其中有一些开发规范,code guidline, 通用组件,基础框架是可以共用的。
每个公司都自己搞一套, 太浪费人力,我想开一帖和大家讨论一下这些python基础设施的搭建。
原则是我们尽量不重新发明轮子,但开源组件这么多,也要有个挑选的过程和组合使用的过程,在这里讨论一下。
另一方面,有些开源组件虽然强大,但我们不能完全的驾驭它,或只使用其中很少的一部分,我们就可以考虑用python实现一个简单的轮子,可控性更强,最好不要超过300行代码。
| def prime_generator(): | |
| _primes = [2] | |
| current = 3 | |
| while True: | |
| if all(current % prime != 0 for prime in _primes): | |
| _primes.append(current) | |
| yield current | |
| current += 1 |
| ; ~/.pip/pip.conf | |
| [global] | |
| use-mirrors=true | |
| mirrors=http://e.pypi.python.org | |
| index-url=http://e.pypi.python.org/simple |
| def slugify(value): | |
| """ | |
| Normalizes string, converts to lowercase, removes non-alpha characters, | |
| and converts spaces to hyphens. | |
| Took from django sources. | |
| """ | |
| value = Markup(value).striptags() | |
| if type(value) == unicode: | |
| import unicodedata |
| #!/usr/bin/env python | |
| # -*- coding: UTF-8 -*- | |
| import json | |
| point1 = 39.988948, 116.417802 | |
| point2 = 39.955797, 116.483548 | |
| results = [] | |
| with open('points.json') as f: |
| # url http://stackoverflow.com/questions/13447226/count-how-many-times-a-part-of-a-key-appears-in-a-dictionary-python | |
| from collections import Counter | |
| from itertools import chain | |
| a = { (1,2):3, (1,3):5, (2,1):6 } | |
| # first | |
| # nested iter, order is important | |
| Counter(j for k in a for j in k) |
| { | |
| "meta": { | |
| "limit": 3, | |
| "next": "/myapp/comment/comment/?note=1&offset=3&limit=3&format=json", | |
| "offset": 0, | |
| "previous": null, | |
| "total_count": 4 | |
| }, | |
| "objects": [ | |
| { |
| <!doctype html> | |
| <meta charset="utf-8"> | |
| <style> | |
| svg { | |
| font: 10px sans-serif; | |
| } | |
| </style> | |
| <body> | |
| <script src="http://d3js.org/d3.v3.min.js"></script> |
| In [1]: %paste | |
| import sys | |
| import logging | |
| PROGRESS = 1000 | |
| class MyLogger(logging.Logger): | |
| PROGRESS = PROGRESS | |
| LOG_FORMATTER = '%(asctime)s - %(levelname)-10s - %(funcName)s - %(message)s' |
| var FindProxyForURL = function(init, profiles) { | |
| return function(url, host) { | |
| "use strict"; | |
| var result = init, scheme = url.substr(0, url.indexOf(":")); | |
| do { | |
| result = profiles[result]; | |
| if (typeof result === "function") result = result(url, host, scheme); | |
| } while (typeof result !== "string" || result.charCodeAt(0) === 43); | |
| return result; | |
| }; |