Skip to content

Commit

Permalink
Merge pull request #116 from RosieBaish/main
Browse files Browse the repository at this point in the history
Add option to extend entity list along with a note about performance
  • Loading branch information
redruin1 authored Jan 24, 2024
2 parents c0f156f + ee8c39d commit 1846203
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
2 changes: 2 additions & 0 deletions draftsman/classes/blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,8 @@ class named :py:class:`.EntityList`, which has all the normal properties
of a regular list, as well as some extra features. For more information
on ``EntityList``, check out this writeup
:ref:`here <handbook.blueprints.blueprint_differences>`.
Note - assigning to this triggers a deep copy, so use .append()
or .extend() if you're building incrementally.
"""
return self._root["entities"]

Expand Down
29 changes: 28 additions & 1 deletion draftsman/classes/entitylist.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from collections import MutableSequence
from copy import deepcopy
import six
from typing import Union, Any, TYPE_CHECKING
from typing import Union, Any, List, TYPE_CHECKING
import warnings

if TYPE_CHECKING: # pragma: no coverage
Expand Down Expand Up @@ -126,6 +126,33 @@ def append(self, name, copy=True, merge=False, **kwargs):
"""
self.insert(len(self.data), name, copy=copy, merge=merge, **kwargs)

@utils.reissue_warnings
def extend(self, entities, copy=True, merge=False):
# type: (List[Union[str, EntityLike]], bool, bool, **dict) -> None
"""
Extends the entity list with the list provided.
Functionally the same as appending one element at a time
:param copy: Passed through to append for each element
:param merge: Passed through to append for each element
:example:
.. code-block :: python
blueprint = Blueprint()
assert isinstance(blueprint.entities, EntityList)
# Append Entity instance
blueprint.entities.extend([Container("steel-chest"), Container("wooden-chest", tile_position=(1, 1)])
assert blueprint.entities[-2].name == "steel-chest"
assert blueprint.entities[-1].name == "wooden-chest"
assert blueprint.entities[-1].tile_position == {"x": 1, "y": 1}
"""
for entity in entities:
self.append(entity, copy=copy, merge=merge)


@utils.reissue_warnings
def insert(self, idx, name, copy=True, merge=False, **kwargs):
# type: (int, Union[str, EntityLike], bool, bool, **dict) -> None
Expand Down
12 changes: 12 additions & 0 deletions test/test_entitylist.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,18 @@ def test_insert(self):
with self.assertRaises(ValueError):
blueprint.entities.append(Container(), copy=False, merge=True)

def test_extend(self):
# Test appending a list vs individually
blueprint1 = Blueprint()
blueprint2 = Blueprint()
entity_list = [new_entity("transport-belt", tile_position=(0, 0)),
new_entity("wooden-chest", tile_position=(1, 1))]
blueprint1.entities.extend(entity_list)
for e in entity_list:
blueprint2.entities.append(e)

self.assertEqual(blueprint1.to_dict(), blueprint2.to_dict())

def test_remove(self):
pass # TODO

Expand Down

0 comments on commit 1846203

Please sign in to comment.