From e70513a147354b2c80e6c55c59a9489c9d0e120c Mon Sep 17 00:00:00 2001 From: apetrynet Date: Tue, 16 Jul 2024 12:03:15 +0200 Subject: [PATCH] renamed `add_item`, `get_item` and `remove_item` methods Signed-off-by: apetrynet --- docs/getting_started.md | 2 +- src/pyfdl/base.py | 10 +++++----- src/pyfdl/canvas.py | 4 ++-- src/pyfdl/pyfdl.py | 10 +++++----- tests/test_canvas.py | 2 +- tests/test_common.py | 18 +++++++++--------- tests/test_pyfdl.py | 10 +++++----- 7 files changed, 28 insertions(+), 28 deletions(-) diff --git a/docs/getting_started.md b/docs/getting_started.md index 43567fe..540385f 100644 --- a/docs/getting_started.md +++ b/docs/getting_started.md @@ -58,7 +58,7 @@ framing_intent = FramingIntent( ) # Add the newly created framing intent to our FDL -fdl.framing_intents.add_item(framing_intent) +fdl.framing_intents.add(framing_intent) # Now let's create a canvas canvas = Canvas( diff --git a/src/pyfdl/base.py b/src/pyfdl/base.py index 6401f56..b8fade9 100644 --- a/src/pyfdl/base.py +++ b/src/pyfdl/base.py @@ -155,7 +155,7 @@ def from_dict(cls, raw: dict) -> Any: tc = TypedCollection(_cls) for item in value: - tc.add_item(_cls.from_dict(item)) + tc.add(_cls.from_dict(item)) value = tc else: value = cls.object_map[key].from_dict(value) @@ -218,7 +218,7 @@ def __init__(self, cls: Any): def ids(self): return list(self._data.keys()) - def add_item(self, item: Any): + def add(self, item: Any): """Add an item to the collection. All items added to a collection get associated to the collection by passing itself as parent @@ -248,7 +248,7 @@ def add_item(self, item: Any): else: raise FDLError(f"Item must have a valid identifier (\"{self._cls.id_attribute}\"), not None or empty string") - def get_item(self, item_id: str) -> Union[Any, None]: + def get(self, item_id: str) -> Union[Any, None]: """Get an item in the collection Args: @@ -259,7 +259,7 @@ def get_item(self, item_id: str) -> Union[Any, None]: """ return self._data.get(item_id) - def remove_item(self, item_id: str): + def remove(self, item_id: str): """Remove an item in the collection if found Args: @@ -291,7 +291,7 @@ def __iter__(self): yield item def __getitem__(self, item): - return self.get_item(self.ids[item]) + return self.get(self.ids[item]) def __contains__(self, item: Any) -> bool: # We support both looking for an item by item.id and "string" for future use of collection diff --git a/src/pyfdl/canvas.py b/src/pyfdl/canvas.py index 594fda7..9d883a4 100644 --- a/src/pyfdl/canvas.py +++ b/src/pyfdl/canvas.py @@ -129,7 +129,7 @@ def place_framing_intent(self, framing_intent: FramingIntent) -> str: """ framing_decision = FramingDecision.from_framing_intent(self, framing_intent) - self.framing_decisions.add_item(framing_decision) + self.framing_decisions.add(framing_decision) return framing_decision.id @@ -181,7 +181,7 @@ def from_canvas_template( id_=f'{canvas.id}-{source_framing_decision.framing_intent_id}', framing_intent_id=source_framing_decision.framing_intent_id ) - canvas.framing_decisions.add_item(framing_decision) + canvas.framing_decisions.add(framing_decision) source_map = { 'framing_decision': source_framing_decision, diff --git a/src/pyfdl/pyfdl.py b/src/pyfdl/pyfdl.py index c00fc94..1bb3ea9 100644 --- a/src/pyfdl/pyfdl.py +++ b/src/pyfdl/pyfdl.py @@ -70,12 +70,12 @@ def place_canvas_in_context(self, context_label: str, canvas: Canvas): canvas: to be placed in context """ - context = self.contexts.get_item(context_label) + context = self.contexts.get(context_label) if context is None: context = Context(label=context_label) - self.contexts.add_item(context) + self.contexts.add(context) - context.canvases.add_item(canvas) + context.canvases.add(canvas) @property def header(self) -> Header: @@ -132,10 +132,10 @@ def validate(self): for canvas_collection in canvas_collections: for canvas in canvas_collection: - canvases.add_item(canvas) + canvases.add(canvas) for canvas in canvases: - if canvases.get_item(canvas.source_canvas_id) is None: + if canvases.get(canvas.source_canvas_id) is None: errors.append( f'{canvas.source_canvas_id} (canvas.source_canvas_id) not found in ' f'registered canvases' diff --git a/tests/test_canvas.py b/tests/test_canvas.py index 47eeed9..8424516 100644 --- a/tests/test_canvas.py +++ b/tests/test_canvas.py @@ -57,7 +57,7 @@ def test_place_framing_intent(sample_framing_intent, sample_canvas, sample_frami decision_id = canvas.place_framing_intent(intent) assert decision_id == f'{canvas.id}-{intent.id}' - decision = canvas.framing_decisions.get_item(decision_id) + decision = canvas.framing_decisions.get(decision_id) facit_decision = pyfdl.FramingDecision.from_dict(sample_framing_decision) assert decision == facit_decision diff --git a/tests/test_common.py b/tests/test_common.py index 3872a2e..cdcf46f 100644 --- a/tests/test_common.py +++ b/tests/test_common.py @@ -117,48 +117,48 @@ def test_rounding_strategy_from_dict(sample_rounding_strategy): def test_typed_collection(sample_framing_intent, sample_framing_intent_kwargs): td = pyfdl.TypedCollection(pyfdl.FramingIntent) fi = pyfdl.FramingIntent.from_dict(sample_framing_intent) - td.add_item(fi) + td.add(fi) assert td.to_list() == [fi.to_dict()] assert td.ids == [f"{fi.id}"] assert fi in td assert fi.id in td - assert td.get_item(fi.id) == fi + assert td.get(fi.id) == fi assert [_fi for _fi in td] == [fi] - td.remove_item(fi.id) + td.remove(fi.id) assert len(td) == 0 assert bool(td) is False with pytest.raises(TypeError) as err: - td.add_item(pyfdl.Point(x=10, y=10)) + td.add(pyfdl.Point(x=10, y=10)) assert "This container does not accept items of type:" in str(err.value) # Test missing id fi1 = pyfdl.FramingIntent() with pytest.raises(pyfdl.FDLError) as err: - td.add_item(fi1) + td.add(fi1) assert f"Item must have a valid identifier (\"id\")" in str(err.value) # Test duplicate id's - td.add_item(fi) + td.add(fi) kwargs = sample_framing_intent_kwargs.copy() kwargs['label'] = 'somethingelse' fi2 = pyfdl.FramingIntent(**kwargs) with pytest.raises(pyfdl.FDLError) as err: - td.add_item(fi2) + td.add(fi2) assert f"FramingIntent.id (\"{fi.id}\") already exists." in str(err.value) # Test object with alternative id_attribute td1 = pyfdl.TypedCollection(pyfdl.Context) ctx1 = pyfdl.Context(label='context1') - td1.add_item(ctx1) + td1.add(ctx1) with pytest.raises(pyfdl.FDLError) as err: - td1.add_item(ctx1) + td1.add(ctx1) assert f"Context.label (\"{ctx1.label}\") already exists." in str(err) diff --git a/tests/test_pyfdl.py b/tests/test_pyfdl.py index 018c0f1..4acbf53 100644 --- a/tests/test_pyfdl.py +++ b/tests/test_pyfdl.py @@ -84,7 +84,7 @@ def test_setting_default_framing_id(sample_framing_intent): fdl = pyfdl.FDL() fi = pyfdl.FramingIntent.from_dict(sample_framing_intent) - fdl.framing_intents.add_item(fi) + fdl.framing_intents.add(fi) fdl.default_framing_intent = fi.id assert fdl.default_framing_intent == fi.id @@ -99,12 +99,12 @@ def test_place_canvas_in_context(sample_canvas, sample_context): fdl = pyfdl.FDL() canvas = pyfdl.Canvas.from_dict(sample_canvas) context = pyfdl.Context.from_dict(sample_context) - fdl.contexts.add_item(context) + fdl.contexts.add(context) fdl.place_canvas_in_context(context_label=context.label, canvas=canvas) - assert canvas in fdl.contexts.get_item(context.label).canvases + assert canvas in fdl.contexts.get(context.label).canvases fdl.place_canvas_in_context(context_label="nonexistent", canvas=canvas) - new_context = fdl.contexts.get_item("nonexistent") + new_context = fdl.contexts.get("nonexistent") assert isinstance(new_context, pyfdl.Context) - assert new_context.canvases.get_item(canvas.id) == canvas + assert new_context.canvases.get(canvas.id) == canvas