serialization

Utilities for (de)serializing Parameterized objects

class pydrobert.param.serialization.DefaultArrayDeserializer(**kwargs)[source]

Default deserializer for numpy arrays

Keyword arguments can be passed to referenced methods by initializing this deserializer with those keyword arguments.

The process:

  1. None check

  2. If already a numpy.ndarray, set it

  3. If a string ending with '.npy', load it as a file path (numpy.load() with kwargs)

  4. If bytes, load it with numpy.frombuffer() and kwargs

  5. If a string, load it with numpy.fromstring() and kwargs

  6. Try initializing to array with numpy.array() and kwargs

class pydrobert.param.serialization.DefaultArraySerializer[source]

Default numpy array serializer

The process: 1. If None, return 2. Call value’s tolist() method

class pydrobert.param.serialization.DefaultBooleanDeserializer[source]

Default deserializer for booleans

The process:

  1. None check

  2. If block is in TRUE_VALUES, set as True

  3. If block is in FALSE_VALUES, set as False

  4. If block is already a bool, use verbatim

FALSE_VALUES = {'0', 'F', 'FALSE', 'False', 'NO', 'OFF', 'f', 'false', 'no', 'off', 0}
TRUE_VALUES = {'1', 'ON', 'T', 'TRUE', 'True', 'YES', 'on', 't', 'true', 'yes', 1}
class pydrobert.param.serialization.DefaultClassSelectorDeserializer(*args, **kwargs)[source]

Default ClassSelector deserializer

The process:

  1. None check

  2. If the parameter’s is_instance attribute is True:

    1. If block is an instance of the parameter’s class_ attribute, set it

    2. Try instantiating the class with block as the first argument, with additional arguments and keyword arguments passed to the deserializer passed allong to the constructor.

  3. Look for the block or the block name in the selector’s param.ClassSelector.get_range dictionary

class pydrobert.param.serialization.DefaultClassSelectorSerializer[source]

Default ClassSelector serializer

The process:

  1. If None, return

  2. If parameter’s is_instance attribute is True, return value verbatim

  3. Search for the corresponding name in the selector’s param.ClassSelector.get_range() dictionary and return that name, if possible

  4. Return the value

class pydrobert.param.serialization.DefaultDataFrameDeserializer(*args, **kwargs)[source]

Default pandas.DataFrame deserializer

Keyword arguments and positional arguments can be passed to referenced methods by initializing this deserializer with those keyword arguments.

The process:

  1. None check

  2. If block is a data frame, set it

  3. If block is a string that ends with one of a number of file suffixes, e.g. ".csv", ".json", ".html", ".xls", use the associated pandas.read_* method with block as the first argument plus the deserializer’s extra args and kwargs

  4. If block is a string, try pandas.read_table()

  5. Try initializing a pandas.DataFrame with block as the first argument plus the deserializer’s extra args and kwargs

class pydrobert.param.serialization.DefaultDataFrameSerializer[source]

Default pandas.DataFrame serializer

The process:

  1. If None, return

  2. Call tolist() on the values property of the parameter’s value and return

class pydrobert.param.serialization.DefaultDateDeserializer(format=('%Y-%m-%dT%H:%M:%S.%f', '%Y-%m-%dT%H:%M:%S', '%Y-%m-%d'))[source]

Default datetime.datetime deserializer

The process:

  1. None check

  2. If block is a datetime.datetime, set it

  3. If the deserializer’s format argument is not None and block is a string:

    1. If format is a string, try to convert block to a datetime using datetime.datetime.strptime()

    2. If format is list-like, parse a datetime.datetime object with datetime.datetime.strptime(element, format). If the parse is successful, use that parsed datetime.

  4. Try casting block to a float

    1. If the float has a remainder or the value exceeds the maximum ordinal value, treat as a UTC timestamp

    2. Otherwise, treat as a Gregorian ordinal time

  5. Try instantiating a datetime with block as an argument to the constructor.

  6. If numpy can be imported, try instantiating a numpy.datetime64 with block as an argument to the constructor.

class pydrobert.param.serialization.DefaultDateRangeDeserializer(format=('%Y-%m-%dT%H:%M:%S.%f', '%Y-%m-%dT%H:%M:%S', '%Y-%m-%d'))[source]

Default date range deserializer

Similar to deserializing a single datetime.datetime, but applied to each element separately. Cast to a tuple.

class pydrobert.param.serialization.DefaultDateRangeSerializer(format=('%Y-%m-%d', '%Y-%m-%dT%H:%M:%S', '%Y-%m-%dT%H:%M:%S.%f'))[source]

Default date range serializer

Similar to serializing a single datetime.datetime, but applied to each element separately. Also cast to a list

class pydrobert.param.serialization.DefaultDateSerializer(format=('%Y-%m-%d', '%Y-%m-%dT%H:%M:%S', '%Y-%m-%dT%H:%M:%S.%f'))[source]

Default datetime.datetime serializer

The process:

  1. If None, return

  2. If a datetime.datetime instance

    1. If the format keyword argument of the serializer is not None:

      1. If format is a string, return the result of the value’s strftime(format) call

      2. If format is list-like, iterate through it, formatting with strftime(element). Whichever string which, when deserialized with strptime(element), produces an equivalent :class`datetime.datetime` object as the value is returned. If no such string exists, the last string is returned.

    2. Return the result of the value’s timestamp() call

  3. If a numpy.datetime64 instance, return the value cast to a string

class pydrobert.param.serialization.DefaultDeserializer[source]

Catch-all deserializer

This serializer performs a none check, then tries to set the parameter with the value of block verbatim.

class pydrobert.param.serialization.DefaultIntegerDeserializer(*args, **kwargs)[source]

Default int deserializer

The process:

  1. None check

  2. If block is a(n) int, set it

  3. Initialize a(n) int instance with block as the first argument plus any extra positional or keyword arguments passed to the deserializer on initialization

class_

alias of int

class pydrobert.param.serialization.DefaultListDeserializer(*args, **kwargs)[source]

Default list deserializer

The process:

  1. None check

  2. If the parameter’s class_ attribute has been set, for each element in block (we always assume block is iterable):

    1. If the element is an instance of the class, leave it alone

    2. Try instantiating a class_ object using the element as the first argument plus any arguments or keyword arguments passed to the deserializer on initialization.

  3. Cast to a list and set

class pydrobert.param.serialization.DefaultListSelectorDeserializer[source]

Default ListSelector deserializer

For each element in block (we assume block is iterable), match a value or name in the selector’s param.ListSelector.get_range() method

class pydrobert.param.serialization.DefaultListSelectorSerializer[source]

Default ListSelector serializer

For each element in the value:

  1. Search for its name in the selector’s param.ListSelector.get_range() dict and swap if for the name, if possible

  2. Otherwise, use that element verbatim

class pydrobert.param.serialization.DefaultNumberDeserializer(*args, **kwargs)[source]

Default float deserializer

The process:

  1. None check

  2. If block is a(n) float, set it

  3. Initialize a(n) float instance with block as the first argument plus any extra positional or keyword arguments passed to the deserializer on initialization

class_

alias of float

class pydrobert.param.serialization.DefaultNumericTupleDeserializer[source]

Default numeric tuple deserializer

The process: 1. None check 2. Cast each element of block to a float 3. Cast block to a tuple

class pydrobert.param.serialization.DefaultObjectSelectorDeserializer[source]

Default param.ObjectSelector deserializer

The process:

  1. None check

  2. Match block to a value or name in the selector’s param.ObjectSelector.get_range() method

class pydrobert.param.serialization.DefaultObjectSelectorSerializer[source]

Default ObjectSelector serializer

The process:

  1. If None, return

  2. Search for the name of the value in the selector’s param.ObjectSelector.get_range() dictionary and return, if possible

  3. Return value verbatim

class pydrobert.param.serialization.DefaultSerializer[source]

Default catch-all serializer. Returns value verbatim

class pydrobert.param.serialization.DefaultSeriesSerializer[source]

Default pandas.Series serializer

The process:

  1. If None, return

  2. Call tolist() on the values property of the parameter’s value and return

class pydrobert.param.serialization.DefaultStringDeserializer(*args, **kwargs)[source]

Default str deserializer

The process:

  1. None check

  2. If block is a(n) str, set it

  3. Initialize a(n) str instance with block as the first argument plus any extra positional or keyword arguments passed to the deserializer on initialization

class_

alias of str

class pydrobert.param.serialization.DefaultTupleSerializer[source]

Default tuple serializer

The process: 1. If None, return 2. Casts the value to a list

class pydrobert.param.serialization.JsonSerializable(*args, **kwargs)[source]

Write/read to/from strings using JSON protocol

class pydrobert.param.serialization.JsonStringArrayDeserializer(**kwargs)[source]

Parses a block as JSON before converting it into a numpy array

The default deserializer used in INI files. Input is always assumed to be a string or None. If None, a none check is performed. Otherwise, it parses the value as JSON, then does the same as DefaultArrayDeserializer. However, if the input ends in the file suffix ".npy", the input will be immediately passed to DefaultArrayDeserializer

See also

deserialize_from_json

To deserialize JSON into param.parameterized.Parameterized instances

class pydrobert.param.serialization.JsonStringDataFrameDeserializer(*args, **kwargs)[source]

Parses block as JSON before converting to pandas.DataFrame

The default deserializer used in INI files. Input is always assumed to be a string or None. If None, a none check is performed. Otherwise, it parses the value as JSON, then does the same as DefaultDataFrameSerializer. However, if the input ends in a file suffix like ".csv", ".xls", etc., the input will be immediately passed to DefaultDataFrameSerializer

See also

deserialize_from_json

To deserialize JSON into param.parameterized.Parameterized instances

file_suffixes = {'csv', 'dta', 'feather', 'h5', 'html', 'json', 'parquet', 'pkl', 'sas7bdat', 'xls'}
class pydrobert.param.serialization.ParamConfigDeserializer[source]

Deserialize part of a configuration into a parameterized object

Subclasses of ParamConfigDeserializer are expected to implement deserialize(). Instances of the subclass can be passed into deserialize_from_dict(). The goal of a deserializer is to convert data into the value of a parameter in a param.parameterized.Parameterized object. The format of the incoming data is specific to where the dict-like input came from. For example, a JSON parser converts numeric strings to floats, and the contents of square braces ([]) as lists. In pydrobert.param.serialization, there are a number of default deserializers (matching the pattern Default*Deserializer) that are best guesses on how to deserialize data from a variety of sources

static check_if_allow_none_and_set(name, block, parameterized)[source]

Check if block can be made none and set it if allowed

Many param.Param parameters allow None as a value. This is a convenience method that deserializers can use to quickly check for a None value and set it in that case. This method sets the parameter and returns True in the following conditions

  1. The parameter allows None values (the allow_None attribute is True)

  2. block is None

If one of these conditions wasn’t met, the parameter remains unset and the method returns False.

In Default*Deserializer documentation, a call to this method is referred to as a “none check”.

abstract deserialize(name, block, parameterized)[source]

Deserialize data and store it in a parameterized object

Parameters:
  • name (str) – The name of the parameter in parameterized to store the value under

  • block (Any) – The data to deserialize into the parameter value

  • parameterized (Parameterized) – The parameterized instance containing a parameter with the name name. On completion of this method, that parameter will be set with the deserialized contents of block

Raises:

ParamConfigTypeError – If deserialization could not be performed

class pydrobert.param.serialization.ParamConfigSerializer[source]

Serialize a parameter value from a Parameterized object

Subclasses of ParamConfigSerializer are expected to implement serialize(). Instances of the subclass can be passed into serialize_to_dict(). The goal of a serializer is to convert a parameter value from a param.parameterized.Parameterized object into something that can be handled by a dict-like data store. The format of the outgoing data should reflect where the dict-like data are going. For example, a JSON serializer can handle lists, but not an INI serializer. In pydrobert.param.serialization, there are a number of default serializers (matching the pattern Default*Serializer) that are best guesses on how to serialize data from a variety of sources

help_string(name, parameterized)[source]

A string that helps explain this serialization

The return string will be included in the second element of the pair returned by serialize_to_dict(). Helps explain the serialized value to the user.

abstract serialize(name, parameterized)[source]

Serialize data from a parameterized object and return it

Parameters:
  • name (str) – The name of the parameter in parameterized to retrieve the value from

  • parameterized (Parameterized) – The parameterized instance containing a parameter with the name name

Returns:

val (obj) – The serialized value of the parameter

Raises:

ParamConfigTypeError – If serialization could not be performed

exception pydrobert.param.serialization.ParamConfigTypeError(parameterized, name, message='')[source]

Raised when failed to (de)serialize Parameterized object

class pydrobert.param.serialization.RecklessJsonSerialization(*args, **kwargs)[source]

Reckless JSON (de)serialization

See also

register_serializer

For how this is used

class pydrobert.param.serialization.RecklessSerializableSerialization(*args, **kwargs)[source]

ABC for reckless serialization

class pydrobert.param.serialization.RecklessYamlSerialization(*args, **kwargs)[source]

Reckless YAML (de)serialization

See also

register_serializer

For how this is used

class pydrobert.param.serialization.Serializable(*args, **kwargs)[source]

Protocol for classes capable of reading and writing objects from/to strings

abstract classmethod dumps(obj, help=None)[source]

Dump object (and optional help object) to string

help should not impact the deserialization of obj. It can be ignored.

abstract classmethod loads(serialized)[source]

Read serialized string into an object

class pydrobert.param.serialization.SerializableSerialization(*args, **kwargs)[source]

ABC for sensible serialization

class pydrobert.param.serialization.YamlSerializable(*args, **kwargs)[source]

Write/read to/from strings using YAML protocol

class pydrobert.param.serialization.YamlSerialization(*args, **kwargs)[source]

YAML (de)serialization

See also

register_serializer

For how this is used

pydrobert.param.serialization.deserialize_from_dict(dict_, parameterized, deserializer_name_dict=None, deserializer_type_dict=None, on_missing='warn')[source]

Deserialize a dictionary into a parameterized object

This function is suitable for deserializing the results of parsing a data storage file such as a YAML, JSON, or a section of an INI file (using the yaml, json, and configparser python modules, resp.) into a param.parameterized.Parameterized object. Each key in dict_ should match the name of a parameter in parameterized. The parameter will be deserialized into parameterized using a ParamConfigDeserializer object matched with the following precedent:

  1. If deserializer_name_dict is specified and contains the same key, the value of the item in deserializer_name_dict will be used.

  2. If deserializer_type_dict and the type of the parameter in question exactly matches a key in deserializer_type_dict, the value of the item in deserializer_type_dict will be used.

  3. If the type of the parameter in question has a default deserializer (i.e. Default<type>Deserializer), it will be used.

  4. DefaultBackupDeserializer will be used.

It is possible to pass a dictionary as parameterized instead of a param.parameterized.Parameterized instance to this function. This is “hierarchical mode”. The values of parameterized can be param.parameterized.Parameterized objects or nested dictionaries. In this case, dict_ and deserializer_name_dict are expected to be dictionaries with the same hierarchical structure (though the latter can still be None). deserializer_type_dict can be a flat dictionary of types to be applied to all nodes, or a hierarchical dictionary of strings like dict_, or some combination. The leaves of dict_ deserialize into the leaves of parameterized. If no leaf of dict_ exists for a given parameterized leaf, that parameterized object will not be updated.

Default deserializers are likely appropriate for basic types like strings, ints, bools, floats, and numeric tuples. For more complex data types, including recursive param.parameterized.Parameterized instances, custom deserializers are recommended.

Parameters:
  • dict

  • parameterized (Union[Parameterized, dict]) –

  • deserializer_name_dict (Optional[dict]) –

  • deserializer_type_dict (Optional[dict]) –

  • on_missing (Literal['ignore', 'warn', 'raise']) – What to do if the parameterized instance does not have a parameter listed in dict_, or, in the case of “hierarchical mode”, if dict_ contains a key with no matching parameterized object to populate

Raises:

ParamConfigTypeError – If deserialization of a value fails

pydrobert.param.serialization.deserialize_from_ini(file, parameterized, deserializer_name_dict=None, deserializer_type_dict=None, on_missing='warn', defaults=None, comment_prefixes=('#', ';'), inline_comment_prefixes=(';',), one_param_section=None)[source]

Deserialize an INI (config) file into a parameterized instance

.INI syntax, extended with configparser. configparser extends the INI syntax with value interpolation. Further, keys missing a value will be interpreted as having the value None. This function converts an INI file to a dictionary, then populates parameterized with the contents of this dictionary.

INI files are broken up into sections; all key-value pairs must belong to a section. If parameterized is a param.parameterized.Parameterized instance (rather than a hierarchical dictionary of them), the action will try to deserialize the section specified by one_param_section keyword argument.

Because the INI syntax does not support standard containers like dicts or lists out-of-the-box, this function uses the JsonString*Deserializer to read container values to JSON strings before trying the standard method of deserialization. This solution was proposed here. Defaults Default<type>Deserializer are clobbered by those of form Json<type>Deserializer. You can get the original defaults back by including them in deserializer_type_dict

Parameters:
  • file (Union[TextIO, str]) – The INI file to deserialize from. Can be a pointer or a path

  • parameterized (Union[Parameterized, dict]) –

  • deserializer_name_dict (Optional[dict]) –

  • deserializer_type_dict (Optional[dict]) –

  • on_missing (Literal['ignore', 'warn', 'raise']) –

  • defaults (Optional[dict]) – Default key-values used in interpolation (substitution). Terms such as (key)%s (like a python 2.7 format string) are substituted with these values

  • comment_prefixes (Sequence[str]) – A sequence of characters that indicate a full-line comment in the INI file

  • inline_comment_prefixes (Sequence[str]) – A sequence of characters that indicate an inline (including full-line) comment in the INI file

  • one_param_section (Optional[str]) – If parameterized refers to a single param.parameterized.Parameterized instance, this keyword is used to indicate which section of the INI file will be deserialized. If unspecified, will default to the name attribute of parameterized

See also

deserialize_from_dict

A description of the deserialization process and the parameters to this function

pydrobert.param.serialization.deserialize_from_json(file_, parameterized, deserializer_name_dict=None, deserializer_type_dict=None, on_missing='warn')[source]

Deserialize a YAML file into a parameterized instance

Composes deserialize_from_json_to_obj() with deserialize_from_dict().

pydrobert.param.serialization.deserialize_from_json_to_obj(file_)[source]

Deserialize a JSON file into an object

JSON syntax.

Parameters:

file – A path or pointer to the JSON file.

pydrobert.param.serialization.deserialize_from_yaml(file, parameterized, deserializer_name_dict=None, deserializer_type_dict=None, on_missing='warn')[source]

Deserialize a YAML file into a parameterized instance

Composes deserialize_from_yaml_to_obj() with deserialize_from_dict().

pydrobert.param.serialization.deserialize_from_yaml_to_obj(file_, ordered=False)[source]

Deserialize a YAML file into an object

YAML syntax.

Parameters:
  • file – A path or pointer to the YAML file.

  • ordered (bool) – Whether to respect ordering in the deserialized dictionaries.

Notes

This function tries to use the YAML (de)serialization module to load the YAML file in the order listed in pydrobert.param.config.YAML_MODULE_PRIORITIES, falling back on the next if there’s an ImportError.

pydrobert.param.serialization.register_serializer(mode)[source]

Add a custom (de)serialization protocol to parameterized instances

The serialization protocol to be registered is passed as mode, which can be one of

  1. 'reckless_json', which is identical to the standard 'json' serializer but for some simplifying assumptions to handle nesting. See the below note for more information.

  2. 'yaml', which follows a similar parsing strategy to vanilla 'json' but (de)serializes in YAML format instead. Requires either yaml or ruamel.yaml to be installed or will raise an import error at the first request.

  3. 'reckless_yaml', which makes the same reckless assumptions as reckless_json' but (de)serialized in the YAML format.

After calling this function, parameters can be serialized with the same mode registered via param.Parameterized.param.serialize_parameters():

>>> str_ = p.param.serialize_parameters(subset, mode)
>>> p = P.param.deserialize_parameters(str_, subset, mode)

Warning

Functionality is in beta and subject to additions and modifications.

Notes

We make strong simplifying assumptions to handle nesting. The usual means of nesting param.parameterized.Parameterized instances is

>>> class Parent(param.Parameterized):
...     child = param.ClassSelector(SomeParameterizedClass)
>>> parent = Parent(child=SomeParameterizedClass(leaf_value=1))

While the obvious solution is to recursively call (de)serialization on child instances, this solution is not suited to all situations. In deserialization, the child may be of type SomeParameterizedClass, but may also be one of its subclasses. If children are sharing references to the same instance, that information will be lost in serialization.

For now (keep track of this bug for changes), mode="json" will throw if it sees a nested parameterized instance in serialization or deserialization. Reckless serialization is performed recursively with no consideration for references. Deserialization is performed by recursing on the class provided to the class selector (i.e. SomeParameterizedClass), not any of its subclasses.

To (de)serialize only a subset of parameters in a child instance, delimit child parameters with <name_in_parent>.<name_in_child> in the subset argument. In the example above, we can serialize only leaf_value using

>>> parent.param.serialize_parameters({'child.leaf_value'}, mode="reckless_json")
pydrobert.param.serialization.serialize_from_obj_to_json(file_, obj, indent=2)[source]

Serialize an object into a json file

JSON syntax. Given a dictionary of parameter values, fills an JSON file with the contents of this dictionary.

Parameters:
  • file – The JSON file to serialize to. Can be a pointer or a path.

  • dict – The sort of dictionary returned by serialized_to_dict().

  • indent (Optional[int]) – The indentation level of nested keys. If None, the output will be compact.

See also

serialize_to_json

Composes

func:serialize_to_dict with this function.

pydrobert.param.serialization.serialize_from_obj_to_yaml(file_, obj, help=None)[source]

Serialize an object into a YAML file

YAML syntax.

Parameters:
  • file – The YAML file to serialize to. Can be a pointer or a path.

  • obj (Any) – The thing to serialize.

  • help (Optional[Any]) – An optional thing containing strings representing help information. Will try to save as comments. If obj and help share a structure (e.g. both list or dict), the backend will try to organize help in similar fashion.

Notes

This function tries to use the YAML (de)serialization module to load the YAML file in the order listed in pydrobert.param.config.YAML_MODULE_PRIORITIES, falling back on the next if there’s an ImportError

pydrobert.param.serialization.serialize_to_dict(parameterized, only=None, serializer_name_dict=None, serializer_type_dict=None, on_missing='raise', include_help=False)[source]

Serialize a parameterized object into a dictionary

This function serializes data into a dictionary format, suitable for storage in a dict-like file format such as YAML or JSON. Each parameter will be serialized into the dictionary using a ParamConfigSerializer object, matched with the following precedent:

  1. If serializer_name_dict is specified and contains the parameter name as a key, the value will be used.

  2. If serializer_type_dict and the type of the parameter in question exactly matches a key in serializer_type_dict, the value of the item in serializer_type_dict will be used.

  3. If the type of the parameter in question has a Default<type>Serializer, it will be used.

  4. DefaultBackupSerializer will be used.

Default serializers are likely appropriate for basic types like strings, ints, bools, floats, and numeric tuples. For more complex data types, including recursive param.parameterized.Parameterized instances, custom serializers are recommended.

It is possible to pass a dictionary as parameterized instead of a param.parameterized.Parameterized instance to this function. This is “hierarchical mode”. The values of parameterized can be param.parameterized.Parameterized objects or nested dictionaries. The returned dictionary will have the same hierarchical dictionary structure as parameterized, but with the param.parameterized.Parameterized values replaced with serialized dictionaries. In this case, only and serializer_name_dict are expected to be dictionaries with the same hierarchical structure (though they can still be None, which propagates to children), whose leaves correspond to the arguments used to serialize the leaves of parameterized. serializer_type_dict can also be hierarchical, can be flat, or be some combination.

Parameters:
  • parameterized (Union[Parameterized, dict]) –

  • only (Optional[Collection[str]]) – If specified, only the parameters with their names in this set will be serialized into the return dictionary. If unset, all parameters except name will be serialized.

  • serializer_name_dict (Optional[dict]) –

  • serializer_type_dict (Optional[dict]) –

  • on_missing (Literal['ignore', 'warn', 'raise']) – What to do if the parameterized instance does not have a parameter listed in only

  • include_help (bool) – If True, the return value will be a pair of dictionaries instead of a single dictionary. This dictionary will contain any help strings any serializers make available through a call to help_string (or None if none is available).

Returns:

collections.OrderedDict or tuple – A dictionary of serialized parameters or a pair of dictionaries if include_help was True (the latter is the help dictionary). If parameterized was an ordered dictionary, the returned serialized dictionary will have the same order. Parameters from a param.parameterized.Parameterized instance are sorted alphabeticallly

Raises:

ParamConfigTypeError – If serialization of a value fails

pydrobert.param.serialization.serialize_to_ini(file, parameterized, only=None, serializer_name_dict=None, serializer_type_dict=None, on_missing='raise', include_help=True, help_prefix='#', one_param_section=None)[source]

Serialize a parameterized instance into an INI (config) file

.INI syntax, extended with configparser. configparser extends the INI syntax with value interpolation. Further, keys missing a value will be interpreted as having the value None. This function converts parameterized to a dictionary, then fills an INI file with the contents of this dictionary.

INI files are broken up into sections; all key-value pairs must belong to a section. If parameterized is a param.parameterized.Parameterized instance (rather than a hierarchical dictionary of them), the action will try to serialize parameterized into the section specified by the one_param_section keyword argument. If parameterized is a hierarchical dictionary, it can only have depth 1, with each leaf being a param.parameterized.Parameterized instance. In this case, each key corresponds to a section. If an ordered dictionary, sections will be written in the same order as they exist in parameterized.

Because the INI syntax does not support standard containers like dicts or lists out-of-the-box, this function uses the JsonString*Serializer to convert container values to JSON strings before writing them to the INI file. This solution was proposed here. Defaults Default<type>Serializer are clobbered with Json<type>Serializer.You can get the original defaults back by including them in serializer_type_dict.

Parameters:
  • file (Union[str, TextIO]) – The INI file to serialize to. Can be a pointer or a path.

  • parameterized (Union[Parameterized, dict]) –

  • only (Optional[Collection[str]]) –

  • serializer_name_dict (Optional[dict]) –

  • serializer_type_dict (Optional[dict]) –

  • on_missing (Literal['ignore', 'warn', 'raise']) –

  • include_help (bool) – If True, help documentation will be included at the top of the INI file for any parameters and/or serializers that support it

  • help_prefix (str) – The character prefix used at the start of each help line, usually indicating a comment

  • one_param_section (Optional[str]) – If parameterized refers to a single param.parameterized.Parameterized instance, this keyword is used to indicate which section of the INI file parameterized will be serialized to. If None, the name attribute of the parameterized instance will be the used

pydrobert.param.serialization.serialize_to_json(file_, parameterized, only=None, serializer_name_dict=None, serializer_type_dict=None, on_missing='raise', indent=2)[source]

Serialize a parameterized instance into a JSON file

Composes serialize_to_dict() with serialize_from_obj_to_json().

pydrobert.param.serialization.serialize_to_yaml(file_, parameterized, only=None, serializer_name_dict=None, serializer_type_dict=None, on_missing='raise', include_help=True)[source]

Serialize a parameterized instance into a YAML file

Composes serialize_to_dict() with serialize_from_obj_to_yaml().

pydrobert.param.serialization.unregister_serializer(mode)[source]

Unregister a previously registered custom serializer

pydrobert.param.serialization.yaml_is_available()[source]

Returns whether one of the YAML backends is available

Checks only those in pydrobert.param.config.YAML_MODULE_PRIORITIES