Transform¶
Transformation utilities.
These functions are used to transform the data sent by Andesite into the Python models.
These functions aren’t exported to the andesite
namespace, if you
want to use them you need to import them from andesite.transform
.
However, there shouldn’t be a need for you to use them in the first place
as andesite.py already does this for you.
-
CONVERTER_MEMO
¶ Memory used for letter case conversion.
- Type
lettercase.ConversionMemo
-
MapFunction
¶ (Type alias) Callable that takes an argument and returns a new one.
- Type
->
Any
-
transform_input
(cls, data)[source]¶ Call the __transform_input__ classmethod on a model.
This is different from calling the method directly because it always returns the current data.
- Parameters
The transform method can either manipulate the provided data or replace it entirely by returning something other than
None
.Notes
When using this function inside of a transformation, make sure that you continue to work on the data returned by this function. Also, you cannot return
None
when using this function, you have to return the data. This is because you can’t be sure whether the data has been modified or replaced and modifications on a replaced value wouldn’t propagate upward unless you return them.
-
transform_output
(cls, data)[source]¶ Call the __transform_output__ classmethod on a model.
This is different from calling the method directly because it always returns the current data.
- Parameters
The transform method can either manipulate the provided data or replace it entirely by returning something other than
None
.Notes
When using this function inside of a transformation, make sure that you continue to work on the data returned by this function. Also, you cannot return
None
when using this function, you have to return the data. This is because you can’t be sure whether the data has been modified or replaced and modifications on a replaced value wouldn’t propagate upward unless you return them.
-
convert_to_raw
(obj)[source]¶ Convert a dataclass to a
dict
.This behaves similar to
dataclasses.asdict
. The difference is outlined below.The function uses
transform_output
, if the model provides a__transform_output__
method it will be called.After transformation the keys of the data dict are converted from snake_case to dromedaryCase.
This does not copy the values of the dataclass, modifying a value of the resulting
dict
will also modify the dataclass’ value.
-
build_from_raw
(cls, raw_data)[source]¶ Build an instance of cls from the passed raw data.
In the spirit of the other transform functions,
None
is treated as a special value and returned directly instead of handling it in any way.The function uses
transform_input
, if the model provides a__transform_input__
method it will be called.After transformation the data is used as the keyword arguments to the cls constructor.
-
seq_build_all_items_from_raw
(items, cls)[source]¶ Build all items of a mutable sequence.
This calls
build_from_raw
on all items in the sequence and assigns the result to the index.- Parameters
items (
MutableSequence
[Dict
[str
,Any
]]) – Mutable sequence of raw data to be convertedcls (
Type
[~T]) – Target type to build.
- Return type
None
- Returns
This method mutates the provided sequence, it does not return anything.
-
map_convert_value
(mapping, key, func)[source]¶ Call a function on the value of a key of the provided mapping.
The return value of the function then replaces the old value.
If the key does not exist in the mapping, it is ignored and the function is not called.
- Parameters
mapping (
MutableMapping
[~KT, ~T]) – Mutable mapping which is to be manipulated.key (~KT) – Key whose value in the mapping is to be converted. If this key does not exist in the mapping, the conversion is aborted and the function doesn’t perform any action.
func (
Callable
[[~T],Any
]) – Callback which will be called with the value of the key. Its return value then replaces the previous value in the mapping.
- Return type
None
- Returns
This method mutates the provided mapping, it does not return anything.
-
map_convert_values
(mapping, **key_funcs)[source]¶ Run a callback for a key.
For each key you can specify which function to run (<key> = <MapFunction>).
- Parameters
mapping (
Dict
[str
,Any
]) – Mutable mapping for which to apply the conversion**key_funcs – key -> map function mapping. For each key the specified function will be applied using
map_convert_value
.
- Return type
None
- Returns
This method mutates the provided mapping, it does not return anything.
-
map_convert_values_all
(mapping, func, *keys)[source]¶ Run the same callback on all keys.
Works like
map_convert_values
but runs the same function for all keys.- Parameters
- Return type
None
- Returns
This method mutates the provided mapping, it does not return anything.
-
map_build_values_from_raw
(mapping, **key_types)[source]¶ Build the values of the specified keys to the specified type.
- Parameters
mapping (
Dict
[str
,Any
]) – Mutable mapping for which to apply the conversion**key_types – key -> type mapping. For each key the value will be converted to the provided type using
build_from_raw
.
- Return type
None
- Returns
This method mutates the provided mapping, it does not return anything.
-
map_build_all_values_from_raw
(mapping, cls)[source]¶ Build all values of a mapping.
This calls
build_from_raw
on all values of the mapping and replaces the old value with the result.
-
to_centi
(value)[source]¶ Convert from base unit to hundredths.
This is really just multiplying by 1000.
-
map_convert_values_from_milli
(mapping, *keys)[source]¶ Run
from_milli
on all specified keys’ values.- Parameters
mapping (
Dict
[str
,Any
]) – Mutable mapping for which to apply the conversion*keys – Keys whose values to convert from milli. Uses
from_milli
to perform the conversion.
- Return type
None
- Returns
This method mutates the provided mapping, it does not return anything.
-
map_filter_none
(mapping)[source]¶ Remove all keys from the mapping whose values are
None
.- Parameters
mapping (
MutableMapping
[Any
,Any
]) – Mutable mapping to filter- Return type
None
- Returns
This method mutates the provided mapping, it does not return anything.
-
map_rename_keys
(mapping, **key_maps)[source]¶ Rename keys of a mapping. This is just deleting the old key and assigning its value to the new key.
- Parameters
mapping (
MutableMapping
[str
,Any
]) – Mutable mapping to manipulate**key_maps – new -> old name mapping. The reason the mapping isn’t from old to new is that you may want to rename keys that aren’t python-friendly.
- Return type
None
-
map_remove_keys
(mapping, *keys)[source]¶ Remove a number of keys (and their values) from a mapping.
- Parameters
mapping (
MutableMapping
[~KT,Any
]) – Mapping to remove keys from.*keys – Keys to remove.
- Return type
None
- Returns
Nothing, the operation mutates the given mapping.