Skip to content

laser.core.propertyset

laser.core.propertyset

Implements a PropertySet class that can be used to store properties in a dictionary-like object.

laser.core.propertyset.PropertySet(*bags)

A class that can be used to store properties in a dictionary-like object with .property access to properties.

Examples

Basic Initialization:

::

1
2
3
4
5
6
from laser.core import PropertySet
ps = PropertySet()
ps['infection_status'] = 'infected'
ps['age'] = 35
print(ps.infection_status)  # Outputs: 'infected'
print(ps['age'])            # Outputs: 35

Combining two PropertySets:

::

1
2
3
4
5
ps1 = PropertySet({'immunity': 'high', 'region': 'north'})
ps2 = PropertySet({'infectivity': 0.7})
combined_ps = ps1 + ps2
print(combined_ps.to_dict())
# Outputs: {'immunity': 'high', 'region': 'north', 'infectivity': 0.7}

Creating a PropertySet from a dictionary:

::

1
2
3
4
5
ps = PropertySet({'mything': 0.4, 'that_other_thing': 42})
print(ps.mything)            # Outputs: 0.4
print(ps.that_other_thing)   # Outputs: 42
print(ps.to_dict())
# Outputs: {'mything': 0.4, 'that_other_thing': 42}

Save and load:

::

1
2
3
ps.save('properties.json')
loaded_ps = PropertySet.load('properties.json')
print(loaded_ps.to_dict())  # Outputs the saved properties

Property access and length:

::

1
2
3
4
ps['status'] = 'susceptible'
ps['exposure_timer'] = 5
print(ps['status'])          # Outputs: 'susceptible'
print(len(ps))               # Outputs: 4

In-Place addition (added keys must not exist in the destination PropertySet):

::

1
2
3
ps += {'new_timer': 10, 'susceptibility': 0.75}
print(ps.to_dict())
# Outputs: {'mything': 0.4, 'that_other_thing': 42, 'status': 'susceptible', 'exposure_timer': 5, 'new_timer': 10, 'susceptibility': 0.75}

In-place update (keys must already exist in the destination PropertySet):

::

1
2
3
ps <<= {'exposure_timer': 10, 'infectivity': 0.8}
print(ps.to_dict())
# Outputs: {'mything': 0.4, 'that_other_thing': 42, 'status': 'susceptible', 'exposure_timer': 10, 'infectivity': 0.8}

In-place addition or update (no restriction on incoming keys):

::

1
2
3
ps |= {'new_timer': 10, 'exposure_timer': 8}
print(ps.to_dict())
# Outputs: {'mything': 0.4, 'that_other_thing': 42, 'status': 'susceptible', 'exposure_timer': 8, 'new_timer': 10}

Initialize a PropertySet to manage properties in a dictionary-like structure.

Parameters:

Name Type Description Default
bags Union[PropertySet, list, tuple, dict]

A sequence of key-value pairs (e.g., lists, tuples, dictionaries) to initialize the PropertySet. Keys must be strings, and values can be any type.

()

laser.core.propertyset.PropertySet.__add__(other)

Add another PropertySet to this PropertySet.

This method allows the use of the + operator to combine two PropertySet instances.

Parameters:

Name Type Description Default
other PropertySet

The other PropertySet instance to add.

required

Returns:

Name Type Description
PropertySet PropertySet

A new PropertySet instance that combines the properties of both instances.

laser.core.propertyset.PropertySet.__contains__(key)

Check if a key is in the property set.

Parameters:

Name Type Description Default
key str

The key to check for existence in the property set.

required

Returns:

Name Type Description
bool bool

True if the key exists in the property set, False otherwise.

laser.core.propertyset.PropertySet.__eq__(other)

Check if two PropertySet instances are equal.

Parameters:

Name Type Description Default
other PropertySet

The other PropertySet instance to compare.

required

Returns:

Name Type Description
bool bool

True if the two instances are equal, False otherwise.

laser.core.propertyset.PropertySet.__getitem__(key)

Retrieve the attribute of the object with the given key (e.g., ps[key]).

Parameters:

Name Type Description Default
key str

The name of the attribute to retrieve.

required

Returns:

Name Type Description
Any any

The value of the attribute with the specified key.

Raises:

Type Description
AttributeError

If the attribute with the specified key does not exist.

laser.core.propertyset.PropertySet.__iadd__(other)

Implements the in-place addition (+=) operator for the class.

This method allows the instance to be updated with attributes from another instance of the same class or from a dictionary. If other is an instance of the same class, its attributes are copied to the current instance. If other is a dictionary, its key-value pairs are added as attributes to the current instance.

Parameters:

Name Type Description Default
other Union[type(self), dict]

The object or dictionary to add to the current instance.

required

Returns:

Name Type Description
self PropertySet

The updated instance with the new attributes.

Raises:

Type Description
AssertionError

If other is neither an instance of the same class nor a dictionary.

ValueError

If other contains keys already present in the PropertySet.

laser.core.propertyset.PropertySet.__ilshift__(other)

Implements the <<= operator on PropertySet to override existing values with new values.

Parameters:

Name Type Description Default
other Union[type(self), dict]

The object or dictionary with overriding values.

required

Returns:

Name Type Description
self PropertySet

The updated instance with the overrides from other.

Raises:

Type Description
AssertionError

If other is neither an instance of the same class nor a dictionary.

ValueError

If other contains keys not present in the PropertySet.

laser.core.propertyset.PropertySet.__ior__(other)

Implements the |= operator on PropertySet to override existing values with new values.

Parameters:

Name Type Description Default
other Union[type(self), dict]

The object or dictionary with overriding values.

required

Returns:

Name Type Description
self PropertySet

The updated instance with all the values of self with new or overriding values from other.

Raises:

Type Description
AssertionError

If other is neither an instance of the same class nor a dictionary.

laser.core.propertyset.PropertySet.__len__()

Return the number of attributes in the instance.

This method returns the number of attributes stored in the instance's dict attribute, which represents the instance's namespace.

Returns:

Name Type Description
int int

The number of attributes in the instance.

laser.core.propertyset.PropertySet.__lshift__(other)

Implements the << operator on PropertySet to override existing values with new values.

Parameters:

Name Type Description Default
other Union[type(self), dict]

The object or dictionary with overriding values.

required

Returns:

Name Type Description
PropertySet PropertySet

A new PropertySet with all the values of the first PropertySet with overrides from the second PropertySet.

Raises:

Type Description
AssertionError

If other is neither an instance of the same class nor a dictionary.

ValueError

If other contains keys not present in the PropertySet.

laser.core.propertyset.PropertySet.__or__(other)

Implements the | operator on PropertySet to add new or override existing values with new values.

Parameters:

Name Type Description Default
other Union[type(self), dict]

The object or dictionary with overriding values.

required

Returns:

Name Type Description
PropertySet PropertySet

A new PropertySet with all the values of the first PropertySet with new or overriding values from the second PropertySet.

Raises:

Type Description
AssertionError

If other is neither an instance of the same class nor a dictionary.

laser.core.propertyset.PropertySet.__repr__()

Return a string representation of the PropertySet instance.

The string representation includes the class name and the dictionary of the instance's attributes.

Returns:

Name Type Description
str str

A string representation of the PropertySet instance.

laser.core.propertyset.PropertySet.__setitem__(key, value)

Set the value of an attribute. This method allows setting an attribute of the instance using the dictionary-like syntax (e.g., ps[key] = value).

Parameters:

Name Type Description Default
key str

The name of the attribute to set.

required
value any

The value to set for the attribute.

required

laser.core.propertyset.PropertySet.__str__()

Returns a string representation of the object's dictionary.

This method is used to provide a human-readable string representation of the object, which includes all the attributes stored in the object's __dict__.

Returns:

Name Type Description
str str

A string representation of the object's dictionary.

laser.core.propertyset.PropertySet.load(filename) staticmethod

Load a PropertySet from a specified file.

Parameters:

Name Type Description Default
filename str

The path to the file where the PropertySet is saved.

required

Returns:

Name Type Description
PropertySet PropertySet

The PropertySet instance loaded from the file.

laser.core.propertyset.PropertySet.save(filename)

Save the PropertySet to a specified file.

Parameters:

Name Type Description Default
filename str

The path to the file where the PropertySet will be saved.

required

laser.core.propertyset.PropertySet.to_dict()

Convert the PropertySet to a dictionary.