mkrd/DictDataBase

Api improvement

mkrd opened this issue · 1 comments

mkrd commented
import dictdatabase as DDB
from path_dict import PathDict
import time

# Measure time to get all cups

t1 = time.perf_counter()
cups = DDB.at("cups/*", key="organizer_email").read()


# REMOVE where selector
# KEEP as_type since it is also important in session
# KEEP at() since it is also used in session
# ADD: read_key(key, as_type=None) and read_keys(keys, as_type=None)
# ... OR ADD: .select_key(key) and .select_keys(keys) and .select_all() and .select_where(f) as intermediate step before read() or session()









# File Alt 1
DDB.read_file("cups/aachen", as_type=PathDict)
DDB.read_file("cups/aachen", key="version", as_type=PathDict)
DDB.read_file("cups/aachen", keys=["version", "name"], as_type=PathDict)
DDB.read_file("cups/aachen", not_keys=["big_part"], as_type=PathDict)
with DDB.file_session("cups/aachen", as_type=PathDict) as (session, cup):
    cup["count"] += 1
    session.write(cup)
with DDB.file_session("cups/aachen", key="version", as_type=PathDict) as (session, version):
    version += 1
    session.write(version)

# File Alt 2
DDB.at_file("cups/aachen").read(as_type=PathDict)
DDB.at_file("cups/aachen").select_key("version").read(as_type=PathDict)
DDB.at_file("cups/aachen").select_keys("version", "name").read(as_type=PathDict)
DDB.at_file("cups/aachen").select_not_keys("big_part").read(as_type=PathDict)
with DDB.at_file("cups/aachen").session(as_type=PathDict) as (session, cup):
    cup["count"] += 1
    session.write(cup)

# File Alt 3
DDB.file.at("cups/aachen").read(as_type=PathDict)
DDB.file.at("cups/aachen").select_key("version").read(as_type=PathDict)
DDB.file.at("cups/aachen").select_keys("version", "name").read(as_type=PathDict)
DDB.file.at("cups/aachen").select_not_keys("big_part").read(as_type=PathDict)
with DDB.file.at("cups/aachen").session(as_type=PathDict) as (session, cup):
    cup["count"] += 1
    session.write(cup)

# Dir Alt 1
DDB.read_dir("cups", as_type=PathDict)
DDB.read_dir("cups", key="version", as_type=PathDict)
DDB.read_dir("cups", keys=["version", "name"], as_type=PathDict)
with DDB.dir_session("cups", as_type=PathDict) as (session, cups):
    cups["aachen"]["count"] += 1
    session.write(cups)
with DDB.dir_session("cups", key="version", as_type=PathDict) as (session, versions):
    versions["aachen", "version"] += 1
    session.write(versions)

# Dir Alt 2
DDB.at_dir("cups").read(as_type=PathDict)
DDB.at_dir("cups").select_key("version").read(as_type=PathDict)
DDB.at_dir("cups").select_keys("version", "name").read(as_type=PathDict)
with DDB.at_dir("cups").session(as_type=PathDict) as (session, cups):
    cups["aachen"]["count"] += 1
    session.write(cups)
with DDB.at_dir("cups").select_key("version").session(as_type=PathDict) as (session, versions):
    versions["aachen", "version"] += 1
    session.write(versions)

# Dir Alt 3
DDB.dir.at("cups").read(as_type=PathDict)
DDB.dir.at("cups").select_key("version").read(as_type=PathDict)
DDB.dir.at("cups").select_keys("version", "name").read(as_type=PathDict)
with DDB.dir.at("cups").session(as_type=PathDict) as (session, cups):
    cups["aachen"]["count"] += 1
    session.write(cups)
with DDB.dir.at("cups").select_key("version").session(as_type=PathDict) as (session, versions):
    versions["aachen", "version"] += 1
    session.write(versions)











# Read key from top level file:
v1 = DDB.at("app_state", key="version").read(as_type=PathDict)
v2 = DDB.at(file="app_state").select_key("version").read(as_type=PathDict)


# Read one cup:
v1 = DDB.at("cups/1").read(as_type=PathDict)
v2 = DDB.at("cups/1").read(as_type=PathDict)


# Read all cups:
v1 = DDB.at("cups/*").read(as_type=PathDict)
v2 = DDB.at("cups").read(as_type=PathDict)
v3 = DDB.at(dir="cups").select_all().read(as_type=PathDict)


# Read only mail addresses:
v1 = DDB.at("cups/*", key="organizer_email").read(as_type=PathDict)
v2 = DDB.at("cups/*").select_key("organizer_email").read(as_type=PathDict)



# Read locations and emails:
v1 = ... # Not Possible
v2 = DDB.at("cups/*").select_keys("organizer_email", "location").read(as_type=PathDict)



t2 = time.perf_counter()

assert len(cups) == 228

print(f"Time to get all cups: {(t2-t1)*1000:.1f} ms")

print(cups) ```
mkrd commented
  • Option 3 looks best, it is explicit, better than the indirection of before
  • select key and keys could be just select and select_not