Skip to content

Instantly share code, notes, and snippets.

@alex8224
Created November 11, 2015 16:09
Show Gist options
  • Save alex8224/1ed506ba233bf70dffdf to your computer and use it in GitHub Desktop.
Save alex8224/1ed506ba233bf70dffdf to your computer and use it in GitHub Desktop.
根据 storm model 生成 dict
def __iter__(self):
valid_fields = (name for name in dir(self ) if name not in self.exclude_field and (not name.startswith("_")))
for name in valid_fields:
prop = getattr(self, name)
if isinstance(prop, (unicode, int)):
yield name, getattr(self, name)
elif isinstance(prop, datetime):
yield name, str(getattr(self, name))
elif isinstance(prop, Reference):
try:
yield name, dict(getattr(self, name))
except:
print("%s contains None or not iterable" % name)
elif isinstance(prop, BoundReferenceSet):
for ref in prop:
yield name, dict(ref)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment