Last active
October 25, 2016 15:28
-
-
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
This file contains hidden or 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
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