관리-도구
편집 파일: collections.cpython-311.pyc
� �܋f�� � �. � d Z ddlZddlZddlmZ ddlmZ ddlmZ ddlm Z dd l mZ g d �Ze j � � � Z G d� de� � Z G d � de� � Z G d� de� � Zd� Z G d� de� � Zd� Zd� Z G d� de� � Z ej d� � Z G d� de� � Zd<d�Zd� Zd� Zd� Zd� Z d � Z!d!� Z"d"� Z#d#� Z$d<d$�Z%d<d%�Z&d<d&�Z'd'� Z(d(� Z)e*e+fZ,d)� Z-d*� Z.d+� Z/ G d,� d-e0� � Z1 G d.� d/e*� � Z2 G d0� d1e3� � Z4e0e1e*e2e3e4iZ5e0d2d3d4d5� e(� � fe*d6d3d4d5� e/� � fe3e j6 rd7d8i e)� � fn d7d9i e)� � fiZ7 G d:� d;e3� � Z8 ee8� � ee1� � ee2� � dS )=aN Support for collections of mapped entities. The collections package supplies the machinery used to inform the ORM of collection membership changes. An instrumentation via decoration approach is used, allowing arbitrary types (including built-ins) to be used as entity collections without requiring inheritance from a base class. Instrumentation decoration relays membership change events to the :class:`.CollectionAttributeImpl` that is currently managing the collection. The decorators observe function call arguments and return values, tracking entities entering or leaving the collection. Two decorator approaches are provided. One is a bundle of generic decorators that map function arguments and 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 appropriate append and remove notifiers around the mutation methods present in the standard Python ``list``, ``set`` and ``dict`` interfaces. These could be specified in terms of generic decorator recipes, but are instead hand-tooled for increased efficiency. The targeted decorators occasionally implement adapter-like behavior, such as mapping bulk-set methods (``extend``, ``update``, ``__setslice__``, etc.) into the series of atomic mutation events that the ORM requires. The targeted decorators are used internally for automatic instrumentation of entity collection classes. Every collection class goes through a transformation process roughly like so: 1. If the class is a built-in, substitute a trivial sub-class 2. Is this class already instrumented? 3. Add in generic decorators 4. Sniff out the collection interface through duck-typing 5. Add targeted decoration to any undecorated interface method This process modifies the class at runtime, decorating methods and adding some bookkeeping properties. This isn't possible (or desirable) for built-in classes like ``list``, so trivial sub-classes are substituted to hold decoration:: class InstrumentedList(list): pass Collection classes can be specified in ``relationship(collection_class=)`` as types or a function that returns an instance. Collection classes are inspected and instrumented during the mapper compilation phase. The collection_class callable will be executed once to produce a specimen instance, and the type of that specimen will be instrumented. Functions that return built-in types like ``lists`` will be adapted to produce instrumented instances. When extending a known type like ``list``, additional decorations are not generally not needed. Odds are, the extension method will delegate to a method 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 already instrumented as part of the ``list`` interface. Decorating them would fire duplicate events, which should be avoided. The targeted decoration tries not to rely on other methods in the underlying collection class, but some are unavoidable. Many depend on 'read' methods being present to properly instrument a 'write', for example, ``__setitem__`` needs ``__getitem__``. "Bulk" methods like ``update`` and ``extend`` may also reimplemented in terms of atomic appends and removes, so the ``extend`` decoration will actually perform many ``append`` operations and not call the underlying method at all. Tight control over bulk operation and the firing of events is also possible by implementing the instrumentation internally in your methods. The basic instrumentation package works under the general assumption that collection mutation will not raise unusual exceptions. If you want to closely orchestrate append and remove events with exception management, internal instrumentation may be the answer. Within your method, ``collection_adapter(self)`` will retrieve an object that you can use for explicit control over triggering append and remove events. The owning object and :class:`.CollectionAttributeImpl` are also reachable through the adapter, allowing for some very sophisticated behavior. � N)�inspect_getfullargspec� )�base� )�exc)�util)� expression)� collection�collection_adapter�mapped_collection�column_mapped_collection�attribute_mapped_collectionc �* � e Zd ZdZd� Zd� Zd� Zd� ZdS )�_PlainColumnGetterz�Plain column getter, stores collection of Column objects directly. Serializes to a :class:`._SerializableColumnGetterV2` which has more expensive __call__() performance and some rare caveats. c �D � || _ t |� � dk | _ d S �Nr )�cols�len� composite)�selfr s �Q/opt/cloudlinux/venv/lib64/python3.11/site-packages/sqlalchemy/orm/collections.py�__init__z_PlainColumnGetter.__init__� s � ��� ��T���Q������ c �@ � t � | j � � S �N)�_SerializableColumnGetterV2�_reduce_from_colsr �r s r � __reduce__z_PlainColumnGetter.__reduce__� s � �*�<�<�T�Y�G�G�Gr c � � | j S r )r )r �mappers r �_colsz_PlainColumnGetter._cols� s � ��y�r c �� ��� t j |� � �t j �� � ���fd�| � �� � D � � }| j rt |� � S |d S )Nc �H �� g | ]}�� ��j |� � ��S � )�_get_state_attr_by_column�dict)�.0�col�m�states ��r � <listcomp>z/_PlainColumnGetter.__call__.<locals>.<listcomp>� s= �� � � � �� �'�'��u�z�3�?�?� � � r r )r �instance_state� _state_mapperr"