ulid-transform 0.10.2__tar.gz → 0.11.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: ulid-transform
3
- Version: 0.10.2
3
+ Version: 0.11.1
4
4
  Summary: Create and transform ULIDs
5
5
  Home-page: https://github.com/bdraco/ulid-transform
6
6
  License: MIT
@@ -1,5 +1,6 @@
1
1
  """Build optional cython modules."""
2
2
 
3
+ import logging
3
4
  import os
4
5
  from distutils.command.build_ext import build_ext
5
6
  from os.path import join
@@ -10,6 +11,16 @@ try:
10
11
  except ImportError:
11
12
  from distutils.core import Extension
12
13
 
14
+
15
+ def getenv_bool(key: str, default: bool = False) -> bool:
16
+ value = os.environ.get(key, str(default)).lower()
17
+ if value in ("1", "true", "yes"):
18
+ return True
19
+ if value in ("0", "false", "no"):
20
+ return False
21
+ raise ValueError(f"Invalid value for boolean envvar {key}: {value}")
22
+
23
+
13
24
  ulid_module = Extension(
14
25
  "ulid_transform._ulid_impl",
15
26
  [
@@ -27,11 +38,13 @@ class BuildExt(build_ext):
27
38
  try:
28
39
  super().build_extensions()
29
40
  except Exception: # nosec
30
- pass
41
+ logging.exception("Failed to build extensions")
42
+ if getenv_bool("REQUIRE_CYTHON"):
43
+ raise
31
44
 
32
45
 
33
46
  def build(setup_kwargs: Any) -> None:
34
- if os.environ.get("SKIP_CYTHON", False):
47
+ if getenv_bool("SKIP_CYTHON"):
35
48
  return
36
49
  try:
37
50
  from Cython.Build import cythonize
@@ -43,6 +56,7 @@ def build(setup_kwargs: Any) -> None:
43
56
  ulid_module,
44
57
  ],
45
58
  compiler_directives={"language_level": "3"}, # Python 3
59
+ verbose=True,
46
60
  ),
47
61
  cmdclass=dict(build_ext=BuildExt),
48
62
  )
@@ -51,6 +65,6 @@ def build(setup_kwargs: Any) -> None:
51
65
  pkg: ["_ulid_impl.cpp"] for pkg in setup_kwargs["packages"]
52
66
  }
53
67
  except Exception:
54
- if os.environ.get("REQUIRE_CYTHON"):
68
+ logging.exception("Failed to configure cython")
69
+ if getenv_bool("REQUIRE_CYTHON"):
55
70
  raise
56
- pass
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "ulid-transform"
3
- version = "0.10.2"
3
+ version = "0.11.1"
4
4
  description = "Create and transform ULIDs"
5
5
  authors = ["J. Nick Koston <nick@koston.org>"]
6
6
  license = "MIT"
@@ -34,6 +34,12 @@ pytest-cov = "^3.0"
34
34
  Cython = ">=3.0.5"
35
35
  setuptools = "^65.4.1"
36
36
 
37
+
38
+ [tool.poetry.group.benchmark.dependencies]
39
+ ulid-py = "^1.1.0"
40
+ ulid2 = "^0.3.0"
41
+ pytest-benchmark = "^4.0.0"
42
+
37
43
  [tool.semantic_release]
38
44
  branch = "main"
39
45
  version_toml = "pyproject.toml:tool.poetry.version"
@@ -41,7 +47,6 @@ version_variable = "src/ulid_transform/__init__.py:__version__"
41
47
  build_command = "pip install poetry && poetry build"
42
48
 
43
49
  [tool.pytest.ini_options]
44
- addopts = "-v -Wdefault --cov=ulid_transform --cov-report=term-missing:skip-covered"
45
50
  pythonpath = ["src"]
46
51
 
47
52
  [tool.coverage.run]
@@ -78,6 +83,9 @@ exclude = [
78
83
  module = "tests.*"
79
84
  allow_untyped_defs = true
80
85
 
86
+ [[tool.mypy.overrides]]
87
+ module = "bench.*"
88
+ allow_untyped_defs = true
81
89
 
82
90
  [build-system]
83
91
  requires = ['setuptools>=65.4.1', 'wheel', 'Cython>=3.0.2', "poetry-core>=1.0.0"]
@@ -12,7 +12,7 @@ package_data = \
12
12
 
13
13
  setup_kwargs = {
14
14
  'name': 'ulid-transform',
15
- 'version': '0.10.2',
15
+ 'version': '0.11.1',
16
16
  'description': 'Create and transform ULIDs',
17
17
  'long_description': '# Fast ULID transformations\n\n<p align="center">\n <a href="https://github.com/bdraco/ulid-transform/actions/workflows/ci.yml?query=branch%3Amain">\n <img src="https://img.shields.io/github/actions/workflow/status/bdraco/ulid-transform/ci.yml?branch=main&label=CI&logo=github&style=flat-square" alt="CI Status" >\n </a>\n <a href="https://codecov.io/gh/bdraco/ulid-transform">\n <img src="https://img.shields.io/codecov/c/github/bdraco/ulid-transform.svg?logo=codecov&logoColor=fff&style=flat-square" alt="Test coverage percentage">\n </a>\n</p>\n<p align="center">\n <a href="https://python-poetry.org/">\n <img src="https://img.shields.io/badge/packaging-poetry-299bd7?style=flat-square&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA4AAAASCAYAAABrXO8xAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAJJSURBVHgBfZLPa1NBEMe/s7tNXoxW1KJQKaUHkXhQvHgW6UHQQ09CBS/6V3hKc/AP8CqCrUcpmop3Cx48eDB4yEECjVQrlZb80CRN8t6OM/teagVxYZi38+Yz853dJbzoMV3MM8cJUcLMSUKIE8AzQ2PieZzFxEJOHMOgMQQ+dUgSAckNXhapU/NMhDSWLs1B24A8sO1xrN4NECkcAC9ASkiIJc6k5TRiUDPhnyMMdhKc+Zx19l6SgyeW76BEONY9exVQMzKExGKwwPsCzza7KGSSWRWEQhyEaDXp6ZHEr416ygbiKYOd7TEWvvcQIeusHYMJGhTwF9y7sGnSwaWyFAiyoxzqW0PM/RjghPxF2pWReAowTEXnDh0xgcLs8l2YQmOrj3N7ByiqEoH0cARs4u78WgAVkoEDIDoOi3AkcLOHU60RIg5wC4ZuTC7FaHKQm8Hq1fQuSOBvX/sodmNJSB5geaF5CPIkUeecdMxieoRO5jz9bheL6/tXjrwCyX/UYBUcjCaWHljx1xiX6z9xEjkYAzbGVnB8pvLmyXm9ep+W8CmsSHQQY77Zx1zboxAV0w7ybMhQmfqdmmw3nEp1I0Z+FGO6M8LZdoyZnuzzBdjISicKRnpxzI9fPb+0oYXsNdyi+d3h9bm9MWYHFtPeIZfLwzmFDKy1ai3p+PDls1Llz4yyFpferxjnyjJDSEy9CaCx5m2cJPerq6Xm34eTrZt3PqxYO1XOwDYZrFlH1fWnpU38Y9HRze3lj0vOujZcXKuuXm3jP+s3KbZVra7y2EAAAAAASUVORK5CYII=" alt="Poetry">\n </a>\n <a href="https://github.com/ambv/black">\n <img src="https://img.shields.io/badge/code%20style-black-000000.svg?style=flat-square" alt="black">\n </a>\n <a href="https://github.com/pre-commit/pre-commit">\n <img src="https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white&style=flat-square" alt="pre-commit">\n </a>\n</p>\n<p align="center">\n <a href="https://pypi.org/project/ulid-transform/">\n <img src="https://img.shields.io/pypi/v/ulid-transform.svg?logo=python&logoColor=fff&style=flat-square" alt="PyPI Version">\n </a>\n <img src="https://img.shields.io/pypi/pyversions/ulid-transform.svg?style=flat-square&logo=python&amp;logoColor=fff" alt="Supported Python versions">\n <img src="https://img.shields.io/pypi/l/ulid-transform.svg?style=flat-square" alt="License">\n</p>\n\nCreate and transform ULIDs\n\nThis library will use the CPP implementation from https://github.com/suyash/ulid if cython is available, and will fallback to pure python if it is not.\n\n## Example\n\n```python\n>>> import ulid_transform\n>>> ulid_transform.ulid_hex()\n\'01869a2ea5fb0b43aa056293e47c0a35\'\n>>> ulid_transform.ulid_now()\n\'0001HZX0NW00GW0X476W5TVBFE\'\n>>> ulid_transform.ulid_at_time(1234)\n\'000000016JC62D620DGYNG2R8H\'\n>>> ulid_transform.ulid_to_bytes(\'0001HZX0NW00GW0X476W5TVBFE\')\nb\'\\x00\\x00c\\xfe\\x82\\xbc\\x00!\\xc0t\\x877\\x0b\\xad\\xad\\xee\'\n>> ulid_transform.bytes_to_ulid(b"\\x01\\x86\\x99?\\xe8\\xf3\\x11\\xbc\\xed\\xef\\x86U.9\\x03z")\n\'01GTCKZT7K26YEVVW6AMQ3J0VT\'\n>>> ulid_transform.ulid_to_bytes_or_none(\'0001HZX0NW00GW0X476W5TVBFE\')\nb\'\\x00\\x00c\\xfe\\x82\\xbc\\x00!\\xc0t\\x877\\x0b\\xad\\xad\\xee\'\n>>> ulid_transform.ulid_to_bytes_or_none(None)\n>>> ulid_transform.bytes_to_ulid_or_none(b\'\\x00\\x00c\\xfe\\x82\\xbc\\x00!\\xc0t\\x877\\x0b\\xad\\xad\\xee\')\n\'0001HZX0NW00GW0X476W5TVBFE\'\n>>> ulid_transform.bytes_to_ulid_or_none(None)\n```\n\n## Installation\n\nInstall this via pip (or your favourite package manager):\n\n`pip install ulid-transform`\n\n## Contributors ✨\n\nThanks to https://github.com/suyash/ulid which provides the cython implementation guts.\n\nThanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):\n\n<!-- prettier-ignore-start -->\n<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->\n<!-- markdownlint-disable -->\n<!-- markdownlint-enable -->\n<!-- ALL-CONTRIBUTORS-LIST:END -->\n<!-- prettier-ignore-end -->\n\nThis project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!\n\n## Credits\n\nThis package was created with\n[Copier](https://copier.readthedocs.io/) and the\n[browniebroke/pypackage-template](https://github.com/browniebroke/pypackage-template)\nproject template.\n',
18
18
  'author': 'J. Nick Koston',
@@ -1,21 +1,25 @@
1
- __version__ = "0.10.2"
1
+ __version__ = "0.11.1"
2
2
 
3
3
  from .ulid_impl import (
4
4
  bytes_to_ulid,
5
5
  bytes_to_ulid_or_none,
6
6
  ulid_at_time,
7
+ ulid_at_time_bytes,
7
8
  ulid_hex,
8
9
  ulid_now,
10
+ ulid_now_bytes,
9
11
  ulid_to_bytes,
10
12
  ulid_to_bytes_or_none,
11
13
  )
12
14
 
13
15
  __all__ = [
14
- "ulid_now",
16
+ "bytes_to_ulid",
17
+ "bytes_to_ulid_or_none",
15
18
  "ulid_at_time",
19
+ "ulid_at_time_bytes",
16
20
  "ulid_hex",
21
+ "ulid_now",
22
+ "ulid_now_bytes",
17
23
  "ulid_to_bytes",
18
- "bytes_to_ulid",
19
24
  "ulid_to_bytes_or_none",
20
- "bytes_to_ulid_or_none",
21
25
  ]
@@ -1,18 +1,23 @@
1
1
  # distutils: language = c++
2
2
  from libcpp.string cimport string
3
+ from libcpp.vector cimport vector
3
4
 
4
- from time import time
5
5
  from typing import Optional
6
6
 
7
- import cython
8
-
9
7
 
10
8
  cdef extern from "ulid_wrapper.h":
11
9
  string _cpp_ulid_at_time(double timestamp)
10
+ vector[unsigned char] _cpp_ulid_at_time_bytes(double timestamp)
12
11
  string _cpp_ulid_to_bytes(const char * ulid_string)
13
12
  string _cpp_ulid()
13
+ vector[unsigned char] _cpp_ulid_bytes()
14
14
  string _cpp_bytes_to_ulid(string ulid_bytes)
15
15
 
16
+ def _ulid_now_bytes() -> bytes:
17
+ return bytes(_cpp_ulid_bytes())
18
+
19
+ def _ulid_at_time_bytes(_time: float) -> bytes:
20
+ return bytes(_cpp_ulid_at_time_bytes(_time))
16
21
 
17
22
  def _ulid_now() -> str:
18
23
  return _cpp_ulid().decode("ascii")
@@ -280,6 +280,21 @@ def ulid_hex() -> str:
280
280
  return f"{int(time()*1000):012x}{getrandbits(80):020x}"
281
281
 
282
282
 
283
+ def ulid_at_time_bytes(timestamp: float) -> bytes:
284
+ """Generate an ULID as 16 bytes that will work for a UUID.
285
+
286
+ uuid.UUID(bytes=ulid_bytes)
287
+ """
288
+ return int(timestamp * 1000).to_bytes(6, byteorder="big") + int(
289
+ getrandbits(80)
290
+ ).to_bytes(10, byteorder="big")
291
+
292
+
293
+ def ulid_now_bytes() -> bytes:
294
+ """Generate an ULID as 16 bytes that will work for a UUID."""
295
+ return ulid_at_time_bytes(time())
296
+
297
+
283
298
  def ulid_now() -> str:
284
299
  """Generate a ULID."""
285
300
  return ulid_at_time(time())
@@ -302,10 +317,7 @@ def ulid_at_time(timestamp: float) -> str:
302
317
  import ulid
303
318
  ulid.parse(ulid_util.ulid())
304
319
  """
305
- return _encode(
306
- int((timestamp) * 1000).to_bytes(6, byteorder="big")
307
- + int(getrandbits(80)).to_bytes(10, byteorder="big")
308
- )
320
+ return _encode(ulid_at_time_bytes(timestamp))
309
321
 
310
322
 
311
323
  def _encode(ulid_bytes: bytes) -> str:
@@ -440,9 +452,15 @@ try:
440
452
  from ._ulid_impl import ( # type: ignore[no-redef] # noqa: F811 F401 # pragma: no cover
441
453
  _ulid_at_time as ulid_at_time,
442
454
  )
455
+ from ._ulid_impl import ( # type: ignore[no-redef] # noqa: F811 F401 # pragma: no cover
456
+ _ulid_at_time_bytes as ulid_at_time_bytes,
457
+ )
443
458
  from ._ulid_impl import ( # type: ignore[no-redef] # noqa: F811 F401 # pragma: no cover
444
459
  _ulid_now as ulid_now,
445
460
  )
461
+ from ._ulid_impl import ( # type: ignore[no-redef] # noqa: F811 F401 # pragma: no cover
462
+ _ulid_now_bytes as ulid_now_bytes,
463
+ )
446
464
  from ._ulid_impl import ( # type: ignore[no-redef] # noqa: F811 F401 # pragma: no cover
447
465
  _ulid_to_bytes as ulid_to_bytes,
448
466
  )
@@ -10,6 +10,13 @@ std::string _cpp_ulid() {
10
10
  return ulid::Marshal(ulid);
11
11
  }
12
12
 
13
+ std::vector<uint8_t> _cpp_ulid_bytes() {
14
+ ulid::ULID ulid;
15
+ ulid::EncodeTimeSystemClockNow(ulid);
16
+ ulid::EncodeEntropyRand(ulid);
17
+ return ulid::MarshalBinary(ulid);
18
+ }
19
+
13
20
  std::string _cpp_ulid_at_time(double epoch_time) {
14
21
  ulid::ULID ulid;
15
22
  ulid::EncodeTimestamp(static_cast<int64_t>(epoch_time*1000), ulid);
@@ -17,6 +24,13 @@ std::string _cpp_ulid_at_time(double epoch_time) {
17
24
  return ulid::Marshal(ulid);
18
25
  }
19
26
 
27
+ std::vector<uint8_t> _cpp_ulid_at_time_bytes(double epoch_time) {
28
+ ulid::ULID ulid;
29
+ ulid::EncodeTimestamp(static_cast<int64_t>(epoch_time*1000), ulid);
30
+ ulid::EncodeEntropyRand(ulid);
31
+ return ulid::MarshalBinary(ulid);
32
+ }
33
+
20
34
  std::string _cpp_ulid_to_bytes(const char * ulid_string) {
21
35
  ulid::ULID ulid;
22
36
  ulid::UnmarshalFrom(ulid_string, ulid);
@@ -1,10 +1,13 @@
1
1
  #include <string>
2
+ #include <vector>
2
3
 
3
4
  #ifndef ULID_WRAPPER_H
4
5
  #define ULID_WRAPPER_H
5
6
 
6
7
  std::string _cpp_ulid();
8
+ std::vector<uint8_t> _cpp_ulid_bytes();
7
9
  std::string _cpp_ulid_at_time(double timestamp);
10
+ std::vector<uint8_t> _cpp_ulid_at_time_bytes(double timestamp);
8
11
  std::string _cpp_ulid_to_bytes(const char * ulid_string);
9
12
  std::string _cpp_bytes_to_ulid(std::string bytes_string);
10
13
 
File without changes