Skip to content

Instantly share code, notes, and snippets.

@theY4Kman
Last active October 25, 2016 15:28
Show Gist options
  • Save theY4Kman/505c955883d717c8d20d660ecc5fc796 to your computer and use it in GitHub Desktop.
Save theY4Kman/505c955883d717c8d20d660ecc5fc796 to your computer and use it in GitHub Desktop.
Demonstrates hybrid properties, which offer the ability to provide equivalent functionality at the instance and query levels with the same attribute name, reducing complexity therefore cognitive load
class Assignment(Model):
completed_at = Column(DateTime)
@hybrid_property
def completed(self):
return self.completed_at is not None
# Note this is a class method
@completed.expression
def completed(cls):
# The != instead of "is not" demonstrates we're no longer reasoning
# about Python objects, but SQL stuffs
return cls.completed_at != None
assignment = Assignment.query.filter(Assignment.completed).first()
assert assignment.completed
# Do *that* shit with Django!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment