This commit is contained in:
kgod
2026-05-26 21:02:17 +08:00
commit 8697477a53
10000 changed files with 1541403 additions and 0 deletions
@@ -0,0 +1,92 @@
from uuid import (
NAMESPACE_DNS,
NAMESPACE_OID,
NAMESPACE_URL,
NAMESPACE_X500,
RESERVED_FUTURE,
RESERVED_MICROSOFT,
RESERVED_NCS,
RFC_4122,
UUID,
SafeUUID,
getnode,
)
import uuid_utils
from uuid_utils import _uuid4_int, _uuid7_int
NIL = UUID("00000000-0000-0000-0000-000000000000")
MAX = UUID("ffffffff-ffff-ffff-ffff-ffffffffffff")
def _from_int(n: int) -> UUID:
u = object.__new__(UUID)
object.__setattr__(u, "int", n)
object.__setattr__(u, "is_safe", SafeUUID.unknown)
return u
def uuid1(node=None, clock_seq=None):
"""Generate a UUID from a host ID, sequence number, and the current time.
If 'node' is not given, getnode() is used to obtain the hardware
address. If 'clock_seq' is given, it is used as the sequence number;
otherwise a random 14-bit sequence number is chosen."""
return _from_int(uuid_utils.uuid1(node, clock_seq).int)
def uuid3(namespace, name):
"""Generate a UUID from the MD5 hash of a namespace UUID and a name."""
namespace = uuid_utils.UUID(namespace.hex) if namespace else namespace
return _from_int(uuid_utils.uuid3(namespace, name).int)
def uuid4():
"""Generate a random UUID."""
return _from_int(_uuid4_int())
def uuid5(namespace, name):
"""Generate a UUID from the SHA-1 hash of a namespace UUID and a name."""
namespace = uuid_utils.UUID(namespace.hex) if namespace else namespace
return _from_int(uuid_utils.uuid5(namespace, name).int)
def uuid6(node=None, timestamp=None):
"""Generate a version 6 UUID using the given timestamp and a host ID.
This is similar to version 1 UUIDs,
except that it is lexicographically sortable by timestamp.
"""
return _from_int(uuid_utils.uuid6(node, timestamp).int)
def uuid7(timestamp=None, nanos=None):
"""Generate a version 7 UUID using a time value and random bytes."""
return _from_int(_uuid7_int(timestamp, nanos))
def uuid8(bytes):
"""Generate a custom UUID comprised almost entirely of user-supplied bytes."""
return _from_int(uuid_utils.uuid8(bytes).int)
__all__ = [
"MAX",
"NAMESPACE_DNS",
"NAMESPACE_OID",
"NAMESPACE_URL",
"NAMESPACE_X500",
"NIL",
"RESERVED_FUTURE",
"RESERVED_MICROSOFT",
"RESERVED_NCS",
"RFC_4122",
"UUID",
"getnode",
"uuid1",
"uuid3",
"uuid4",
"uuid5",
"uuid6",
"uuid7",
"uuid8",
]
@@ -0,0 +1,79 @@
import sys
from typing import Final
from uuid import (
NAMESPACE_DNS,
NAMESPACE_OID,
NAMESPACE_URL,
NAMESPACE_X500,
RESERVED_FUTURE,
RESERVED_MICROSOFT,
RESERVED_NCS,
RFC_4122,
UUID,
SafeUUID,
getnode,
)
def uuid1(node: int | None = None, clock_seq: int | None = None) -> UUID:
"""Generate a UUID from a host ID, sequence number, and the current time.
If 'node' is not given, getnode() is used to obtain the hardware
address. If 'clock_seq' is given, it is used as the sequence number;
otherwise a random 14-bit sequence number is chosen."""
...
if sys.version_info >= (3, 12):
def uuid3(namespace: UUID, name: str | bytes) -> UUID:
"""Generate a UUID from the MD5 hash of a namespace UUID and a name."""
...
else:
def uuid3(namespace: UUID, name: str) -> UUID:
"""Generate a UUID from the MD5 hash of a namespace UUID and a name."""
...
def uuid4() -> UUID:
"""Generate a random UUID."""
...
if sys.version_info >= (3, 12):
def uuid5(namespace: UUID, name: str | bytes) -> UUID:
"""Generate a UUID from the SHA-1 hash of a namespace UUID and a name."""
...
else:
def uuid5(namespace: UUID, name: str) -> UUID:
"""Generate a UUID from the SHA-1 hash of a namespace UUID and a name."""
...
def uuid6(node: int | None = None, timestamp: int | None = None) -> UUID:
"""Generate a version 6 UUID using the given timestamp and a host ID.
This is similar to version 1 UUIDs,
except that it is lexicographically sortable by timestamp.
"""
...
def uuid7(timestamp: int | None = None, nanos: int | None = None) -> UUID:
"""Generate a version 7 UUID using a time value and random bytes."""
...
def uuid8(bytes: bytes) -> UUID:
"""Generate a custom UUID comprised almost entirely of user-supplied bytes."""
...
NIL: Final[UUID]
MAX: Final[UUID]
__all__ = [
"MAX",
"NAMESPACE_DNS",
"NAMESPACE_OID",
"NAMESPACE_URL",
"NAMESPACE_X500",
"NIL",
"RESERVED_FUTURE",
"RESERVED_MICROSOFT",
"RESERVED_NCS",
"RFC_4122",
"UUID",
"SafeUUID",
"getnode",
]