The Container base class provides a common superclass for State classes that can contain other States, such as Sequence and AnyOrder.
It inherits from the State base class and extends the protocol defined there in the following way:
-
Subclasses of
Containerare recommended to define an__init__method that takes at least the one optional parameter described here.The first parameter will be an iterable, each element of which will be a State. These States represent the pool of states the Container may delegate validation requests to.
It is recommended that an iterable of States (representing the available State pool) be stored in the instance's
_statesattribute. -
If the recommended storage strategy outlined in the above item has been followed, it is not necessary for subclasses of
Containerto define an__eq__or__ne__method.Containerprovides a default implementation of__eq__that follows the above strategy.In this default implementation, the objects'
_statesattributes will be compared with==. -
Subclasses of
Containerneed not define a__len__method;Containerdefines a__len__implementation that sums the lengths of the states in the invocant's_statesattribute. This should be suitable for most, if not all, subclasses. -
Subclasses of
Containerneed not define aclonemethod.Containerprovides a default implementation that works by iterating over each State in the invocant's_statesattribute, calling the State'sclonemethod. This should be sufficient for most, if not all, subclasses. -
Subclasses of
Containermust be iterable. However,Containerimplements an__iter__method that should be sufficient for most, if not all, subclasses: it iterates over the invocant's_statesattribute, yielding successive elements. -
Subclasses of
Containermust define anextmethod. This will be called by States contained by the invocant Container to indicate that the contained State has exhausted its ability to validate API calls. -
Subclasses of
Containermust define anadd_sequencemethod. This method accepts one optional parameter, an iterable of States, defaulting to an empty iterable.This method will return an instance of the Sequence class, inserted in the proper place in the invocant's pool of available states and populated with the States taken from the passed-in iterable.
-
Subclasses of
Containermust define anadd_any_ordermethod. This method accepts one optional parameter, an iterable of States, defaulting to an empty iterable.This method will return an instance of the AnyOrder class, inserted in the proper place in the invocant's pool of available states and populated with the States taken from the passed-in iterable.
-
Subclasses of
Containermust define anadd_commandmethod. This method accepts the arguments accepted by the constructor for the Command class.This method will return an instance of the Command class, inserted in the proper place in the invocant's pool of available states and created from the passed-in arguments.
-
Subclasses of
Containermust define anadd_errormethod. This method accepts the arguments accepted by the constructor for the Error class.This method will return an instance of the Error class, inserted in the proper place in the invocant's pool of available states and created from the passed-in arguments.
Note that if the protocol outlined above does not meet your needs, you should probably consider subclassing State directly, rather than trying to shoehorn your functionality into this protocol.