labthings_fastapi.descriptors.property

Define an object to represent an Action, as a descriptor.

Module Contents

Classes

ThingProperty

A property that can be accessed via the HTTP API

ThingSetting

A setting can be accessed via the HTTP API and is persistent between sessions

API

class labthings_fastapi.descriptors.property.ThingProperty(model: type, initial_value: Any = None, readonly: bool = False, observable: bool = False, description: Optional[str] = None, title: Optional[str] = None, getter: Optional[Callable] = None, setter: Optional[Callable] = None)

A property that can be accessed via the HTTP API

By default, a ThingProperty is “dumb”, i.e. it acts just like a normal variable.

model: type[pydantic.BaseModel]

None

readonly: bool

False

__init__(model: type, initial_value: Any = None, readonly: bool = False, observable: bool = False, description: Optional[str] = None, title: Optional[str] = None, getter: Optional[Callable] = None, setter: Optional[Callable] = None)
__set_name__(owner, name: str)
property title

A human-readable title

property description

A description of the property

__get__(obj, type=None) Any

The value of the property

If obj is none (i.e. we are getting the attribute of the class), we return the descriptor.

If no getter is set, we’ll return either the initial value, or the value from the object’s dict, i.e. we behave like a variable.

If a getter is set, we will use it, unless the property is observable, at which point the getter is only ever used once, to set the initial value.

__set__(obj, value)

Set the property’s value

_observers_set(obj)

A set used to notify changes

emit_changed_event(obj: labthings_fastapi.thing.Thing, value: Any) None

Notify subscribers that the property has changed

This function is run when properties are upadated. It must be run from within a thread. This could be the Invocation thread of a running action, or the property should be updated over via a client/http. It must be run from a thread as it is communicating with the event loop via an asyncio blocking portal.

Raises:

NotConnectedToServerError – if the Thing that is calling the property update is not connected to a server with a running event loop.

async emit_changed_event_async(obj: labthings_fastapi.thing.Thing, value: Any)

Notify subscribers that the property has changed

property name

The name of the property

add_to_fastapi(app: fastapi.FastAPI, thing: labthings_fastapi.thing.Thing)

Add this action to a FastAPI app, bound to a particular Thing.

property_affordance(thing: labthings_fastapi.thing.Thing, path: Optional[str] = None) labthings_fastapi.thing_description.model.PropertyAffordance

Represent the property in a Thing Description.

getter(func: Callable) typing_extensions.Self

set the function that gets the property’s value

setter(func: Callable) typing_extensions.Self

Decorator to set the property’s value

ThingProperty descriptors return the value they hold when they are accessed. However, they can run code when they are set: this decorator sets a function as that code.

class labthings_fastapi.descriptors.property.ThingSetting(model: type, initial_value: Any = None, readonly: bool = False, observable: bool = False, description: Optional[str] = None, title: Optional[str] = None, getter: Optional[Callable] = None, setter: Optional[Callable] = None)

Bases: labthings_fastapi.descriptors.property.ThingProperty

A setting can be accessed via the HTTP API and is persistent between sessions

A ThingSetting is a ThingProperty with extra functionality for triggering a Thing to save its settings.

Note: If a setting is mutated rather than assigned to, this will not trigger saving. For example: if a Thing has a setting called dictsetting holding the dictionary {"a": 1, "b": 2} then self.dictsetting = {"a": 2, "b": 2} would trigger saving but self.dictsetting[a] = 2 would not, as the setter for dictsetting is never called.

The setting otherwise acts just like a normal variable.

__set__(obj, value)

Set the property’s value

set_without_emit(obj, value)

Set the property’s value, but do not emit event to notify the server

This function is not expected to be used externally. It is called during initial setup so that the setting can be set from disk before the server is fully started.