Skip to content

Commit

Permalink
Add rename_dependent function (#78)
Browse files Browse the repository at this point in the history
* Add rename_dependent function

* Blacken

* Simplify rename, add tests for behavior
  • Loading branch information
ksunden authored and untzag committed Dec 27, 2019
1 parent ef566bc commit c37b46c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
11 changes: 11 additions & 0 deletions attune/curve/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,17 @@ def plot(self, autosave=False, save_path="", title=None):
plt.savefig(image_path, transparent=True, dpi=300)
plt.close(fig)

def rename_dependent(self, old_name, new_name):
try:
dep = self.dependents[old_name]
except KeyError:
raise ValueError(f"Dependent '{old_name}' not found")
dep.name = new_name
delattr(self, old_name)
setattr(self, new_name, dep)
self.dependents = {k if k != old_name else new_name: v for k, v in self.dependents.items()}
self.interpolate()

@classmethod
def read(cls, filepath, subcurve=None):
filepath = pathlib.Path(filepath)
Expand Down
25 changes: 25 additions & 0 deletions tests/Curve/rename.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import attune
import numpy as np
import pytest


def test_rename():
d = attune.Dependent(np.linspace(0, 10, 21), "d1")
s = attune.Setpoints(np.linspace(120, 240, 21), "s1")
c = attune.Curve(s, d, "c")

c.rename_dependent("d1", "d2")

assert "d2" in c.dependents
assert "d1" not in c.dependents
assert np.allclose(c["d2"][:], d[:])
assert c["d2"].name == "d2"


def test_rename_not_present():
d = attune.Dependent(np.linspace(0, 10, 21), "d1")
s = attune.Setpoints(np.linspace(120, 240, 21), "s1")
c = attune.Curve(s, d, "c")

with pytest.raises(ValueError):
c.rename_dependent("d2", "d1")

0 comments on commit c37b46c

Please sign in to comment.