Alchemize API Documentation

Transmuters

class alchemize.JsonTransmuter
classmethod transmute_from(data, mapped_model_type, coerce_values=False, decoder=None, decoder_kwargs=None)

Converts a JSON string or dict into a corresponding Mapping Object.

Parameters:
  • data – JSON data in string or dictionary form.
  • mapped_model_type – A type that extends the JsonMappedModel base.
  • coerce_values – Boolean value to allow for values with python types to be coerced with their mapped type.
  • decoder – A module that implements loads(…).
  • decoder_kwargs – A dictionary containing kwargs to use with the decoder.
Returns:

An instance of your mapped model type.

classmethod transmute_to(mapped_model, to_string=True, assign_all=False, coerce_values=True, serialize_all=False, encoder=None, encoder_kwargs=None)

Converts a model based off of a JsonMappedModel into JSON.

Parameters:
  • mapped_model – An instance of a subclass of JsonMappedModel.
  • to_string – Boolean value to disable the return of a string and return a dictionary instead.
  • assign_all – Boolean value to force assignment of all values, including null values.
  • coerce_values – Boolean value to allow for values with python types to be coerced with their mapped type.
  • serialize_all – Boolean value that allows for you to force serialization of values regardless of the attribute settings.
  • encoder – module that implements dumps(…).
  • encoder_kwargs – A dictionary containing kwargs to be used with the encoder.
Returns:

A string or dictionary containing the JSON form of your mapped model.

class alchemize.AbstractBaseTransmuter

The abtract base class from which all Transmuters are built.

classmethod transmute_from(data, mapped_model_type)

Generic Abstract Base Method to deserialize into a Python object

Parameters:mapped_model_type – A type that extends BaseMappedModel.
Returns:The an instance of the passed in mapped model type containing the deserialized data.
classmethod transmute_to(mapped_model)

Generic Abstract Base Method to convert to serialized form

Parameters:mapped_model – An instance of a class based from BaseMappedModel.
Returns:A serialized or serializable form of your mapped model.

Mapped Models

class alchemize.Attr(attr_name, attr_type, serialize=True, required=False, coerce=None)

Attribute Definition

Parameters:
  • name – Python attribute name
  • type – Attribute type (e.g str, int, dict, etc)
  • serialize – Determines if the attribute can be serialized
  • required – Forces attribute to be defined
  • coerce – Forces attribute to be coerced to its type (primitive types)
class alchemize.JsonMappedModel

Creates an explicit mapping for de/serialization by the JsonTransmuter

Map Structure:

'json_attr_name': Attr('python_attr_name', StorageType)

Mapping Types:

__mapping__ = {
    'name': Attr('name', str),
    'number': Attr('number', int),
    'dict': Attr('sample_dict', dict),
    'list': Attr('sample_list', list),
    'child': Attr('child', ChildModel),
    'children': Attr('children', [ChildModel])
}
alchemize.mapping.get_key_paths(model, sep='/', prefix='')

Walks a model class and returns a list of all key paths

Parameters:
  • model – Mapped Model instance or class
  • sep – Separator used to join the keys together
  • prefix – Prefix to add to all keys
Returns:

List of key paths

alchemize.mapping.get_normalized_map(model)

Normalizes mapping data to support backward compatibility.

Helpers

class alchemize.JsonModel(**attrs)

Model helper that is designed to provide common usage methods.

as_dict(serialize_all=False)

Converts the model into a dictionary.

as_json(serialize_all=False)

Converts the model into a JSON string.

classmethod from_dict(data, **transmute_options)

Creates a new instance of the model from a dictionary.

classmethod from_json(data, **transmute_options)

Creates a new instance of the model from a JSON string.

update(**attrs)

Updates object attributes with specified kwarg values.

class alchemize.JsonListModel(**attrs)

The list model helper is designed to be a top-level container for more complex list structures.

While you can map any number of attributes to this model, it expects a special mapping to the ‘collection’ attribute that it’ll alias normal iterable operations to.

Mapping Usage:

'my-items': Attr('collection', [ChildModel])

Note

The child items should be mapped into the ‘collection’ attribute to properly use this helper.

append(item)

Appends an item to the collection.

as_dict(serialize_all=False)

Converts the model into a dictionary.

as_json(serialize_all=False)

Converts the model into a JSON string.

extend(items)

Appends multiple items to the collection.

classmethod from_dict(data, **transmute_options)

Creates a new instance of the model from a dictionary.

classmethod from_json(data, **transmute_options)

Creates a new instance of the model from a JSON string.

update(**attrs)

Updates object attributes with specified kwarg values.

Exceptions

class alchemize.AlchemizeError

Base Exception for all Alchemize errors.

class alchemize.transmute.RequiredAttributeError(attribute_name)

Exception that is raised when attempting to retrieve/apply an attribute that isn’t available.

class alchemize.transmute.UnsupportedMappedModelError

Exception that is raised when attempting to transmute a model that is not supported by the specified transmuter.

Custom Types

class alchemize.ExpandedType

Custom Expanded Type Definition (Unstable Feature)

classmethod deserialize(attr_type, value)

Deserialization implementation for the type.

classmethod serialize(value)

Serialization implementation for the type.