magic-parameter 
WARNING: Interfaces of this project is UNSTABLE, might be changed in the future!
magic-parameter handles type checking for you.
Install
pip install magic-parameter
Supports Python 2.7 and 3.3+.
Usage
Concepts
Parameter Declaration:
(<name>, <type declaration>)
(<name>, <type declaration>, <default value>)
(<name>, <type declaration>, None)
Type Declaration:
any type object, i.e. list, int, str, FooBar.
instance of magic-parameter.<nested type>:
magic_parameter.list_t(<type declaration>)
magic_parameter.tuple_t(<type declaration>)
magic_parameter.set_t(<type declaration>)
magic_parameter.dict_t(<key type declaration>, <val type declaration>)
instance of magic-parameter.<relation type>
magic_parameter.or_t(<type declaration>, ...)
Decorators:
magic_parameter.function_parameter
magic_parameter.method_parameter
magic_parameter.method_init_parameter
magic_parameter.class_init_parameter
function_parameter(raw_parameter_decls)
function_parameter is a function decorator.
Flowing example defines a function with parameter a that accepts list of ints.
from magic_parameter import function_parameter, list_t
@function_parameter([
('a', list_t(int)),
])
def func(args):
return args.a
Runtime:
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.0
method_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_parameter is a class method decorator. method_parameter provides two ways to pass arguments:
By function argument, same as function_parameter. In this case, pass_by_function_argument should be set as True.
By attributes binding to the first parameter of class method. In this case, pass_by_cls_or_self_attributes should be set as True. 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:
func1 is a classmethod with a parameter a, accepting a dict with str as its key and int as its value.
func2 is a function with a parameter a, accepting a list or a tuple.
from magic_parameter import method_parameter, dict_t, or_t
class Example(object):
@classmethod
@method_parameter(
[
('a', dict_t(str, int)),
],
pass_by_function_argument=True,
)
def func1(cls, args):
return args.a
@method_parameter(
[
('a', or_t(list, tuple)),
],
pass_by_cls_or_self_attributes=True,
)
def func2(self):
return self.a
Runtime:
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_parameter could be viewed as method_parameter with pass_by_cls_or_self_attributes=True, no_warning_on_cls_or_self_attributes=True partial binding. It’s designed to declare the parameters of __init__ class method.
Following example defines a class accepting an int for the initialization.
from magic_parameter import method_init_parameter
class Example(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.0
class_init_parameter(user_defined_class)
class_init_parameter is a class decorator. class_init_parameter will extract parameter declarations from user_defined_class.PARAMETERS, then inject a __init__ function to user_defined_class.
Following example defines a class accepting an int for the initialization.
from magic_parameter import class_init_parameter
@class_init_parameter
class Example1(object):
PARAMETERS = [
('a', int),
]
@class_init_parameter
class Example2(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>
Metadata
Release files for magic_parameter 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| magic_parameter-0.1.0.tar.gz | 19.0 kB | Without provenance | Details for magic_parameter-0.1.0.tar.gz |
Release files / magic_parameter-0.1.0.tar.gz
| Download URL | magic_parameter-0.1.0.tar.gz |
|---|---|
| Size | 19.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
fcc24eda1ade8cbfc43e6c94f3ad6731ea105673a78351b424d7c401864660bb
|
|
BLAKE2b-256 checksum How to use checksums |
e2422a9f21377d4826bc0e69f2f9b4a2fa4ccedf1fe4431fbcd3fe87b9cb4e99
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for magic_parameter 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.