Skip to content

Instantly share code, notes, and snippets.

@hachibeeDI
Created September 24, 2014 08:29
Show Gist options
  • Save hachibeeDI/d12577aadd06c0347e55 to your computer and use it in GitHub Desktop.
Save hachibeeDI/d12577aadd06c0347e55 to your computer and use it in GitHub Desktop.
Angular like ロケーター。DIとロケーターの違いよくわからん。
# -*- coding: utf-8 -*-
from __future__ import (print_function, division, absolute_import, unicode_literals, )
from inspect import getargspec
from functools import wraps
from operator import methodcaller
PREFIX_FOR_INJECT_ARGS = 'pij_'
def _snake_to_camel(text):
'''
>>> _snake_to_camel('_snake_to_camel')
u'SnakeToCamel'
>>> _snake_to_camel('parse_file')
u'parseFile'
'''
words = text.split('_')
first_words = words.pop(0)
return first_words + ''.join(word.title() for word in words)
def _camel_to_snake(text):
'''
>>> _camel_to_snake('SnakeToCamel')
u'snake_to_camel'
>>> _camel_to_snake('parseFile')
u'parse_file'
>>> _camel_to_snake('WordPressPlugins')
u'word_press_plugins'
'''
letters = list(text)
letters[0] = letters[0].lower()
def _conv(x1, x2):
if x2.isupper():
return x1 + '_' + x2.lower()
return x1 + x2
return ''.join(
reduce(_conv, letters, )
)
class PInserter(object):
INITIZLIZEZR_PROTOCOL = PREFIX_FOR_INJECT_ARGS + 'initializer'
def __init__(self):
self.container = {}
def get_obj(self, name):
return self.container[PREFIX_FOR_INJECT_ARGS + name]
def register_with_variable(self, *arg, **kw):
def _dec(clazz):
if not hasattr(clazz, PInserter.INITIZLIZEZR_PROTOCOL):
raise TypeError('PInserter initializer should implements protocol names ' + PInserter.INITIZLIZEZR_PROTOCOL)
instance = methodcaller(PInserter.INITIZLIZEZR_PROTOCOL, *arg, **kw)(clazz) # TODO: initialize protocol
self.container[
PREFIX_FOR_INJECT_ARGS + _camel_to_snake(clazz.__name__)
] = instance
return _dec
def register(self, clazz):
instance = clazz()
self.container[
PREFIX_FOR_INJECT_ARGS + _camel_to_snake(clazz.__name__)
] = instance
def injectable(self, func):
injectable_variables_with_index = [
(i, ij_arg) for i, ij_arg in enumerate(getargspec(func).args)
if ij_arg.startswith(PREFIX_FOR_INJECT_ARGS)
]
print(injectable_variables_with_index)
@wraps(func)
def _inner(*args, **kw):
args = list(args)
for i, ij_arg in injectable_variables_with_index:
args.insert(
i,
self.container[ij_arg],
)
func(*args, **kw)
return _inner
if __name__ == '__main__':
import doctest
doctest.testmod()
PI = PInserter()
@PI.register
class Arinko(object):
def arinkable(self):
return 'arinko instance'
@PI.register_with_variable('a', 'b')
class Ababa(object):
def __init__(self, a, b):
self.a = a
self.b = b
def ababable(self):
return 'ababa instance val = {0}, {1}'.format(self.a, self.b)
@classmethod
def pij_initializer(cls, a, b):
return cls(a, b)
@PI.injectable
def tesf(pij_arinko, oa, pij_ababa, c, ):
print(pij_arinko.arinkable(), oa, pij_ababa.ababable(), c)
tesf('oa', 'c')
print(PI.get_obj('ababa'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment