python笔记


TIME

GMT时间格式

GMT_FORMAT = '%a, %d %b %Y %H:%M:%S GMT'

获取某个时间的时间戳

import datetime
import time

string = "2018-03-31"
date_time = datetime.datetime.strptime(string, '%Y-%m-%d')
time_time = time.mktime(date_time.timetuple())
print(time_time)

得到当天0点时间

today = datetime.datetime.today()
b = datetime.datetime(today.year, today.month, today.day, 0, 0, 0)
print(b)

now = time.time()
midnight = now - (now % 86400) + time.timezone
itime = time.ctime(midnight)
print(itime)

时间或时间戳与字符串转换

#把datetime转成字符串
def datetime_toString(dt):
    return dt.strftime("%Y-%m-%d-%H")

#把字符串转成datetime
def string_toDatetime(string):
    return datetime.strptime(string, "%Y-%m-%d-%H")

#把字符串转成时间戳形式
def string_toTimestamp(strTime):
    return time.mktime(string_toDatetime(strTime).timetuple())

#把时间戳转成字符串形式
def timestamp_toString(stamp):
    return time.strftime("%Y-%m-%d-%H", time.localtime(stamp))

#把时间戳转成datetime
def timestamp_toDatetime(stamp):
    return datetime.fromtimestamp(stamp)

#把datetime类型转外时间戳形式
def datetime_toTimestamp(dateTim):
    return time.mktime(dateTim.timetuple())

秒转化为时分秒

def time_string(seconds):
    hours, _minutes = divmod(seconds, 3600)
    minutes, seconds = divmod(_minutes, 60)
    return (hours, minutes, seconds)

获取上个月开始与结束时间

from datetime import datetime, timedelta


def gen_zero_time():
    '''
    生成当天零点时间
    '''
    today = datetime.today()
    b = datetime(today.year, today.month, today.day, 0, 0, 0)
    return b


d = gen_zero_time()
end_date = d.replace(day=1)
start_date = (d.replace(day=1) + timedelta(days=-1)).replace(day=1)
print(start_date)
print(end_date)

获取上周星期天与星期六

from datetime import datetime, timedelta
from dateutil import relativedelta
today = datetime.now()
start = today - timedelta((today.weekday() + 1) % 7)
sat = start + relativedelta.relativedelta(weekday=relativedelta.SA(-1))
sun = sat + relativedelta.relativedelta(weekday=relativedelta.SU(-1))
print(sat)
print(sun)

获取上周时间(星期天零点到星期天零点)

from datetime import datetime, timedelta


def gen_zero_time():
    '''
    生成当天零点时间
    '''
    today = datetime.today()
    b = datetime(today.year, today.month, today.day, 0, 0, 0)
    return b


a = gen_zero_time()
start_date = a + timedelta(days=-a.weekday() - 1, weeks=-1)
end_date = start_date + timedelta(days=7)
print(start_date)
print(end_date)

HTTP

在网址中加入参数

import urllib
import urlparse


def url_add_params(url, **params):
    """ 在网址中加入新参数 """
    pr = urlparse.urlparse(url)
    query = dict(urlparse.parse_qsl(pr.query))
    query.update(params)
    prlist = list(pr)
    prlist[4] = urllib.urlencode(query)
    return urlparse.ParseResult(*prlist).geturl()


if __name__ == "__main__":
    url = 'http://bbs.163.com'
    print url_add_params(url, token=123, site="bbs")

urllib2发送json数据 POST请求

import json
import urllib2

data = {
        'ids': [12, 3, 4, 5, 6]
}

req = urllib2.Request('http://example.com/api/posts/create')
req.add_header('Content-Type', 'application/json')

response = urllib2.urlopen(req, json.dumps(data))

urllib2发送PUT或DELETE请求

import urllib2
opener = urllib2.build_opener(urllib2.HTTPHandler)
request = urllib2.Request('http://example.org', data='your_put_data')
request.add_header('Content-Type', 'your/contenttype')
request.get_method = lambda: 'PUT'
url = opener.open(request)

# 或者使用httplib
import httplib
conn = httplib.HTTPConnection('www.foo.com')
conn.request('PUT', '/myurl', body)
resp = conn.getresponse()
content = resp.read()

Syntax

解析赋值字符串

即把

a=b c=d e=f g.a=1 g.a.b=2 g.c.d=3 h="a b c" i='sss'

解析成

{'a': 'b', 'c': 'd', 'e': 'f', 'g': {'a': {'b': '2'}, 'c': {'d': '3'}}, 'h': 'a b c', 'i': 'sss'}
regex = re.compile(r"([\w.]+|\"[^=]*|'[^=]*)=(\"[^\"]*\"|'[^']*'|.*?)(\s|$)")

def parse_string(string):
    r = {}

    def _get(key):
        key = key.strip()
        if key[0] == key[-1] in ["'", "\""]:
            return key[1:-1]
        return key

    def _update(value, nvalue):
        if not isinstance(nvalue, dict) or not isinstance(value, dict):
            return nvalue
        for k, v in nvalue.items():
            value.setdefault(k, dict())
            if isinstance(v, dict):
                v = _update(value[k], v)
            value[k] = v
        return value

    def _set(key, value):
        for i in key.split(".")[::-1]:
            value = {i: value}
        return value

    for key, value in regex.findall(string):
        r = _update(r, _set(_get(key), _get(value)))
    return r

字典递归更新

即当更新dict的value也是一个dict时,递归更新相应内容

def update(value, nvalue):
    if not isinstance(nvalue, dict) or not isinstance(value, dict):
        return nvalue
    for k, v in nvalue.items():
        if isinstance(v, dict):
            v = update(value[k], v)
        value.update({k: v})
    return value

字典使用dot

class dotdict(dict):

    __setattr__ = dict.__setitem__
    __delattr__ = dict.__delitem__

    def __getattr__(self, name):
        value = self[name]
        if isinstance(value, dict):
            return dotdict(value)
        return value


a = {'a': 1, 'b': 2, 'c': 3, 'd': {'a': '1', '1': 1}}
c = dotdict(a)
print(c.a)
print(c.d.a)

或者

class dotdict(object):
    def __init__(self, **kwargs):
        for key, value in kwargs.items():
            setattr(self, key, value)

单例模式

from functools import wraps
from threading import Lock


def singleton(class_):
    instances = {}
    _lock = Lock()

    @wraps(class_)
    def _instance(*args, **kwargs):
        with _lock:
            if class_ not in instances:
                instances[class_] = class_(*args, **kwargs)
            instances[class_].__init__(*args, **kwargs)
        return instances[class_]

    return _instance

@singleton
class AA(object):
    def __init__(self, a):
        self.a = a

    def aa(self):
        print(self.a)


a = AA('ccc').aa()
b = AA('baabb').aa()
print(id(a) == id(b))

Pip

pip更新所有的packages

pip freeze --local | grep -v '^\-e' | cut -d = -f 1  | xargs -n1 pip install -U

如果是更新sudo安装的packages

pip freeze --local | grep -v '^\-e' | cut -d = -f 1  | xargs -n1 sudo pip install -U

pip指定国内源

pip install flask_migrate -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com

判斷是否激活virtualenv

import sys
if hasattr(sys, 'real_prefix'):
   return True

Django model得到所有field name

MyModel._meta.get_all_field_names()

python运行超时设置

import signal

class TimeoutError(Exception):
    pass

def timeout(seconds=10, error_message="Timer expired"):
    def _timeout(func):
        def _handle_timeout(signum, frame):
            raise TimeoutError(error_message)

        def wrapper(*args, **kwargs):
            signal.signal(signal.SIGALRM, _handle_timeout)
            signal.alarm(seconds)
            try:
                result = func(*args, **kwargs)
            finally:
                signal.alarm(0)
            return result

        return wrapper

    return _timeout

json.loads时无法保持字典列表的顺序

https://stackoverflow.com/questions/47111058/json-loads-doesnt-keep-order

from json import loads
from collections import OrderedDict
loads(your_json_string, object_pairs_hook=OrderedDict)

http://www.grokcode.com/864/snakefooding-python-code-for-complexity-visualization/

其它

获取文件最后n行数据

import mmap


def getlastline(fname, n=1):
    with open(fname) as source:
        mapping = mmap.mmap(source.fileno(), 0, prot=mmap.PROT_READ)
    cursor = -1
    while n > 0:
        n -= 1
        cursor = mapping.rfind(b'\n', 0, cursor)
    return mapping[cursor:].decode().split("\n")

python列表中去除烦人的"u"

# !/usr/bin/env python2
# -*- coding: utf-8 -*-

s = [u"python", u"golang", u"lua"]
print(s)
  • python script

    s = [u"python", u"golang", u"lua"]
    print([str(i) for i in s])
  • jinja2

    from jinja2 import Template
    template = Template(
        '[{% for i in consul_join %}"{{ i | string }}"{% if not loop.last %},{% endif %}{% endfor %}]'
    )
    template.render(consul_join=[u"python", u"golang", u"lua"])

常见问题

PROTOCOL_SSLv3 is not defined

https://github.com/docker-library/python/issues/29

bash -c 'sed -i s/PROTOCOL_SSLv3/PROTOCOL_SSLv23/g /usr/local/lib/python2.7/site-packages/gevent/ssl.py && python prober.py'
作者: honmaple
链接: https://honmaple.me/articles/2017/01/python笔记.html
版权: CC BY-NC-SA 4.0 知识共享署名-非商业性使用-相同方式共享4.0国际许可协议
wechat
alipay

加载评论