Removed redundent flow class.

This commit is contained in:
Stephen Diehl
2012-05-16 17:19:14 -04:00
parent f88aacc214
commit b1f8bbd189
2 changed files with 63 additions and 56 deletions
+34 -28
View File
@@ -25,8 +25,10 @@ from zipline.protocol import CONTROL_PROTOCOL, COMPONENT_STATE, \
LOGGER = logging.getLogger('ZiplineLogger')
from zipline.exceptions import ComponentNoInit
from zipline.transitions import WorkflowMeta
class Component(object):
"""
Base class for components. Defines the the base messaging
interface for components.
@@ -70,6 +72,9 @@ class Component(object):
# Construction
# ------------
abstract = True
#__metaclass__ = WorkflowMeta
def __init__(self, *args, **kwargs):
self.zmq = None
self.context = None
@@ -91,8 +96,8 @@ class Component(object):
self.note = None
self.confirmed = False
# Humanhashes make this way easier to debug because they
# stick in your mind unlike a 32 byte string of random hex.
# Humanhashes make this way easier to debug because they stick
# in your mind unlike a 32 byte string of random hex.
self.guid = uuid.uuid4()
self.huid = humanhash.humanize(self.guid.hex)
@@ -102,8 +107,8 @@ class Component(object):
def init(self):
"""
Subclasses should override this to extend the setup for
the class. Shouldn't have side effects.
Subclasses should override this to extend the setup for the
class. Shouldn't have side effects.
"""
raise ComponentNoInit(self.__class__)
@@ -120,15 +125,16 @@ class Component(object):
def ready(self):
"""
Return ``True`` if and only if the component has finished execution.
Return ``True`` if and only if the component has finished
execution.
"""
return self.state_flag in [COMPONENT_STATE.DONE, \
COMPONENT_STATE.EXCEPTION]
def successful(self):
"""
Return ``True`` if and only if the component has finished execution
successfully, that is, without raising an error.
Return ``True`` if and only if the component has finished
execution successfully, that is, without raising an error.
"""
return self.state_flag == COMPONENT_STATE.DONE and not \
self.exception
@@ -136,8 +142,8 @@ class Component(object):
@property
def exception(self):
"""
Holds the exception that the component failed on, or
``None`` if the component has not failed.
Holds the exception that the component failed on, or ``None`` if
the component has not failed.
"""
return self._exception
@@ -201,9 +207,9 @@ class Component(object):
"""
Run the component.
Optionally takes an argument to catch and log all exceptions raised
during execution ues this with care since it makes it very hard to
debug since it mucks up your stacktraces.
Optionally takes an argument to catch and log all exceptions
raised during execution ues this with care since it makes it
very hard to debug since it mucks up your stacktraces.
"""
if catch_exceptions:
@@ -259,8 +265,8 @@ class Component(object):
def teardown_sockets(self):
"""
Close all zmq sockets safely. This is universal, no matter
where this is running it will need the sockets closed.
Close all zmq sockets safely. This is universal, no matter where
this is running it will need the sockets closed.
"""
#close all the sockets
for sock in self.sockets:
@@ -279,8 +285,8 @@ class Component(object):
"""
Unclean shutdown.
Tear down ( fast ) as a mode of failure in the
simulation or on service halt.
Tear down ( fast ) as a mode of failure in the simulation or on
service halt.
Context specific.
"""
@@ -294,8 +300,8 @@ class Component(object):
"""
This is *very* important error tracking handler.
Will inform the system that the component has failed and
how it has failed.
Will inform the system that the component has failed and how it
has failed.
"""
if scope == 'algo':
@@ -437,9 +443,9 @@ class Component(object):
def setup_control(self):
"""
Set up the control socket. Used to monitor the
overall status of the simulation and to forcefully tear
down the simulation in case of a failure.
Set up the control socket. Used to monitor the overall status
of the simulation and to forcefully tear down the simulation in
case of a failure.
"""
# Allow for the possibility of not having a controller,
@@ -516,10 +522,10 @@ class Component(object):
@property
def get_pure(self):
"""
Describes whehter this component purely functional,
i.e. for a given set of inputs is it guaranteed to
always give the same output . Components that are
side-effectful are, generally, not pure.
Describes whehter this component purely functional, i.e. for a
given set of inputs is it guaranteed to always give the same
output . Components that are side-effectful are, generally, not
pure.
"""
return False
@@ -545,9 +551,9 @@ class Component(object):
def __repr__(self):
"""
Return a usefull string representation of the component
to indicate its type, unique identifier, and computational
context identifier name.
Return a usefull string representation of the component to
indicate its type, unique identifier, and computational context
identifier name.
"""
return "<{name} {uuid} at {host} {pid} {pointer}>".format(
+29 -28
View File
@@ -39,34 +39,6 @@ class WorkflowMeta(type):
"""
Base metaclass component workflows.
"""
def __new__(cls, name, mro, attrs):
state = attrs.get('states', None)
transitions = attrs.get('transitions', None)
initial_state = attrs.get('initial_state', None)
if attrs.get('workflow'):
raise RuntimeError('`workflow` is a reserved attribute.')
if not state:
raise RuntimeError('Must specify states')
if not transitions:
raise RuntimeError('Must specify transitions')
if not transitions:
raise RuntimeError('Must specify initial_state')
new_class = super(WorkflowMeta, cls).__new__(cls, name, mro, attrs)
new_class.workflow = Workflow(state, transitions, initial_state)
return new_class
class Flowable(object):
__metaclass__ = WorkflowMeta
@property
def state(self):
if not hasattr(self, '_state'):
@@ -85,3 +57,32 @@ class Flowable(object):
self._state = new
else:
raise RuntimeError("Invalid State Transition : %s -> %s" %(old, new))
def __new__(cls, name, mro, attrs):
base = 'Component'
state = attrs.get('states', None)
transitions = attrs.get('transitions', None)
initial_state = attrs.get('initial_state', None)
if not 'abstract' in attrs:
if attrs.get('workflow'):
raise RuntimeError('`workflow` is a reserved attribute.')
if not state:
import pdb; pdb.set_trace()
raise RuntimeError('Must specify states')
if not transitions:
raise RuntimeError('Must specify transitions')
if not transitions:
raise RuntimeError('Must specify initial_state')
new_class = super(WorkflowMeta, cls).__new__(cls, name, mro, attrs)
if not 'abstract' in attrs:
new_class.workflow = Workflow(state, transitions, initial_state)
return new_class