scistag.datastag.data_stag_connection.DataStagConnection¶
- class DataStagConnection(url='', local=True, _request_client=None)[source]¶
Bases:
objectDefines a connection to a DataStack vault - locally or remote
Initializer
- Parameters
url (str) – The connection url of a remote server
local – Defines if the local in memory database shall be used
_request_client (FlaskClient) – For unit tests only. Uses Flask’s internal test client to test the API.
Methods
Adds given value to the element stored in the database.
Implicit garbage collection request
Converts data of a binary type, e.g.
Deletes an element :type name:
str:param name: The element's name.Deletes a set of elements
Verifies if given element exists
Finds a list of elements by name
Tries to read an element from the database :type name:
str:param name: The element's name :type default:Union[str,bool,int,float,bytes,dict,list,ndarray,None] :param default: The default return value if the element does nto exist :rtype:Union[str,bool,int,float,bytes,dict,list,ndarray,None] :return: The elementTries to read an element from the database.
Returns the database status
Returns an object's update time stamp (if it was set via set_ts)
Returns the object if it was modified since timestamp :param name: The element's name :param timestamp: The previous timestamp :return: The timestamp or counter, the data on success, otherwise the old timestamp, None
Returns the data of a set of elements by name.
Converts an element converted to JSOn back to it's original type.
Tries to receive a list of elements from the vault
Tries to retrieve the data of a list
Tries to remove an element at the beginning of a list
Inserts an element at the beginning of a list
Stores a named element in the database
Stores a named element in the database and adds a timestamp to it in a neighbour entry named :timeStamp
Starts a new transaction. All write operations will be bundled and
Locks the access to the vault and bundles upcoming write events
Unlocks the access to the vault and bundles upcoming write events
Attributes
_ADVANCED_COMMAND_COMMAND_ADD_COMMAND_COLLECT_GARBAGE_COMMAND_DELETE_COMMAND_DELETE_MULTIPLE_COMMAND_EXISTS_COMMAND_FIND_COMMAND_GET_COMMAND_GET_EX_COMMAND_GET_VALUES_BY_NAME_COMMAND_LELEMENTS_COMMAND_LLEN_COMMAND_POP_COMMAND_PUSH_COMMAND_SET_COMMAND_STATUSName of a timestamp neighbour entry in which the time when an entry was written is stored
_DATA_DEFAULT_ELEMENTS_END_FLAT_INDEX_LIMIT_MASK_NAME_RECURSIVE_RELATIVE_NAMES_SEARCH_MASKS_START_TIMESTAMP_IDENTIFIER_TIME_OUT_TYPE_TYPE_BYTES_TYPE_NUMPY_VALUE_VERSION_COUNTER__dict____doc____module____weakref__list of weak references to the object (if defined)
Defines if a local storage is being used
Link to the local data vault
The target url (when not using a local db)
When testing locally this links to the local test client
The current transaction
- classmethod _verify_result(result, supported_types)[source]¶
Verifies if the result is of a given type
- add(name, value=1, timeout_s=None, default=0)[source]¶
Adds given value to the element stored in the database. If it does not exist yet, it will be initialized with default.
- Parameters
name – The element’s name
value – The value to be added. 1 by default.
timeout_s – The timeout for automatic deletion
default – The default value
- Returns
The new value
- collect_garbage()[source]¶
Implicit garbage collection request
- Return type
- Returns
True if at least one time interval passed and something could get collected
- classmethod data_to_json(data)[source]¶
Converts data of a binary type, e.g. np.ndarray or bytes to a JSON representation
- delete(name)[source]¶
Deletes an element :type name:
str:param name: The element’s name. :rtype:bool:return: True on success
- find(mask, limit=100, relative_names=False, recursive=False)[source]¶
Finds a list of elements by name
- get(name, default=None)[source]¶
Tries to read an element from the database :type name:
str:param name: The element’s name :type default:Union[str,bool,int,float,bytes,dict,list,ndarray,None] :param default: The default return value if the element does nto exist :rtype:Union[str,bool,int,float,bytes,dict,list,ndarray,None] :return: The element
- get_ex(name, default=None, version_counter=-1)[source]¶
Tries to read an element from the database. Allows to add a version check so only data will be returned if it changed since the last get_ex. :param name: The element’s name :param default: The default return value if the element does not exist :param version_counter: If set then a value will only be returned if the element’s update counter does not match :return: The element’s version, The element
- get_status(advanced=False)[source]¶
Returns the database status
- Parameters
advanced (
bool) – Defines if advanced details shall be received as well- Returns
A dictionary containing the status
- get_ts_modified(name, timestamp=0.0)[source]¶
Returns the object if it was modified since timestamp :param name: The element’s name :param timestamp: The previous timestamp :return: The timestamp or counter, the data on success, otherwise the
old timestamp, None
- get_values_by_name(mask, limit=100, flat=True)[source]¶
Returns the data of a set of elements by name.
- Parameters
- Returns
A list containing the data and names of all valid elements
- classmethod json_to_data(data)[source]¶
Converts an element converted to JSOn back to it’s original type. Special types, e.g. binary representations are flagged as those using a _TYPE and a _VALUE element in a dictionary.
- pop(name, default=None, index=0)[source]¶
Tries to remove an element at the beginning of a list
- push(name, data, timeout_s=None, index=-1)[source]¶
Inserts an element at the beginning of a list
- Parameters
- Return type
- Returns
The new length of the list
- set_ts(name, data, timeout_s=None, timestamp=None)[source]¶
Stores a named element in the database and adds a timestamp to it in a neighbour entry named :timeStamp
- Parameters
- Return type
- Returns
True on success
- start_transaction()[source]¶
- Starts a new transaction. All write operations will be bundled and
executed in a single lock.
Use like: with my_connection.start_transaction(): … :return: The transaction object
- ts_lock(target)[source]¶
Locks the access to the vault and bundles upcoming write events
- Parameters
target (
DataStagTransaction) – The current transaction
- _COUNTER_IDENTIFIER = ':counter'¶
Name of a timestamp neighbour entry in which the time when an entry was written is stored
- local¶
Defines if a local storage is being used
- request_client: FlaskClient¶
When testing locally this links to the local test client
- vault: DataStagVault | None¶
Link to the local data vault