A class for filtering and manipulating lists of dictionaries in less than 60 lines of Python
Create a "database"...
from ddb import DDB
test_data = [
{"a" : 1, "b" : {"x" : 2}},
{"a" : 2, "b" : {"x" : 2}},
{"a" : 2, "b" : {"x" : 3}}
]
db = DDB(test_data)
Filter the "database" using db.select()
, which returns
a new DDB with the selection for method chaining / new selections
selection = db.select({"a" : 2})
print(selection)
# => [{"a" : 2, "b" : {"x" : 2}}, {"a" : 2, "b" : {"x" : 3}}]
print(selection.select({"b" : {"x" : 2}}))
# => [{'a': 2, 'b': {'x': 2}}]
Insert stuff
print(selection.insert({"a" : 2}))
# => [{'a': 2, 'b': {'x': 2}}, {'a': 2, 'b': {'x': 3}}, {'a': 2}]
Select using functions!
print(selection.select({"b" : {"x" : lambda x : x < 3}}))
# => [{'a': 2, 'b': {'x': 2}}]
Iterate through the selection!
for item in selection:
print(item)
# =>
# {'a': 2, 'b': {'x': 2}}
# {'a': 2, 'b': {'x': 3}}
# {'a': 2}
Apply transforms, and chain operations!
import random
def addFuzzyAsquared(item):
item['a_sq'] = item['a']**2 + random.randint(0, 20)
return item
print(selection.map(addFuzzyAsquared).select({"a_sq" : lambda x : x < 10}))