Skip to content

Instantly share code, notes, and snippets.

@zenwerk
zenwerk / codejam-practice-A-small.py
Created October 1, 2011 16:28
google code jam practice A-small
# coding:utf-8
# Snapperクラス
class Snapper(object):
def __init__(self):
self.state = 'OFF'
self.hasPower = False # 電源が供給されているか
self.isWorking = False # Snapperが稼働しているか
self.previous = None # 前に接続しているSnapper
self.follow = None # 次に接続しているSnapper
@zenwerk
zenwerk / session_decolator.py
Created November 20, 2011 15:55
decolator study
def session_required(session_name, redirect_url=None):
"""
関数にセッションチェックを行う機能を追加する
デコレータ関数
"""
def outer_wrapper(func):
def session_checker(*args, **kargs):
if session.get(session_name) is not None:
return func(*args, **kargs)
else:
@zenwerk
zenwerk / driver.php
Last active December 12, 2015 10:48
<?php
/**
* read a cookie
*
* @access private
* @return void
*/
protected function _get_cookie()
{
@zenwerk
zenwerk / dump_queryset.py
Created July 23, 2013 09:42
dump django queryset object to yaml
def dump_queryset(qs, format='yaml', indent=2, filename=None):
""" QuerySet をカレントディレクトリにyamlで出力するCLI Utility """
if not isinstance(qs, QuerySet):
raise ValueError(u'QuerySet以外はシリアライズできません')
data = serializers.serialize(format, qs, indent=indent)
if not filename:
model_name = qs[0].__class__.__name__
now_str = datetime.now().strftime('%Y_%M_%d__%H_%M_%S')
@zenwerk
zenwerk / divide.py
Created September 27, 2013 08:51
与えらた数をバランスよくN分割したリストを返す関数。
# -*- coding:utf-8 -*-
def divide(num, by):
""" num を バランスよく by分割する関数
e,g:num=13, by=2 => [7, 6]
num=30, by=4 => [8, 8, 7, 7]
"""
quotient, rest = divmod(num, by)
ans = [quotient for i in range(by)]
@zenwerk
zenwerk / class_decorator.py
Created December 11, 2013 16:38
Python Class Decorator
class test(object):
"""A property whose value is computed only once. """
def __init__(self, function):
self._function = function
def __get__(self, obj, _=None):
if obj is None:
return self
value = self._function(obj)
def call_classmethod(*args):
"""
モジュール内のクラスのクラスメソッドを実行しやすくするためのヘルパ関数
args にはメソッドを実行したいclassを指定
"""
current_module = sys.modules[__name__]
factories = {}
results = {}
@zenwerk
zenwerk / 0_reuse_code.js
Created March 24, 2014 04:03
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@zenwerk
zenwerk / print_r.lua
Created May 7, 2014 05:07
php like print_r function.
-- from http://lua-users.org/wiki/TableSerialization
function print_r (t, name, indent)
local tableList = {}
function table_r (t, name, indent, full)
local serial=string.len(full) == 0 and name
or type(name)~="number" and '["'..tostring(name)..'"]' or '['..name..']'
io.write(indent,serial,' = ')
if type(t) == "table" then
if tableList[t] ~= nil then io.write('{}; -- ',tableList[t],' (self reference)\n')
@zenwerk
zenwerk / sendmail_with_attachment.go
Last active March 27, 2017 17:18
go / smtp で添付ファイル付きメール送信
package main
import (
"crypto/tls"
"fmt"
"net/mail"
"net/smtp"
"github.com/scorredoira/email"
)