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: Optional[Union['VirtualFileSystem', Dict[str, Any]]] = None, comm: Optional[Union[Comm, Dict[str, Any]]] = None, timezone: Union[str, datetime.tzinfo] = 'utc', location: Optional[Union[str, Dict[str, Any], EarthLocation]] = None, observer: Optional[Observer] = 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[[...], collections.abc.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: Union[Dict[str, Any], pyobs.object.ObjectClass, Type[pyobs.object.ObjectClass], Any], object_class: Type[pyobs.object.ObjectClass], copy_comm: bool = True, **kwargs: Any) pyobs.object.ObjectClass[source]
add_child_object(config_or_object: Type[pyobs.object.ObjectClass], object_class: None = None, copy_comm: bool = True, **kwargs: Any) pyobs.object.ObjectClass
add_child_object(config_or_object: Union[Dict[str, Any], pyobs.object.ObjectClass, Type[pyobs.object.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: Union[Dict[str, Any], pyobs.object.ObjectClass, Type[pyobs.object.ObjectClass], Any], object_class: Type[pyobs.object.ObjectClass], copy_comm: bool = True, **kwargs: Any) pyobs.object.ObjectClass[source]
get_object(config_or_object: Type[pyobs.object.ObjectClass], object_class: None = None, copy_comm: bool = True, **kwargs: Any) pyobs.object.ObjectClass
get_object(config_or_object: Union[Dict[str, Any], pyobs.object.ObjectClass, Type[pyobs.object.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: Union[Dict[str, Any], pyobs.object.ObjectClass, Type[pyobs.object.ObjectClass], Any], object_class: Type[pyobs.object.ObjectClass], copy_comm: bool = True, **kwargs: Any) Optional[pyobs.object.ObjectClass][source]
get_safe_object(config_or_object: Union[Dict[str, Any], pyobs.object.ObjectClass, Type[pyobs.object.ObjectClass], Any], object_class: None, copy_comm: bool = True, **kwargs: Any) Optional[Any]

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: Union[str, object], obj_type: Type[pyobs.object.ProxyType]) pyobs.object.ProxyType[source]
async proxy(name_or_object: Union[str, object], obj_type: Optional[Type[pyobs.object.ProxyType]] = 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: Union[Dict[str, Any], pyobs.object.ObjectClass, Type[pyobs.object.ObjectClass], Any], object_class: Type[pyobs.object.ObjectClass], **kwargs: Any) pyobs.object.ObjectClass[source]
get_object(config_or_object: Union[Dict[str, Any], pyobs.object.ObjectClass, Type[pyobs.object.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: Union[pyobs.object.ObjectClass, Dict[str, Any]], object_class: Type[pyobs.object.ObjectClass], **kwargs: Any) Optional[pyobs.object.ObjectClass][source]
get_safe_object(config_or_object: Union[pyobs.object.ObjectClass, Any], object_class: None, **kwargs: Any) Optional[Any]

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.