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

Make table and db iterable #139

Merged
merged 2 commits into from Jun 5, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/getting-started.rst
Expand Up @@ -40,6 +40,13 @@ Now you can get all elements stored in the database by running:
>>> db.all()
[{'count': 7, 'type': 'apple'}, {'count': 3, 'type': 'peach'}]

You can also iter over stored elements:

>>> for item in db:
>>> print(item)
{'count': 7, 'type': 'apple'}
{'count': 3, 'type': 'peach'}

Of course you'll also want to search for specific elements. Let's try:

>>> Fruit = Query()
Expand Down Expand Up @@ -83,6 +90,8 @@ Before we dive deeper, let's recapitulate the basics:
+-------------------------------+---------------------------------------------------------------+
| ``db.all()`` | Get all elements |
+-------------------------------+---------------------------------------------------------------+
| ``iter(db)`` | Iter over all elements |
+-------------------------------+---------------------------------------------------------------+
| ``db.search(query)`` | Get a list of elements matching the query |
+-------------------------------+---------------------------------------------------------------+
| **Updating** |
Expand Down
3 changes: 3 additions & 0 deletions docs/usage.rst
Expand Up @@ -339,6 +339,9 @@ the ``TinyDB`` class. To create and use a table, use ``db.table(name)``.
>>> table.insert({'value': True})
>>> table.all()
[{'value': True}]
>>> for row in table:
>>> print(row)
{'value': True}

To remove a table from a database, use:

Expand Down
8 changes: 8 additions & 0 deletions tests/test_tables.py
Expand Up @@ -74,3 +74,11 @@ def test_lru_cache(db):
assert len(table._query_cache) == 1
table.clear_cache()
assert len(table._query_cache) == 0


def test_table_is_iterable(db):
table = db.table('table1')

table.insert_multiple({'int': i} for i in range(3))

assert [r for r in table] == table.all()
4 changes: 4 additions & 0 deletions tests/test_tinydb.py
Expand Up @@ -472,3 +472,7 @@ def test_query_cache():
results.extend([1])

assert db.search(query) == [{'name': 'foo', 'value': 42}]


def test_tinydb_is_iterable(db):
assert [r for r in db] == db.all()
19 changes: 18 additions & 1 deletion tinydb/database.py
Expand Up @@ -110,7 +110,7 @@ def table(self, name=DEFAULT_TABLE, **options):
table = self.table_class(StorageProxy(self._storage, name), **options)

self._table_cache[name] = table

# table._read will create an empty table in the storage, if necessary
table._read()

Expand Down Expand Up @@ -181,6 +181,12 @@ def __len__(self):
"""
return len(self._table)

def __iter__(self):
"""
Iter over all elements from default table.
"""
return self._table.__iter__()


class Table(object):
"""
Expand Down Expand Up @@ -303,6 +309,17 @@ def all(self):

return list(itervalues(self._read()))

def __iter__(self):
"""
Iter over all elements stored in the table.

:returns: an iterator over all elements.
:rtype: listiterator[Element]
"""

for value in itervalues(self._read()):
yield value

def insert(self, element):
"""
Insert a new element into the table.
Expand Down