Skip to content

qruise-kb

qruise.kb.create_session(profile=None, profiles=None, logger=LOGGER.info, check=False, connect_elasticsearch=True, configuration_manager=None, **kwargs)

Create a session to a QruiseOS knowledge database.

Parameters:

Name Type Description Default
profile str

The name of the profile to use, by default uses default profile specified as default.kb key under the [default] section .

None
profiles str

The path to the profiles file, by default uses the file ~/.config/qruise/configuration.yaml.

None
logger callable

The logger to use, by default uses qruise.kb.connection.LOGGER.info.

info
check bool

Whether to check the connection, by default True.

False
connect_elasticsearch bool

Whether to connect to elasticsearch, by default True.

True
configuration_manager QruiseConfigurationManager

The configuration manager to use, by default None. Can't be specified together with profiles.

None
**kwargs Any

Additional keyword arguments to pass to qruise.kb.connection.connect.

{}

Returns:

Type Description
Session

The session to the QruiseOS knowledge database.

qruise.kb.Session

Session class for interacting with QruiseOS knowledge base.

branch property writable

The current branch.

default_load_type = default_load_type instance-attribute

The default document type to load if no type is specified.

ref property writable

The current commit ref used for reads.

Examples:

This property can also be set to do time travel. To do so you can have a look at commit history

>>> session.get_commit_history()
And then decide to go back 5 commits
>>> session.ref = session.get_commit_hashes()[5]
To use the latest commit from the current branch.
>>> session.ref = None

schema property

The schema of the database. If schema is not loaded, load it from the database.

Returns:

Type Description
Schema

The current schema of the database.

__init__(client, default_load_type='Entity', store=None, blob_store=None, search_client=None)

Create a new session.

Parameters:

Name Type Description Default
client Client

The client to use for interacting with the database.

required
default_load_type Optional[str]

The default document type to load if no type is specified, by default "Entity".

'Entity'
store Optional[QruiseStore]

The object store to use for storing documents, by default None.

None
blob_store Optional[BlobStore]

The blob store to use for storing blobs, by default None.

None
search_client Optional[QruiseSearchClient]

The search client to use for searching documents, by default None.

None
See Also

create_session : A helper function to create a Session instance from a profile.

load(doc_type=None, skip=0, limit=None)

Load all documents of a given type from the database. If schema is not loaded, load it from the database. Loading the schema locks the client to the specific commit to ensure consistent reads.

Parameters:

Name Type Description Default
doc_type str or List[str]

The type of document to load, by default :attr:default_load_type.

None

Returns:

Type Description
List[DocumentTemplate]

A list of documents of the given type.

Examples:

When just creating a session, nothing is loaded into memory by default. For example, if interested in getting parameters from Qubit documents, just load qubits.

>>> session.load('Qubit');
Then it is possible to display what are the available qubits
>>> session.Qubit.get_instances()
So that one can see the values it holds
>>> session.Qubit['Q1']
See Also

load_documents : for additional query and ordering possibilities load_document : for loading a specific document from its identifier

load_document(id)

Load a document by its IRI ID.

Parameters:

Name Type Description Default
id Optional[str]

The IRI ID of the document to load.

required

Returns:

Type Description
Optional[DocumentTemplate]

The loaded document, or None if no document was found.

load_documents(doc_type=None, skip=0, limit=10, where=None, order_by=None)

Load all documents of a given type from the database and add the to current session.

Parameters:

Name Type Description Default
doc_type str

The type of document to load. default is None.

None
skip int

The number of documents to skip. default is 0.

0
limit int

The number of documents to load. default is 10.

10
where Sequence[Tuple[str, str, Any]]

The where clause as triple of property name, operator and value. default is None (no where clause). Supported operators: - 'eq': equal - 'neq': not equal

None
order_by Sequence[Tuple[str, str]]

The sort order by pair of property name and sort order as 'asc' or desc. default is None (unsorted)

None

Examples:

Load qubit with name 'Q3'

>>> qubits = session.load_documents(doc_type="Qubit", 
                                where=(("name", "eq", "Q3"),),
                                limit=1)
>>> print(qubits[0].name)

Load last 5 experiments of type AmplitudeRabi sorted by timestamp descending, skipping the last experiment

>>> last_experiments = session.load_documents(doc_type="AmplitudeRabi", 
                                          order_by=(("timestamp", "desc"),), 
                                          limit=5, 
                                          skip=1)
>>> for experiment in last_experiments:
    print(experiment.id, experiment.timestamp)

Returns:

Type Description
List[DocumentTemplate]

A list of documents of the given type found in the database.

See Also

load : for a simpler loading function load_document : for loading a specific document from its identifier

load_schema()

Load the schema from the database. Pin the client to the specific commit to ensure consistent reads.

save(commit_msg, post_commit_hook=None)

Save all changes to the database. Reset client ref.

save_schema(commit_msg, full_replace=None)

Save the schema to the database. Sets the session ref to the new commit.

Parameters:

Name Type Description Default
commit_msg str

The commit message.

required
full_replace Optional[bool]

Whether to fully replace the schema, by default None, effectively False.

None

qruise.kb.session.Schema

add_enum_class(class_name, class_values)

Construct a TerminusDB Enum class by providing class name and member values then add into the schema.

Parameters:

Name Type Description Default
class_name str

Name of the class object constructed.

required
class_values list

A list of values in this Enum.

required

Returns:

Type Description
EnumMetaTemplate

A Enum object with the specified name and members

commit(client, commit_msg=None, full_replace=None)

Commit the schema to database

Parameters:

Name Type Description Default
client Client

A client that is connected to a database.

required
commit_msg str

Commit message.

None
full_replace Optional[bool]

Does the commit fully wiped out the old schema graph. Default to be False.

None

from_db(client, select=None)

Load classes in the database schema into schema

Parameters:

Name Type Description Default
client Client

Client that is connected to the database

required
select Optional[List[str]]

The classes (and depended classes) that will be imported, default to None which will import all classes

None

Returns:

Type Description
List[Dict[str, Any]]

list of all existing classes in the database schema as list of dictionaries

from_json_schema(name, json_schema, pipe=False, subdocument=False)

Load class object from json schema (http://json-schema.org/) and, if pipe mode is off, add into schema. All referenced object will be treated as subdocuments.

Parameters:

Name Type Description Default
name str

Name of the class object.

required
json_schema Union[dict, str, StringIO]

Json Schema in dictionary or json-able string format or json file stream.

required
pipe

Pipe mode, if True will return the schema in TerminusDB dictionary format (just like calling to_dict) WITHOUT loading the schema into the schema object. Default to False.

False
subdocument

If not in pipe mode, the class object will be added as a subdocument class.

False

import_objects(obj_dict, blob_store=None, is_load=False)

Import a list of documents in json format to Python objects. The schema of those documents need to be in this schema.

to_dict()

Return the schema in the TerminusDB dictionary format

Returns:

Type Description
List[Dict[str, Any]]

List of dictionaries representing the classes prepended with the context information. The classes are sorted on their names (@id).

to_json_schema(class_object)

Return the schema in the json schema (http://json-schema.org/) format as a dictionary for the class object.

Parameters:

Name Type Description Default
class_object Union[str, dict]

Name of the class object or the class object represented as dictionary.

required

update_from(module='schema', filepath=None, dir=None)

Update schema from a module. Adds methods and adds/updates properties of schema classes.

Parameters:

Name Type Description Default
module str

The module name to load, default to "schema".

'schema'