Objects (pyobs.object)

Object is the base for almost all classes in pyobs. It adds some convenience methods and helper methods for creating other Objects.

There are a few convenience functions:

class Object(vfs: 'VirtualFileSystem' | Dict[str, Any] | None = None, comm: Comm | Dict[str, Any] | None = None, timezone: str | datetime.tzinfo = 'utc', location: str | Dict[str, Any] | EarthLocation | None = None, observer: Observer | None = None, **kwargs: Any)[source]

Base class for all objects in pyobs.

Note

Objects must always be opened and closed using open() and close(), respectively.

This class provides a VirtualFileSystem, a timezone and a location. From the latter two, an observer object is automatically created.

Object also adds support for easily adding threads using the add_background_task() method as well as a watchdog thread that automatically restarts threads, if requested.

Using add_child_object(), other objects can be (created an) attached to this object, which then automatically handles calls to open() and close() on those objects.

Parameters:
  • vfs – VFS to use (either object or config)

  • comm – Comm object to use

  • timezone – Timezone at observatory.

  • location – Location of observatory, either a name or a dict containing latitude, longitude, and elevation.

add_background_task(func: Callable[[...], Coroutine[Any, Any, None]], restart: bool = True) None[source]

Add a new function that should be run in the background.

MUST be called in constructor of derived class or at least before calling open() on the object.

Parameters:
  • func – Func to add.

  • restart – Whether to restart this function.

add_child_object(config_or_object: Dict[str, Any] | ObjectClass | Type[ObjectClass] | Any, object_class: Type[ObjectClass], copy_comm: bool = True, **kwargs: Any) ObjectClass[source]
add_child_object(config_or_object: Type[ObjectClass], object_class: None = None, copy_comm: bool = True, **kwargs: Any) ObjectClass
add_child_object(config_or_object: Dict[str, Any] | ObjectClass | Type[ObjectClass] | Any, object_class: None, copy_comm: bool = True, **kwargs: Any) Any

Create a new sub-module, which will automatically be opened and closed.

Parameters:
  • config_or_object – Module definition

  • object_class – Class for new module

  • copy_comm – Copy comm from this object to the new one.

Returns:

The created module.

async close() None[source]

Close module.

get_object(config_or_object: Dict[str, Any] | ObjectClass | Type[ObjectClass] | Any, object_class: Type[ObjectClass], copy_comm: bool = True, **kwargs: Any) ObjectClass[source]
get_object(config_or_object: Type[ObjectClass], object_class: None = None, copy_comm: bool = True, **kwargs: Any) ObjectClass
get_object(config_or_object: Dict[str, Any] | ObjectClass | Type[ObjectClass] | Any, object_class: None, copy_comm: bool = True, **kwargs: Any) Any

Creates object from config or returns object directly, both optionally after check of type.

Parameters:
  • config_or_object – A configuration dict or an object itself to create/check. If a dict with a class key is given, a new object is created.

  • object_class – Class to check object against.

  • copy_comm – Copy comm from this object to the new one.

Returns:

(New) object (created from config) that optionally passed class check.

Raises:

TypeError – If the object does not match the given class.

get_safe_object(config_or_object: Dict[str, Any] | ObjectClass | Type[ObjectClass] | Any, object_class: Type[ObjectClass], copy_comm: bool = True, **kwargs: Any) ObjectClass | None[source]
get_safe_object(config_or_object: Dict[str, Any] | ObjectClass | Type[ObjectClass] | Any, object_class: None, copy_comm: bool = True, **kwargs: Any) Any | None

Calls get_object in a safe way and returns None, if an exceptions thrown.

async open() None[source]

Open module.

property opened: bool

Whether object has been opened.

async proxy(name_or_object: str | object, obj_type: Type[ProxyType]) ProxyType[source]
async proxy(name_or_object: str | object, obj_type: Type[ProxyType] | None = None) Any

Returns object directly if it is of given type. Otherwise get proxy of client with given name and check type.

If name_or_object is an object:
  • If it is of type (or derived), return object.

  • Otherwise raise exception.

If name_name_or_object is string:
  • Create proxy from name and raise exception, if it doesn’t exist.

  • Check type and raise exception if wrong.

  • Return object.

Parameters:
  • name_or_object – Name of object or object itself.

  • obj_type – Expected class of object.

Returns:

Object or proxy to object.

Raises:

ValueError – If proxy does not exist or wrong type.

quit() None[source]

Can be overloaded to quit program.

get_object(config_or_object: Dict[str, Any] | ObjectClass | Type[ObjectClass] | Any, object_class: Type[ObjectClass], **kwargs: Any) ObjectClass[source]
get_object(config_or_object: Dict[str, Any] | ObjectClass | Type[ObjectClass] | Any, object_class: None, **kwargs: Any) Any

Creates object from config or returns object directly, both optionally after check of type.

Parameters:
  • config_or_object – A configuration dict or an object itself to create/check. If a dict with a class key is given, a new object is created.

  • object_class – Class to check object against.

Returns:

(New) object (created from config) that optionally passed class check.

Raises:

TypeError – If the object does not match the given class.

get_safe_object(config_or_object: ObjectClass | Dict[str, Any], object_class: Type[ObjectClass], **kwargs: Any) ObjectClass | None[source]
get_safe_object(config_or_object: ObjectClass | Any, object_class: None, **kwargs: Any) Any | None

Calls get_object in a safe way and returns None, if an exceptions thrown.

Parameters:
  • config_or_object – A configuration dict or an object itself to create/check. If a dict with a class key is given, a new object is created.

  • object_class – Class to check object against.

Returns:

(New) object (created from config) that optionally passed class check or None.

create_object(config: Dict[str, Any], *args: Any, **kwargs: Any) Any[source]

Create object from dict config.

Parameters:
  • config – Config to create object from

  • *args – Parameters to be passed to object.

  • **kwargs – Parameters to be passed to object.

Returns:

Created object.

get_class_from_string(class_name: str) Type[Any][source]

Get class from a given string.

Parameters:

class_name – Name of class as string.

Returns:

Actual class.