Created
February 12, 2015 17:19
-
-
Save ericzundel/5f8f740b6975fc641928 to your computer and use it in GitHub Desktop.
Decorating classes for product_types
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
gbl_product_types={} | |
class product_type: | |
def __init__(self, product_types): | |
self._product_types = product_types; | |
def __call__(self, cls): | |
gbl_product_types[cls.__name__] = self._product_types | |
class Wrapped(cls): | |
pass | |
return Wrapped | |
class Base(object): | |
pass | |
@product_type(["a"]) | |
class Task(Base): | |
pass | |
@product_type(["b"]) | |
class Mixin(Base): | |
pass | |
class Composite(Mixin, Task): | |
pass | |
def get_product_types(cls): | |
result = [] | |
def recursive_search(cls): | |
for base in cls.__bases__: | |
recursive_search(base) | |
if cls.__name__ in gbl_product_types.keys(): | |
result.extend(gbl_product_types[cls.__name__]) | |
recursive_search(cls) | |
return result | |
print("gbl_product_types= {0}".format(gbl_product_types)) | |
print("Mixin = {0}".format(get_product_types(Mixin))) | |
print("Task = {0}".format(get_product_types(Task))) | |
print("Composite = {0}".format(get_product_types(Composite))) | |
# python ./task_product_type.py | |
# output: | |
# gbl_product_types= {'Task': ['a'], 'Mixin': ['b']} | |
# Mixin = ['b'] | |
# Task = ['a'] | |
# Composite = ['b', 'a'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can definitely avoid odd names - several ways. Here is one: