Package: magic_parameter
WARNING: Interfaces of this project is UNSTABLE, might be changed in
the future!magic-parameterhandles type checking for you.Installpip install magic-parameterSupports Python 2.7 and 3.3+.UsageConceptsParameter Declaration:(<name>, <type declaration>)(<name>, <type declaration>, <default value>)(<name>, <type declaration>, None)Type Declaration:anytype object, i.e.list,int,str,FooBar.instance ofmagic-parameter.<nestedtype>:magic_parameter.list_t(<typedeclaration>)magic_parameter.tuple_t(<typedeclaration>)magic_parameter.set_t(<typedeclaration>)magic_parameter.dict_t(<keytype declaration>, <val type declaration>)instance ofmagic-parameter.<relationtype>magic_parameter.or_t(<typedeclaration>,...)Decorators:magic_parameter.function_parametermagic_parameter.method_parametermagic_parameter.method_init_parametermagic_parameter.class_init_parameterfunction_parameter(raw_parameter_decls)function_parameteris a function decorator.Flowing example defines a function with parameterathat acceptslistofints.frommagic_parameterimportfunction_parameter,list_t@function_parameter([('a',list_t(int)),])deffunc(args):returnargs.aRuntime:In [2]: func([1, 2, 3])
Out[2]: [1, 2, 3]
In [3]: func([1, 2.0, 3])
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
...
TypeError: Rule:
name: None
type: <class 'int'>
Arg: 2.0method_parameter(raw_parameter_decls, pass_by_function_argument=False, pass_by_cls_or_self_attributes=False, no_warning_on_cls_or_self_attributes=True)method_parameteris a class method decorator.method_parameterprovides two ways to pass arguments:By function argument, same as function_parameter. In this case,pass_by_function_argumentshould be set asTrue.By attributes binding to the first parameter of class method. In this
case,pass_by_cls_or_self_attributesshould be set asTrue.
By default,no_warning_on_cls_or_self_attributes=True, meaning
there’s no warning on attribute override.Flowing example defines a class with two methods:func1is aclassmethodwith a parametera, accepting adictwithstras its key andintas its value.func2is a function with a parametera, accepting alistor atuple.frommagic_parameterimportmethod_parameter,dict_t,or_tclassExample(object):@classmethod@method_parameter([('a',dict_t(str,int)),],pass_by_function_argument=True,)deffunc1(cls,args):returnargs.a@method_parameter([('a',or_t(list,tuple)),],pass_by_cls_or_self_attributes=True,)deffunc2(self):returnself.aRuntime:In [8]: Example.func1({'k1': 1, 'k2': 2})
Out[8]: {'k1': 1, 'k2': 2}
In [9]: Example.func1({'k1': 1, 1: 2})
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
...
TypeError: Rule:
name: None
type: <class 'str'>
Arg: 1

In [16]: example = Example()

In [17]: example.func2([1, 2, 3])
Out[17]: [1, 2, 3]

In [18]: example.func2((1, 2, 3))
Out[18]: (1, 2, 3)

In [19]: example.func2(1)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
...
TypeError: 1 cannot match [<magic_parameter.type_declaration.TypeDecl object at 0x1076ac048>, <magic_parameter.type_declaration.TypeDecl object at 0x1076ac080>]method_init_parameter(raw_parameter_decls)method_init_parametercould be viewed asmethod_parameterwithpass_by_cls_or_self_attributes=True, no_warning_on_cls_or_self_attributes=Truepartial binding. It’s designed to declare the parameters of__init__class method.Following example defines a class accepting anintfor the
initialization.frommagic_parameterimportmethod_init_parameterclassExample(object):@method_init_parameter([('a',int),])def__init__(self):print(self.a)Runtime:In [3]: Example(1)
1
Out[3]: <__main__.Example at 0x1029bda20>

In [4]: Example(1.0)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
...
TypeError: Rule:
name: None
type: <class 'int'>
Arg: 1.0class_init_parameter(user_defined_class)class_init_parameteris a class decorator.class_init_parameterwill extract parameter declarations fromuser_defined_class.PARAMETERS, then inject a__init__function
touser_defined_class.Following example defines a class accepting anintfor the
initialization.frommagic_parameterimportclass_init_parameter@class_init_parameterclassExample1(object):PARAMETERS=[('a',int),]@class_init_parameterclassExample2(object):PARAMETERS=[('a',int),]def__init__(self):print(self.a)Runtime:In [5]: Example1(1)
Out[5]: <f.Example1 at 0x106b529b0>

In [6]: Example1(a=1)
Out[6]: <f.Example1 at 0x106ba83c8>

In [7]: Example1(1.0)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
...
TypeError: Rule:
name: None
type: <class 'int'>
Arg: 1.0

In [8]: Example2(1)
1
Out[8]: <f.Example2 at 0x107405828>