tlc.objects¶
Base classes for the 3LC object hierarchy.
The four-level hierarchy Object → SerializableObject → AddressableObject → MutableObject underpins
every persistable 3LC type (tables, runs, configurations, …). While most cases reach for the concrete classes
(e.g. tlc.Table, tlc.Run, tlc.configuration.Configuration) directly, this
module exposes the bases for completeness and for expert authors of custom serializable types.
Package Contents¶
Classes¶
Class |
Description |
|---|---|
A 3LC object that lives at a URL and supports persistence, write-back, and telemetry. |
|
Base class for objects that can be mutated. |
|
Lightweight base for all 3LC objects: type identity and init-parameter assignment. |
|
Adds schema, type identity, serialization versioning, and JSON serialization. |
|
An immutable access interface to the rows of a Table object |
API¶
- class AddressableObject( )¶
Bases:
tlc._core.serializable_object.SerializableObjectA 3LC object that lives at a URL and supports persistence, write-back, and telemetry.
- Variables:
url – The URL which this object instance was deserialized FROM, or should be serialized TO. Note that this value is NOT written to JSON, since the JSON representation could potentially be moved around (as in e.g. a file being moved to another folder).
created – The time when this object was first created.
is_url_writable – Whether the URL where the object is stored is writable.
transaction_id – The transaction associated with this object.
- Parameters:
url – The URL of the object.
created – The creation time of the object.
init_parameters – A dictionary containing the initial values of the object’s properties.
- absolute_url_from_relative(
- input_url: Url,
Convert a relative URL to be absolute, given the URL of this object.
- Parameters:
input_url – The relative URL to convert.
- Returns:
The absolute URL.
- static add_is_url_writable_property_to_schema(
- schema: Schema,
Add the ‘is_url_writable’ property to the provided schema.
- Parameters:
schema – The schema to add the property to.
- static add_object_created_property_to_schema(
- schema: Schema,
Add the ‘created’ property to the provided schema.
- Parameters:
schema – The schema to add the property to.
- static add_object_url_property_to_schema( ) None¶
Add the ‘url’ property to the provided schema.
- Parameters:
schema – The schema to add the property to.
url_string_icon – The icon to display next to the URL string.
- copy(
- *,
- destination_url: tlc.Url | None = None,
- if_exists: typing.Literal[raise,
- rename,
- overwrite] = 'raise',
Return a copy of this object, with the specified URL.
- Parameters:
destination_url – The url to write the copy to. If not provided, a new url will be generated based on the objects own url.
if_exists – How to handle the case where the destination URL already exists.
- Returns:
A copy of this object.
- delete() None¶
Delete this object from storage.
This method permanently removes the object from storage by deleting the underlying files or objects that the object’s URL points to. The object’s URL will no longer be valid after this operation.
Example:
table = Table.from_dict({"a": [1, 2, 3]}) table.write_to_url() # persist to storage ... table.delete() # delete the backing files and objects
To delete an object by URL without loading it first, use:
Url("path/to/object").delete()
Note: This is different from Python’s
deloperator -del objonly removes the variable name, whileobj.delete()removes the object from storage (and keeps the variable name).
- ensure_fully_defined() None¶
Make sure the internal state of the object is fully defined.
Extends
SerializableObject.ensure_fully_defined()with write-back: after the schema and dependent properties are resolved, the object is persisted to its URL if appropriate.
- classmethod from_url( ) AddressableObject¶
Create an AddressableObject from a URL.
- Parameters:
url – The URL of the object.
- Returns:
The deserialized object.
- is_stale( ) bool¶
Indicate whether this object is stale compared to a given timestamp.
The base implementation never considers an object stale.
MutableObjectoverrides this to compare against thelast_modifiedproperty.- Parameters:
timestamp – The timestamp against which to check staleness. Can be None.
epsilon – The tolerance in seconds for staleness. If the difference between the object’s timestamp and the provided timestamp exceeds this value, the object is considered stale. Defaults to 0.0s.
- Returns:
True if the object is stale, False otherwise.
- relative_url_from_absolute(
- input_url: Url,
Convert an absolute URL to be relative, given the URL of this object.
- Parameters:
input_url – The absolute URL to convert.
- Returns:
The relative URL.
- property root_url: Url | None¶
The root URL of the object, if set.
This is a transient property that is not serialized.
- Returns:
The root URL of the object, or
Noneif not set.
- class MutableObject(
- *,
- url: Url | None = None,
- created: str | None = None,
- last_modified: str | None = None,
- init_parameters: Any = None,
Bases:
tlc._core.addressable_object.AddressableObjectBase class for objects that can be mutated.
MutableObject properties:
‘last_modified’: The time when this object was last modified.
The MutableObject have designated update_attribute/s methods that are meant to be used to modify the object’s attributes. Using these function to modify objects makes sure that the objects’ internal state are updated correctly, e.g. sets the ‘last_modified’ timestamp.
Directly modifying MutableObject (public) attributes from the outside using
mutable_obj.attr = valueorsetattr(mutable_obj, "attr", value)is not recommended. But, if object attributes have to be set directly, make sure to call theupdate_internal_stateto signal the update.- Parameters:
url – The URL of the object.
created – The creation time of the object.
init_parameters – A dictionary containing the initial values of the object’s properties.
- static add_last_modified_property_to_schema( ) None¶
Add the ‘last_modified’ property to this schema
- is_stale( ) bool¶
Indicates whether this object is stale compared to the given timestamp (using
last_modifiedproperty).The function compares the object’s
last_modifiedtimestamp with the provided timestamp. If the difference exceeds the provided epsilon value, the object is considered stale.- Parameters:
timestamp – The timestamp against which to check staleness. Can be None.
epsilon – The tolerance in seconds for staleness. If the difference between the object’s timestamp and the provided timestamp exceeds this value, the object is considered stale. Defaults to 0.0s.
- Returns:
True if the object is stale, False otherwise.
- Raises:
ValueError – if the timestamp is invalid.
- to_json(
- init_level: int = 0,
Returns a JSON representation of this object. This will be sufficient to recreate a fully functioning clone of the object at a later time.
Note that for brevity, properties with default values are not written to the string.
- touch_last_modified() None¶
Updates the last_modified timestamp to the current UTC time.
Guarantees the timestamp strictly advances on each call: Windows clock resolution can otherwise return the same value for back-to-back calls, which would hide a mutation from polling consumers.
- update_attribute( ) None¶
Updates the given attribute with the given value.
This method will only allow modification of attributes that are defined in the object’s schema and are writeable. If the object schema is has not yet been evaluated, this method will do so. If the object does not have a Schema a ValueError will be raised.
If the attribute is modified the object’s ‘last_modified’ timestamp is updated. The internal state of the object is also updated by calling the
_update_internal_statemethod.- Parameters:
attr_name – The name of the attribute to update.
value – The value to set.
- Returns:
True if the object was changed, False otherwise.
- Raises:
ValueError – if the schema is not set, or attribute name is unknown.
- update_attributes( ) None¶
Updates the object with the given attribute_name,value pairs.
This method will only allow modification of attributes that are defined in the object’s schema and are writeable. If the object schema is has not yet been evaluated, this method will do so. If the object does not have a Schema a ValueError will be raised.
If the attribute is modified the object’s ‘last_modified’ timestamp is updated. The internal state of the object is also updated by calling the
update_internal_statemethod.- Parameters:
attribute_dict – A mapping of attribute names to values to update.
- Raises:
ValueError – if the schema is not set, or attribute name is unknown.
- class Object(
- *,
- init_parameters: Any = None,
Lightweight base for all 3LC objects: type identity and init-parameter assignment.
This level carries no type/version fields, no schema, no URL, and no persistence logic.
- Parameters:
init_parameters – A dictionary containing the initial values of the object’s properties.
- initial_value( ) Any¶
Return the initial value of a property.
Returns
self.<property_name>if it already exists (set via init_parameters), otherwise new_value if notNone, otherwise default_value. This lets every object be fully initialized through its constructor.- Parameters:
property_name – The name of the property to get the initial value for.
new_value – The value to return if
self.<property_name>is not set.default_value – The value to return if
self.<property_name>is not set and new_value isNone.
- Returns:
The initial value of the property.
- class SerializableObject(
- *,
- init_parameters: Any = None,
Bases:
tlc._core.object.ObjectAdds schema, type identity, serialization versioning, and JSON serialization.
Extends
Object. This level carries no URL and no persistence logic.- Parameters:
init_parameters – A dictionary containing the initial values of the object’s properties.
- ensure_dependent_properties() None¶
Make sure dependent properties are populated.
Override in subclasses to ensure the required dependent properties are populated.
- ensure_fully_defined() None¶
Make sure the internal state of the object is fully defined.
For most objects, this simply amounts to populating the
schemaproperty according to the properties which are directly present within the class.AddressableObjectoverrides this with a@finalversion that adds write-back logic.
- classmethod from_dict( ) SerializableObject¶
Create a SerializableObject from a dictionary containing a
typefield and init parameters.This performs a type-registry lookup and calls the constructor with the dictionary as init_parameters. Use this to reconstruct inline (non-URL) objects from their serialized form.
.. note::
If the resolved type is an
AddressableObjectsubclass, calling this method will trigger URL generation and object-registry side effects. Preferfrom_url()for URL-backed objects.- Parameters:
d – A dictionary with at least a
typekey mapping to a registered type name.- Returns:
A fully initialized instance of the registered type.
- Raises:
KeyError – If d does not contain a
typekey.ValueError – If the
typevalue is not a registered type name.
- should_include_schema_in_json(
- _schema: Schema,
Indicate whether the schema property of this object should be included when serializing to JSON.
- Parameters:
_schema – The schema of the object. This is passed in to allow subclasses to make decisions based on the schema of the object, but it is not used in the base implementation.
- to_json(
- init_level: int = 1,
Return a JSON representation of this object.
This will be sufficient to recreate a fully functioning clone of the object at a later time.
Note that for brevity, properties with default values are not written to the string.
- Parameters:
init_level – The level of initialization to use when serializing the object. 1: Minimal schema 2: Complete schema 3: Fully defined object
- Returns:
A JSON representation of this object.