Container Base Class

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 Container are 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 _states attribute.

  • If the recommended storage strategy outlined in the above item has been followed, it is not necessary for subclasses of Container to define an __eq__ or __ne__ method. Container provides a default implementation of __eq__ that follows the above strategy.

    In this default implementation, the objects' _states attributes will be compared with ==.

  • Subclasses of Container need not define a __len__ method; Container defines a __len__ implementation that sums the lengths of the states in the invocant's _states attribute. This should be suitable for most, if not all, subclasses.

  • Subclasses of Container need not define a clone method. Container provides a default implementation that works by iterating over each State in the invocant's _states attribute, calling the State's clone method. This should be sufficient for most, if not all, subclasses.

  • Subclasses of Container must be iterable. However, Container implements an __iter__ method that should be sufficient for most, if not all, subclasses: it iterates over the invocant's _states attribute, yielding successive elements.

  • Subclasses of Container must define a next method. 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 Container must define an add_sequence method. 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 Container must define an add_any_order method. 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 Container must define an add_command method. 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 Container must define an add_error method. 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.

svnmock