You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
1.1 KiB
69 lines
1.1 KiB
""" |
|
A collection of update operations for TinyDB. |
|
|
|
They are used for updates like this: |
|
|
|
>>> db.update(delete('foo'), where('foo') == 2) |
|
|
|
This would delete the ``foo`` field from all documents where ``foo`` equals 2. |
|
""" |
|
|
|
|
|
def delete(field): |
|
""" |
|
Delete a given field from the document. |
|
""" |
|
def transform(doc): |
|
del doc[field] |
|
|
|
return transform |
|
|
|
|
|
def add(field, n): |
|
""" |
|
Add ``n`` to a given field in the document. |
|
""" |
|
def transform(doc): |
|
doc[field] += n |
|
|
|
return transform |
|
|
|
|
|
def subtract(field, n): |
|
""" |
|
Subtract ``n`` to a given field in the document. |
|
""" |
|
def transform(doc): |
|
doc[field] -= n |
|
|
|
return transform |
|
|
|
|
|
def set(field, val): |
|
""" |
|
Set a given field to ``val``. |
|
""" |
|
def transform(doc): |
|
doc[field] = val |
|
|
|
return transform |
|
|
|
|
|
def increment(field): |
|
""" |
|
Increment a given field in the document by 1. |
|
""" |
|
def transform(doc): |
|
doc[field] += 1 |
|
|
|
return transform |
|
|
|
|
|
def decrement(field): |
|
""" |
|
Decrement a given field in the document by 1. |
|
""" |
|
def transform(doc): |
|
doc[field] -= 1 |
|
|
|
return transform
|
|
|