adapter Module

An extension to PyProtocols to simplify the declaration of adapters.


class traits.adapter.Adapter(*args, **kw)[source]
__init__(*args, **kw)[source]


traits.adapter.adapts(*args, **kw)[source]

A class advisor for declaring adapters.

  • from_ (type or interface) – What the adapter adapts from, or a list of such types or interfaces (the ‘_’ suffix is used because ‘from’ is a Python keyword).
  • to (type or interface) – What the adapter adapts to, or a list of such types or interfaces.
  • factory (callable) – An (optional) factory for actually creating the adapters. This is any callable that takes a single argument which is the object to be adapted. The factory should return an adapter if it can perform the adaptation and None if it cannot.
  • cached (bool) – Should the adapters be cached? If an adapter is cached, then the factory will produce at most one adapter per instance.
  • when (str) – A Python expression that selects which instances of a particular type can be adapted by this factory. The expression is evaluated in a namespace that contains a single name adaptee, which is bound to the object to be adapted (e.g., ‘adaptee.is_folder’).


The cached and when arguments are ignored if factory is specified.