TIME
GMT时间格式
1GMT_FORMAT = '%a, %d %b %Y %H:%M:%S GMT'
获取某个时间的时间戳
1import datetime
2import time
3
4string = "2018-03-31"
5date_time = datetime.datetime.strptime(string, '%Y-%m-%d')
6time_time = time.mktime(date_time.timetuple())
7print(time_time)
得到当天0点时间
1today = datetime.datetime.today()
2b = datetime.datetime(today.year, today.month, today.day, 0, 0, 0)
3print(b)
4
5now = time.time()
6midnight = now - (now % 86400) + time.timezone
7itime = time.ctime(midnight)
8print(itime)
时间或时间戳与字符串转换
1#把datetime转成字符串
2def datetime_toString(dt):
3 return dt.strftime("%Y-%m-%d-%H")
4
5#把字符串转成datetime
6def string_toDatetime(string):
7 return datetime.strptime(string, "%Y-%m-%d-%H")
8
9#把字符串转成时间戳形式
10def string_toTimestamp(strTime):
11 return time.mktime(string_toDatetime(strTime).timetuple())
12
13#把时间戳转成字符串形式
14def timestamp_toString(stamp):
15 return time.strftime("%Y-%m-%d-%H", time.localtime(stamp))
16
17#把时间戳转成datetime
18def timestamp_toDatetime(stamp):
19 return datetime.fromtimestamp(stamp)
20
21#把datetime类型转外时间戳形式
22def datetime_toTimestamp(dateTim):
23 return time.mktime(dateTim.timetuple())
秒转化为时分秒
1def time_string(seconds):
2 hours, _minutes = divmod(seconds, 3600)
3 minutes, seconds = divmod(_minutes, 60)
4 return (hours, minutes, seconds)
获取上个月开始与结束时间
1from datetime import datetime, timedelta
2
3
4def gen_zero_time():
5 '''
6 生成当天零点时间
7 '''
8 today = datetime.today()
9 b = datetime(today.year, today.month, today.day, 0, 0, 0)
10 return b
11
12
13d = gen_zero_time()
14end_date = d.replace(day=1)
15start_date = (d.replace(day=1) + timedelta(days=-1)).replace(day=1)
16print(start_date)
17print(end_date)
获取上周星期天与星期六
1from datetime import datetime, timedelta
2from dateutil import relativedelta
3today = datetime.now()
4start = today - timedelta((today.weekday() + 1) % 7)
5sat = start + relativedelta.relativedelta(weekday=relativedelta.SA(-1))
6sun = sat + relativedelta.relativedelta(weekday=relativedelta.SU(-1))
7print(sat)
8print(sun)
获取上周时间(星期天零点到星期天零点)
1from datetime import datetime, timedelta
2
3
4def gen_zero_time():
5 '''
6 生成当天零点时间
7 '''
8 today = datetime.today()
9 b = datetime(today.year, today.month, today.day, 0, 0, 0)
10 return b
11
12
13a = gen_zero_time()
14start_date = a + timedelta(days=-a.weekday() - 1, weeks=-1)
15end_date = start_date + timedelta(days=7)
16print(start_date)
17print(end_date)
HTTP
在网址中加入参数
1import urllib
2import urlparse
3
4
5def url_add_params(url, **params):
6 """ 在网址中加入新参数 """
7 pr = urlparse.urlparse(url)
8 query = dict(urlparse.parse_qsl(pr.query))
9 query.update(params)
10 prlist = list(pr)
11 prlist[4] = urllib.urlencode(query)
12 return urlparse.ParseResult(*prlist).geturl()
13
14
15if __name__ == "__main__":
16 url = 'http://bbs.163.com'
17 print url_add_params(url, token=123, site="bbs")
urllib2发送json数据 POST请求
1import json
2import urllib2
3
4data = {
5 'ids': [12, 3, 4, 5, 6]
6}
7
8req = urllib2.Request('http://example.com/api/posts/create')
9req.add_header('Content-Type', 'application/json')
10
11response = urllib2.urlopen(req, json.dumps(data))
urllib2发送PUT或DELETE请求
1import urllib2
2opener = urllib2.build_opener(urllib2.HTTPHandler)
3request = urllib2.Request('http://example.org', data='your_put_data')
4request.add_header('Content-Type', 'your/contenttype')
5request.get_method = lambda: 'PUT'
6url = opener.open(request)
7
8# 或者使用httplib
9import httplib
10conn = httplib.HTTPConnection('www.foo.com')
11conn.request('PUT', '/myurl', body)
12resp = conn.getresponse()
13content = resp.read()
Syntax
解析赋值字符串
即把
1a=b c=d e=f g.a=1 g.a.b=2 g.c.d=3 h="a b c" i='sss'
解析成
1{'a': 'b', 'c': 'd', 'e': 'f', 'g': {'a': {'b': '2'}, 'c': {'d': '3'}}, 'h': 'a b c', 'i': 'sss'}
1regex = re.compile(r"([\w.]+|\"[^=]*|'[^=]*)=(\"[^\"]*\"|'[^']*'|.*?)(\s|$)")
2
3def parse_string(string):
4 r = {}
5
6 def _get(key):
7 key = key.strip()
8 if key[0] == key[-1] in ["'", "\""]:
9 return key[1:-1]
10 return key
11
12 def _update(value, nvalue):
13 if not isinstance(nvalue, dict) or not isinstance(value, dict):
14 return nvalue
15 for k, v in nvalue.items():
16 value.setdefault(k, dict())
17 if isinstance(v, dict):
18 v = _update(value[k], v)
19 value[k] = v
20 return value
21
22 def _set(key, value):
23 for i in key.split(".")[::-1]:
24 value = {i: value}
25 return value
26
27 for key, value in regex.findall(string):
28 r = _update(r, _set(_get(key), _get(value)))
29 return r
字典递归更新
即当更新dict的value也是一个dict时,递归更新相应内容
1def update(value, nvalue):
2 if not isinstance(nvalue, dict) or not isinstance(value, dict):
3 return nvalue
4 for k, v in nvalue.items():
5 if isinstance(v, dict):
6 v = update(value[k], v)
7 value.update({k: v})
8 return value
字典使用dot
1class dotdict(dict):
2
3 __setattr__ = dict.__setitem__
4 __delattr__ = dict.__delitem__
5
6 def __getattr__(self, name):
7 value = self[name]
8 if isinstance(value, dict):
9 return dotdict(value)
10 return value
11
12
13a = {'a': 1, 'b': 2, 'c': 3, 'd': {'a': '1', '1': 1}}
14c = dotdict(a)
15print(c.a)
16print(c.d.a)
或者
1class dotdict(object):
2 def __init__(self, **kwargs):
3 for key, value in kwargs.items():
4 setattr(self, key, value)
单例模式
1from functools import wraps
2from threading import Lock
3
4
5def singleton(class_):
6 instances = {}
7 _lock = Lock()
8
9 @wraps(class_)
10 def _instance(*args, **kwargs):
11 with _lock:
12 if class_ not in instances:
13 instances[class_] = class_(*args, **kwargs)
14 instances[class_].__init__(*args, **kwargs)
15 return instances[class_]
16
17 return _instance
18
19@singleton
20class AA(object):
21 def __init__(self, a):
22 self.a = a
23
24 def aa(self):
25 print(self.a)
26
27
28a = AA('ccc').aa()
29b = AA('baabb').aa()
30print(id(a) == id(b))
Pip
pip更新所有的packages
1pip freeze --local | grep -v '^\-e' | cut -d = -f 1 | xargs -n1 pip install -U
如果是更新sudo安装的packages
1pip freeze --local | grep -v '^\-e' | cut -d = -f 1 | xargs -n1 sudo pip install -U
pip指定国内源
1pip install flask_migrate -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
判斷是否激活virtualenv
1import sys
2if hasattr(sys, 'real_prefix'):
3 return True
Django model得到所有field name
1MyModel._meta.get_all_field_names()
python运行超时设置
1import signal
2
3class TimeoutError(Exception):
4 pass
5
6def timeout(seconds=10, error_message="Timer expired"):
7 def _timeout(func):
8 def _handle_timeout(signum, frame):
9 raise TimeoutError(error_message)
10
11 def wrapper(*args, **kwargs):
12 signal.signal(signal.SIGALRM, _handle_timeout)
13 signal.alarm(seconds)
14 try:
15 result = func(*args, **kwargs)
16 finally:
17 signal.alarm(0)
18 return result
19
20 return wrapper
21
22 return _timeout
库
json.loads时无法保持字典列表的顺序
https://stackoverflow.com/questions/47111058/json-loads-doesnt-keep-order
1from json import loads
2from collections import OrderedDict
3loads(your_json_string, object_pairs_hook=OrderedDict)
http://www.grokcode.com/864/snakefooding-python-code-for-complexity-visualization/
其它
获取文件最后n行数据
1import mmap
2
3
4def getlastline(fname, n=1):
5 with open(fname) as source:
6 mapping = mmap.mmap(source.fileno(), 0, prot=mmap.PROT_READ)
7 cursor = -1
8 while n > 0:
9 n -= 1
10 cursor = mapping.rfind(b'\n', 0, cursor)
11 return mapping[cursor:].decode().split("\n")
python列表中去除烦人的"u"
1# !/usr/bin/env python2
2# -*- coding: utf-8 -*-
3
4s = [u"python", u"golang", u"lua"]
5print(s)
-
python script
1s = [u"python", u"golang", u"lua"] 2print([str(i) for i in s]) -
jinja2
1from jinja2 import Template 2template = Template( 3 '[{% for i in consul_join %}"{{ i | string }}"{% if not loop.last %},{% endif %}{% endfor %}]' 4) 5template.render(consul_join=[u"python", u"golang", u"lua"])
常见问题
PROTOCOL_SSLv3 is not defined
https://github.com/docker-library/python/issues/29
1bash -c 'sed -i s/PROTOCOL_SSLv3/PROTOCOL_SSLv23/g /usr/local/lib/python2.7/site-packages/gevent/ssl.py && python prober.py'
知识共享署名-非商业性使用-相同方式共享4.0国际许可协议