Skip to content

Instantly share code, notes, and snippets.

View hirokiky's full-sized avatar

Hiroki Kiyohara hirokiky

View GitHub Profile
@hirokiky
hirokiky / README.txt
Created January 11, 2012 15:00
love r-pentomino
$ python rpentomino.py 1
 ○○
○○
 ○
$ python rpentomino.py 2
  ○○○○
  ○○○○
○○○○
○○○○
○○
@hirokiky
hirokiky / mahican.py
Created January 11, 2012 15:55
mahican-saibai
#!/usr/bin/env python -tt
# -*- coding: utf-8 -*-
daglike = ['シュークリーム']
daghate = ['Flask', 'Django']
def main():
m = 0
while True:
@hirokiky
hirokiky / long_csrf.py
Created March 4, 2012 02:03
Too log CSRF token: Django
import thread
from django.conf import settings
from django.core.context_processors import csrf
from django.http import HttpRequest, HttpResponse
from django.middleware.csrf i
{
"secret_key":
"akismet_api_key":
"db_default_user":
"db_default_password":
"db_default_host":
"db_default_port":
}
@hirokiky
hirokiky / widgets.py
Created November 11, 2012 10:33
Django's widget verbose_radioselect
from django import forms
from django.forms import widgets
from django.utils.translation import ugettext_lazy
from django.utils.encoding import force_unicode
from django.utils.html import escape, conditional_escape
from django.utils.safestring import mark_safe
class InlineRadioInput(widgets.RadioInput):
def render(self, name=None, value=None, attrs=None, choices=()):
name = name or self.name
@hirokiky
hirokiky / params.py
Last active December 10, 2015 01:38
Django's Class base view, checking params and dispatch to either valid or invalid case method.
from django.http import HttpResponseRedirect
from django.views.generic.base import TemplateResponseMixin, View
class ParamMixin(object):
failed_url = None
allowed_params = None
def get_failed_url(self):
return self.failed_url
@hirokiky
hirokiky / views.py
Created January 2, 2013 09:50
Django's View class to try using a new View.dispatch of this commit https://github.com/hirokiky/django/commit/e3399495dca9a727568626f64e2fa276c2857da9
from django.views.generic import View
from django.http.response import HttpResponse
def corn_predicate(request):
params = getattr(request, request.method)
corn = params.get('corn', None)
return corn == u'1'
class PonyView(View):
@hirokiky
hirokiky / predicate_views.py
Created January 3, 2013 05:10
PredicateProcessView which call method in considering of value returned by `predicate`. The returned value is True, PredicateProcessView calles corresponding method named `receiver`. You can set this `receiver` and `predicate` to `dispatch_config`. Watch PonyView for usage.
from django.views.generic import View
from django.http.response import HttpResponse
class RequestParamPredicate(object):
def __init__(self, val):
k = val
v = None
if '=' in val:
k, v = val.split('=', 1)
k, v = k.strip(), v.strip()
@hirokiky
hirokiky / aggregation_with_sqlexpressions.py
Last active December 14, 2015 15:29
Aggregating with SQLAlchemy's SQLExpression. Getting total of price, number of sales, piechart, linechart and ranking.
# -*- coding: utf-8 -*-
from sqlalchemy import (
create_engine,
Table,
Column,
Integer,
String,
MetaData,
ForeignKey,
@hirokiky
hirokiky / params_extra.py
Last active December 14, 2015 18:09
Django's template filters for prams (QueryDict's instance). - query_string: Takes QueryDict's instance and argument. It returns a querystirng only including the values specified by argument. The argument should be constructed by query names separated by spaces.
from urllib import urlencode
from django.template import Library
register = Library()
@register.filter(name='query_string'
def query_string(value, arg):
"""
Takes QueryDict's instance and argument.