# 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`](#i2.io_trans.cast_to_jdict)(value)                 | Tries to cast to a json-friendly dictionary.                |
|---------------------------------------------------------------------------------------|-------------------------------------------------------------|
| [`cast_to_list`](#i2.io_trans.cast_to_list)(value)                  | Tries to case to a list (with json friendly elements)       |
| [`identity_func`](#i2.io_trans.identity_func)(x)                     | Return the input unchanged.                                 |
| [`pickle_out_trans`](#i2.io_trans.pickle_out_trans)(self, argval, func) | Output transformer that pickles the value (`pickle.dumps`). |

### Classes

| [`AnnotAndDfltIoTrans`](#i2.io_trans.AnnotAndDfltIoTrans)()                            | Transforms argument values using annotations and default type                                                  |
|---------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------|
| [`ArgnameIoTrans`](#i2.io_trans.ArgnameIoTrans)(argname_2_trans_func)             | Transforms argument values according to their names                                                            |
| [`IoTrans`](#i2.io_trans.IoTrans)()                                        |                                                                                                                |
| [`JSONAnnotAndDfltIoTrans`](#i2.io_trans.JSONAnnotAndDfltIoTrans)()                        | Transforms argument values using annotations and default type, including lists, iterables, dicts, and booleans |
| [`TypedBasedOutIoTrans`](#i2.io_trans.TypedBasedOutIoTrans)([trans_func_for_type, ...]) | Transform output according to it's type.                                                                       |

### *class* i2.io_trans.AnnotAndDfltIoTrans

Bases: [`IoTrans`](#i2.io_trans.IoTrans)

Transforms argument values using annotations and default type

```pycon
>>> 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)

Bases: [`IoTrans`](#i2.io_trans.IoTrans)

Transforms argument values according to their names

```pycon
>>> 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

Bases: [`object`](https://docs.python.org/3/builtins/functions.html#object)

### *class* i2.io_trans.JSONAnnotAndDfltIoTrans

Bases: [`AnnotAndDfltIoTrans`](#i2.io_trans.AnnotAndDfltIoTrans)

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

```pycon
>>> 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)

Bases: [`IoTrans`](#i2.io_trans.IoTrans)

Transform output according to it’s type.

### i2.io_trans.cast_to_jdict(value)

Tries to cast to a json-friendly dictionary.

```pycon
>>> 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:

```pycon
>>> 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)

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

```pycon
>>> 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:

```pycon
>>> 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)

Return the input unchanged.

### i2.io_trans.pickle_out_trans(self, argval, func)

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