Skip to content

Instantly share code, notes, and snippets.

@Kroisse
Forked from longfin/gist:1441283
Created December 7, 2011 04:37
Show Gist options
  • Save Kroisse/1441484 to your computer and use it in GitHub Desktop.
Save Kroisse/1441484 to your computer and use it in GitHub Desktop.
Class Factory??
tag_types = ['location', 'category']
class Tag(db.Model, BaseEntityMixIn,
IgnoreDeletionMixIn):
__tablename__ = "tag"
name = db.Column(db.Unicode(255), index=True)
type = db.Column(db.Enum(*tag_types, native_enum=False), index=True)
__mapper_args__ = {'polymorphic_on': type}
def __build_sub_tag(tag_type):
class SubTag(Tag):
__tablename__ = None
__mapper_args__ = {'polymorphic_identity': tag_type}
def __init__(self, **kwargs):
kwargs["type"] = tag_type
Tag.__init__(self, **kwargs)
return SubTag
g = globals()
for name in tag_types:
g[name.capitalize() + 'Tag'] = __build_sub_tag(name)
@longfin
Copy link

longfin commented Dec 7, 2011

globals() 을 생각 못하고 있었는데 이거 좋네요.

@Kroisse
Copy link
Author

Kroisse commented Dec 7, 2011

다른 모듈에서 import할때도 정확히 먹힐지는 잘 모르겠네요;

@longfin
Copy link

longfin commented Dec 7, 2011

repl에서 임포트해서 써봤는데 잘 됩니다. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment