Skip to content

Instantly share code, notes, and snippets.

View FGtatsuro's full-sized avatar

FGtatsuro FGtatsuro

View GitHub Profile
@FGtatsuro
FGtatsuro / _logging.py
Created July 5, 2018 23:54
General logger factory
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from logging import getLogger, Formatter, StreamHandler, DEBUG, NOTSET
def get_logger(name, level=DEBUG, formatter=None, handler=None):
_formatter = formatter or Formatter('%(levelname)s:%(name)s:%(asctime)s:%(message)s')
@FGtatsuro
FGtatsuro / namespace.py
Last active July 13, 2018 10:42
Namespace which can be accessed via both attributes and subscripts.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from itertools import chain
class Namespace(dict):
"""Create a namespace.
Compared with types.SimpleNamespace, this class supports
@FGtatsuro
FGtatsuro / mapper.py
Last active May 10, 2018 08:18
Object Mapper
d = {
'key': 'value'
}
# https://tokibito.hatenablog.com/entry/20130406/1365232594
class Mapper(object):
# https://docs.python.jp/3/reference/datamodel.html#object.__init_subclass__
def __init_subclass__(cls, fields, **kwargs):
super().__init_subclass__(**kwargs)
@FGtatsuro
FGtatsuro / nvim_apiinfo.sh
Last active March 21, 2018 01:06
API info of Neovim
# Following doc is deprecated.
# https://github.com/neovim/neovim/blob/master/runtime/doc/msgpack_rpc.txt#L56
nvim --api-info | python -c 'import msgpack, sys, json, neovim.api.common; \
print(json.dumps(neovim.api.common.walk(neovim.api.common.decode_if_bytes, \
msgpack.unpackb(sys.stdin.buffer.read())), indent=4))'
@FGtatsuro
FGtatsuro / pyqt_test.py
Created March 15, 2018 05:26
PyQt/PySide tests
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
class Form(QWidget):
def __init__(self, parent=None):
super(Form, self).__init__(parent)
@FGtatsuro
FGtatsuro / python_build.sh
Last active December 19, 2018 03:26
Python build (on OSX)
# FYI: Homebrew example
# CONFIG_ARGS = "'--prefix=/usr/local/opt/python' '--enable-ipv6' '--datarootdir=/usr/local/opt/python/share' '--datadir=/usr/local/opt/python/share' '--enable-framework=/usr/local/opt/python/Frameworks' '--without-ensurepip' '--with-dtrace' '--without-gcc' '--enable-loadable-sqlite-extensions' 'MACOSX_DEPLOYMENT_TARGET=10.12' 'CC=clang' 'PKG_CONFIG_PATH=/usr/local/opt/sqlite/lib/pkgconfig:/usr/local/opt/openssl/lib/pkgconfig:/usr/local/opt/xz/lib/pkgconfig' 'PKG_CONFIG_LIBDIR=/usr/lib/pkgconfig:/usr/local/Homebrew/Library/Homebrew/os/mac/pkgconfig/10.12'"
#
# FYI: pyenv example
# CONFIG_ARGS = "'--prefix=/Users/tatsuro/.anyenv/envs/pyenv/versions/3.3.6' '--libdir=/Users/tatsuro/.anyenv/envs/pyenv/versions/3.3.6/lib' '--enable-framework' '--enable-framework=/Users/tatsuro/.anyenv/envs/pyenv/versions/3.3.6' 'LDFLAGS=-L/usr/local/opt/readline/lib -L/usr/local/opt/readline/lib -L/Users/tatsuro/.anyenv/envs/pyenv/versions/3.3.6/lib ' 'CPPFLAGS=-I/usr/local/opt/readline/include -I/usr/local
@FGtatsuro
FGtatsuro / flatten.py
Created January 28, 2018 12:48
Flatten on Python
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# https://docs.python.org/3.5/howto/pyporting.html#prevent-compatibility-regressions
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
def flatten(_iter):
''' Flatten iterable even if it includes other iterables.
@FGtatsuro
FGtatsuro / java_to_groovy.py
Last active December 14, 2017 02:23
Move Java files To Groovy directories
import os, shutil
p = 'path'
moves = [(dirpath, filenames) for dirpath, dirnames, filenames in os.walk(p)]
for dirpath_src, filenames in moves:
dirpath_dest = dirpath_src.replace('java', 'groovy')
if not os.path.exists(dirpath_dest):
os.mkdir(dirpath_dest)
for f in filenames:
abspath_src = os.path.join(dirpath_src, f)
@FGtatsuro
FGtatsuro / wsgi.py
Created September 29, 2017 09:22
Example of WSGI server/application/middleware
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# https://docs.python.org/3.5/howto/pyporting.html#prevent-compatibility-regressions
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from logging import getLogger, Formatter, StreamHandler, DEBUG
import pprint
@FGtatsuro
FGtatsuro / ipaddress.sh
Created February 28, 2017 11:12
IPアドレスの適当な取得(シェル)
$ ifconfig en0 | awk '/inet addr:/{$2=substr($2,6)} /inet /{print $2}'
10.5.2.55