Participer au site avec un Tip
Rechercher
 

Améliorations / Corrections

Vous avez des améliorations (ou des corrections) à proposer pour ce document : je vous remerçie par avance de m'en faire part, cela m'aide à améliorer le site.

Emplacement :

Description des améliorations :

Vous êtes un professionnel et vous avez besoin d'une formation ? Machine Learning
avec Scikit-Learn
Voir le programme détaillé
Module « sqlalchemy.orm »

Classe « AttributeEvents »

Informations générales

Héritage

builtins.object
    Generic
        _HasEventsDispatch
            Events
                AttributeEvents

Définition

class AttributeEvents(Events):

help(AttributeEvents)

Define events for object attributes.

These are typically defined on the class-bound descriptor for the
target class.

For example, to register a listener that will receive the
:meth:`_orm.AttributeEvents.append` event::

    from sqlalchemy import event


    @event.listens_for(MyClass.collection, "append", propagate=True)
    def my_append_listener(target, value, initiator):
        print("received append event for target: %s" % target)

Listeners have the option to return a possibly modified version of the
value, when the :paramref:`.AttributeEvents.retval` flag is passed to
:func:`.event.listen` or :func:`.event.listens_for`, such as below,
illustrated using the :meth:`_orm.AttributeEvents.set` event::

    def validate_phone(target, value, oldvalue, initiator):
        "Strip non-numeric characters from a phone number"

        return re.sub(r"\D", "", value)


    # setup listener on UserContact.phone attribute, instructing
    # it to use the return value
    listen(UserContact.phone, "set", validate_phone, retval=True)

A validation function like the above can also raise an exception
such as :exc:`ValueError` to halt the operation.

The :paramref:`.AttributeEvents.propagate` flag is also important when
applying listeners to mapped classes that also have mapped subclasses,
as when using mapper inheritance patterns::


    @event.listens_for(MySuperClass.attr, "set", propagate=True)
    def receive_set(target, value, initiator):
        print("value set: %s" % target)

The full list of modifiers available to the :func:`.event.listen`
and :func:`.event.listens_for` functions are below.

:param active_history=False: When True, indicates that the
  "set" event would like to receive the "old" value being
  replaced unconditionally, even if this requires firing off
  database loads. Note that ``active_history`` can also be
  set directly via :func:`.column_property` and
  :func:`_orm.relationship`.

:param propagate=False: When True, the listener function will
  be established not just for the class attribute given, but
  for attributes of the same name on all current subclasses
  of that class, as well as all future subclasses of that
  class, using an additional listener that listens for
  instrumentation events.
:param raw=False: When True, the "target" argument to the
  event will be the :class:`.InstanceState` management
  object, rather than the mapped instance itself.
:param retval=False: when True, the user-defined event
  listening must return the "value" argument from the
  function.  This gives the listening function the opportunity
  to change the value that is ultimately used for a "set"
  or "append" event.

Constructeur(s)

Signature du constructeur Description
__init__(self, /, *args, **kwargs) Initialize self. See help(type(self)) for accurate signature. [extrait de __init__.__doc__]

Liste des attributs statiques

Nom de l'attribut Valeur
dispatch<sqlalchemy.event.base.AttributeEventsDispatch object at 0x0000020DA147C7A0>

Liste des opérateurs

Opérateurs hérités de la classe object

__eq__, __ge__, __gt__, __le__, __lt__, __ne__

Liste des méthodes

Toutes les méthodes Méthodes d'instance Méthodes statiques Méthodes dépréciées
Signature de la méthodeDescription
__class_getitem__ Parameterizes a generic class. [extrait de __class_getitem__.__doc__]
append(self, target: '_O', value: '_T', initiator: 'Event', *, key: 'EventConstants' = <EventConstants.NO_KEY: 4>) -> 'Optional[_T]' Receive a collection append event. [extrait de append.__doc__]
append_wo_mutation(self, target: '_O', value: '_T', initiator: 'Event', *, key: 'EventConstants' = <EventConstants.NO_KEY: 4>) -> 'None' Receive a collection append event where the collection was not [extrait de append_wo_mutation.__doc__]
bulk_replace(self, target: '_O', values: 'Iterable[_T]', initiator: 'Event', *, keys: 'Optional[Iterable[EventConstants]]' = None) -> 'None' Receive a collection 'bulk replace' event. [extrait de bulk_replace.__doc__]
dispose_collection(self, target: '_O', collection: 'Collection[Any]', collection_adapter: 'CollectionAdapter') -> 'None' Receive a 'collection dispose' event. [extrait de dispose_collection.__doc__]
init_collection(self, target: '_O', collection: 'Type[Collection[Any]]', collection_adapter: 'CollectionAdapter') -> 'None' Receive a 'collection init' event. [extrait de init_collection.__doc__]
init_scalar(self, target: '_O', value: '_T', dict_: 'Dict[Any, Any]') -> 'None' Receive a scalar "init" event. [extrait de init_scalar.__doc__]
modified(self, target: '_O', initiator: 'Event') -> 'None' Receive a 'modified' event. [extrait de modified.__doc__]
remove(self, target: '_O', value: '_T', initiator: 'Event', *, key: 'EventConstants' = <EventConstants.NO_KEY: 4>) -> 'None' Receive a collection remove event. [extrait de remove.__doc__]
set(self, target: '_O', value: '_T', oldvalue: '_T', initiator: 'Event') -> 'None' Receive a scalar set event. [extrait de set.__doc__]

Méthodes héritées de la classe Events

__subclasshook__

Méthodes héritées de la classe _HasEventsDispatch

__init_subclass__

Méthodes héritées de la classe object

__delattr__, __dir__, __format__, __getattribute__, __getstate__, __hash__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__

Vous êtes un professionnel et vous avez besoin d'une formation ? Machine Learning
avec Scikit-Learn
Voir le programme détaillé