Skip to content

Instantly share code, notes, and snippets.

View hirokiky's full-sized avatar

Hiroki Kiyohara hirokiky

View GitHub Profile
@hirokiky
hirokiky / generate_cache_key.py
Created February 24, 2014 03:47
Generating cache key to correspond to functions and it's arguments.
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 August 29, 2015 13:56
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'.
#! /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 August 29, 2015 14:00
A bash script to get IPA dictionary for igo.
#!/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
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 August 29, 2015 14:01
Get a LGHT picture from LGTM.in ramdomly
#! /usr/bin/env python
"""
Get a LGHT picture from LGTM.in ramdomly.
Usage
=====
::
$ ./lgtm.py
@hirokiky
hirokiky / wraperrordict.py
Last active August 29, 2015 14:01
Adding methods for django's ErrorDict. (by ugly way). I can't prefigure what will happen by using this. maybe some interfaces are lacking.
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 August 29, 2015 14:01
trying to get pagent having children on django 1.6.
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 02:33
Django Middleware to add attribute for Accept header of HTTP
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 June 25, 2014 01:45
Creating random 8 letter string constructed by alphanumeric.
#! /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)])
_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