Created
May 28, 2010 04:36
-
-
Save shazow/416755 to your computer and use it in GitHub Desktop.
Inspect whether a path of properties is eagerloaded or not. (Hack)
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
# Helper methods for an SQLAlchemy declarative base class. | |
def _is_loaded(self, key): | |
return key in self.__dict__ | |
def _is_loaded_all(self, path): | |
""" | |
Check if the given path of properties are eager-loaded. | |
`path` is similar to sqlalchemy.orm.eagerload_all, checking happens | |
by inspecting obj.__data__. | |
""" | |
current = self | |
for k in path.split('.'): | |
if not current._is_loaded(k): | |
return False | |
current = getattr(current, k) | |
if not current: | |
return False | |
if isinstance(current, list): | |
current = current[0] | |
return True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment