Skip to content

Instantly share code, notes, and snippets.

Avatar

Hiroki Kiyohara hirokiky

View GitHub Profile
@hirokiky
hirokiky / generate_cache_key.py
Created Feb 24, 2014
Generating cache key to correspond to functions and it's arguments.
View generate_cache_key.py
from hashlib import sha1
def default_key_generator(func, *args, **kwargs):
""" モジュール名、関数名 + 文字列化した引数から生成したsha1をキーとするキャッシュ用キー生成関数
"""
func_identifier = '{func.__module__}:{func.__name__}'.format(func=func)
arg_identifier = 'args:{args}kwargs:{kwargs}'.format(args=args, kwargs=kwargs)
return sha1(func_identifier + '::' + arg_identifier).hexdigest()
@hirokiky
hirokiky / xrt.py
Last active Aug 29, 2015
Replace inputed contents with spaces on each line like C-x r t of emacs. 4 white-spaces will be inserted by default. But you can change this stirng by specifying '-i' option'.
View xrt.py
#! /usr/bin/env python
"""
===
xrt
===
Replace inputed contents with spaces on each line like C-x r t of emacs.
4 white-spaces will be inserted by default.
But you can change this stirng by specifying '-i' option'.
@hirokiky
hirokiky / getigoipadic.sh
Last active Aug 29, 2015
A bash script to get IPA dictionary for igo.
View getigoipadic.sh
#!/bin/bash
# This bash script is to get the IPA dictionary for igo,
# allow to morpholigical analysis of Japanese for your project.
# You won't install anything. this script is for just downloading and compiling the dic.
# Actual process for MA is provided by igo-python.
#
# This will do:
# * create 'ipadic' directory here
# * download mecab-ipadic
# * download jar of igo
View mispecparse.py
import argparse
def parse(args):
parser = argparse.ArgumentParser()
parser.add_argument('--send', choices=['zlib', 'bz2', 'none'], dest='send')
parsed = parser.parse_args(args)
if parsed.send:
return parsed.send
else:
return 'zlib'
@hirokiky
hirokiky / lgtm.py
Last active Aug 29, 2015
Get a LGHT picture from LGTM.in ramdomly
View lgtm.py
#! /usr/bin/env python
"""
Get a LGHT picture from LGTM.in ramdomly.
Usage
=====
::
$ ./lgtm.py
@hirokiky
hirokiky / wraperrordict.py
Last active Aug 29, 2015
Adding methods for django's ErrorDict. (by ugly way). I can't prefigure what will happen by using this. maybe some interfaces are lacking.
View wraperrordict.py
class ErrorDict(object):
def __init__(self, errordict):
self.errordict = errordict
def __getitem__(self, item):
return self.errordict[item]
def __setitem__(self, key, value):
self.errordict[key] = value
@hirokiky
hirokiky / has_children_query.py
Last active Aug 29, 2015
trying to get pagent having children on django 1.6.
View has_children_query.py
from django.db import models
class Parent(models.Model):
name = models.CharField(max_length=255)
def __str__(self):
return self.name
@hirokiky
hirokiky / accept_header_middleware.py
Created May 29, 2014
Django Middleware to add attribute for Accept header of HTTP
View accept_header_middleware.py
class AcceptMiddleware(object):
""" Middleware to add attribute for Accept header of HTTP
"""
def process_request(self, request):
acc = [a.split(';')[0] for a in request.META['HTTP_ACCEPT'].split(',')]
setattr(request, 'accepted_types', acc)
return None
@hirokiky
hirokiky / mkpass.py
Created Jun 25, 2014
Creating random 8 letter string constructed by alphanumeric.
View mkpass.py
#! /usr/bin/env python
import string
import random
alphanumeric = string.ascii_letters + string.digits
def main():
return ''.join([random.choice(alphanumeric) for _ in range(8)])
View nyaocat.py
_some_global_value = 'hoge'
class A(object):
def __del__(self):
global _some_global_value
_some_global_value = 'fuga'
class B(object):
def hoge(self):
return _some_global_value