protocols Package

protocols Package

Trivial Interfaces and Adaptation from PyProtocols.

This package is a subset of the files from Phillip J. Eby’s PyProtocols package. They are only included here to help remove dependencies on external packages from the Traits package. The code has been reorganized to address circular imports that were discovered when explicit relative imports were added.

adapters Module

Basic Adapters and Adapter Operations

advice Module

traits.protocols.advice.addClassAdvisor(callback, depth=2)[source]

Set up ‘callback’ to be passed the containing class upon creation

This function is designed to be called by an “advising” function executed in a class suite. The “advising” function supplies a callback that it wishes to have executed when the containing class is created. The callback will be given one argument: the newly created containing class. The return value of the callback will be used in place of the class, so the callback should return the input if it does not wish to replace the class.

The optional ‘depth’ argument to this function determines the number of frames between this function and the targeted class suite. ‘depth’ defaults to 2, since this skips this function’s frame and one calling function frame. If you use this function from a function called directly in the class suite, the default will be correct, otherwise you will need to determine the correct depth yourself.

This function works by installing a special class factory function in place of the ‘__metaclass__’ of the containing class. Therefore, only callbacks after the last ‘__metaclass__’ assignment in the containing class will be executed. Be sure that classes using “advising” functions declare any ‘__metaclass__’ first, to ensure all callbacks are run.

traits.protocols.advice.isClassAdvisor(ob)[source]

True if ‘ob’ is a class advisor function

traits.protocols.advice.metamethod(func)[source]

Wrapper for metaclass method that might be confused w/instance method

traits.protocols.advice.supermeta(typ, ob)[source]
traits.protocols.advice.minimalBases(classes)[source]

Reduce a list of base classes to its ordered minimum equivalent

traits.protocols.advice.determineMetaclass(bases, explicit_mc=None)[source]

Determine metaclass from 1+ bases and optional explicit __metaclass__

traits.protocols.advice.getFrameInfo(frame)[source]

Return (kind,module,locals,globals) for a frame

‘kind’ is one of “exec”, “module”, “class”, “function call”, or “unknown”.

traits.protocols.advice.getMRO(ob, extendedClassic=False)[source]
traits.protocols.advice.classicMRO(ob, extendedClassic=False)[source]
traits.protocols.advice.mkRef(ob, *args)[source]

Return either a weakref or a StrongRef for ‘ob’

Note that extra args are forwarded to weakref.ref() if applicable.

class traits.protocols.advice.StrongRef(referent)[source]

Bases: object

Like a weakref, but for non-weakrefable objects

referent

classic Module

Declaration support for Python built-in types

class traits.protocols.classic.ProviderMixin[source]

Mixin to support per-instance declarations

declareProvides[source]
newProtocolImplied[source]

generate Module

Autogenerated protocols from type+method names, URI, sequence, etc.

traits.protocols.generate.protocolForType(baseType, methods=(), implicit=False)[source]

Return a protocol representing a subset of methods of a specific type

traits.protocols.generate.protocolForURI(uri)[source]

Return a unique protocol object representing the supplied URI/UUID

traits.protocols.generate.sequenceOf(baseProtocol)[source]

Return a protocol representing an sequence of a given base protocol

class traits.protocols.generate.IBasicSequence[source]

Bases: traits.protocols.protocols.Interface

Non-string, iterable object sequence

class traits.protocols.generate.URIProtocol(uri)[source]

Bases: traits.protocols.protocols.Protocol

Protocol representing a URI, UUID, or other unique textual identifier

class traits.protocols.generate.TypeSubset(baseType, methods)[source]

Bases: traits.protocols.protocols.Protocol

Protocol representing some set of a type’s methods

class traits.protocols.generate.WeakSubset(baseType, methods)[source]

Bases: traits.protocols.generate.TypeSubset, object

TypeSubset that accepts any object with the right attributes

traits.protocols.generate.ADAPT_SEQUENCE(ob, proto)[source]

Convert iterable ‘ob’ into list of objects implementing ‘proto’

class traits.protocols.generate.SequenceProtocol(baseProtocol)[source]

Bases: traits.protocols.protocols.Protocol

Protocol representing a “sequence of” some base protocol

interfaces Module

Implement Interfaces and define the interfaces used by the package

protocols Module

Basic Adapters and Adapter Operations

traits.protocols.protocols.NO_ADAPTER_NEEDED(obj, protocol=None)[source]

Assume ‘obj’ implements ‘protocol’ directly

traits.protocols.protocols.DOES_NOT_SUPPORT(obj, protocol=None)[source]

Prevent ‘obj’ from supporting ‘protocol’

class traits.protocols.protocols.Adapter(ob, proto)[source]

Bases: object

Convenient base class for adapters

protocol
traits.protocols.protocols.minimumAdapter(a1, a2, d1=0, d2=0)[source]

Shortest route to implementation, ‘a1’ @ depth ‘d1’, or ‘a2’ @ ‘d2’?

Assuming both a1 and a2 are interchangeable adapters (i.e. have the same source and destination protocols), return the one which is preferable; that is, the one with the shortest implication depth, or, if the depths are equal, then the adapter that is composed of the fewest chained adapters. If both are the same, then prefer ‘NO_ADAPTER_NEEDED’, followed by anything but ‘DOES_NOT_SUPPORT’, with ‘DOES_NOT_SUPPORT’ being least preferable. If there is no unambiguous choice, and ‘not a1 is a2’, TypeError is raised.

traits.protocols.protocols.composeAdapters(baseAdapter, baseProtocol, extendingAdapter)[source]

Return the composition of ‘baseAdapter’+’extendingAdapter’

traits.protocols.protocols.updateWithSimplestAdapter(mapping, key, adapter, depth)[source]

Replace ‘mapping[key]’ w/’adapter’ @ ‘depth’, return true if changed

class traits.protocols.protocols.StickyAdapter(ob, proto)[source]

Bases: object

Adapter that attaches itself to its subject for repeated use

attachForProtocols = ()
protocol
exception traits.protocols.protocols.AdaptationFailure[source]

Bases: exceptions.NotImplementedError, exceptions.TypeError

A suitable implementation/adapter could not be found

traits.protocols.protocols.bindAdapter(adapter, proto)[source]

Backward compatibility: wrap ‘adapter’ to support old 2-arg signature

traits.protocols.protocols.adapt(obj, protocol, default=<object object at 0xb68d70>, factory=<object object at 0xb68d70>)[source]

PEP 246-alike: Adapt ‘obj’ to ‘protocol’, return ‘default’

If ‘default’ is not supplied and no implementation is found, the result of ‘factory(obj,protocol)’ is returned. If ‘factory’ is also not supplied, ‘NotImplementedError’ is then raised.

traits.protocols.protocols.declareAdapterForType(protocol, adapter, typ, depth=1)[source]

Declare that ‘adapter’ adapts instances of ‘typ’ to ‘protocol’

traits.protocols.protocols.declareAdapterForProtocol(protocol, adapter, proto, depth=1)[source]

Declare that ‘adapter’ adapts ‘proto’ to ‘protocol’

traits.protocols.protocols.declareAdapterForObject(protocol, adapter, ob, depth=1)[source]

Declare that ‘adapter’ adapts ‘ob’ to ‘protocol’

traits.protocols.protocols.advise(**kw)[source]
traits.protocols.protocols.declareImplementation(typ, instancesProvide=(), instancesDoNotProvide=())[source]

Declare information about a class, type, or ‘IOpenImplementor’

traits.protocols.protocols.declareAdapter(factory, provides, forTypes=(), forProtocols=(), forObjects=())[source]

‘factory’ is an IAdapterFactory providing ‘provides’ protocols

traits.protocols.protocols.adviseObject(ob, provides=(), doesNotProvide=())[source]

Tell an object what it does or doesn’t provide

class traits.protocols.protocols.Protocol[source]

Generic protocol w/type-based adapter registry

addImplicationListener[source]
addImpliedProtocol[source]
getImpliedProtocols()[source]
registerImplementation[source]
registerObject[source]
class traits.protocols.protocols.InterfaceClass(__name__, __bases__, __dict__)[source]

Bases: traits.protocols.protocols.AbstractBaseMeta

Metaclass for ‘Interface’ - a non-instantiable protocol

(Note that this should not be used as an explicit metaclass - always subclass from ‘AbstractBase’ or ‘Interface’ instead.)

getBases()[source]
class traits.protocols.protocols.Interface[source]

Bases: object

class traits.protocols.protocols.AbstractBase[source]

Bases: object

Base class for a protocol that’s a class

class traits.protocols.protocols.AbstractBaseMeta(__name__, __bases__, __dict__)[source]

Bases: traits.protocols.protocols.Protocol, type

Metaclass for ‘AbstractBase’ - a protocol that’s also a class

(Note that this should not be used as an explicit metaclass - always subclass from ‘AbstractBase’ or ‘Interface’ instead.)

class traits.protocols.protocols.IAdapterFactory[source]

Bases: traits.protocols.protocols.Interface

Callable that can adapt an object to a protocol

class traits.protocols.protocols.IProtocol[source]

Bases: traits.protocols.protocols.Interface

Object usable as a protocol by ‘adapt()’

class traits.protocols.protocols.IAdaptingProtocol[source]

Bases: traits.protocols.protocols.IProtocol

A protocol that potentially knows how to adapt some object to itself

class traits.protocols.protocols.IOpenProtocol[source]

Bases: traits.protocols.protocols.IAdaptingProtocol

A protocol that be told what it implies, and what supports it

Note that these methods are for the use of the declaration APIs only, and you should NEVER call them directly.

classmethod addImplicationListener(listener)[source]
classmethod addImpliedProtocol(proto, adapter=<function NO_ADAPTER_NEEDED at 0x16e1b18>, depth=1)[source]
classmethod registerImplementation(klass, adapter=<function NO_ADAPTER_NEEDED at 0x16e1b18>, depth=1)[source]
classmethod registerObject(ob, adapter=<function NO_ADAPTER_NEEDED at 0x16e1b18>, depth=1)[source]
class traits.protocols.protocols.IOpenProvider[source]

Bases: traits.protocols.protocols.Interface

An object that can be told how to adapt to protocols

declareProvides(protocol, adapter=<function NO_ADAPTER_NEEDED at 0x16e1b18>, depth=1)[source]

Register ‘adapter’ as providing ‘protocol’ for this object

Return a true value if the provided adapter is the “shortest path” to ‘protocol’ for the object, or false if a shorter path already existed.

class traits.protocols.protocols.IOpenImplementor[source]

Bases: traits.protocols.protocols.Interface

Object/type that can be told how its instances adapt to protocols

declareClassImplements(protocol, adapter=<function NO_ADAPTER_NEEDED at 0x16e1b18>, depth=1)[source]

Register ‘adapter’ as implementing ‘protocol’ for instances

class traits.protocols.protocols.IImplicationListener[source]

Bases: traits.protocols.protocols.Interface

newProtocolImplied(srcProto, destProto, adapter, depth)[source]

‘srcProto’ now implies ‘destProto’ via ‘adapter’ at ‘depth’

traits.protocols.protocols.Attribute[source]

Attribute declaration; should we get rid of this?

class traits.protocols.protocols.Variation(baseProtocol, context=None)[source]

Bases: traits.protocols.protocols.Protocol

A variation of a base protocol - “inherits” the base’s adapters

See the ‘LocalProtocol’ example in the reference manual for more info.

Table Of Contents

Previous topic

etsconfig Package

Next topic

testing Package

This Page