Created
July 22, 2019 12:20
-
-
Save numberoverzero/ce31b514ca0bf1dfdb14bcf06a13ef45 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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