Skip to content

base_classes

Modules:

Name Description
easy_base
easy_list
model_base
serializer_base

Classes

Modules

easy_base

Classes:

Name Description
EasyBase

This is the base class for easyscience objects.

Classes

EasyBase(unique_name=None, display_name=None)

This is the base class for easyscience objects.

It provides serialization capabilities as well as unique naming and display naming.

Methods:

Name Description
to_dict

Convert an EasyScience object into a full dictionary using

from_dict

Re-create an EasyScience object from a full encoded dictionary.

__copy__

Return a copy of the object.

Attributes:

Name Type Description
unique_name str

Get the unique name of the object.

display_name str

Get a pretty display name.

Attributes
unique_name property writable

Get the unique name of the object.

display_name property writable

Get a pretty display name.

Returns:

Type Description
str

The pretty display name.

Methods:
to_dict(skip=None)

Convert an EasyScience object into a full dictionary using SerializerBases generic convert_to_dict method.

Parameters:

Name Type Description Default
skip Optional[List[str]]

List of field names as strings to skip when forming the dictionary. By default, None.

None

Returns:

Type Description
Dict[str, Any]

Encoded object containing all information to reform an EasyScience object.

Notes

A unique_name that was generated automatically is not written; only an explicitly supplied one is, so that a deserialized object gets a fresh name instead of clashing with the original. Likewise display_name is omitted when it is None. Pass skip to drop further fields.

from_dict(obj_dict) classmethod

Re-create an EasyScience object from a full encoded dictionary.

Parameters:

Name Type Description Default
obj_dict Dict[str, Any]

Dictionary containing the serialized contents (from to_dict) of an EasyScience object.

required

Returns:

Type Description
EasyBase

Reformed EasyScience object.

Raises:

Type Description
ValueError

If the input dictionary does not describe the expected class.

__copy__()

Return a copy of the object.

Modules

easy_list

Classes:

Name Description
EasyList

Classes

EasyList(*args, protected_types=None, unique_name=None, display_name=None, **kwargs)

Parameters:

Name Type Description Default
*args ProtectedType_ | Iterable[ProtectedType_]

Initial items to add to the list.

()
protected_types list[Type[EasyBase]] | Type[EasyBase] | None

Types that are allowed in the list. Can be a single EasyBase subclass or a list of them. If None, any EasyBase object is accepted, including descriptors and parameters. Both bare descriptors and ModelBase items contribute to get_all_variables and hence to fitting. By default, None.

None
unique_name Optional[str]

Optional unique name for the list. By default, None.

None
display_name Optional[str]

Optional display name for the list. By default, None.

None
**kwargs Any

Additional keyword arguments used during deserialization.

{}

Raises:

Type Description
TypeError

If protected_types is not a supported EasyBase type or iterable of supported types.

Methods:

Name Description
__getitem__

Get an item by index, slice, or unique_name.

__setitem__

Set an item at an index.

__delitem__

Delete an item by index, slice, or name.

__len__

Return the number of items in the collection.

insert

Insert an item at an index.

get_all_variables

Get all Descriptor and Parameter objects held by this

sort

Sort the collection according to the given key function.

pop

Remove and return an item at the given index or unique_name.

to_dict

Convert the EasyList to a dictionary for serialization.

from_dict

Re-create an EasyScience object from a full encoded dictionary.

__copy__

Return a copy of the object.

get_all_parameters

Get all Parameter objects as a list.

get_fittable_parameters

Get all parameters which can be fitted as a list.

get_free_parameters

Get all parameters which are currently free to be fitted as a

Attributes:

Name Type Description
unique_name str

Get the unique name of the object.

display_name str

Get a pretty display name.

Attributes
unique_name property writable

Get the unique name of the object.

display_name property writable

Get a pretty display name.

Returns:

Type Description
str

The pretty display name.

Methods:
__getitem__(idx)
__getitem__(idx: int) -> ProtectedType_
__getitem__(idx: slice) -> 'EasyList[ProtectedType_]'
__getitem__(idx: str) -> ProtectedType_

Get an item by index, slice, or unique_name.

Parameters:

Name Type Description Default
idx int | slice | str

Index, slice, or unique_name of the item.

required

Returns:

Type Description
ProtectedType_ | 'EasyList[ProtectedType_]'

The item or a new EasyList for slices.

Raises:

Type Description
KeyError

If idx is a string that does not match any item.

TypeError

If idx is not an int, slice, or string.

__setitem__(idx, value)
__setitem__(idx: int, value: ProtectedType_) -> None
__setitem__(
    idx: slice, value: Iterable[ProtectedType_]
) -> None

Set an item at an index.

Parameters:

Name Type Description Default
idx int | slice

Index to set.

required
value ProtectedType_ | Iterable[ProtectedType_]

New value.

required

Raises:

Type Description
TypeError

If idx or value has an invalid type.

ValueError

If slice assignment changes the slice length.

__delitem__(idx)

Delete an item by index, slice, or name.

Parameters:

Name Type Description Default
idx int | slice | str

Index, slice, or name of item to delete.

required

Raises:

Type Description
KeyError

If idx is a string that does not match any item.

TypeError

If idx is not an int, slice, or string.

__len__()

Return the number of items in the collection.

insert(index, value)

Insert an item at an index.

Parameters:

Name Type Description Default
index int

Index to insert at.

required
value ProtectedType_

Item to insert.

required

Raises:

Type Description
TypeError

If index is not an integer or value is of a protected type mismatch.

get_all_variables()

Get all Descriptor and Parameter objects held by this list.

Elements that are DescriptorBase instances (e.g. a bare Parameter) are collected directly, while elements derived from ModelBase contribute the result of their own get_all_variables() call. Everything is collected into a single flat list.

Returns:

Type Description
List[DescriptorBase]

Flat list of all DescriptorBase objects from all ModelBase elements.

sort(key=None, reverse=False)

Sort the collection according to the given key function.

Parameters:

Name Type Description Default
key Callable[[ProtectedType_], Any]

Mapping function to sort by. By default, None.

None
reverse bool

Whether to reverse the sort. By default, False.

False
pop(index=-1)

Remove and return an item at the given index or unique_name.

Parameters:

Name Type Description Default
index int | str

Index or unique_name of the item to remove. By default, -1.

-1

Returns:

Type Description
ProtectedType_

The removed item.

Raises:

Type Description
KeyError

If index is a string that does not match any item.

TypeError

If index is not an int or string.

to_dict(skip=None)

Convert the EasyList to a dictionary for serialization.

The serializer calls to_dict on nested EasyScience objects.

Parameters:

Name Type Description Default
skip list[str] | None

List of field names as strings to skip when forming the dictionary. By default, None.

None

Returns:

Type Description
dict

Dictionary representation of the EasyList.

from_dict(obj_dict) classmethod

Re-create an EasyScience object from a full encoded dictionary.

Parameters:

Name Type Description Default
obj_dict Dict[str, Any]

Dictionary containing the serialized contents of an EasyScience object.

required

Returns:

Type Description
EasyBase

Reformed EasyScience object.

Raises:

Type Description
ImportError

If a protected type cannot be imported.

ValueError

If the input dictionary does not describe an EasyList.

__copy__()

Return a copy of the object.

get_all_parameters()

Get all Parameter objects as a list.

Returns:

Type Description
List[Parameter]

List of Parameter objects.

get_fittable_parameters()

Get all parameters which can be fitted as a list.

Returns:

Type Description
List[Parameter]

List of Parameter objects.

get_free_parameters()

Get all parameters which are currently free to be fitted as a list.

Returns:

Type Description
List[Parameter]

List of Parameter objects.

Modules

model_base

Classes:

Name Description
ModelBase

This is the base class for all model classes in EasyScience. It

Classes

ModelBase(unique_name=None, display_name=None)

This is the base class for all model classes in EasyScience. It provides methods to get parameters for fitting and analysis as well as proper serialization/deserialization for DescriptorNumber/Parameter attributes.

It assumes that Parameters/DescriptorNumbers are assigned as properties with the getters returning the parameter but the setter only setting the value of the parameter. e.g.

@property
def my_param(self) -> Parameter:
    '''My param.'''
    return self._my_param


@my_param.setter
def my_param(self, new_value: float) -> None:
    '''My param.'''
    self._my_param.value = new_value

Methods:

Name Description
get_all_variables

Get all Descriptor and Parameter objects as a list.

get_all_parameters

Get all Parameter objects as a list.

get_fittable_parameters

Get all parameters which can be fitted as a list.

get_free_parameters

Get all parameters which are currently free to be fitted as a

from_dict

Re-create an EasyScience object with DescriptorNumber attributes

to_dict

Convert an EasyScience object into a full dictionary using

__copy__

Return a copy of the object.

Attributes:

Name Type Description
unique_name str

Get the unique name of the object.

display_name str

Get a pretty display name.

Attributes
unique_name property writable

Get the unique name of the object.

display_name property writable

Get a pretty display name.

Returns:

Type Description
str

The pretty display name.

Methods:
get_all_variables()

Get all Descriptor and Parameter objects as a list.

Returns:

Type Description
List[DescriptorBase]

List of Descriptor and Parameter objects.

get_all_parameters()

Get all Parameter objects as a list.

Returns:

Type Description
List[Parameter]

List of Parameter objects.

get_fittable_parameters()

Get all parameters which can be fitted as a list.

Returns:

Type Description
List[Parameter]

List of Parameter objects.

get_free_parameters()

Get all parameters which are currently free to be fitted as a list.

Returns:

Type Description
List[Parameter]

List of Parameter objects.

from_dict(obj_dict) classmethod

Re-create an EasyScience object with DescriptorNumber attributes from a full encoded dictionary.

Parameters:

Name Type Description Default
obj_dict Dict[str, Any]

Dictionary containing the serialized contents (from to_dict) of an EasyScience object.

required

Returns:

Type Description
ModelBase

Reformed EasyScience object.

Raises:

Type Description
SyntaxError

If a deserialized parameter cannot be attached back to the class definition.

ValueError

If the input dictionary does not describe the expected class.

to_dict(skip=None)

Convert an EasyScience object into a full dictionary using SerializerBases generic convert_to_dict method.

Parameters:

Name Type Description Default
skip Optional[List[str]]

List of field names as strings to skip when forming the dictionary. By default, None.

None

Returns:

Type Description
Dict[str, Any]

Encoded object containing all information to reform an EasyScience object.

Notes

A unique_name that was generated automatically is not written; only an explicitly supplied one is, so that a deserialized object gets a fresh name instead of clashing with the original. Likewise display_name is omitted when it is None. Pass skip to drop further fields.

__copy__()

Return a copy of the object.

serializer_base

Classes:

Name Description
SerializerBase

This is the base class for creating an encoder/decoder which can

Classes

SerializerBase

This is the base class for creating an encoder/decoder which can convert EasyScience objects.

encode and decode are abstract methods to be implemented for each serializer. It is expected that the helper function _convert_to_dict will be used as a base for encoding.

Methods:

Name Description
encode

Abstract implementation of an encoder.

decode

Re-create an EasyScience object from the output of an encoder.

get_arg_spec

Get the full argument specification of a function (typically

deserialize_dict

Deserialize a dictionary using from_dict for ES objects and

Methods:
encode(obj, skip=None, **kwargs) abstractmethod

Abstract implementation of an encoder.

Parameters:

Name Type Description Default
obj Any

Object to be encoded.

required
skip List[str] | None

List of field names as strings to skip when forming the encoded object. By default, None.

None
**kwargs

Any additional key word arguments to be passed to the encoder.

{}

Returns:

Type Description
any

Encoded object containing all information to reform an EasyScience object.

decode(obj) abstractmethod classmethod

Re-create an EasyScience object from the output of an encoder.

Parameters:

Name Type Description Default
obj Any

Encoded EasyScience object.

required

Returns:

Type Description
Any

Reformed EasyScience object.

get_arg_spec(func) staticmethod

Get the full argument specification of a function (typically __init__)

Both positional-or-keyword and keyword-only arguments are returned, so constructors that use * to force keyword-only arguments are serialized correctly.

Parameters:

Name Type Description Default
func Callable

Function to be inspected.

required

Returns:

Type Description
Tuple[Any, List[str]]

Tuple of argument spec and arguments.

deserialize_dict(in_dict) staticmethod

Deserialize a dictionary using from_dict for ES objects and SerializerBase otherwise. This method processes constructor arguments, skipping metadata keys starting with '@'.

Parameters:

Name Type Description Default
in_dict Dict[str, Any]

Dictionary to deserialize.

required

Returns:

Type Description
Dict[str, Any]

Deserialized dictionary with constructor arguments.