pinject.initializers
モジュールをスタンドアロン バージョンに変換する:
import inspect
from functools import wraps
def copy_args_to_internal_fields(fn):
"""Copies the initializer args to internal member fields.
This is a decorator that applies to __init__.
"""
return _copy_args_to_fields(fn, 'copy_args_to_internal_fields', '_')
def copy_args_to_public_fields(fn):
"""Copies the initializer args to public member fields.
This is a decorator that applies to __init__.
"""
return _copy_args_to_fields(fn, 'copy_args_to_public_fields', '')
def _copy_args_to_fields(fn, decorator_name, field_prefix):
if fn.__name__ != '__init__':
raise ValueError('@{0} cannot be applied to non-initializer {1}.{2}'.format(
decorator_name, inspect.getmodule(fn).__name__, fn.__name__))
arg_names, varargs, unused_keywords, unused_defaults = (
inspect.getargspec(fn))
if varargs is not None:
raise ValueError('decorator @{0} cannot be applied to {1}.{2} with *{3}'.format(
decorator_name, inspect.getmodule(fn).__name__, fn.__name__, varargs))
@wraps(fn)
def CopyThenCall(self, *pargs, **kwargs):
for index, parg in enumerate(pargs, start=1):
setattr(self, field_prefix + arg_names[index], parg)
for kwarg, kwvalue in kwargs.iteritems():
setattr(self, field_prefix + kwarg, kwvalue)
fn(self, *pargs, **kwargs)
return CopyThenCall
これは、decorator
パッケージの依存関係がなくても実行できます。
デモ:
>>> from standalone_initializers import *
>>> class Foo(object):
... @copy_args_to_public_fields
... def __init__(self, foo, bar=None, baz=1):
... print vars(self)
...
>>> Foo(1, 2)
{'foo': 1, 'bar': 2}
<__main__.Foo object at 0x106bb2e90>
>>> class Foo(object):
... @copy_args_to_internal_fields
... def __init__(self, foo, bar=None, baz=1):
... print vars(self)
...
>>> Foo(1, 2)
{'_foo': 1, '_bar': 2}
<__main__.Foo object at 0x106bb2f50>