-
-
Save anonymous/a3e5befe71a708273988 to your computer and use it in GitHub Desktop.
деление всего на объёмы и услуги: у каждого объёма может быть услуга; у каждой услуги может быть объём
This file contains 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
class ParamMeta(type): | |
def __new__(cls, name, bases, dct): | |
def _set_param(self, param): | |
expected_class = globals()[self.child_param_class] | |
child_dict = '_' + self.child_param_class.lower() + 's' | |
if not isinstance(param, expected_class): | |
raise Exception, '%s is not instance of %s' % (param.__class__, expected_class) | |
self.__dict__[child_dict][param.name] = param | |
self.__dict__[param.name] = param | |
return self | |
def _init(self, name, param=None): | |
child_dict = '_' + self.child_param_class.lower() + 's' | |
child_setter = 'set_' + self.child_param_class.lower() | |
self.name = name | |
self.__dict__[child_dict] = {} | |
if param: | |
self.__class__.__getattribute__(self, child_setter)(param) | |
def _call(self): | |
child_dict = '_' + self.child_param_class.lower() + 's' | |
if self.__dict__[child_dict]: | |
print '%s provides %s: %s' % \ | |
(self.name, child_dict, ', '.join(self.__dict__[child_dict].keys())) | |
else: | |
print '%s has no %s' % (self.name, child_dict) | |
if 'child_param_class' in dct: | |
child_dict = '_' + dct['child_param_class'].lower() + 's' | |
dct[child_dict] = {} | |
child_setter = 'set_' + dct['child_param_class'].lower() | |
dct[child_setter] = _set_param | |
dct['__init__'] = _init | |
dct['__call__'] = _call | |
return super(ParamMeta, cls).__new__(cls, name, bases, dct) | |
Service = ParamMeta('Service', (), dict(child_param_class='Volume')) | |
Volume = ParamMeta('Volume', (), dict(child_param_class='Service')) | |
if __name__ == '__main__': | |
whore = Service('whore') | |
# quick set via constructor | |
whore.set_volume(Volume('armpit', Service('rub', Volume('for_15_min')))) | |
# set a bunch of services for one volume at time | |
asshole = Volume('asshole') \ | |
.set_service(Service('puk') \ | |
.set_volume(Volume('pretty_much')) \ | |
.set_volume(Volume('not_so_much')) \ | |
.set_volume(Volume('srenk')) \ | |
) \ | |
.set_service(Service('copro', Volume('for_2_hours'))) \ | |
.set_service(Service('fuck', Volume('until_ass_grapes_bleed'))) | |
whore.set_volume(asshole) | |
mouth = Volume('mouth') \ | |
.set_service(Service('suck', Volume('until_it_sleeps'))) \ | |
.set_service(Service('vomit', Volume('happy_meal'))) | |
whore.set_volume(mouth) | |
whore.armpit() | |
whore.armpit.rub() | |
whore.armpit.rub.for_15_min() | |
whore.asshole() | |
whore.asshole.fuck() | |
whore.asshole.copro() | |
whore.asshole.puk() | |
whore.mouth() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment