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

cast_to_jdict(value)

Tries to cast to a json-friendly dictionary.

cast_to_list(value)

Tries to case to a list (with json friendly elements)

identity_func(x)

Return the input unchanged.

pickle_out_trans(self, argval, func)

Output transformer that pickles the value (pickle.dumps).

Classes

AnnotAndDfltIoTrans()

Transforms argument values using annotations and default type

ArgnameIoTrans(argname_2_trans_func)

Transforms argument values according to their names

IoTrans()

JSONAnnotAndDfltIoTrans()

Transforms argument values using annotations and default type, including lists, iterables, dicts, and booleans

TypedBasedOutIoTrans([trans_func_for_type, ...])

Transform output according to it's type.

class i2.io_trans.AnnotAndDfltIoTrans[source]

Bases: IoTrans

Transforms 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: IoTrans

Transforms 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.IoTrans[source]

Bases: object

class i2.io_trans.JSONAnnotAndDfltIoTrans[source]

Bases: AnnotAndDfltIoTrans

Transforms 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: IoTrans

Transform 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']]
i2.io_trans.identity_func(x)[source]

Return the input unchanged.

i2.io_trans.pickle_out_trans(self, argval, func)[source]

Output transformer that pickles the value (pickle.dumps).