
    Ng                    |    d Z ddlmZ ddlZddlmZmZmZmZm	Z	  edd          Z
 G d d	ee
                   ZdS )
z:Stand-alone utility functions independent of OXMSG domain.    )annotationsN)AnyCallableGenericTypeVarcast_TT)	covariantc                  ,    e Zd ZdZddZdddZddZdS )lazypropertya$  Decorator like @property, but evaluated only on first access.

    Like @property, this can only be used to decorate methods having only a `self` parameter, and
    is accessed like an attribute on an instance, i.e. trailing parentheses are not used. Unlike
    @property, the decorated method is only evaluated on first access; the resulting value is
    cached and that same value returned on second and later access without re-evaluation of the
    method.

    Like @property, this class produces a *data descriptor* object, which is stored in the __dict__
    of the *class* under the name of the decorated method ('fget' nominally). The cached value is
    stored in the __dict__ of the *instance* under that same name.

    Because it is a data descriptor (as opposed to a *non-data descriptor*), its `__get__()` method
    is executed on each access of the decorated attribute; the __dict__ item of the same name is
    "shadowed" by the descriptor.

    While this may represent a performance improvement over a property, its greater benefit may be
    its other characteristics. One common use is to construct collaborator objects, removing that
    "real work" from the constructor, while still only executing once. It also de-couples client
    code from any sequencing considerations; if it's accessed from more than one location, it's
    assured it will be ready whenever needed.

    Loosely based on: https://stackoverflow.com/a/6849299/1902513.

    A lazyproperty is read-only. There is no counterpart to the optional "setter" (or deleter)
    behavior of an @property. This is critically important to maintaining its immutability and
    idempotence guarantees. Attempting to assign to a lazyproperty raises AttributeError
    unconditionally.

    The parameter names in the methods below correspond to this usage example::

        class Obj(object)

            @lazyproperty
            def fget(self):
                return 'some result'

        obj = Obj()

    Not suitable for wrapping a function (as opposed to a method) because it is not callable.
    fgetCallable[..., _T]returnNonec                V    || _         |j        | _        t          j        | |           dS )aY  *fget* is the decorated method (a "getter" function).

        A lazyproperty is read-only, so there is only an *fget* function (a regular
        @property can also have an fset and fdel function). This name was chosen for
        consistency with Python's `property` class which uses this name for the
        corresponding parameter.
        N)_fget__name___name	functoolsupdate_wrapper)selfr   s     F/var/www/html/ai-engine/env/lib/python3.11/site-packages/oxmsg/util.py__init__zlazyproperty.__init__6   s-     
]
 t,,,,,    Nobjr   typer	   c                    || S |j                             | j                  }|$|                     |          }||j         | j        <   t	          t
          |          S )a  Called on each access of 'fget' attribute on class or instance.

        *self* is this instance of a lazyproperty descriptor "wrapping" the property
        method it decorates (`fget`, nominally).

        *obj* is the "host" object instance when the attribute is accessed from an
        object instance, e.g. `obj = Obj(); obj.fget`. *obj* is None when accessed on
        the class, e.g. `Obj.fget`.

        *type* is the class hosting the decorated getter method (`fget`) on both class
        and instance attribute access.
        )__dict__getr   r   r   r	   )r   r   r   values       r   __get__zlazyproperty.__get__E   sY     ;K   ,,= JJsOOE',CL$Br   r    c                     t          d          )a  Raises unconditionally, to preserve read-only behavior.

        This decorator is intended to implement immutable (and idempotent) object
        attributes. For that reason, assignment to this property must be explicitly
        prevented.

        If this __set__ method was not present, this descriptor would become a
        *non-data descriptor*. That would be nice because the cached value would be
        accessed directly once set (__dict__ attrs have precedence over non-data
        descriptors on instance attribute lookup). The problem is, there would be
        nothing to stop assignment to the cached value, which would overwrite the result
        of `fget()` and break both the immutability and idempotence guarantees of this
        decorator.

        The performance with this __set__() method in place was roughly 0.4 usec per
        access when measured on a 2.8GHz development machine; so quite snappy and
        probably not a rich target for optimization efforts.
        zcan't set attribute)AttributeError)r   r   r    s      r   __set__zlazyproperty.__set__b   s    & 2333r   )r   r   r   r   )N)r   r   r   r   r   r	   )r   r   r    r   r   r   )r   
__module____qualname____doc__r   r!   r$    r   r   r   r      s`        ( (T- - - -    :4 4 4 4 4 4r   r   )r'   
__future__r   r   typingr   r   r   r   r   r	   r   r(   r   r   <module>r+      s    @ @ " " " " " "     8 8 8 8 8 8 8 8 8 8 8 8 8 8WTT"""j4 j4 j4 j4 j472; j4 j4 j4 j4 j4r   