heat.db.types 模块¶
- class heat.db.types.Json(*args: Any, **kwargs: Any)[source]¶
基类:
LongText- cache_ok: bool | None = True¶
指示使用此
ExternalType的语句是否“可以安全地缓存”。默认值
None将发出警告,然后不允许缓存包含此类型的语句。设置为False以完全禁用使用此类型语句的缓存,而无需发出警告。当设置为True时,该对象的类及其状态中的选定元素将用作缓存键的一部分。例如,使用TypeDecoratorclass MyType(TypeDecorator): impl = String cache_ok = True def __init__(self, choices): self.choices = tuple(choices) self.internal_only = True
上述类型的缓存键将等效于
>>> MyType(["a", "b", "c"])._static_cache_key (<class '__main__.MyType'>, ('choices', ('a', 'b', 'c')))
缓存方案将从类型中提取与
__init__()方法中的参数名称对应的属性。上述示例中,“choices”属性成为缓存键的一部分,但“internal_only”则不然,因为没有名为“internal_only”的参数。可缓存元素的必要条件是它们是可哈希的,并且对于给定缓存值,它们表示相同的 SQL 渲染表达式。
为了适应引用不可哈希结构(如字典、集合和列表)的数据类型,可以通过将可哈希结构分配给与参数名称对应的属性来使这些对象“可缓存”。例如,接受查找值字典的数据类型可以将此值作为排序后的元组序列发布。给定先前不可缓存的类型如下
class LookupType(UserDefinedType): """a custom type that accepts a dictionary as a parameter. this is the non-cacheable version, as "self.lookup" is not hashable. """ def __init__(self, lookup): self.lookup = lookup def get_col_spec(self, **kw): return "VARCHAR(255)" def bind_processor(self, dialect): ... # works with "self.lookup" ...
其中“lookup”是一个字典。该类型将无法生成缓存键
>>> type_ = LookupType({"a": 10, "b": 20}) >>> type_._static_cache_key <stdin>:1: SAWarning: UserDefinedType LookupType({'a': 10, 'b': 20}) will not produce a cache key because the ``cache_ok`` flag is not set to True. Set this flag to True if this type object's state is safe to use in a cache key, or False to disable this warning. symbol('no_cache')
如果确实设置了这样的缓存键,它将无法使用。我们将获得一个包含字典的元组结构,而该字典本身不能用作 SQLAlchemy 的语句缓存等“缓存字典”中的键,因为 Python 字典不可哈希
>>> # set cache_ok = True >>> type_.cache_ok = True >>> # this is the cache key it would generate >>> key = type_._static_cache_key >>> key (<class '__main__.LookupType'>, ('lookup', {'a': 10, 'b': 20})) >>> # however this key is not hashable, will fail when used with >>> # SQLAlchemy statement cache >>> some_cache = {key: "some sql value"} Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unhashable type: 'dict'
可以通过将排序后的元组的元组分配给“.lookup”属性来使该类型可缓存
class LookupType(UserDefinedType): """a custom type that accepts a dictionary as a parameter. The dictionary is stored both as itself in a private variable, and published in a public variable as a sorted tuple of tuples, which is hashable and will also return the same value for any two equivalent dictionaries. Note it assumes the keys and values of the dictionary are themselves hashable. """ cache_ok = True def __init__(self, lookup): self._lookup = lookup # assume keys/values of "lookup" are hashable; otherwise # they would also need to be converted in some way here self.lookup = tuple((key, lookup[key]) for key in sorted(lookup)) def get_col_spec(self, **kw): return "VARCHAR(255)" def bind_processor(self, dialect): ... # works with "self._lookup" ...
上述示例中,
LookupType({"a": 10, "b": 20})的缓存键将为>>> LookupType({"a": 10, "b": 20})._static_cache_key (<class '__main__.LookupType'>, ('lookup', (('a', 10), ('b', 20))))
在 1.4.14 版本中添加: - 添加了
cache_ok标志,以允许对TypeDecorator类的缓存进行一些配置。在 1.4.28 版本中添加: - 添加了
ExternalType混合类,该混合类将cache_ok标志推广到TypeDecorator和UserDefinedType类。参见
- class heat.db.types.List(*args: Any, **kwargs: Any)[source]¶
基类:
TypeDecorator- cache_ok: bool | None = True¶
指示使用此
ExternalType的语句是否“可以安全地缓存”。默认值
None将发出警告,然后不允许缓存包含此类型的语句。设置为False以完全禁用使用此类型语句的缓存,而无需发出警告。当设置为True时,该对象的类及其状态中的选定元素将用作缓存键的一部分。例如,使用TypeDecoratorclass MyType(TypeDecorator): impl = String cache_ok = True def __init__(self, choices): self.choices = tuple(choices) self.internal_only = True
上述类型的缓存键将等效于
>>> MyType(["a", "b", "c"])._static_cache_key (<class '__main__.MyType'>, ('choices', ('a', 'b', 'c')))
缓存方案将从类型中提取与
__init__()方法中的参数名称对应的属性。上述示例中,“choices”属性成为缓存键的一部分,但“internal_only”则不然,因为没有名为“internal_only”的参数。可缓存元素的必要条件是它们是可哈希的,并且对于给定缓存值,它们表示相同的 SQL 渲染表达式。
为了适应引用不可哈希结构(如字典、集合和列表)的数据类型,可以通过将可哈希结构分配给与参数名称对应的属性来使这些对象“可缓存”。例如,接受查找值字典的数据类型可以将此值作为排序后的元组序列发布。给定先前不可缓存的类型如下
class LookupType(UserDefinedType): """a custom type that accepts a dictionary as a parameter. this is the non-cacheable version, as "self.lookup" is not hashable. """ def __init__(self, lookup): self.lookup = lookup def get_col_spec(self, **kw): return "VARCHAR(255)" def bind_processor(self, dialect): ... # works with "self.lookup" ...
其中“lookup”是一个字典。该类型将无法生成缓存键
>>> type_ = LookupType({"a": 10, "b": 20}) >>> type_._static_cache_key <stdin>:1: SAWarning: UserDefinedType LookupType({'a': 10, 'b': 20}) will not produce a cache key because the ``cache_ok`` flag is not set to True. Set this flag to True if this type object's state is safe to use in a cache key, or False to disable this warning. symbol('no_cache')
如果确实设置了这样的缓存键,它将无法使用。我们将获得一个包含字典的元组结构,而该字典本身不能用作 SQLAlchemy 的语句缓存等“缓存字典”中的键,因为 Python 字典不可哈希
>>> # set cache_ok = True >>> type_.cache_ok = True >>> # this is the cache key it would generate >>> key = type_._static_cache_key >>> key (<class '__main__.LookupType'>, ('lookup', {'a': 10, 'b': 20})) >>> # however this key is not hashable, will fail when used with >>> # SQLAlchemy statement cache >>> some_cache = {key: "some sql value"} Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unhashable type: 'dict'
可以通过将排序后的元组的元组分配给“.lookup”属性来使该类型可缓存
class LookupType(UserDefinedType): """a custom type that accepts a dictionary as a parameter. The dictionary is stored both as itself in a private variable, and published in a public variable as a sorted tuple of tuples, which is hashable and will also return the same value for any two equivalent dictionaries. Note it assumes the keys and values of the dictionary are themselves hashable. """ cache_ok = True def __init__(self, lookup): self._lookup = lookup # assume keys/values of "lookup" are hashable; otherwise # they would also need to be converted in some way here self.lookup = tuple((key, lookup[key]) for key in sorted(lookup)) def get_col_spec(self, **kw): return "VARCHAR(255)" def bind_processor(self, dialect): ... # works with "self._lookup" ...
上述示例中,
LookupType({"a": 10, "b": 20})的缓存键将为>>> LookupType({"a": 10, "b": 20})._static_cache_key (<class '__main__.LookupType'>, ('lookup', (('a', 10), ('b', 20))))
在 1.4.14 版本中添加: - 添加了
cache_ok标志,以允许对TypeDecorator类的缓存进行一些配置。在 1.4.28 版本中添加: - 添加了
ExternalType混合类,该混合类将cache_ok标志推广到TypeDecorator和UserDefinedType类。参见
- load_dialect_impl(dialect)[source]¶
返回与方言对应的
TypeEngine对象。这是一个用户覆盖钩子,可用于为给定的方言提供不同的类型。它由
TypeDecorator实现的type_engine()使用,以帮助确定应为给定的TypeDecorator返回什么类型。默认情况下返回
self.impl。
- class heat.db.types.LongText(*args: Any, **kwargs: Any)[source]¶
基类:
TypeDecorator- cache_ok: bool | None = True¶
指示使用此
ExternalType的语句是否“可以安全地缓存”。默认值
None将发出警告,然后不允许缓存包含此类型的语句。设置为False以完全禁用使用此类型语句的缓存,而无需发出警告。当设置为True时,该对象的类及其状态中的选定元素将用作缓存键的一部分。例如,使用TypeDecoratorclass MyType(TypeDecorator): impl = String cache_ok = True def __init__(self, choices): self.choices = tuple(choices) self.internal_only = True
上述类型的缓存键将等效于
>>> MyType(["a", "b", "c"])._static_cache_key (<class '__main__.MyType'>, ('choices', ('a', 'b', 'c')))
缓存方案将从类型中提取与
__init__()方法中的参数名称对应的属性。上述示例中,“choices”属性成为缓存键的一部分,但“internal_only”则不然,因为没有名为“internal_only”的参数。可缓存元素的必要条件是它们是可哈希的,并且对于给定缓存值,它们表示相同的 SQL 渲染表达式。
为了适应引用不可哈希结构(如字典、集合和列表)的数据类型,可以通过将可哈希结构分配给与参数名称对应的属性来使这些对象“可缓存”。例如,接受查找值字典的数据类型可以将此值作为排序后的元组序列发布。给定先前不可缓存的类型如下
class LookupType(UserDefinedType): """a custom type that accepts a dictionary as a parameter. this is the non-cacheable version, as "self.lookup" is not hashable. """ def __init__(self, lookup): self.lookup = lookup def get_col_spec(self, **kw): return "VARCHAR(255)" def bind_processor(self, dialect): ... # works with "self.lookup" ...
其中“lookup”是一个字典。该类型将无法生成缓存键
>>> type_ = LookupType({"a": 10, "b": 20}) >>> type_._static_cache_key <stdin>:1: SAWarning: UserDefinedType LookupType({'a': 10, 'b': 20}) will not produce a cache key because the ``cache_ok`` flag is not set to True. Set this flag to True if this type object's state is safe to use in a cache key, or False to disable this warning. symbol('no_cache')
如果确实设置了这样的缓存键,它将无法使用。我们将获得一个包含字典的元组结构,而该字典本身不能用作 SQLAlchemy 的语句缓存等“缓存字典”中的键,因为 Python 字典不可哈希
>>> # set cache_ok = True >>> type_.cache_ok = True >>> # this is the cache key it would generate >>> key = type_._static_cache_key >>> key (<class '__main__.LookupType'>, ('lookup', {'a': 10, 'b': 20})) >>> # however this key is not hashable, will fail when used with >>> # SQLAlchemy statement cache >>> some_cache = {key: "some sql value"} Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unhashable type: 'dict'
可以通过将排序后的元组的元组分配给“.lookup”属性来使该类型可缓存
class LookupType(UserDefinedType): """a custom type that accepts a dictionary as a parameter. The dictionary is stored both as itself in a private variable, and published in a public variable as a sorted tuple of tuples, which is hashable and will also return the same value for any two equivalent dictionaries. Note it assumes the keys and values of the dictionary are themselves hashable. """ cache_ok = True def __init__(self, lookup): self._lookup = lookup # assume keys/values of "lookup" are hashable; otherwise # they would also need to be converted in some way here self.lookup = tuple((key, lookup[key]) for key in sorted(lookup)) def get_col_spec(self, **kw): return "VARCHAR(255)" def bind_processor(self, dialect): ... # works with "self._lookup" ...
上述示例中,
LookupType({"a": 10, "b": 20})的缓存键将为>>> LookupType({"a": 10, "b": 20})._static_cache_key (<class '__main__.LookupType'>, ('lookup', (('a', 10), ('b', 20))))
在 1.4.14 版本中添加: - 添加了
cache_ok标志,以允许对TypeDecorator类的缓存进行一些配置。在 1.4.28 版本中添加: - 添加了
ExternalType混合类,该混合类将cache_ok标志推广到TypeDecorator和UserDefinedType类。参见