| 1 |
import array |
| 2 |
from random import getrandbits |
| 3 |
from time import time |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
_DECODE = array.array( |
| 8 |
"B", |
| 9 |
( |
| 10 |
0xFF, |
| 11 |
0xFF, |
| 12 |
0xFF, |
| 13 |
0xFF, |
| 14 |
0xFF, |
| 15 |
0xFF, |
| 16 |
0xFF, |
| 17 |
0xFF, |
| 18 |
0xFF, |
| 19 |
0xFF, |
| 20 |
0xFF, |
| 21 |
0xFF, |
| 22 |
0xFF, |
| 23 |
0xFF, |
| 24 |
0xFF, |
| 25 |
0xFF, |
| 26 |
0xFF, |
| 27 |
0xFF, |
| 28 |
0xFF, |
| 29 |
0xFF, |
| 30 |
0xFF, |
| 31 |
0xFF, |
| 32 |
0xFF, |
| 33 |
0xFF, |
| 34 |
0xFF, |
| 35 |
0xFF, |
| 36 |
0xFF, |
| 37 |
0xFF, |
| 38 |
0xFF, |
| 39 |
0xFF, |
| 40 |
0xFF, |
| 41 |
0xFF, |
| 42 |
0xFF, |
| 43 |
0xFF, |
| 44 |
0xFF, |
| 45 |
0xFF, |
| 46 |
0xFF, |
| 47 |
0xFF, |
| 48 |
0xFF, |
| 49 |
0xFF, |
| 50 |
0xFF, |
| 51 |
0xFF, |
| 52 |
0xFF, |
| 53 |
0xFF, |
| 54 |
0xFF, |
| 55 |
0xFF, |
| 56 |
0xFF, |
| 57 |
0xFF, |
| 58 |
0x00, |
| 59 |
0x01, |
| 60 |
0x02, |
| 61 |
0x03, |
| 62 |
0x04, |
| 63 |
0x05, |
| 64 |
0x06, |
| 65 |
0x07, |
| 66 |
0x08, |
| 67 |
0x09, |
| 68 |
0xFF, |
| 69 |
0xFF, |
| 70 |
0xFF, |
| 71 |
0xFF, |
| 72 |
0xFF, |
| 73 |
0xFF, |
| 74 |
0xFF, |
| 75 |
0x0A, |
| 76 |
0x0B, |
| 77 |
0x0C, |
| 78 |
0x0D, |
| 79 |
0x0E, |
| 80 |
0x0F, |
| 81 |
0x10, |
| 82 |
0x11, |
| 83 |
0x01, |
| 84 |
0x12, |
| 85 |
0x13, |
| 86 |
0x01, |
| 87 |
0x14, |
| 88 |
0x15, |
| 89 |
0x00, |
| 90 |
0x16, |
| 91 |
0x17, |
| 92 |
0x18, |
| 93 |
0x19, |
| 94 |
0x1A, |
| 95 |
0xFF, |
| 96 |
0x1B, |
| 97 |
0x1C, |
| 98 |
0x1D, |
| 99 |
0x1E, |
| 100 |
0x1F, |
| 101 |
0xFF, |
| 102 |
0xFF, |
| 103 |
0xFF, |
| 104 |
0xFF, |
| 105 |
0xFF, |
| 106 |
0xFF, |
| 107 |
0x0A, |
| 108 |
0x0B, |
| 109 |
0x0C, |
| 110 |
0x0D, |
| 111 |
0x0E, |
| 112 |
0x0F, |
| 113 |
0x10, |
| 114 |
0x11, |
| 115 |
0x01, |
| 116 |
0x12, |
| 117 |
0x13, |
| 118 |
0x01, |
| 119 |
0x14, |
| 120 |
0x15, |
| 121 |
0x00, |
| 122 |
0x16, |
| 123 |
0x17, |
| 124 |
0x18, |
| 125 |
0x19, |
| 126 |
0x1A, |
| 127 |
0xFF, |
| 128 |
0x1B, |
| 129 |
0x1C, |
| 130 |
0x1D, |
| 131 |
0x1E, |
| 132 |
0x1F, |
| 133 |
0xFF, |
| 134 |
0xFF, |
| 135 |
0xFF, |
| 136 |
0xFF, |
| 137 |
0xFF, |
| 138 |
0xFF, |
| 139 |
0xFF, |
| 140 |
0xFF, |
| 141 |
0xFF, |
| 142 |
0xFF, |
| 143 |
0xFF, |
| 144 |
0xFF, |
| 145 |
0xFF, |
| 146 |
0xFF, |
| 147 |
0xFF, |
| 148 |
0xFF, |
| 149 |
0xFF, |
| 150 |
0xFF, |
| 151 |
0xFF, |
| 152 |
0xFF, |
| 153 |
0xFF, |
| 154 |
0xFF, |
| 155 |
0xFF, |
| 156 |
0xFF, |
| 157 |
0xFF, |
| 158 |
0xFF, |
| 159 |
0xFF, |
| 160 |
0xFF, |
| 161 |
0xFF, |
| 162 |
0xFF, |
| 163 |
0xFF, |
| 164 |
0xFF, |
| 165 |
0xFF, |
| 166 |
0xFF, |
| 167 |
0xFF, |
| 168 |
0xFF, |
| 169 |
0xFF, |
| 170 |
0xFF, |
| 171 |
0xFF, |
| 172 |
0xFF, |
| 173 |
0xFF, |
| 174 |
0xFF, |
| 175 |
0xFF, |
| 176 |
0xFF, |
| 177 |
0xFF, |
| 178 |
0xFF, |
| 179 |
0xFF, |
| 180 |
0xFF, |
| 181 |
0xFF, |
| 182 |
0xFF, |
| 183 |
0xFF, |
| 184 |
0xFF, |
| 185 |
0xFF, |
| 186 |
0xFF, |
| 187 |
0xFF, |
| 188 |
0xFF, |
| 189 |
0xFF, |
| 190 |
0xFF, |
| 191 |
0xFF, |
| 192 |
0xFF, |
| 193 |
0xFF, |
| 194 |
0xFF, |
| 195 |
0xFF, |
| 196 |
0xFF, |
| 197 |
0xFF, |
| 198 |
0xFF, |
| 199 |
0xFF, |
| 200 |
0xFF, |
| 201 |
0xFF, |
| 202 |
0xFF, |
| 203 |
0xFF, |
| 204 |
0xFF, |
| 205 |
0xFF, |
| 206 |
0xFF, |
| 207 |
0xFF, |
| 208 |
0xFF, |
| 209 |
0xFF, |
| 210 |
0xFF, |
| 211 |
0xFF, |
| 212 |
0xFF, |
| 213 |
0xFF, |
| 214 |
0xFF, |
| 215 |
0xFF, |
| 216 |
0xFF, |
| 217 |
0xFF, |
| 218 |
0xFF, |
| 219 |
0xFF, |
| 220 |
0xFF, |
| 221 |
0xFF, |
| 222 |
0xFF, |
| 223 |
0xFF, |
| 224 |
0xFF, |
| 225 |
0xFF, |
| 226 |
0xFF, |
| 227 |
0xFF, |
| 228 |
0xFF, |
| 229 |
0xFF, |
| 230 |
0xFF, |
| 231 |
0xFF, |
| 232 |
0xFF, |
| 233 |
0xFF, |
| 234 |
0xFF, |
| 235 |
0xFF, |
| 236 |
0xFF, |
| 237 |
0xFF, |
| 238 |
0xFF, |
| 239 |
0xFF, |
| 240 |
0xFF, |
| 241 |
0xFF, |
| 242 |
0xFF, |
| 243 |
0xFF, |
| 244 |
0xFF, |
| 245 |
0xFF, |
| 246 |
0xFF, |
| 247 |
0xFF, |
| 248 |
0xFF, |
| 249 |
0xFF, |
| 250 |
0xFF, |
| 251 |
0xFF, |
| 252 |
0xFF, |
| 253 |
0xFF, |
| 254 |
0xFF, |
| 255 |
0xFF, |
| 256 |
0xFF, |
| 257 |
0xFF, |
| 258 |
0xFF, |
| 259 |
0xFF, |
| 260 |
0xFF, |
| 261 |
0xFF, |
| 262 |
0xFF, |
| 263 |
0xFF, |
| 264 |
0xFF, |
| 265 |
0xFF, |
| 266 |
), |
| 267 |
) |
| 268 |
|
| 269 |
|
| 270 |
def ulid_hex() -> str: |
| 271 |
""" |
| 272 |
Generate a ULID in lowercase hex that will work for a UUID. |
| 273 |
|
| 274 |
This ulid should not be used for cryptographically secure |
| 275 |
operations. |
| 276 |
|
| 277 |
This string can be converted with https://github.com/ahawker/ulid |
| 278 |
|
| 279 |
ulid.from_uuid(uuid.UUID(ulid_hex)) |
| 280 |
""" |
| 281 |
return f"{int(time() * 1000):012x}{getrandbits(80):020x}" |
| 282 |
|
| 283 |
|
| 284 |
def ulid_at_time_bytes(timestamp: float) -> bytes: |
| 285 |
""" |
| 286 |
Generate an ULID as 16 bytes that will work for a UUID. |
| 287 |
|
| 288 |
uuid.UUID(bytes=ulid_bytes) |
| 289 |
""" |
| 290 |
if not isinstance(timestamp, (int, float)): |
| 291 |
msg = f"must be real number, not {type(timestamp).__name__}" |
| 292 |
raise TypeError(msg) |
| 293 |
ms = int(timestamp * 1000) |
| 294 |
if ms == 0 and timestamp < 0: |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
msg = "can't convert negative int to unsigned" |
| 302 |
raise OverflowError(msg) |
| 303 |
return ms.to_bytes(6, byteorder="big") + int(getrandbits(80)).to_bytes( |
| 304 |
10, byteorder="big" |
| 305 |
) |
| 306 |
|
| 307 |
|
| 308 |
def ulid_now_bytes() -> bytes: |
| 309 |
"""Generate an ULID as 16 bytes that will work for a UUID.""" |
| 310 |
return ulid_at_time_bytes(time()) |
| 311 |
|
| 312 |
|
| 313 |
def ulid_now() -> str: |
| 314 |
"""Generate a ULID.""" |
| 315 |
return ulid_at_time(time()) |
| 316 |
|
| 317 |
|
| 318 |
def ulid_at_time(timestamp: float) -> str: |
| 319 |
""" |
| 320 |
Generate a ULID. |
| 321 |
|
| 322 |
This ulid should not be used for cryptographically secure |
| 323 |
operations. |
| 324 |
|
| 325 |
01AN4Z07BY 79KA1307SR9X4MV3 |
| 326 |
|----------| |----------------| |
| 327 |
Timestamp Randomness |
| 328 |
48bits 80bits |
| 329 |
|
| 330 |
This string can be loaded directly with https://github.com/ahawker/ulid |
| 331 |
|
| 332 |
import ulid_transform as ulid_util |
| 333 |
import ulid |
| 334 |
ulid.parse(ulid_util.ulid()) |
| 335 |
""" |
| 336 |
return _encode(ulid_at_time_bytes(timestamp)) |
| 337 |
|
| 338 |
|
| 339 |
def _encode(ulid_bytes: bytes) -> str: |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
enc = "0123456789ABCDEFGHJKMNPQRSTVWXYZ" |
| 346 |
return ( |
| 347 |
enc[(ulid_bytes[0] & 224) >> 5] |
| 348 |
+ enc[ulid_bytes[0] & 31] |
| 349 |
+ enc[(ulid_bytes[1] & 248) >> 3] |
| 350 |
+ enc[((ulid_bytes[1] & 7) << 2) | ((ulid_bytes[2] & 192) >> 6)] |
| 351 |
+ enc[((ulid_bytes[2] & 62) >> 1)] |
| 352 |
+ enc[((ulid_bytes[2] & 1) << 4) | ((ulid_bytes[3] & 240) >> 4)] |
| 353 |
+ enc[((ulid_bytes[3] & 15) << 1) | ((ulid_bytes[4] & 128) >> 7)] |
| 354 |
+ enc[(ulid_bytes[4] & 124) >> 2] |
| 355 |
+ enc[((ulid_bytes[4] & 3) << 3) | ((ulid_bytes[5] & 224) >> 5)] |
| 356 |
+ enc[ulid_bytes[5] & 31] |
| 357 |
+ enc[(ulid_bytes[6] & 248) >> 3] |
| 358 |
+ enc[((ulid_bytes[6] & 7) << 2) | ((ulid_bytes[7] & 192) >> 6)] |
| 359 |
+ enc[(ulid_bytes[7] & 62) >> 1] |
| 360 |
+ enc[((ulid_bytes[7] & 1) << 4) | ((ulid_bytes[8] & 240) >> 4)] |
| 361 |
+ enc[((ulid_bytes[8] & 15) << 1) | ((ulid_bytes[9] & 128) >> 7)] |
| 362 |
+ enc[(ulid_bytes[9] & 124) >> 2] |
| 363 |
+ enc[((ulid_bytes[9] & 3) << 3) | ((ulid_bytes[10] & 224) >> 5)] |
| 364 |
+ enc[ulid_bytes[10] & 31] |
| 365 |
+ enc[(ulid_bytes[11] & 248) >> 3] |
| 366 |
+ enc[((ulid_bytes[11] & 7) << 2) | ((ulid_bytes[12] & 192) >> 6)] |
| 367 |
+ enc[(ulid_bytes[12] & 62) >> 1] |
| 368 |
+ enc[((ulid_bytes[12] & 1) << 4) | ((ulid_bytes[13] & 240) >> 4)] |
| 369 |
+ enc[((ulid_bytes[13] & 15) << 1) | ((ulid_bytes[14] & 128) >> 7)] |
| 370 |
+ enc[(ulid_bytes[14] & 124) >> 2] |
| 371 |
+ enc[((ulid_bytes[14] & 3) << 3) | ((ulid_bytes[15] & 224) >> 5)] |
| 372 |
+ enc[ulid_bytes[15] & 31] |
| 373 |
) |
| 374 |
|
| 375 |
|
| 376 |
def ulid_to_bytes(value: str) -> bytes: |
| 377 |
"""Decode a ulid to bytes.""" |
| 378 |
if not isinstance(value, str): |
| 379 |
msg = f"ULID must be a string, not {type(value).__name__}" |
| 380 |
raise TypeError(msg) |
| 381 |
if len(value) != 26 or not value.isascii(): |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
msg = f"ULID must be a 26 character string: {value!r}" |
| 392 |
raise ValueError(msg) |
| 393 |
encoded = value.encode("ascii") |
| 394 |
decoding = _DECODE |
| 395 |
return bytes( |
| 396 |
( |
| 397 |
((decoding[encoded[0]] << 5) | decoding[encoded[1]]) & 0xFF, |
| 398 |
((decoding[encoded[2]] << 3) | (decoding[encoded[3]] >> 2)) & 0xFF, |
| 399 |
( |
| 400 |
(decoding[encoded[3]] << 6) |
| 401 |
| (decoding[encoded[4]] << 1) |
| 402 |
| (decoding[encoded[5]] >> 4) |
| 403 |
) |
| 404 |
& 0xFF, |
| 405 |
((decoding[encoded[5]] << 4) | (decoding[encoded[6]] >> 1)) & 0xFF, |
| 406 |
( |
| 407 |
(decoding[encoded[6]] << 7) |
| 408 |
| (decoding[encoded[7]] << 2) |
| 409 |
| (decoding[encoded[8]] >> 3) |
| 410 |
) |
| 411 |
& 0xFF, |
| 412 |
((decoding[encoded[8]] << 5) | (decoding[encoded[9]])) & 0xFF, |
| 413 |
((decoding[encoded[10]] << 3) | (decoding[encoded[11]] >> 2)) & 0xFF, |
| 414 |
( |
| 415 |
(decoding[encoded[11]] << 6) |
| 416 |
| (decoding[encoded[12]] << 1) |
| 417 |
| (decoding[encoded[13]] >> 4) |
| 418 |
) |
| 419 |
& 0xFF, |
| 420 |
((decoding[encoded[13]] << 4) | (decoding[encoded[14]] >> 1)) & 0xFF, |
| 421 |
( |
| 422 |
(decoding[encoded[14]] << 7) |
| 423 |
| (decoding[encoded[15]] << 2) |
| 424 |
| (decoding[encoded[16]] >> 3) |
| 425 |
) |
| 426 |
& 0xFF, |
| 427 |
((decoding[encoded[16]] << 5) | (decoding[encoded[17]])) & 0xFF, |
| 428 |
((decoding[encoded[18]] << 3) | (decoding[encoded[19]] >> 2)) & 0xFF, |
| 429 |
( |
| 430 |
(decoding[encoded[19]] << 6) |
| 431 |
| (decoding[encoded[20]] << 1) |
| 432 |
| (decoding[encoded[21]] >> 4) |
| 433 |
) |
| 434 |
& 0xFF, |
| 435 |
((decoding[encoded[21]] << 4) | (decoding[encoded[22]] >> 1)) & 0xFF, |
| 436 |
( |
| 437 |
(decoding[encoded[22]] << 7) |
| 438 |
| (decoding[encoded[23]] << 2) |
| 439 |
| (decoding[encoded[24]] >> 3) |
| 440 |
) |
| 441 |
& 0xFF, |
| 442 |
((decoding[encoded[24]] << 5) | (decoding[encoded[25]])) & 0xFF, |
| 443 |
) |
| 444 |
) |
| 445 |
|
| 446 |
|
| 447 |
def bytes_to_ulid(value: bytes) -> str: |
| 448 |
"""Encode bytes to a ulid.""" |
| 449 |
if not isinstance(value, bytes): |
| 450 |
msg = f"ULID bytes must be bytes, not {type(value).__name__}" |
| 451 |
raise TypeError(msg) |
| 452 |
if len(value) != 16: |
| 453 |
msg = f"ULID bytes must be 16 bytes: {value!r}" |
| 454 |
raise ValueError(msg) |
| 455 |
return _encode(value) |
| 456 |
|
| 457 |
|
| 458 |
def ulid_to_bytes_or_none(ulid: str | None) -> bytes | None: |
| 459 |
"""Convert an ulid to bytes.""" |
| 460 |
if not isinstance(ulid, str) or len(ulid) != 26: |
| 461 |
return None |
| 462 |
try: |
| 463 |
return ulid_to_bytes(ulid) |
| 464 |
except (ValueError, UnicodeEncodeError): |
| 465 |
return None |
| 466 |
|
| 467 |
|
| 468 |
def bytes_to_ulid_or_none(ulid_bytes: bytes | None) -> str | None: |
| 469 |
"""Convert bytes to a ulid.""" |
| 470 |
if not isinstance(ulid_bytes, bytes) or len(ulid_bytes) != 16: |
| 471 |
return None |
| 472 |
return bytes_to_ulid(ulid_bytes) |
| 473 |
|
| 474 |
|
| 475 |
def ulid_to_timestamp(ulid: str | bytes) -> int: |
| 476 |
""" |
| 477 |
Get the timestamp from a ULID. |
| 478 |
The returned value is in milliseconds since the UNIX epoch. |
| 479 |
""" |
| 480 |
if isinstance(ulid, bytes): |
| 481 |
if len(ulid) != 16: |
| 482 |
msg = f"ULID bytes must be 16 bytes: {ulid!r}" |
| 483 |
raise ValueError(msg) |
| 484 |
ulid_bytes = ulid |
| 485 |
elif isinstance(ulid, str): |
| 486 |
ulid_bytes = ulid_to_bytes(ulid) |
| 487 |
else: |
| 488 |
msg = f"ULID must be a string or bytes, not {type(ulid).__name__}" |
| 489 |
raise TypeError(msg) |
| 490 |
return int.from_bytes(b"\x00\x00" + ulid_bytes[:6], "big") |