Skip to content
This repository has been archived by the owner on Dec 27, 2022. It is now read-only.

Commit

Permalink
Remove attribute from an element (#19)
Browse files Browse the repository at this point in the history
+add docstrings for QuaCalNode

close #20
  • Loading branch information
qguyk authored Jul 29, 2021
1 parent 710ee79 commit b56bad5
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
25 changes: 24 additions & 1 deletion entropylab_qpudb/_entropy_cal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
17 changes: 17 additions & 0 deletions entropylab_qpudb/_qpudatabase.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
20 changes: 20 additions & 0 deletions entropylab_qpudb/tests/test_qpudatabase.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit b56bad5

Please # to comment.