Skip to content

Instantly share code, notes, and snippets.

View atsuya046's full-sized avatar

Nobuya Oshiro atsuya046

View GitHub Profile
@atsuya046
atsuya046 / image_file_attachement.rb
Last active August 29, 2015 14:17
papeclipで画像ファイル向けのUtilityメソッドを定義するサンプル
# paperclipでは画像ファイル以外も添付できるので、気をつけないと画像ファイル以外のObjectからも呼ばれる危険性がある。
# 画像ファイルに限定したUtilityメソッドを安全に定義するための例として以下の様な実装を考えてみた。
module ImageFileDecorator
extend ActiveSupport::Concern
module ClassMethods
# Public
#
@atsuya046
atsuya046 / grammar.clj
Created February 1, 2015 01:51
clj_grammar
; ====== 値の束縛 ========
; シンボルに値を束縛する(いわゆる代入)
(def x 3)
(def y (+ x 1))
; ====== 関数 ========
; 関数を定義する
(defn hello-clojure []
(println "Hello Clojure"))
@atsuya046
atsuya046 / prototype.py
Created March 13, 2014 14:09
GoF design pattern with Python - Prototype
# -*- coding: utf-8 -*-
import copy
class Prototype:
def __init__(self):
self._objects = {}
def register_object(self, name, obj):
"""Register an object"""
@atsuya046
atsuya046 / visitor.py
Created March 13, 2014 14:07
GoF design pattern with Python - Visitor
"""http://peter-hoffmann.com/2010/extrinsic-visitor-pattern-python-inheritance.html"""
class Node(object):
pass
class A(Node):
pass
class B(Node):
pass
@atsuya046
atsuya046 / abstract_factory.py
Created March 13, 2014 14:06
GoF design pattern with Python - AbstractFactory
# -*- coding: utf-8 -*-
# http://ginstrom.com/scribbles/2007/10/08/design-patterns-python-style/
"""Implementation of the abstract factory pattern"""
import random
class PetShop:
"""A pet shop"""
@atsuya046
atsuya046 / interpreter.py
Created March 2, 2014 01:42
GoF design pattern with Python - Interpreter
# -*- coding: utf-8 -*-
import os
import fnmatch
class Expression(object):
def __and__(self, other):
return And(self, other)
def __or__(self, other):
@atsuya046
atsuya046 / composite.py
Created February 28, 2014 22:33
GoF design pattern with Python - Composite
# -*- coding: utf-8 -*-
"""
A class which defines a composit object which can store
hierarchical dictionaries with names.
This class is same as a hierarchical dictionary, but it
provides method to add/accesss/modify children by name,
like a Composit.
@atsuya046
atsuya046 / bridge.py
Created February 17, 2014 16:04
GoF design pattern - Bridge
# -*- coding: utf-8 -*-
"""http://en.wikibooks.org/wiki/Computer_Science_Design_Patterns/Bridge_Pattern#Python"""
# ConcreteImplementor 1/2
class DrawingAPI1(object):
def draw_circle(self, x, y, radius):
print('API1.circle at {}:{} radius {}'.format(x, y, radius))
@atsuya046
atsuya046 / memento.py
Created February 13, 2014 04:45
GoF design pattern - Memento
# -*- coding: utf-8 -*-
"""http://code.activestate.com/recipes/413838-memento-closure/"""
import copy
def Memento(obj, deep=False):
state = (copy.copy, copy.deepcopy)[bool(deep)](obj.__dict__)
def Restore():
@atsuya046
atsuya046 / command.py
Created February 12, 2014 04:44
GoF design pattern - Command
# -*- coding: utf-8 -*-
import os
class MoveFileCommand(object):
def __init__(self, src, dest):
self.src = src
self.dest = dest
def execute(self):