Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

renamed add_item, get_item and remove_item methods #33

Merged
merged 1 commit into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/getting_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
10 changes: 5 additions & 5 deletions src/pyfdl/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/pyfdl/canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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,
Expand Down
10 changes: 5 additions & 5 deletions src/pyfdl/pyfdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion tests/test_canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
18 changes: 9 additions & 9 deletions tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
10 changes: 5 additions & 5 deletions tests/test_pyfdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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