From 5c9a6f2c7e62500a0dce9ffc2e09c906cf43089b Mon Sep 17 00:00:00 2001 From: Yusur Princeps Date: Sun, 23 Nov 2025 19:13:14 +0100 Subject: [PATCH] 0.10.0 add peewee.SnowflakeField() --- CHANGELOG.md | 4 ++++ src/suou/__init__.py | 2 +- src/suou/peewee.py | 27 ++++++++++++++++++++++++--- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 150dfde..56d279b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 0.10.0 + ++ `peewee`: add `SnowflakeField` class + ## 0.9.0 + Fix to make experimental `Waiter` usable diff --git a/src/suou/__init__.py b/src/suou/__init__.py index a7c96c8..df20b7b 100644 --- a/src/suou/__init__.py +++ b/src/suou/__init__.py @@ -37,7 +37,7 @@ from .redact import redact_url_password from .http import WantsContentType from .color import chalk, WebColor -__version__ = "0.9.0" +__version__ = "0.10.0" __all__ = ( 'ConfigOptions', 'ConfigParserConfigSource', 'ConfigSource', 'ConfigValue', diff --git a/src/suou/peewee.py b/src/suou/peewee.py index f1a3f1e..830ffaf 100644 --- a/src/suou/peewee.py +++ b/src/suou/peewee.py @@ -18,10 +18,11 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. from contextvars import ContextVar from typing import Iterable from playhouse.shortcuts import ReconnectMixin -from peewee import CharField, Database, MySQLDatabase, _ConnectionState +from peewee import BigIntegerField, CharField, Database, MySQLDatabase, _ConnectionState import re from suou.iding import Siq +from suou.snowflake import Snowflake from .codecs import StringCase @@ -117,6 +118,26 @@ class SiqField(Field): def python_value(self, value: bytes) -> Siq: return Siq.from_bytes(value) -# Optional dependency: do not import into __init__.py -__all__ = ('connect_reconnect', 'RegexCharField', 'SiqField') + +class SnowflakeField(BigIntegerField): + ''' + Field holding a snowflake. + + Stored as bigint. + + XXX UNTESTED! + ''' + field_type = 'bigint' + + def db_value(self, value: int | Snowflake) -> int: + if isinstance(value, Snowflake): + value = int(value) + if not isinstance(value, int): + raise TypeError + return value + def python_value(self, value: int) -> Snowflake: + return Snowflake(value) + +# Optional dependency: do not import into __init__.py +__all__ = ('connect_reconnect', 'RegexCharField', 'SiqField', 'Snowflake')