Третий коммит, добавление share, share_kb, а также ADMIN_ID

This commit is contained in:
2025-07-22 13:50:14 +03:00
parent 849feb7beb
commit b98123f4dc
1479 changed files with 323549 additions and 11 deletions

View File

@@ -0,0 +1,32 @@
"""propcache: An accelerated property cache for Python classes."""
from typing import TYPE_CHECKING
_PUBLIC_API = ("cached_property", "under_cached_property")
__version__ = "0.3.2"
__all__ = ()
# Imports have moved to `propcache.api` in 0.2.0+.
# This module is now a facade for the API.
if TYPE_CHECKING:
from .api import cached_property as cached_property # noqa: F401
from .api import under_cached_property as under_cached_property # noqa: F401
def _import_facade(attr: str) -> object:
"""Import the public API from the `api` module."""
if attr in _PUBLIC_API:
from . import api # pylint: disable=import-outside-toplevel
return getattr(api, attr)
raise AttributeError(f"module '{__package__}' has no attribute '{attr}'")
def _dir_facade() -> list[str]:
"""Include the public API in the module's dir() output."""
return [*_PUBLIC_API, *globals().keys()]
__getattr__ = _import_facade
__dir__ = _dir_facade

View File

@@ -0,0 +1,39 @@
import os
import sys
from typing import TYPE_CHECKING
__all__ = ("cached_property", "under_cached_property")
NO_EXTENSIONS = bool(os.environ.get("PROPCACHE_NO_EXTENSIONS")) # type: bool
if sys.implementation.name != "cpython":
NO_EXTENSIONS = True
# isort: off
if TYPE_CHECKING:
from ._helpers_py import cached_property as cached_property_py
from ._helpers_py import under_cached_property as under_cached_property_py
cached_property = cached_property_py
under_cached_property = under_cached_property_py
elif not NO_EXTENSIONS: # pragma: no branch
try:
from ._helpers_c import cached_property as cached_property_c # type: ignore[attr-defined, unused-ignore]
from ._helpers_c import under_cached_property as under_cached_property_c # type: ignore[attr-defined, unused-ignore]
cached_property = cached_property_c
under_cached_property = under_cached_property_c
except ImportError: # pragma: no cover
from ._helpers_py import cached_property as cached_property_py
from ._helpers_py import under_cached_property as under_cached_property_py
cached_property = cached_property_py # type: ignore[assignment, misc]
under_cached_property = under_cached_property_py
else:
from ._helpers_py import cached_property as cached_property_py
from ._helpers_py import under_cached_property as under_cached_property_py
cached_property = cached_property_py # type: ignore[assignment, misc]
under_cached_property = under_cached_property_py
# isort: on

View File

@@ -0,0 +1,86 @@
# cython: language_level=3, freethreading_compatible=True
from types import GenericAlias
cdef _sentinel = object()
cdef class under_cached_property:
"""Use as a class method decorator. It operates almost exactly like
the Python `@property` decorator, but it puts the result of the
method it decorates into the instance dict after the first call,
effectively replacing the function it decorates with an instance
variable. It is, in Python parlance, a data descriptor.
"""
cdef readonly object wrapped
cdef object name
def __init__(self, wrapped):
self.wrapped = wrapped
self.name = wrapped.__name__
@property
def __doc__(self):
return self.wrapped.__doc__
def __get__(self, inst, owner):
if inst is None:
return self
cdef dict cache = inst._cache
val = cache.get(self.name, _sentinel)
if val is _sentinel:
val = self.wrapped(inst)
cache[self.name] = val
return val
def __set__(self, inst, value):
raise AttributeError("cached property is read-only")
__class_getitem__ = classmethod(GenericAlias)
cdef class cached_property:
"""Use as a class method decorator. It operates almost exactly like
the Python `@property` decorator, but it puts the result of the
method it decorates into the instance dict after the first call,
effectively replacing the function it decorates with an instance
variable. It is, in Python parlance, a data descriptor.
"""
cdef readonly object func
cdef object name
def __init__(self, func):
self.func = func
self.name = None
@property
def __doc__(self):
return self.func.__doc__
def __set_name__(self, owner, name):
if self.name is None:
self.name = name
elif name != self.name:
raise TypeError(
"Cannot assign the same cached_property to two different names "
f"({self.name!r} and {name!r})."
)
def __get__(self, inst, owner):
if inst is None:
return self
if self.name is None:
raise TypeError(
"Cannot use cached_property instance"
" without calling __set_name__ on it.")
cdef dict cache = inst.__dict__
val = cache.get(self.name, _sentinel)
if val is _sentinel:
val = self.func(inst)
cache[self.name] = val
return val
__class_getitem__ = classmethod(GenericAlias)

View File

@@ -0,0 +1,60 @@
"""Various helper functions."""
import sys
from collections.abc import Mapping
from functools import cached_property
from typing import Any, Callable, Generic, Optional, Protocol, TypeVar, Union, overload
__all__ = ("under_cached_property", "cached_property")
if sys.version_info >= (3, 11):
from typing import Self
else:
Self = Any
_T = TypeVar("_T")
# We use Mapping to make it possible to use TypedDict, but this isn't
# technically type safe as we need to assign into the dict.
_Cache = TypeVar("_Cache", bound=Mapping[str, Any])
class _CacheImpl(Protocol[_Cache]):
_cache: _Cache
class under_cached_property(Generic[_T]):
"""Use as a class method decorator.
It operates almost exactly like
the Python `@property` decorator, but it puts the result of the
method it decorates into the instance dict after the first call,
effectively replacing the function it decorates with an instance
variable. It is, in Python parlance, a data descriptor.
"""
def __init__(self, wrapped: Callable[[Any], _T]) -> None:
self.wrapped = wrapped
self.__doc__ = wrapped.__doc__
self.name = wrapped.__name__
@overload
def __get__(self, inst: None, owner: Optional[type[object]] = None) -> Self: ...
@overload
def __get__(self, inst: _CacheImpl[Any], owner: Optional[type[object]] = None) -> _T: ... # type: ignore[misc]
def __get__(
self, inst: Optional[_CacheImpl[Any]], owner: Optional[type[object]] = None
) -> Union[_T, Self]:
if inst is None:
return self
try:
return inst._cache[self.name] # type: ignore[no-any-return]
except KeyError:
val = self.wrapped(inst)
inst._cache[self.name] = val
return val
def __set__(self, inst: _CacheImpl[Any], value: _T) -> None:
raise AttributeError("cached property is read-only")

View File

@@ -0,0 +1,8 @@
"""Public API of the property caching library."""
from ._helpers import cached_property, under_cached_property
__all__ = (
"cached_property",
"under_cached_property",
)

View File

@@ -0,0 +1 @@
# Placeholder