Skip to content

Instantly share code, notes, and snippets.

@esehara
Last active December 18, 2015 22:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save esehara/5857997 to your computer and use it in GitHub Desktop.
Save esehara/5857997 to your computer and use it in GitHub Desktop.
DCI for Python (for Blog)
# -*- coding: utf-8 -*-
import re
class MethodProvider(object):
not_use_method = re.compile('^_')
has_goods = (
(u'てつのけん', '500G'),
(u'はがねのけん', '1000G'))
@classmethod
def bind(cls, target):
instance = cls()
use_methods = instance._find_method()
for method in use_methods:
setattr(
target,
method,
getattr(instance, method))
def _find_method(cls):
return_method = []
method_list = dir(cls)
for method in method_list:
if cls.not_use_method.match(method) is None:
return_method.append(method)
return return_method
class Trader(MethodProvider):
def sell(self):
for goods in self.has_goods:
print goods[0], goods[1]
class Visiter(MethodProvider):
def buy(self):
print "i have %d money." % self.has_money
class Person(object):
def __init__(self):
self.has_goods = [
(u'ひのきのぼう', u'10G'),
(u'やくそう', u'5G')]
self.has_money = 100
def test():
person = Person()
Trader.bind(person)
person.sell()
if __name__ == '__main__':
test()
# -*- coding: utf-8 -*-
import re
class MethodProvider(object):
not_use_method = re.compile('^_')
@classmethod
def bind(cls, target):
instance = cls()
use_methods = instance._find_method()
for method in use_methods:
setattr(
target,
method,
getattr(instance, method).im_func.__get__(target))
def _find_method(cls):
return_method = []
not_use_method = ['bind', 'not_use_method']
method_list = dir(cls)
for method in method_list:
if (not method in not_use_method and
cls.not_use_method.match(method) is None):
return_method.append(method)
return return_method
class Trader(MethodProvider):
def sell(self):
print "[Trader]"
for goods in self.has_goods:
print goods[0], goods[1]
class Visiter(MethodProvider):
def buy(self):
print "[Visitor]"
print "i have %d money." % self.has_money
class Person(object):
def __init__(self):
self.has_goods = [
(u'ひのきのぼう', u'10G'),
(u'やくそう', u'5G')]
self.has_money = 100
def test():
person = Person()
Trader.bind(person)
person.sell()
print
person2 = Person()
Visiter.bind(person2)
person2.buy()
if __name__ == '__main__':
test()
# -*- coding: utf-8 -*-
import re
class MethodProvider(object):
not_use_method = re.compile('^_')
@classmethod
def bind(cls, target):
instance = cls()
use_methods = instance._find_method()
print use_methods
for method in use_methods:
setattr(
target,
method,
getattr(instance, method))
def _find_method(cls):
return_method = []
method_list = dir(cls)
for method in method_list:
if cls.not_use_method.match(method) is None:
return_method.append(method)
return return_method
class Trader(MethodProvider):
def sell(self):
for goods in self.has_goods:
print goods[0], goods[1]
class Visiter(MethodProvider):
def buy(self):
print "i have %d money." % self.has_money
class Person(object):
def __init__(self):
self.has_goods = [
(u'ひのきのぼう', u'10G'),
(u'やくそう', u'5G')]
self.has_money = 100
def test():
person = Person()
Trader.bind(person)
person.sell()
if __name__ == '__main__':
test()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment