From b56bad5fcd56012d5f24e1293718f2a96f2a0350 Mon Sep 17 00:00:00 2001 From: qguyk Date: Thu, 29 Jul 2021 10:50:48 +0300 Subject: [PATCH] Remove attribute from an element (#19) +add docstrings for QuaCalNode close #20 --- entropylab_qpudb/_entropy_cal.py | 25 +++++++++++++++++++++- entropylab_qpudb/_qpudatabase.py | 17 +++++++++++++++ entropylab_qpudb/tests/test_qpudatabase.py | 20 +++++++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) diff --git a/entropylab_qpudb/_entropy_cal.py b/entropylab_qpudb/_entropy_cal.py index 2f5f75b..dc646f0 100644 --- a/entropylab_qpudb/_entropy_cal.py +++ b/entropylab_qpudb/_entropy_cal.py @@ -6,11 +6,12 @@ from itertools import count from typing import Callable, List, Optional, Union, Iterable, Set -from entropylab_qpudb._quaconfig import QuaConfig from entropylab.api.execution import EntropyContext from entropylab.api.graph import Node from entropylab.graph_experiment import PyNode +from entropylab_qpudb._quaconfig import QuaConfig + class AncestorRunStrategy(enum.Enum): RunAll = 1 @@ -142,12 +143,34 @@ def get_patches(config): @abstractmethod def prepare_config(self, config: QuaConfig, context: EntropyContext): + """ + Gets a config, and modify it in place (modifies the QuaConfig object) + with necessary changes for the node execution + :param config: + :param context: + """ pass @abstractmethod def run_program(self, config, context: EntropyContext): + """ + defines the node program/measurement. + If AncestorRunStrategy.RunOnlyLast is used, only the program + of the last node will be executed. + :param config: + :param context: + """ pass @abstractmethod def update_config(self, config: QuaConfig, context: EntropyContext): + """ + Updates the config with needed changes in place + (modifies the QuaConfig object) + Changes here will affect the following nodes. + :param config: + :param context: + """ + pass + pass diff --git a/entropylab_qpudb/_qpudatabase.py b/entropylab_qpudb/_qpudatabase.py index e737059..453352e 100644 --- a/entropylab_qpudb/_qpudatabase.py +++ b/entropylab_qpudb/_qpudatabase.py @@ -297,6 +297,23 @@ def add_attribute( ) root["elements"]._p_changed = True + def remove_attribute(self, element: str, attribute: str) -> None: + """ + remove an existing attribute. + + :raises: AttributeError if attribute does not exist. + :param element: the name of the element + :param attribute: the name of the atrribute to remove + """ + root = self._con.root() + if attribute not in root["elements"][element]: + raise AttributeError( + f"attribute {attribute} does not exist for element {element}" + ) + else: + del root["elements"][element][attribute] + root["elements"]._p_changed = True + def add_element(self, element: str) -> None: """ :raises: AttributeError if element already exists. diff --git a/entropylab_qpudb/tests/test_qpudatabase.py b/entropylab_qpudb/tests/test_qpudatabase.py index aa9578f..1f9e618 100644 --- a/entropylab_qpudb/tests/test_qpudatabase.py +++ b/entropylab_qpudb/tests/test_qpudatabase.py @@ -353,6 +353,26 @@ def test_add_elements_persistence(testdb): assert db.get("q_new", "p_new").value == "something" +def test_add_and_remove_elements_persistence(testdb): + with _QpuDatabaseConnectionBase(testdb) as db: + db.add_element("q_new") + db.add_attribute("q_new", "p_new", "something") + db.commit() + db.remove_attribute("q_new", "p_new") + db.commit() + + with _QpuDatabaseConnectionBase(testdb) as db: + with pytest.raises(AttributeError): + db.get("q_new", "p_new") + + with _QpuDatabaseConnectionBase(testdb) as db: + db.restore_from_history(1) + assert db.get("q_new", "p_new").value == "something" + db.restore_from_history(2) + with pytest.raises(AttributeError): + db.get("q_new", "p_new") + + def test_with_resolver(testdb, simp_resolver): with QpuDatabaseConnection(testdb, simp_resolver) as db: db.update_q(1, "p1", 555)