This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# paperclipでは画像ファイル以外も添付できるので、気をつけないと画像ファイル以外のObjectからも呼ばれる危険性がある。 | |
# 画像ファイルに限定したUtilityメソッドを安全に定義するための例として以下の様な実装を考えてみた。 | |
module ImageFileDecorator | |
extend ActiveSupport::Concern | |
module ClassMethods | |
# Public | |
# |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; ====== 値の束縛 ======== | |
; シンボルに値を束縛する(いわゆる代入) | |
(def x 3) | |
(def y (+ x 1)) | |
; ====== 関数 ======== | |
; 関数を定義する | |
(defn hello-clojure [] | |
(println "Hello Clojure")) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import copy | |
class Prototype: | |
def __init__(self): | |
self._objects = {} | |
def register_object(self, name, obj): | |
"""Register an object""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""http://peter-hoffmann.com/2010/extrinsic-visitor-pattern-python-inheritance.html""" | |
class Node(object): | |
pass | |
class A(Node): | |
pass | |
class B(Node): | |
pass |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import os | |
import fnmatch | |
class Expression(object): | |
def __and__(self, other): | |
return And(self, other) | |
def __or__(self, other): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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)) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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(): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import os | |
class MoveFileCommand(object): | |
def __init__(self, src, dest): | |
self.src = src | |
self.dest = dest | |
def execute(self): |