📄 collections.py
字号:
"""Support for collections of mapped entities.The collections package supplies the machinery used to inform the ORM ofcollection membership changes. An instrumentation via decoration approach isused, allowing arbitrary types (including built-ins) to be used as entitycollections without requiring inheritance from a base class.Instrumentation decoration relays membership change events to the``InstrumentedCollectionAttribute`` that is currently managing the collection.The decorators observe function call arguments and return values, trackingentities entering or leaving the collection. Two decorator approaches areprovided. One is a bundle of generic decorators that map function argumentsand return values to events:: from sqlalchemy.orm.collections import collection class MyClass(object): # ... @collection.adds(1) def store(self, item): self.data.append(item) @collection.removes_return() def pop(self): return self.data.pop()The second approach is a bundle of targeted decorators that wrap appropriateappend and remove notifiers around the mutation methods present in thestandard Python ``list``, ``set`` and ``dict`` interfaces. These could bespecified in terms of generic decorator recipes, but are instead hand-tooled forincreased efficiency. The targeted decorators occasionally implementadapter-like behavior, such as mapping bulk-set methods (``extend``, ``update``,``__setslice__``, etc.) into the series of atomic mutation events that the ORMrequires.The targeted decorators are used internally for automatic instrumentation ofentity collection classes. Every collection class goes through atransformation process roughly like so:1. If the class is a built-in, substitute a trivial sub-class2. Is this class already instrumented?3. Add in generic decorators4. Sniff out the collection interface through duck-typing5. Add targeted decoration to any undecorated interface methodThis process modifies the class at runtime, decorating methods and adding somebookkeeping properties. This isn't possible (or desirable) for built-inclasses like ``list``, so trivial sub-classes are substituted to holddecoration:: class InstrumentedList(list): passCollection classes can be specified in ``relation(collection_class=)`` astypes or a function that returns an instance. Collection classes areinspected and instrumented during the mapper compilation phase. Thecollection_class callable will be executed once to produce a specimeninstance, and the type of that specimen will be instrumented. Functions thatreturn built-in types like ``lists`` will be adapted to produce instrumentedinstances.When extending a known type like ``list``, additional decorations are notgenerally not needed. Odds are, the extension method will delegate to amethod that's already instrumented. For example:: class QueueIsh(list): def push(self, item): self.append(item) def shift(self): return self.pop(0)There's no need to decorate these methods. ``append`` and ``pop`` are alreadyinstrumented as part of the ``list`` interface. Decorating them would fireduplicate events, which should be avoided.The targeted decoration tries not to rely on other methods in the underlyingcollection class, but some are unavoidable. Many depend on 'read' methodsbeing present to properly instrument a 'write', for example, ``__setitem__``needs ``__getitem__``. "Bulk" methods like ``update`` and ``extend`` may alsoreimplemented in terms of atomic appends and removes, so the ``extend``decoration will actually perform many ``append`` operations and not call theunderlying method at all.Tight control over bulk operation and the firing of events is also possible byimplementing the instrumentation internally in your methods. The basicinstrumentation package works under the general assumption that collectionmutation will not raise unusual exceptions. If you want to closelyorchestrate append and remove events with exception management, internalinstrumentation may be the answer. Within your method,``collection_adapter(self)`` will retrieve an object that you can use forexplicit control over triggering append and remove events.The owning object and InstrumentedCollectionAttribute are also reachablethrough the adapter, allowing for some very sophisticated behavior."""import copy, inspect, sys, weakreffrom sqlalchemy import exceptions, schema, util as sautilfrom sqlalchemy.util import attrgetter, Set__all__ = ['collection', 'collection_adapter', 'mapped_collection', 'column_mapped_collection', 'attribute_mapped_collection']def column_mapped_collection(mapping_spec): """A dictionary-based collection type with column-based keying. Returns a MappedCollection factory with a keying function generated from mapping_spec, which may be a Column or a sequence of Columns. The key value must be immutable for the lifetime of the object. You can not, for example, map on foreign key values if those key values will change during the session, i.e. from None to a database-assigned integer after a session flush. """ from sqlalchemy.orm import object_mapper if isinstance(mapping_spec, schema.Column): def keyfunc(value): m = object_mapper(value) return m._get_attr_by_column(value, mapping_spec) else: cols = [] for c in mapping_spec: if not isinstance(c, schema.Column): raise exceptions.ArgumentError( "mapping_spec tuple may only contain columns") cols.append(c) mapping_spec = tuple(cols) def keyfunc(value): m = object_mapper(value) return tuple([m._get_attr_by_column(value, c) for c in mapping_spec]) return lambda: MappedCollection(keyfunc)def attribute_mapped_collection(attr_name): """A dictionary-based collection type with attribute-based keying. Returns a MappedCollection factory with a keying based on the 'attr_name' attribute of entities in the collection. The key value must be immutable for the lifetime of the object. You can not, for example, map on foreign key values if those key values will change during the session, i.e. from None to a database-assigned integer after a session flush. """ return lambda: MappedCollection(attrgetter(attr_name))def mapped_collection(keyfunc): """A dictionary-based collection type with arbitrary keying. Returns a MappedCollection factory with a keying function generated from keyfunc, a callable that takes an entity and returns a key value. The key value must be immutable for the lifetime of the object. You can not, for example, map on foreign key values if those key values will change during the session, i.e. from None to a database-assigned integer after a session flush. """ return lambda: MappedCollection(keyfunc)class collection(object): """Decorators for entity collection classes. The decorators fall into two groups: annotations and interception recipes. The annotating decorators (appender, remover, iterator, internally_instrumented, on_link) indicate the method's purpose and take no arguments. They are not written with parens:: @collection.appender def append(self, append): ... The recipe decorators all require parens, even those that take no arguments:: @collection.adds('entity'): def insert(self, position, entity): ... @collection.removes_return() def popitem(self): ... Decorators can be specified in long-hand for Python 2.3, or with the class-level dict attribute '__instrumentation__'- see the source for details. """ # Bundled as a class solely for ease of use: packaging, doc strings, # importability. def appender(cls, fn): """Tag the method as the collection appender. The appender method is called with one positional argument: the value to append. The method will be automatically decorated with 'adds(1)' if not already decorated:: @collection.appender def add(self, append): ... # or, equivalently @collection.appender @collection.adds(1) def add(self, append): ... # for mapping type, an 'append' may kick out a previous value # that occupies that slot. consider d['a'] = 'foo'- any previous # value in d['a'] is discarded. @collection.appender @collection.replaces(1) def add(self, entity): key = some_key_func(entity) previous = None if key in self: previous = self[key] self[key] = entity return previous If the value to append is not allowed in the collection, you may raise an exception. Something to remember is that the appender will be called for each object mapped by a database query. If the database contains rows that violate your collection semantics, you will need to get creative to fix the problem, as access via the collection will not work. If the appender method is internally instrumented, you must also receive the keyword argument '_sa_initiator' and ensure its promulgation to collection events. """ setattr(fn, '_sa_instrument_role', 'appender') return fn appender = classmethod(appender) def remover(cls, fn): """Tag the method as the collection remover. The remover method is called with one positional argument: the value to remove. The method will be automatically decorated with 'removes_return()' if not already decorated:: @collection.remover def zap(self, entity): ... # or, equivalently @collection.remover @collection.removes_return() def zap(self, ): ... If the value to remove is not present in the collection, you may raise an exception or return None to ignore the error. If the remove method is internally instrumented, you must also receive the keyword argument '_sa_initiator' and ensure its promulgation to collection events. """ setattr(fn, '_sa_instrument_role', 'remover') return fn remover = classmethod(remover) def iterator(cls, fn): """Tag the method as the collection remover. The iterator method is called with no arguments. It is expected to return an iterator over all collection members:: @collection.iterator def __iter__(self): ... """ setattr(fn, '_sa_instrument_role', 'iterator') return fn iterator = classmethod(iterator) def internally_instrumented(cls, fn): """Tag the method as instrumented. This tag will prevent any decoration from being applied to the method. Use this if you are orchestrating your own calls to collection_adapter in one of the basic SQLAlchemy interface methods, or to prevent an automatic ABC method decoration from wrapping your implementation:: # normally an 'extend' method on a list-like class would be # automatically intercepted and re-implemented in terms of # SQLAlchemy events and append(). your implementation will # never be called, unless: @collection.internally_instrumented def extend(self, items): ... """ setattr(fn, '_sa_instrumented', True) return fn internally_instrumented = classmethod(internally_instrumented) def on_link(cls, fn): """Tag the method as a the "linked to attribute" event handler. This optional event handler will be called when the collection class is linked to or unlinked from the InstrumentedAttribute. It is invoked immediately after the '_sa_adapter' property is set on the instance. A single argument is passed: the collection adapter that has been linked, or None if unlinking. """ setattr(fn, '_sa_instrument_role', 'on_link') return fn on_link = classmethod(on_link) def converter(cls, fn): """Tag the method as the collection converter. This optional method will be called when a collection is being replaced entirely, as in:: myobj.acollection = [newvalue1, newvalue2] The converter method will receive the object being assigned and should return an iterable of values suitable for use by the ``appender`` method. A converter must not assign values or mutate the collection, it's sole job is to adapt the value the user provides into an iterable of values for the ORM's use. The default converter implementation will use duck-typing to do the conversion. A dict-like collection will be convert into an iterable of dictionary values, and other types will simply be iterated. @collection.converter def convert(self, other): ... If the duck-typing of the object does not match the type of this collection, a TypeError is raised. Supply an implementation of this method if you want to expand the range of possible types that can be assigned in bulk or perform validation on the values about to be assigned. """
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -