Update 2025-04-24_11:44:19
This commit is contained in:
@ -0,0 +1,6 @@
|
||||
# cyextension/__init__.py
|
||||
# Copyright (C) 2005-2025 the SQLAlchemy authors and contributors
|
||||
# <see AUTHORS file>
|
||||
#
|
||||
# This module is part of SQLAlchemy and is released under
|
||||
# the MIT License: https://www.opensource.org/licenses/mit-license.php
|
Binary file not shown.
Binary file not shown.
@ -0,0 +1,409 @@
|
||||
# cyextension/collections.pyx
|
||||
# Copyright (C) 2005-2024 the SQLAlchemy authors and contributors
|
||||
# <see AUTHORS file>
|
||||
#
|
||||
# This module is part of SQLAlchemy and is released under
|
||||
# the MIT License: https://www.opensource.org/licenses/mit-license.php
|
||||
cimport cython
|
||||
from cpython.long cimport PyLong_FromLongLong
|
||||
from cpython.set cimport PySet_Add
|
||||
|
||||
from collections.abc import Collection
|
||||
from itertools import filterfalse
|
||||
|
||||
cdef bint add_not_present(set seen, object item, hashfunc):
|
||||
hash_value = hashfunc(item)
|
||||
if hash_value not in seen:
|
||||
PySet_Add(seen, hash_value)
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
cdef list cunique_list(seq, hashfunc=None):
|
||||
cdef set seen = set()
|
||||
if not hashfunc:
|
||||
return [x for x in seq if x not in seen and not PySet_Add(seen, x)]
|
||||
else:
|
||||
return [x for x in seq if add_not_present(seen, x, hashfunc)]
|
||||
|
||||
def unique_list(seq, hashfunc=None):
|
||||
return cunique_list(seq, hashfunc)
|
||||
|
||||
cdef class OrderedSet(set):
|
||||
|
||||
cdef list _list
|
||||
|
||||
@classmethod
|
||||
def __class_getitem__(cls, key):
|
||||
return cls
|
||||
|
||||
def __init__(self, d=None):
|
||||
set.__init__(self)
|
||||
if d is not None:
|
||||
self._list = cunique_list(d)
|
||||
set.update(self, self._list)
|
||||
else:
|
||||
self._list = []
|
||||
|
||||
cpdef OrderedSet copy(self):
|
||||
cdef OrderedSet cp = OrderedSet.__new__(OrderedSet)
|
||||
cp._list = list(self._list)
|
||||
set.update(cp, cp._list)
|
||||
return cp
|
||||
|
||||
@cython.final
|
||||
cdef OrderedSet _from_list(self, list new_list):
|
||||
cdef OrderedSet new = OrderedSet.__new__(OrderedSet)
|
||||
new._list = new_list
|
||||
set.update(new, new_list)
|
||||
return new
|
||||
|
||||
def add(self, element):
|
||||
if element not in self:
|
||||
self._list.append(element)
|
||||
PySet_Add(self, element)
|
||||
|
||||
def remove(self, element):
|
||||
# set.remove will raise if element is not in self
|
||||
set.remove(self, element)
|
||||
self._list.remove(element)
|
||||
|
||||
def pop(self):
|
||||
try:
|
||||
value = self._list.pop()
|
||||
except IndexError:
|
||||
raise KeyError("pop from an empty set") from None
|
||||
set.remove(self, value)
|
||||
return value
|
||||
|
||||
def insert(self, Py_ssize_t pos, element):
|
||||
if element not in self:
|
||||
self._list.insert(pos, element)
|
||||
PySet_Add(self, element)
|
||||
|
||||
def discard(self, element):
|
||||
if element in self:
|
||||
set.remove(self, element)
|
||||
self._list.remove(element)
|
||||
|
||||
def clear(self):
|
||||
set.clear(self)
|
||||
self._list = []
|
||||
|
||||
def __getitem__(self, key):
|
||||
return self._list[key]
|
||||
|
||||
def __iter__(self):
|
||||
return iter(self._list)
|
||||
|
||||
def __add__(self, other):
|
||||
return self.union(other)
|
||||
|
||||
def __repr__(self):
|
||||
return "%s(%r)" % (self.__class__.__name__, self._list)
|
||||
|
||||
__str__ = __repr__
|
||||
|
||||
def update(self, *iterables):
|
||||
for iterable in iterables:
|
||||
for e in iterable:
|
||||
if e not in self:
|
||||
self._list.append(e)
|
||||
set.add(self, e)
|
||||
|
||||
def __ior__(self, iterable):
|
||||
self.update(iterable)
|
||||
return self
|
||||
|
||||
def union(self, *other):
|
||||
result = self.copy()
|
||||
result.update(*other)
|
||||
return result
|
||||
|
||||
def __or__(self, other):
|
||||
return self.union(other)
|
||||
|
||||
def intersection(self, *other):
|
||||
cdef set other_set = set.intersection(self, *other)
|
||||
return self._from_list([a for a in self._list if a in other_set])
|
||||
|
||||
def __and__(self, other):
|
||||
return self.intersection(other)
|
||||
|
||||
def symmetric_difference(self, other):
|
||||
cdef set other_set
|
||||
if isinstance(other, set):
|
||||
other_set = <set> other
|
||||
collection = other_set
|
||||
elif isinstance(other, Collection):
|
||||
collection = other
|
||||
other_set = set(other)
|
||||
else:
|
||||
collection = list(other)
|
||||
other_set = set(collection)
|
||||
result = self._from_list([a for a in self._list if a not in other_set])
|
||||
result.update(a for a in collection if a not in self)
|
||||
return result
|
||||
|
||||
def __xor__(self, other):
|
||||
return self.symmetric_difference(other)
|
||||
|
||||
def difference(self, *other):
|
||||
cdef set other_set = set.difference(self, *other)
|
||||
return self._from_list([a for a in self._list if a in other_set])
|
||||
|
||||
def __sub__(self, other):
|
||||
return self.difference(other)
|
||||
|
||||
def intersection_update(self, *other):
|
||||
set.intersection_update(self, *other)
|
||||
self._list = [a for a in self._list if a in self]
|
||||
|
||||
def __iand__(self, other):
|
||||
self.intersection_update(other)
|
||||
return self
|
||||
|
||||
cpdef symmetric_difference_update(self, other):
|
||||
collection = other if isinstance(other, Collection) else list(other)
|
||||
set.symmetric_difference_update(self, collection)
|
||||
self._list = [a for a in self._list if a in self]
|
||||
self._list += [a for a in collection if a in self]
|
||||
|
||||
def __ixor__(self, other):
|
||||
self.symmetric_difference_update(other)
|
||||
return self
|
||||
|
||||
def difference_update(self, *other):
|
||||
set.difference_update(self, *other)
|
||||
self._list = [a for a in self._list if a in self]
|
||||
|
||||
def __isub__(self, other):
|
||||
self.difference_update(other)
|
||||
return self
|
||||
|
||||
cdef object cy_id(object item):
|
||||
return PyLong_FromLongLong(<long long> (<void *>item))
|
||||
|
||||
# NOTE: cython 0.x will call __add__, __sub__, etc with the parameter swapped
|
||||
# instead of the __rmeth__, so they need to check that also self is of the
|
||||
# correct type. This is fixed in cython 3.x. See:
|
||||
# https://docs.cython.org/en/latest/src/userguide/special_methods.html#arithmetic-methods
|
||||
cdef class IdentitySet:
|
||||
"""A set that considers only object id() for uniqueness.
|
||||
|
||||
This strategy has edge cases for builtin types- it's possible to have
|
||||
two 'foo' strings in one of these sets, for example. Use sparingly.
|
||||
|
||||
"""
|
||||
|
||||
cdef dict _members
|
||||
|
||||
def __init__(self, iterable=None):
|
||||
self._members = {}
|
||||
if iterable:
|
||||
self.update(iterable)
|
||||
|
||||
def add(self, value):
|
||||
self._members[cy_id(value)] = value
|
||||
|
||||
def __contains__(self, value):
|
||||
return cy_id(value) in self._members
|
||||
|
||||
cpdef remove(self, value):
|
||||
del self._members[cy_id(value)]
|
||||
|
||||
def discard(self, value):
|
||||
try:
|
||||
self.remove(value)
|
||||
except KeyError:
|
||||
pass
|
||||
|
||||
def pop(self):
|
||||
cdef tuple pair
|
||||
try:
|
||||
pair = self._members.popitem()
|
||||
return pair[1]
|
||||
except KeyError:
|
||||
raise KeyError("pop from an empty set")
|
||||
|
||||
def clear(self):
|
||||
self._members.clear()
|
||||
|
||||
def __eq__(self, other):
|
||||
cdef IdentitySet other_
|
||||
if isinstance(other, IdentitySet):
|
||||
other_ = other
|
||||
return self._members == other_._members
|
||||
else:
|
||||
return False
|
||||
|
||||
def __ne__(self, other):
|
||||
cdef IdentitySet other_
|
||||
if isinstance(other, IdentitySet):
|
||||
other_ = other
|
||||
return self._members != other_._members
|
||||
else:
|
||||
return True
|
||||
|
||||
cpdef issubset(self, iterable):
|
||||
cdef IdentitySet other
|
||||
if isinstance(iterable, self.__class__):
|
||||
other = iterable
|
||||
else:
|
||||
other = self.__class__(iterable)
|
||||
|
||||
if len(self) > len(other):
|
||||
return False
|
||||
for m in filterfalse(other._members.__contains__, self._members):
|
||||
return False
|
||||
return True
|
||||
|
||||
def __le__(self, other):
|
||||
if not isinstance(other, IdentitySet):
|
||||
return NotImplemented
|
||||
return self.issubset(other)
|
||||
|
||||
def __lt__(self, other):
|
||||
if not isinstance(other, IdentitySet):
|
||||
return NotImplemented
|
||||
return len(self) < len(other) and self.issubset(other)
|
||||
|
||||
cpdef issuperset(self, iterable):
|
||||
cdef IdentitySet other
|
||||
if isinstance(iterable, self.__class__):
|
||||
other = iterable
|
||||
else:
|
||||
other = self.__class__(iterable)
|
||||
|
||||
if len(self) < len(other):
|
||||
return False
|
||||
for m in filterfalse(self._members.__contains__, other._members):
|
||||
return False
|
||||
return True
|
||||
|
||||
def __ge__(self, other):
|
||||
if not isinstance(other, IdentitySet):
|
||||
return NotImplemented
|
||||
return self.issuperset(other)
|
||||
|
||||
def __gt__(self, other):
|
||||
if not isinstance(other, IdentitySet):
|
||||
return NotImplemented
|
||||
return len(self) > len(other) and self.issuperset(other)
|
||||
|
||||
cpdef IdentitySet union(self, iterable):
|
||||
cdef IdentitySet result = self.__class__()
|
||||
result._members.update(self._members)
|
||||
result.update(iterable)
|
||||
return result
|
||||
|
||||
def __or__(self, other):
|
||||
if not isinstance(other, IdentitySet) or not isinstance(self, IdentitySet):
|
||||
return NotImplemented
|
||||
return self.union(other)
|
||||
|
||||
cpdef update(self, iterable):
|
||||
for obj in iterable:
|
||||
self._members[cy_id(obj)] = obj
|
||||
|
||||
def __ior__(self, other):
|
||||
if not isinstance(other, IdentitySet):
|
||||
return NotImplemented
|
||||
self.update(other)
|
||||
return self
|
||||
|
||||
cpdef IdentitySet difference(self, iterable):
|
||||
cdef IdentitySet result = self.__new__(self.__class__)
|
||||
if isinstance(iterable, self.__class__):
|
||||
other = (<IdentitySet>iterable)._members
|
||||
else:
|
||||
other = {cy_id(obj) for obj in iterable}
|
||||
result._members = {k:v for k, v in self._members.items() if k not in other}
|
||||
return result
|
||||
|
||||
def __sub__(self, other):
|
||||
if not isinstance(other, IdentitySet) or not isinstance(self, IdentitySet):
|
||||
return NotImplemented
|
||||
return self.difference(other)
|
||||
|
||||
cpdef difference_update(self, iterable):
|
||||
cdef IdentitySet other = self.difference(iterable)
|
||||
self._members = other._members
|
||||
|
||||
def __isub__(self, other):
|
||||
if not isinstance(other, IdentitySet):
|
||||
return NotImplemented
|
||||
self.difference_update(other)
|
||||
return self
|
||||
|
||||
cpdef IdentitySet intersection(self, iterable):
|
||||
cdef IdentitySet result = self.__new__(self.__class__)
|
||||
if isinstance(iterable, self.__class__):
|
||||
other = (<IdentitySet>iterable)._members
|
||||
else:
|
||||
other = {cy_id(obj) for obj in iterable}
|
||||
result._members = {k: v for k, v in self._members.items() if k in other}
|
||||
return result
|
||||
|
||||
def __and__(self, other):
|
||||
if not isinstance(other, IdentitySet) or not isinstance(self, IdentitySet):
|
||||
return NotImplemented
|
||||
return self.intersection(other)
|
||||
|
||||
cpdef intersection_update(self, iterable):
|
||||
cdef IdentitySet other = self.intersection(iterable)
|
||||
self._members = other._members
|
||||
|
||||
def __iand__(self, other):
|
||||
if not isinstance(other, IdentitySet):
|
||||
return NotImplemented
|
||||
self.intersection_update(other)
|
||||
return self
|
||||
|
||||
cpdef IdentitySet symmetric_difference(self, iterable):
|
||||
cdef IdentitySet result = self.__new__(self.__class__)
|
||||
cdef dict other
|
||||
if isinstance(iterable, self.__class__):
|
||||
other = (<IdentitySet>iterable)._members
|
||||
else:
|
||||
other = {cy_id(obj): obj for obj in iterable}
|
||||
result._members = {k: v for k, v in self._members.items() if k not in other}
|
||||
result._members.update(
|
||||
[(k, v) for k, v in other.items() if k not in self._members]
|
||||
)
|
||||
return result
|
||||
|
||||
def __xor__(self, other):
|
||||
if not isinstance(other, IdentitySet) or not isinstance(self, IdentitySet):
|
||||
return NotImplemented
|
||||
return self.symmetric_difference(other)
|
||||
|
||||
cpdef symmetric_difference_update(self, iterable):
|
||||
cdef IdentitySet other = self.symmetric_difference(iterable)
|
||||
self._members = other._members
|
||||
|
||||
def __ixor__(self, other):
|
||||
if not isinstance(other, IdentitySet):
|
||||
return NotImplemented
|
||||
self.symmetric_difference(other)
|
||||
return self
|
||||
|
||||
cpdef IdentitySet copy(self):
|
||||
cdef IdentitySet cp = self.__new__(self.__class__)
|
||||
cp._members = self._members.copy()
|
||||
return cp
|
||||
|
||||
def __copy__(self):
|
||||
return self.copy()
|
||||
|
||||
def __len__(self):
|
||||
return len(self._members)
|
||||
|
||||
def __iter__(self):
|
||||
return iter(self._members.values())
|
||||
|
||||
def __hash__(self):
|
||||
raise TypeError("set objects are unhashable")
|
||||
|
||||
def __repr__(self):
|
||||
return "%s(%r)" % (type(self).__name__, list(self._members.values()))
|
Binary file not shown.
@ -0,0 +1,8 @@
|
||||
# cyextension/immutabledict.pxd
|
||||
# Copyright (C) 2005-2024 the SQLAlchemy authors and contributors
|
||||
# <see AUTHORS file>
|
||||
#
|
||||
# This module is part of SQLAlchemy and is released under
|
||||
# the MIT License: https://www.opensource.org/licenses/mit-license.php
|
||||
cdef class immutabledict(dict):
|
||||
pass
|
@ -0,0 +1,133 @@
|
||||
# cyextension/immutabledict.pyx
|
||||
# Copyright (C) 2005-2024 the SQLAlchemy authors and contributors
|
||||
# <see AUTHORS file>
|
||||
#
|
||||
# This module is part of SQLAlchemy and is released under
|
||||
# the MIT License: https://www.opensource.org/licenses/mit-license.php
|
||||
from cpython.dict cimport PyDict_New, PyDict_Update, PyDict_Size
|
||||
|
||||
|
||||
def _readonly_fn(obj):
|
||||
raise TypeError(
|
||||
"%s object is immutable and/or readonly" % obj.__class__.__name__)
|
||||
|
||||
|
||||
def _immutable_fn(obj):
|
||||
raise TypeError(
|
||||
"%s object is immutable" % obj.__class__.__name__)
|
||||
|
||||
|
||||
class ReadOnlyContainer:
|
||||
|
||||
__slots__ = ()
|
||||
|
||||
def _readonly(self, *a,**kw):
|
||||
_readonly_fn(self)
|
||||
|
||||
__delitem__ = __setitem__ = __setattr__ = _readonly
|
||||
|
||||
|
||||
class ImmutableDictBase(dict):
|
||||
def _immutable(self, *a,**kw):
|
||||
_immutable_fn(self)
|
||||
|
||||
@classmethod
|
||||
def __class_getitem__(cls, key):
|
||||
return cls
|
||||
|
||||
__delitem__ = __setitem__ = __setattr__ = _immutable
|
||||
clear = pop = popitem = setdefault = update = _immutable
|
||||
|
||||
|
||||
cdef class immutabledict(dict):
|
||||
def __repr__(self):
|
||||
return f"immutabledict({dict.__repr__(self)})"
|
||||
|
||||
@classmethod
|
||||
def __class_getitem__(cls, key):
|
||||
return cls
|
||||
|
||||
def union(self, *args, **kw):
|
||||
cdef dict to_merge = None
|
||||
cdef immutabledict result
|
||||
cdef Py_ssize_t args_len = len(args)
|
||||
if args_len > 1:
|
||||
raise TypeError(
|
||||
f'union expected at most 1 argument, got {args_len}'
|
||||
)
|
||||
if args_len == 1:
|
||||
attribute = args[0]
|
||||
if isinstance(attribute, dict):
|
||||
to_merge = <dict> attribute
|
||||
if to_merge is None:
|
||||
to_merge = dict(*args, **kw)
|
||||
|
||||
if PyDict_Size(to_merge) == 0:
|
||||
return self
|
||||
|
||||
# new + update is faster than immutabledict(self)
|
||||
result = immutabledict()
|
||||
PyDict_Update(result, self)
|
||||
PyDict_Update(result, to_merge)
|
||||
return result
|
||||
|
||||
def merge_with(self, *other):
|
||||
cdef immutabledict result = None
|
||||
cdef object d
|
||||
cdef bint update = False
|
||||
if not other:
|
||||
return self
|
||||
for d in other:
|
||||
if d:
|
||||
if update == False:
|
||||
update = True
|
||||
# new + update is faster than immutabledict(self)
|
||||
result = immutabledict()
|
||||
PyDict_Update(result, self)
|
||||
PyDict_Update(
|
||||
result, <dict>(d if isinstance(d, dict) else dict(d))
|
||||
)
|
||||
|
||||
return self if update == False else result
|
||||
|
||||
def copy(self):
|
||||
return self
|
||||
|
||||
def __reduce__(self):
|
||||
return immutabledict, (dict(self), )
|
||||
|
||||
def __delitem__(self, k):
|
||||
_immutable_fn(self)
|
||||
|
||||
def __setitem__(self, k, v):
|
||||
_immutable_fn(self)
|
||||
|
||||
def __setattr__(self, k, v):
|
||||
_immutable_fn(self)
|
||||
|
||||
def clear(self, *args, **kw):
|
||||
_immutable_fn(self)
|
||||
|
||||
def pop(self, *args, **kw):
|
||||
_immutable_fn(self)
|
||||
|
||||
def popitem(self, *args, **kw):
|
||||
_immutable_fn(self)
|
||||
|
||||
def setdefault(self, *args, **kw):
|
||||
_immutable_fn(self)
|
||||
|
||||
def update(self, *args, **kw):
|
||||
_immutable_fn(self)
|
||||
|
||||
# PEP 584
|
||||
def __ior__(self, other):
|
||||
_immutable_fn(self)
|
||||
|
||||
def __or__(self, other):
|
||||
return immutabledict(dict.__or__(self, other))
|
||||
|
||||
def __ror__(self, other):
|
||||
# NOTE: this is used only in cython 3.x;
|
||||
# version 0.x will call __or__ with args inversed
|
||||
return immutabledict(dict.__ror__(self, other))
|
Binary file not shown.
@ -0,0 +1,68 @@
|
||||
# cyextension/processors.pyx
|
||||
# Copyright (C) 2005-2024 the SQLAlchemy authors and contributors
|
||||
# <see AUTHORS file>
|
||||
#
|
||||
# This module is part of SQLAlchemy and is released under
|
||||
# the MIT License: https://www.opensource.org/licenses/mit-license.php
|
||||
import datetime
|
||||
from datetime import datetime as datetime_cls
|
||||
from datetime import time as time_cls
|
||||
from datetime import date as date_cls
|
||||
import re
|
||||
|
||||
from cpython.object cimport PyObject_Str
|
||||
from cpython.unicode cimport PyUnicode_AsASCIIString, PyUnicode_Check, PyUnicode_Decode
|
||||
from libc.stdio cimport sscanf
|
||||
|
||||
|
||||
def int_to_boolean(value):
|
||||
if value is None:
|
||||
return None
|
||||
return True if value else False
|
||||
|
||||
def to_str(value):
|
||||
return PyObject_Str(value) if value is not None else None
|
||||
|
||||
def to_float(value):
|
||||
return float(value) if value is not None else None
|
||||
|
||||
cdef inline bytes to_bytes(object value, str type_name):
|
||||
try:
|
||||
return PyUnicode_AsASCIIString(value)
|
||||
except Exception as e:
|
||||
raise ValueError(
|
||||
f"Couldn't parse {type_name} string '{value!r}' "
|
||||
"- value is not a string."
|
||||
) from e
|
||||
|
||||
def str_to_datetime(value):
|
||||
if value is not None:
|
||||
value = datetime_cls.fromisoformat(value)
|
||||
return value
|
||||
|
||||
def str_to_time(value):
|
||||
if value is not None:
|
||||
value = time_cls.fromisoformat(value)
|
||||
return value
|
||||
|
||||
|
||||
def str_to_date(value):
|
||||
if value is not None:
|
||||
value = date_cls.fromisoformat(value)
|
||||
return value
|
||||
|
||||
|
||||
|
||||
cdef class DecimalResultProcessor:
|
||||
cdef object type_
|
||||
cdef str format_
|
||||
|
||||
def __cinit__(self, type_, format_):
|
||||
self.type_ = type_
|
||||
self.format_ = format_
|
||||
|
||||
def process(self, object value):
|
||||
if value is None:
|
||||
return None
|
||||
else:
|
||||
return self.type_(self.format_ % value)
|
Binary file not shown.
@ -0,0 +1,102 @@
|
||||
# cyextension/resultproxy.pyx
|
||||
# Copyright (C) 2005-2024 the SQLAlchemy authors and contributors
|
||||
# <see AUTHORS file>
|
||||
#
|
||||
# This module is part of SQLAlchemy and is released under
|
||||
# the MIT License: https://www.opensource.org/licenses/mit-license.php
|
||||
import operator
|
||||
|
||||
cdef class BaseRow:
|
||||
cdef readonly object _parent
|
||||
cdef readonly dict _key_to_index
|
||||
cdef readonly tuple _data
|
||||
|
||||
def __init__(self, object parent, object processors, dict key_to_index, object data):
|
||||
"""Row objects are constructed by CursorResult objects."""
|
||||
|
||||
self._parent = parent
|
||||
|
||||
self._key_to_index = key_to_index
|
||||
|
||||
if processors:
|
||||
self._data = _apply_processors(processors, data)
|
||||
else:
|
||||
self._data = tuple(data)
|
||||
|
||||
def __reduce__(self):
|
||||
return (
|
||||
rowproxy_reconstructor,
|
||||
(self.__class__, self.__getstate__()),
|
||||
)
|
||||
|
||||
def __getstate__(self):
|
||||
return {"_parent": self._parent, "_data": self._data}
|
||||
|
||||
def __setstate__(self, dict state):
|
||||
parent = state["_parent"]
|
||||
self._parent = parent
|
||||
self._data = state["_data"]
|
||||
self._key_to_index = parent._key_to_index
|
||||
|
||||
def _values_impl(self):
|
||||
return list(self)
|
||||
|
||||
def __iter__(self):
|
||||
return iter(self._data)
|
||||
|
||||
def __len__(self):
|
||||
return len(self._data)
|
||||
|
||||
def __hash__(self):
|
||||
return hash(self._data)
|
||||
|
||||
def __getitem__(self, index):
|
||||
return self._data[index]
|
||||
|
||||
def _get_by_key_impl_mapping(self, key):
|
||||
return self._get_by_key_impl(key, 0)
|
||||
|
||||
cdef _get_by_key_impl(self, object key, int attr_err):
|
||||
index = self._key_to_index.get(key)
|
||||
if index is not None:
|
||||
return self._data[<int>index]
|
||||
self._parent._key_not_found(key, attr_err != 0)
|
||||
|
||||
def __getattr__(self, name):
|
||||
return self._get_by_key_impl(name, 1)
|
||||
|
||||
def _to_tuple_instance(self):
|
||||
return self._data
|
||||
|
||||
|
||||
cdef tuple _apply_processors(proc, data):
|
||||
res = []
|
||||
for i in range(len(proc)):
|
||||
p = proc[i]
|
||||
if p is None:
|
||||
res.append(data[i])
|
||||
else:
|
||||
res.append(p(data[i]))
|
||||
return tuple(res)
|
||||
|
||||
|
||||
def rowproxy_reconstructor(cls, state):
|
||||
obj = cls.__new__(cls)
|
||||
obj.__setstate__(state)
|
||||
return obj
|
||||
|
||||
|
||||
cdef int is_contiguous(tuple indexes):
|
||||
cdef int i
|
||||
for i in range(1, len(indexes)):
|
||||
if indexes[i-1] != indexes[i] -1:
|
||||
return 0
|
||||
return 1
|
||||
|
||||
|
||||
def tuplegetter(*indexes):
|
||||
if len(indexes) == 1 or is_contiguous(indexes) != 0:
|
||||
# slice form is faster but returns a list if input is list
|
||||
return operator.itemgetter(slice(indexes[0], indexes[-1] + 1))
|
||||
else:
|
||||
return operator.itemgetter(*indexes)
|
Binary file not shown.
@ -0,0 +1,91 @@
|
||||
# cyextension/util.pyx
|
||||
# Copyright (C) 2005-2024 the SQLAlchemy authors and contributors
|
||||
# <see AUTHORS file>
|
||||
#
|
||||
# This module is part of SQLAlchemy and is released under
|
||||
# the MIT License: https://www.opensource.org/licenses/mit-license.php
|
||||
from collections.abc import Mapping
|
||||
|
||||
from sqlalchemy import exc
|
||||
|
||||
cdef tuple _Empty_Tuple = ()
|
||||
|
||||
cdef inline bint _mapping_or_tuple(object value):
|
||||
return isinstance(value, dict) or isinstance(value, tuple) or isinstance(value, Mapping)
|
||||
|
||||
cdef inline bint _check_item(object params) except 0:
|
||||
cdef object item
|
||||
cdef bint ret = 1
|
||||
if params:
|
||||
item = params[0]
|
||||
if not _mapping_or_tuple(item):
|
||||
ret = 0
|
||||
raise exc.ArgumentError(
|
||||
"List argument must consist only of tuples or dictionaries"
|
||||
)
|
||||
return ret
|
||||
|
||||
def _distill_params_20(object params):
|
||||
if params is None:
|
||||
return _Empty_Tuple
|
||||
elif isinstance(params, list) or isinstance(params, tuple):
|
||||
_check_item(params)
|
||||
return params
|
||||
elif isinstance(params, dict) or isinstance(params, Mapping):
|
||||
return [params]
|
||||
else:
|
||||
raise exc.ArgumentError("mapping or list expected for parameters")
|
||||
|
||||
|
||||
def _distill_raw_params(object params):
|
||||
if params is None:
|
||||
return _Empty_Tuple
|
||||
elif isinstance(params, list):
|
||||
_check_item(params)
|
||||
return params
|
||||
elif _mapping_or_tuple(params):
|
||||
return [params]
|
||||
else:
|
||||
raise exc.ArgumentError("mapping or sequence expected for parameters")
|
||||
|
||||
cdef class prefix_anon_map(dict):
|
||||
def __missing__(self, str key):
|
||||
cdef str derived
|
||||
cdef int anonymous_counter
|
||||
cdef dict self_dict = self
|
||||
|
||||
derived = key.split(" ", 1)[1]
|
||||
|
||||
anonymous_counter = self_dict.get(derived, 1)
|
||||
self_dict[derived] = anonymous_counter + 1
|
||||
value = f"{derived}_{anonymous_counter}"
|
||||
self_dict[key] = value
|
||||
return value
|
||||
|
||||
|
||||
cdef class cache_anon_map(dict):
|
||||
cdef int _index
|
||||
|
||||
def __init__(self):
|
||||
self._index = 0
|
||||
|
||||
def get_anon(self, obj):
|
||||
cdef long long idself
|
||||
cdef str id_
|
||||
cdef dict self_dict = self
|
||||
|
||||
idself = id(obj)
|
||||
if idself in self_dict:
|
||||
return self_dict[idself], True
|
||||
else:
|
||||
id_ = self.__missing__(idself)
|
||||
return id_, False
|
||||
|
||||
def __missing__(self, key):
|
||||
cdef str val
|
||||
cdef dict self_dict = self
|
||||
|
||||
self_dict[key] = val = str(self._index)
|
||||
self._index += 1
|
||||
return val
|
||||
|
Reference in New Issue
Block a user