Skip to content

Instantly share code, notes, and snippets.

@exallium
Created September 1, 2012 03:42
Show Gist options
  • Save exallium/3563425 to your computer and use it in GitHub Desktop.
Save exallium/3563425 to your computer and use it in GitHub Desktop.
Useful BaseContainer and BaseContainerElement classes
from inspect import getmembers
class BaseContainerElement(object):
"""
BaseContainerElement is a generic element for a container
Process is where the magic should take place.
"""
def process(self, **kwargs):
pass
class BaseContainer(object):
"""
BaseContainer is a generic elements container.
:params
element_class -- The class for a container element.
element_sequence -- Either the explicit list of elements of
container elements to use by key, or all of the elements
in the class.
"""
element_class = BaseContainerElement
element_sequence = None
def __init__(self):
if not self.element_sequence:
self.element_sequence = []
for key, value in getmembers(self):
if isinstance(value, self.element_class):
self.element_sequence.append(key)
def get_element(self, key):
return getattr(self, key) if key in self.element_sequence else None
def get_result(self, key, process_kwargs=None):
if not process_kwargs: process_kwargs = {}
return self.get_element(key).process(**process_kwargs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment