Skip to content

Commit

Permalink
Added __len__ to Item.
Browse files Browse the repository at this point in the history
  • Loading branch information
synchronizing committed Nov 9, 2021
1 parent 309bad5 commit 2762667
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
8 changes: 7 additions & 1 deletion docs/source/module/collections.rst
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,13 @@ item

.. code-block:: python
hash(Item(100)) == hash(b"100") # True
hash(Item(100)) == hash(b"100") # >>> True
.. function:: __len__()

.. code-block:: python
len(Item("hello world")) == 11 # >>> True
.. function:: __bool__()

Expand Down
5 changes: 5 additions & 0 deletions tests/test_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ def test_item_iter(self):
def test_item_hash(self):
assert hash(b"hello") == hash(Item("hello")) == hash(Item(b"hello"))

def test_item_len(self):
assert len(Item(None)) == 0
assert len(Item(123)) == 3
assert len(Item("hello world")) == 11

def test_item_bool(self):
item = Item(100)
assert bool(item) == item.boolean
Expand Down
3 changes: 3 additions & 0 deletions toolbox/collections/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ def __iter__(self) -> Iterator[str]:
def __hash__(self) -> int:
return hash(self._item)

def __len__(self) -> int:
return len(self.raw)

def __bool__(self) -> bool:
return self.boolean

Expand Down

0 comments on commit 2762667

Please sign in to comment.