Skip to content

Instantly share code, notes, and snippets.

@numberoverzero
Created July 22, 2019 12:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save numberoverzero/ce31b514ca0bf1dfdb14bcf06a13ef45 to your computer and use it in GitHub Desktop.
Save numberoverzero/ce31b514ca0bf1dfdb14bcf06a13ef45 to your computer and use it in GitHub Desktop.
# fragments from evaluating some toolkit classes to succinctly build common queries in bloop
class QueryBuilder:
def __init__(self, engine):
self.engine = engine
self.index_cache = {}
def by_key(self, key_condition, *, index=None, **kwargs):
if isinstance(key_condition, AndCondition):
model = key_condition.values[0].model
elif isinstance(key_condition, ComparisonCondition) and key_condition.operation == "==":
model = key_condition.model
else:
raise ValueError("malformed key condition")
return self.engine.query(model, key=key_condition, **kwargs)
__call__ = by_key
# comparing syntax of a few options
q = QueryBuilder(engine)
q.by_key(Users.email == email)
engine.query(Users, Users.email == email)
# typing deltas
"q.by_key("
"engine.query(Users, "
q(Users.email == email)
engine.query(Users, Users.email == email)
"q("
"engine.query(Users, "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment