Download Traits 4 User Manual
Transcript
Traits 4 User Manual, Release 4.5.0-rc.1 Implementation details The algorithm for finding a sequence of adapters adapting an object adaptee to a protocol to_protocol is based on a weighted graph. Nodes on the graphs are protocols (types or interfaces). offer.from_protocol to a offer.to_protocol. Edges are adaptation offers that connect a Edges connect protocol A to protocol B and are weighted by two numbers in this priority: 1. a unit weight (1) representing the fact that we use 1 adaptation offer to go from A to B 2. the number of steps up the type hierarchy that we need to take to go from A to offer.from_protocol, so that more specific adapters are always preferred The algorithm finds the shortest weighted path between adaptee and to_protocol. Once a candidate path is found, it tries to create the chain of adapters using the factories in the adaptation offers that compose the path. If this fails because of conditional adaptation (i.e., an adapter factory returns None), the path is discarded and the algorithm looks for the next shortest path. Cycles in adaptation are avoided by only considering path were every adaptation offer is used at most once. Migration guide The implementation of the adaptation mechanism changed in Traits 4.4.0 from one based on PyProtocols to a new, smaller, and more robust implementation. Code written against traits.protocols will continue to work, although the traits.protocols API has been deprecated and its members will log a warning the first time they are accessed. The traits.protocols package will be removed in Traits 5.0 . This is a list of replacements for the old API: • traits.protocols.api.AdaptationFailure Use traits.api.AdaptationError instead. • traits.api.adapts() Use the traits.api.register_factory() function. • implements() Use the traits.api.provides() decorator instead. • traits.protocols.api.declareAdapter() Use the function traits.api.register_factory(), or the function traits.adaptation.api.register_offer() instead. It is no longer necessary to distinguish between “types”, “protocols”, and “objects”. • traits.protocols.api.declareImplementation() This function was used occasionally to declare that an arbitrary type (e.g., dict) implements an interface. Users that use Python ABCs can use the register method for achieving the same result. Otherwise, use the function traits.adaptation.api.register_provides() that declares a “null” adapter to adapt the type to the interface. • Testing if a class is an Interface issubclass(klass, Interface) is not reliable, use traits.api.isinterface() instead 44 Chapter 1. User Reference