i2.io_trans¶
Tools to make input and output transforming decorators.
Input value transformers can be conditioned on argument value and name, as well as the wrapped function itself.
Output value tranformers can be conditioned on argument value and the wrapped function.
Functions
|
Tries to cast to a json-friendly dictionary. |
|
Tries to case to a list (with json friendly elements) |
Return the input unchanged. |
|
|
Output transformer that pickles the value ( |
Classes
Transforms argument values using annotations and default type |
|
|
Transforms argument values according to their names |
|
|
Transforms argument values using annotations and default type, including lists, iterables, dicts, and booleans |
|
|
Transform output according to it's type. |
- class i2.io_trans.AnnotAndDfltIoTrans[source]¶
Bases:
IoTransTransforms argument values using annotations and default type
>>> def foo(a: int, b=1.0): ... return a + b >>> >>> input_trans = AnnotAndDfltIoTrans() >>> foo3 = input_trans(foo) >>> assert foo3(3) == 4.0 >>> assert foo3(-2, 2) == 0.0 >>> assert foo3("3") == 4.0 >>> assert foo3("-2", "2") == 0.0 >>> assert signature(foo) == signature(foo3)
- class i2.io_trans.ArgnameIoTrans(argname_2_trans_func)[source]¶
Bases:
IoTransTransforms argument values according to their names
>>> def foo(a, b=1.0): ... return a + b >>> >>> input_trans = ArgnameIoTrans({'a': int, 'b': float}) >>> foo2 = input_trans(foo) >>> assert foo2(3) == 4.0 >>> assert foo2(-2, 2) == 0.0 >>> assert foo2("3") == 4.0 >>> assert foo2("-2", "2") == 0.0 >>> assert signature(foo) == signature(foo2)
- class i2.io_trans.JSONAnnotAndDfltIoTrans[source]¶
Bases:
AnnotAndDfltIoTransTransforms argument values using annotations and default type, including lists, iterables, dicts, and booleans
>>> def foo(a: dict, b=['dflt'], c=False): ... return dict({}, a=a, b=b, c=c) >>> >>> input_trans = JSONAnnotAndDfltIoTrans() >>> foo4 = input_trans(foo) >>> assert foo4('{}') == {'a': {}, 'b': ['dflt'], 'c': False} >>> assert foo4({'d': 'e'}, '["some_value"]', 'true') == {'a': {'d': 'e'}, 'b': ['some_value'], 'c': True} >>> complex_types_result = foo4('{"None": null, "True": true, "False": false}', '[null, true, false]', 'false') >>> assert complex_types_result == {'a': {'None': None, 'True': True, 'False': False}, 'b': [None, True, False], 'c': False} >>> assert signature(foo) == signature(foo4)
- class i2.io_trans.TypedBasedOutIoTrans(trans_func_for_type=(), dflt_trans_func=None)[source]¶
Bases:
IoTransTransform output according to it’s type.
- i2.io_trans.cast_to_jdict(value)[source]¶
Tries to cast to a json-friendly dictionary.
>>> cast_to_jdict('3') [3] >>> cast_to_jdict("[3]") [3] >>> cast_to_jdict("[4,2]") [4, 2] >>> cast_to_jdict('[4, "string", ["another", "list"], {"nested": 10.2}]') [4, 'string', ['another', 'list'], {'nested': 10.2}] >>> cast_to_jdict('{"here": "is", "a": {"nested": "json"}, "with": [null, true, false, 1, 2.3]}') {'here': 'is', 'a': {'nested': 'json'}, 'with': [None, True, False, 1, 2.3]}And csvs too:
>>> cast_to_jdict('1,2,3.4, "string" , null, true, false, ["a", "list"]') [1, 2, 3.4, 'string', None, True, False, ['a', 'list']]
- i2.io_trans.cast_to_list(value)[source]¶
Tries to case to a list (with json friendly elements)
>>> cast_to_list('3') [3] >>> cast_to_list("[3]") [3] >>> cast_to_list("[4,2]") [4, 2] >>> cast_to_list('[4, "string", ["another", "list"], {"nested": 10.2}]') [4, 'string', ['another', 'list'], {'nested': 10.2}]And csvs too:
>>> cast_to_list('1,2,3.4, "string" , null, true, false, ["a", "list"]') [1, 2, 3.4, 'string', None, True, False, ['a', 'list']]