lds package

Submodules

lds.lds module

class lds.lds.DocStore[source]

Bases: object

The main class for this module, which stores and operates on documents.

This class attempts to replicate some of the functionality of the popular document store MongoDB. It provides method to add and store JSON documents, search those documents using an example JSON query, and modify those documents using a search query and a replacement document.

Methods

add(doc)[source]

Adds a document to the list holding the documents.

Constructs a tuple of a document, passed in as a parameter, and a set of tuples representing the (key: value) pairings of the document (to be used for querying the documents), and appends aforementioned tuple to the list of documents initialized in __init__.

.note:: the document passed in should be a valid json. This will be checked if you pass in a string, but not a dictionary.

>>> store = DocStore()
>>> store.add('{"name": "oski", "school": "california"}') #string
True
>>> store.add({'name': 'harry', 'school': 'washington'}) #dict
True
Parameters:

doc: str/dict

doc, the document to be added, can be passed in as a string or a Python dictionary.

Returns:

True: if add operation was successfully completed.

add_many(docs)[source]

Adds multiple documents to the class’ internal list of documents.

Calls the add function over the list of documents passed in.

Parameters:

docs: list

the list of documents, which can be strings or Python dictionaries.

Returns:

True: if all add operations were successfully completed.

search(query)[source]

Searches for documents that match the query passed in.

Converts the query passed in as a parameter to a set of tuples representing the (key: value) pairings of the document, and checks if the set of tuples of the query parameter is a subset of any of the sets of tuples of the documents currently in the store. If it finds matching documents, it returns them as a list.

>>> store.add({'name': 'harry', 'school': 'washington'})
True
>>> store.add('{"name": "oski", "school": "california"}')
True
>>> store.search({'name': 'oski'})
[{'name': 'oski', 'school': 'california'}]
Parameters:

query: str/dict

the query that the list of documents will be matched against.

Returns:

list: of documents that match the query passed in.

update(match, new, exact=False)[source]

Updates documents with the new values passed in to the documents that match the query passed in.

Searches the document with the same logic as described in search, and updates documents that match. The exact parameter determines what type of update will be performed. If exact is False (its default value), the old document will simply be merged with the new one using {**old, **new}. If exact is True, it is important to pass in a single (key: value) pairing to both the match and new parameter; the document will be iterated over and only when the match is found in the current document will the value be updated.

.note:: if you are updating a nested value of the document, you must
use `exact`=True. Python’s merge operation, as detailed above, will not accurately merge nested documents.
>>> store.add('{"name": "oski", "details": {"school": "california", "year": "senior"}}')
True
>>> store.add('{"name": "harry", "details": {"school": "washington", "year": "junior"}}')
True
>>> store.search({"year": "junior"})
[{'name': 'harry', 'details': {'year': 'junior', 'school': 'washington'}}]
>>> store.update({"year": "senior"}, {"year": "junior"}, exact=True)
>>> store.search({"year": "junior"})
[{'name': 'oski', 'details': {'year': 'junior', 'school': 'california'}},        {'name': 'harry', 'details': {'year': 'junior', 'school': 'washington'}}]
Parameters:

match: str/dict

the query that the list of documents will be matched against.

new: str/dict

the document which will be merged into the existing, matching document.

Returns:

True: if the update was performed successfully.

Module contents