Skip to content

Instantly share code, notes, and snippets.

View hirokiky's full-sized avatar

Hiroki Kiyohara hirokiky

View GitHub Profile
@hirokiky
hirokiky / nodechain.py
Last active December 15, 2015 01:39
The decorator named 'node' takes mapping and call next wsgi apprication. The mapping's key is URL pattern you want to match, and the value is a name for a function which will be called when the key pattern matches to URL. In this example, '/a/' calls node2, '/b/' calls node3, '/' calls node1 and '/b/a/' calls node1 too.
from wsgiref.simple_server import make_server
from webob.dec import wsgify
def node(mapping):
def deco(func):
def wrapper(request):
try:
called_count = request.environ['nodechain.chaincount']
except KeyError:
@hirokiky
hirokiky / fib.py
Created April 7, 2013 14:50
Getting fibonacci series using block-chain (https://github.com/podhmo/block-chain)
from block.chain import chain, MaybeF
def fib(ctx, x):
x.append(x[-2] + x[-1])
return x
print chain.chain.do(fib).do(fib).do(fib).do(fib).value(MaybeF(), [1, 1])
@hirokiky
hirokiky / __init__.py
Created April 20, 2013 04:31
Supplying deep copied attributes from a module. Following python code expect to be placed like app/consts/__init__.py. Then you can place app/consts/constant_values.py having constant values, and get deep copied values.
import copy
class AttributeSupplier(object):
def __init__(self, *args):
self.consts_module = __import__(*args)
def __getattr__(self, name):
return copy.deepcopy(getattr(self.consts_module, name))
consts = AttributeSupplier('constant_values', globals(), locals(), ['app', 'consts'])
config.add_chart_type('linechart', 'tochart.charts.Linechart')
config.add_chart_backend('highcharts', 'tochart.backends.Highcharts')
@tochart_config('daily.linechart.highcharts',
chart_type='linechart',
chart_backend='highcharts')
def daily(request, data)
x = 1
y = 2
return [(x, y), (x, y)]
@hirokiky
hirokiky / garam.py
Last active November 22, 2016 03:36
A Web server. I wrote it for studying. https://gist.github.com/knzm/5675317
from itertools import chain
import multiprocessing
import socket
import sys
def static_view(env, start_response):
mes = 'test'
start_response('200 OK', [('Cache-Control', 'private'),
('Content-Type', 'text/html'),
@hirokiky
hirokiky / models.py
Last active December 18, 2015 00:38
A customed FileSystemStorage of Django providing asynchronous deletion of files.
from django.db import models
from .storage import AsyncFileSystemStorage
a_fs = AsyncFileSystemStorage()
class UploadedFile(models.Model):
image = models.ImageField('Uploaded image file', upload_to='.', storage=a_fs)
class CoreModel(object):
def __init__(self, name, published=False):
self.name = name
self.published = published
def publish(self):
self.publish = True
def unpublish(self):
self.publish = False
@hirokiky
hirokiky / mongodb_backend.py
Last active December 20, 2015 12:29
MongoDB backend class for a document filter on https://bitbucket.org/knzm/collective/src/default/ch6/docclass.py.
class MongoDBBackend(object):
def __init__(self, db, user):
self.db = db
self.user = user
def inc_feature(self, feature, cat):
"""特徴 feature がカテゴリ cat に出現した回数を 1 増やす"""
features = self.db.features.find({'user': self.user,
'feature': feature,
'category': cat})
@hirokiky
hirokiky / chunker.py
Last active December 20, 2015 12:39
[0, 1, 2, 3, 4, 5] => [[0, 1], [2, 3], [4, 5]]
chunker1 = lambda xs: zip(xs[:-1:2], xs[1::2])
chunker2 = lambda xs: [xs[i:i+2] for i in range(0,len(xs),2)]
def chunker3(xs):
i = 0
while xs[i:i+2]:
yield xs[i:i+2]
i += 2
@hirokiky
hirokiky / choice.py
Last active December 20, 2015 14:19
A Class handling choices and it's schema type.
import colandar
class InvalidChoice(Exception):
pass
class Choice(str):
"""Class for choice value of some schema
"""