From a662c02a2c2e4be4b837a72ab371fcef847ae932 Mon Sep 17 00:00:00 2001 From: WH-2099 Date: Fri, 2 May 2025 16:34:59 +0800 Subject: [PATCH] optimize property to cached_property --- ulid/__init__.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ulid/__init__.py b/ulid/__init__.py index 96178e3..d5107bb 100644 --- a/ulid/__init__.py +++ b/ulid/__init__.py @@ -190,7 +190,7 @@ def parse(cls: type[U], value: Any) -> U: return cls.from_bytes(value) raise TypeError(f"Cannot parse ULID from type {type(value)}") - @property + @functools.cached_property def milliseconds(self) -> int: """The timestamp part as epoch time in milliseconds. @@ -201,7 +201,7 @@ def milliseconds(self) -> int: """ return int.from_bytes(self.bytes[: constants.TIMESTAMP_LEN], byteorder="big") - @property + @functools.cached_property def timestamp(self) -> float: """The timestamp part as epoch time in seconds. @@ -212,7 +212,7 @@ def timestamp(self) -> float: """ return self.milliseconds / constants.MILLISECS_IN_SECS - @property + @functools.cached_property def datetime(self) -> datetime: """Return the timestamp part as timezone-aware :class:`datetime` in UTC. @@ -223,7 +223,7 @@ def datetime(self) -> datetime: """ return datetime.fromtimestamp(self.timestamp, timezone.utc) - @property + @functools.cached_property def hex(self) -> str: """Encode the :class:`ULID`-object as a 32 char sequence of hex values.""" return self.bytes.hex()