miscset.io.Parsable

class miscset.io.Parsable[source]

Bases: object

A class where slots are parsable.

Provides methods to import and export values for data slots from dictionaries.

Use case:
  • Get a dictionary from any object structure.

  • Get a JSON or YAML string from any object structure.

  • Parse a dictionary to add/overwrite object slots with values.

Example code:

import miscset
class Container(miscset.io.Parsable):
    def __init__(self, value):
        self.value = value
c = Container([1,2,3])
c.import_dict({"foo": "bar"}, add = True)
print(c.get_json())

Result:

{"value": [1, 2, 3], "foo": "bar"}
__init__()[source]

Initialize a Parsable object.

Initializes the object and calls the reset function.

Methods

__init__()

Initialize a Parsable object.

get_dict()

Return values of the data slots as dictionary.

get_json()

"Return values of the data slots as a json string.

get_text([name, sep, private])

Return a description.

get_yaml()

"Return values of the data slots as a yaml string.

import_dict(obj[, add])

Import a dictionary into data slots.

reset()

Reset slots to default values.

get_dict()[source]

Return values of the data slots as dictionary.

Same as accessing the __dict__ slot from a class instance.

Returns

A dictionary with keys named as the class instance slots

containing the respective values.

Return type

dict

get_json()[source]

“Return values of the data slots as a json string.

Returns

A JSON formatted string.

Return type

str

get_text(name=None, sep='\n  ', private=True)[source]

Return a description.

Parameters
  • name (str) – Provide a custom name of the class shown as prefix.

  • sep (str) – A string separating the values.

  • private (bool) – Show privat slots (starting with an underscore “_”).

Returns

A text representation of the object data slots.

Return type

str

get_yaml()[source]

“Return values of the data slots as a yaml string.

Returns

A YAML formatted string.

Return type

str

import_dict(obj, add=False)[source]

Import a dictionary into data slots.

Parameters
  • obj (dict) – A dictionary from which keys become slots and assigned the values.

  • add (bool) – Whether to add a key as slot if it did not yet exist.

reset()[source]

Reset slots to default values.

This method implemented here does not affect anything.

It is a placeholder for subclasses implementing this method. When calling the class constructor, this method will be called.