Skip to content

Instantly share code, notes, and snippets.

Avatar
🐈
Stray Episode

秋葉 Hanaasagi

🐈
Stray Episode
View GitHub Profile
View update.py
import sys
import os
import time
py = sys.version_info
if py < (3, 0, 0):
input = raw_input
_version = 1.0
View methodcaller.py
# -*-coding:UTF-8-*-
from operator import methodcaller
l = ['abc', 'efg']
map(methodcaller('upper'), l)
View get_ip_address.py
# -*-coding:UTF-8-*-
# python2.x
import socket
import struct
import fcntl
def get_ip_address(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(
s.fileno(),
View tokenizer.py
# copy from https://docs.python.org/3.5/library/re.html
import collections
import re
Token = collections.namedtuple('Token', ['typ', 'value', 'line', 'column'])
def tokenize(code):
keywords = {'IF', 'THEN', 'ENDIF', 'FOR', 'NEXT', 'GOSUB', 'RETURN'}
token_specification = [
View flask_slow_query_log.py
#!/usr/bin/env python
# encoding: utf-8
import logging
from logging.handlers import RotatingFileHandler
from flask_sqlalchemy import get_debug_queries
# ...
app.config['DATABASE_QUERY_TIMEOUT'] = 0.001
@Hanaasagi
Hanaasagi / re_flatten.py
Last active March 19, 2017 03:47
将正则表达式中的捕获组转换为非捕获组
View re_flatten.py
# borrow from bottle.py
def _re_flatten(p):
''' Turn all capturing groups in a regular expression pattern into
non-capturing groups. '''
if '(' not in p: return p
return re.sub(r'(\\*)(\(\?P<[^>]+>|\((?!\?))',
lambda m: m.group(0) if len(m.group(1)) % 2 else m.group(1) + '(?:', p)
@Hanaasagi
Hanaasagi / epoll_server.py
Created March 20, 2017 07:48
epoll server
View epoll_server.py
import socket
import select
import argparse
'''
epoll 水平触发模式demo
'''
SERVER_HOST = '127.0.0.1'
EOL1 = b'\n\n'
EOL2 = b'\n\r\n'
@Hanaasagi
Hanaasagi / count_lines.sh
Last active March 29, 2017 04:40
统计代码行数
View count_lines.sh
find . -name "*.py" |xargs cat |grep -v ^$ |wc -l
@Hanaasagi
Hanaasagi / traceback.py
Created March 26, 2017 14:29
捕捉异常并进行格式化输出
View traceback.py
import sys
import inspect
import textwrap
def error():
raise ZeroDivisionError
try:
error()
except:
@Hanaasagi
Hanaasagi / ANSI_escape_color_code.py
Last active April 18, 2017 05:25
list all colors combination in terminal
View ANSI_escape_color_code.py
for color in itertools.product([x for x in range(30, 38)], [y for y in range(40, 48)]):
print('\x1b[{0}m\x1b[{1}m{0}{1}\x1b[0m'.format(*color))