Skip to content

Instantly share code, notes, and snippets.

@whitekid
whitekid / inject.py
Created October 27, 2017 17:45
inject method into class
from functools import wraps
def inject(klass):
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
setattr(klass, func.__name__, func)
@whitekid
whitekid / app.py
Last active October 26, 2017 05:16
flaskrestful with blueprint
import importlib
import inspect
from flask import Flask
from flask_restful import Resource
app = Flask(__name__)
for module_name in ['v1', 'v2']:
module = importlib.import_module(module_name)
@whitekid
whitekid / extra_args.sh
Last active January 26, 2017 01:02
bash extra argement(--) parsing with getopts
#!/bin/bash
#
# extra_args.sh -l args script_args -- -l extra args
#
# OUTPUT
# opt: l=args
# arg: %script_args%
# extra_args: %-l extra args%
#
@whitekid
whitekid / ansible.cfg
Created December 7, 2016 02:47
ansible.cfg
[defaults]
gathering = smart
fact_caching = redis
fact_caching_timeout = 86400
fact_caching_connection = <redis-host>
roles_path = ~/ansible/roles
filter_plugins = ~/ansible/plugins/filter_plugins
sudo_exe = sudo
sudo_flags = -i
accelerate_multi_key = yes
@whitekid
whitekid / timeit.py
Last active November 9, 2016 04:28
measure execution time of function or code block
import time
from functools import wraps
class timeit(object):
"""measure execution time and print(or log) it
Args:
printer: message print function
if None, print message to stdout
msg: messsage
@whitekid
whitekid / synchronized.py
Created November 8, 2016 06:31
synchronized decorator
from functools import wraps
class synchronized(object):
"""synchronized decorator
usage:
@synchronized(lock)
def foo():
@whitekid
whitekid / ps.py
Last active November 10, 2016 07:15
Profile show
#!/usr/bin/env python
# -*- coding: utf8 -*-
"""very very simple profile viewer
To generate profile data run:
python -m cProfile -o profile.dump myscript
OR
@whitekid
whitekid / combi.py
Last active October 18, 2016 01:57
"""Just simple combination
todo recursive call s should be call by reference
"""
import itertools
import string
import unittest
def combination(s, n):
@whitekid
whitekid / ipaddr.py
Last active September 28, 2016 02:49
#!/usr/bin/python
"""network interface address를 관리하는 ansible 모듈
library 디렉토리에 넣어서 사용하면 됩니다.
"""
import re
from ansible.module_utils.basic import AnsibleModule
DOCUMENTATION = '''
---
"""Test Helper"""
from unittest.mock import patch
class MockMixin(object):
"""TestCase Helper
아래처럼 사용하면, Mocking된 object가 cleanup되지 않아서 다음 테스트에도
영향을 미친다. ::