Skip to content

Commit

Permalink
refactor(table): Table.delete() to prefer argument doc_ids over cond (#…
Browse files Browse the repository at this point in the history
…424)

`Table.get(cond, doc_id)`, `.contains()`, `.update()`. These methods
support both query condition and doc_id(s) as the filter, and `doc_ids`
is preferred over `cond`.

Refactor `Table.delete()` to make it behave consistent with above
methods.
  • Loading branch information
laggardkernel authored Aug 14, 2021
1 parent 71283fd commit 3362a4b
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions tinydb/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,25 @@ def remove(
:param doc_ids: a list of document IDs
:returns: a list containing the removed documents' ID
"""
if doc_ids is not None:
# This function returns the list of IDs for the documents that have
# been removed. When removing documents identified by a set of
# document IDs, it's this list of document IDs we need to return
# later.
# We convert the document ID iterator into a list so we can both
# use the document IDs to remove the specified documents as well as
# to return the list of affected document IDs
removed_ids = list(doc_ids)

def updater(table: dict):
for doc_id in removed_ids:
table.pop(doc_id)

# Perform the remove operation
self._update_table(updater)

return removed_ids

if cond is not None:
removed_ids = []

Expand Down Expand Up @@ -524,25 +543,6 @@ def updater(table: dict):

return removed_ids

if doc_ids is not None:
# This function returns the list of IDs for the documents that have
# been removed. When removing documents identified by a set of
# document IDs, it's this list of document IDs we need to return
# later.
# We convert the document ID iterator into a list so we can both
# use the document IDs to remove the specified documents as well as
# to return the list of affected document IDs
removed_ids = list(doc_ids)

def updater(table: dict):
for doc_id in removed_ids:
table.pop(doc_id)

# Perform the remove operation
self._update_table(updater)

return removed_ids

raise RuntimeError('Use truncate() to remove all documents')

def truncate(self) -> None:
Expand Down

0 comments on commit 3362a4b

Please sign in to comment.