Skip to content

Instantly share code, notes, and snippets.

View vimiix's full-sized avatar
🧱
Coding

Vimiix Yao vimiix

🧱
Coding
View GitHub Profile
@vimiix
vimiix / python-six-module.md
Created December 26, 2017 06:46
关于python six库的小知识

由于 Python 在 2-3 版本的升级过程中的激进演变,导致了应用的兼容困难, 很多第三方库为了同时兼容 Python 2 和 3 不得不使用一些难看的 patch 来解决兼容性问题。 比较常见的做法是自己编写 _compat.py(compatibility 兼容性的缩写) 或者直接使用 six 库。

six 支持 Python 2.5+ 版本,你可以引用它,也可以将其直接复制到代码库中。

six 是 23 的意思,之所以使用 23 而不是 2+3 ,是因为 five 这个名字已经被 Zope Five 所使用。

官方文档:six

@vimiix
vimiix / view.py
Created January 9, 2018 03:12
view class in Django.
class View:
"""
Intentionally simple parent class for all views. Only implements
dispatch-by-method and simple sanity checking.
"""
http_method_names = ['get', 'post', 'put', 'patch', 'delete', 'head', 'options', 'trace']
def __init__(self, **kwargs):
"""
@vimiix
vimiix / RESTMiddleware.py
Created January 9, 2018 06:51 — forked from g00fy-/RESTMiddleware.py
Simple Django Middleware for handling Form & Multipart Form PUT & DELETE methods AKA REST (GET,POST,PUT,DELETE)
from django.http import QueryDict
from django.http.multipartparser import MultiValueDict
class RESTMiddleware(object):
def process_request(self,request):
request.PUT=QueryDict('')
request.DELETE = QueryDict('')
method = request.META.get('REQUEST_METHOD','').upper() #upper ? rly?
if method == 'PUT':
self.handle_PUT(request)
@vimiix
vimiix / secrets.py
Created February 1, 2018 08:14 — forked from onjin/secrets.py
python 3.6 secrets module
"""Generate cryptographically strong pseudo-random numbers suitable for
managing secrets such as account authentication, tokens, and similar.
See PEP 506 for more information.
https://www.python.org/dev/peps/pep-0506/
"""
__all__ = ['choice', 'randbelow', 'randbits', 'SystemRandom',
'token_bytes', 'token_hex', 'token_urlsafe',
class MySQLCursor:
"""创建一个游标类"""
def __init__(self,cursor,logger):
self.cursor=cursor
self.logger=logger
def execute(self,sql,params=None):
self.logger.info(sql+str(params))
self.cursor.execute(sql, params)
class OracleCursor:
"""游标类"""
def __init__(self, cursor, logger, dict_result):
self.cursor = cursor
self.logger = logger
self.dict_result = dict_result
def _dict_result(self, cursor):
cols = [d[0].lower() for d in cursor.description]
@vimiix
vimiix / merge_sort.py
Last active February 8, 2018 06:49
归并排序
# !/usr/bin/python3
# coding=utf-8
# Time: O(n*logn)
# Space: O(n)
# 合并
def merge(l1, l2):
index1 = index2 = 0
r = []
@vimiix
vimiix / quick_sort.py
Last active May 30, 2018 08:12
快速排序
# !/usr/bin/python3
# coding=utf-8
# Time: O(n*logn)
# Space: O(n)
def qsort(arr):
# 基线条件
if len(arr) < 2:
return arr
# coding:utf-8
import socket
EOL1 = "\n\n"
EOL2 = "\n\r\n"
body = """Hello World! <h1> Simple socket server</h1>"""
response_params = [
'HTTP/1.0 200 OK',
@vimiix
vimiix / get_first_item_from_OrderedDict.py
Created February 27, 2018 07:42
从有序字典中获取第一个元素的方法
from collections import OrderedDict
od = OrderedDict(zip('foo', 'bar'))
# 方法1
od.keys()[-1] # 仅适用于2
od.values()[-1]
od.items()[-1]
list(od.items())[-1] # 兼容 3.x