Skip to content

bsouthga/DDB

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 

Repository files navigation

[D]ictionary "[D]ata[B]ase"

A class for lazily filtering and manipulating lists of dictionaries in less than 60 lines of Python. Everything is passed as generators, so memory efficiency should be pretty good! There are no dependencies other then the built-in itertools.

Usage

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}))

About

Dictionary "DataBase"

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages