Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .github/workflows/continuous_integration.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Continuous Integration
on:
workflow_dispatch: {}
schedule:
# Weekly every Saturday at midnight
- cron: '0 0 * * 6'

jobs:
test-lint-scan:
uses: ./.github/workflows/test_lint_scan.yml

collision-test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Set up PDM
uses: pdm-project/setup-pdm@v3
with:
python-version: 3.11
- name: Install dependencies
run: |
pdm install -dG test
- name: Run Testing
run: |
pdm run -v testing-slow
22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,29 @@ pip install cuid2
```

## Usage
You can generate CUIDs with the following:

You can generate CUIDs directly in the terminal with the following:
```bash
$ cuid2
```

Or you can rely on a CUID wrapper if you don't need any customizations:
```python
from typing import Callable
from cuid2 import cuid_wrapper

cuid_generator: Callable[[], str] = cuid_wrapper()

def main():
my_cuid: str = cuid_generator()
next_cuid: str = cuid_generator()
```

Finally, for more explicit control of the CUID generator, you can instantiate the class directly:
```python
from cuid2 import Cuid

CUID_GENERATOR: Cuid = Cuid()
CUID_GENERATOR: Cuid = Cuid(length=10)

def main():
my_cuid: str = CUID_GENERATOR.generate()
Expand Down
4 changes: 2 additions & 2 deletions local/tests/test_collision.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import pytest

from cuid2 import CUID
from cuid2 import Cuid


def cuid_generator(max_ids: int) -> Dict[str, Any]:
Expand All @@ -31,7 +31,7 @@ def cuid_generator(max_ids: int) -> Dict[str, Any]:
def _id_to_int(value: str, radix: int = str_base) -> int:
return reduce(lambda r, v: r * radix + int(v, radix), value, 0)

cuid: Callable[[], str] = CUID().generate
cuid: Callable[[], str] = Cuid().generate
bucket_count: int = 20

result: dict[str, Any] = {
Expand Down
39 changes: 26 additions & 13 deletions local/tests/test_cuid.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from math import floor
from secrets import SystemRandom
from timeit import repeat
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Callable

import pytest

from cuid2 import CUID, DEFAULT_LENGTH, INITIAL_COUNT_MAX, Cuid
from cuid2 import DEFAULT_LENGTH, INITIAL_COUNT_MAX, Cuid, cuid_wrapper

if TYPE_CHECKING:
from unittest.mock import Mock
Expand Down Expand Up @@ -163,15 +163,28 @@ def test_generate_uses_base36_encoding(self: "TestCuid", mocker: "Mock") -> None
assert len(result) == DEFAULT_LENGTH
assert result == "l9j3ikop1bi8tcvzme3x3yv7"

# Tests that instantiating the CUID class provides a deprecation warning.
def test_constructor_deprecation_warning(self: "TestCuid") -> None:
with pytest.deprecated_call():
cuid = CUID()
assert isinstance(cuid, Cuid)

# Tests that subclassing the CUID class provides a deprecation warning.
def test_subclass_deprecation_warning(self: "TestCuid") -> None:
with pytest.deprecated_call():

class CuidSubclass(CUID):
pass
class TestCuidWrapper:
def test_cuid_wrapper_is_callable(self: "TestCuidWrapper") -> None:
"""Tests that the function returns a callable."""
cuid_func: Callable[[], str] = cuid_wrapper()
assert callable(cuid_func)

def test_cuid_wrapper_return_type(self: "TestCuidWrapper") -> None:
"""Tests that the function returns a callable that generates a CUID string."""
cuid_func: Callable[[], str] = cuid_wrapper()
assert isinstance(cuid_func(), str)

def test_cuid_wrapper_no_parameters(self: "TestCuidWrapper") -> None:
"""Tests that the function doesn't accept any parameters."""
cuid_func: Callable[[], str] = cuid_wrapper()
with pytest.raises(
TypeError,
match="cuid\\(\\) takes 0 positional arguments but 1 was given",
):
cuid_func(DEFAULT_LENGTH) # type: ignore[call-arg]

def test_cuid_wrapper_length(self: "TestCuidWrapper") -> None:
"""Tests that the function returns a callable that generates a CUID string with the default length."""
cuid_func: Callable[[], str] = cuid_wrapper()
assert len(cuid_func()) == DEFAULT_LENGTH
50 changes: 20 additions & 30 deletions pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 9 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ keywords = ['crypt', 'security', 'uuid', 'guid', 'cuid', 'cryptography']
[project.urls]
repository = "https://github.com/gordon-code/cuid2/"

[project.scripts]
cuid2 = "cuid2.cli:main"

[tool.pdm.version]
source = "scm"

Expand All @@ -49,22 +52,22 @@ lint = [
"black~=23.3.0", # https://github.com/psf/black (latest: 23.3.0)
"codespell~=2.2.4", # https://github.com/codespell-project/codespell (latest: 2.2.4)
"pylint~=2.17.4", # https://github.com/PyCQA/pylint (latest: 2.17.4)
"ruff~=0.0.267", # https://github.com/charliermarsh/ruff (latest: 0.0.267)
"safety==2.4.0b1", # https://github.com/pyupio/safety (latest: 2.3.5)
"ruff~=0.0.269", # https://github.com/charliermarsh/ruff (latest: 0.0.269)
"safety==2.4.0b1", # https://github.com/pyupio/safety (latest: 2.3.5)
]
test = [
"pytest~=7.3.1", # https://github.com/pytest-dev/pytest (latest: 7.3.1)
"pytest-mock~=3.10.0", # https://github.com/pytest-dev/pytest-mock/ (latest: 3.10.0)
"pytest-sugar~=0.9.7", # https://github.com/Teemu/pytest-sugar/ (latest: 0.9.7)
]
typing = [
"mypy~=1.3.0", # https://github.com/python/mypy (latest: 1.3.0)
]
tox = [
# Version reduced to prevent `packaging` conflict with safety
"tox~=4.4.12", # https://github.com/tox-dev/tox (latest: 4.5.1)
"tox~=4.4.12", # https://github.com/tox-dev/tox (latest: 4.5.1)
"tox-pdm~=0.6.1", # https://github.com/pdm-project/tox-pdm (latest: 0.6.1)
]
typing = [
"mypy~=1.3.0", # https://github.com/python/mypy (latest: 1.3.0)
]

[tool.tox]
legacy_tox_ini = """
Expand Down
5 changes: 2 additions & 3 deletions src/cuid2/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Next generation GUIDs. Collision-resistant ids optimized for horizontal scaling and performance."""
from .generator import DEFAULT_LENGTH, INITIAL_COUNT_MAX, Cuid, cuid_wrapper

from .generator import CUID, DEFAULT_LENGTH, INITIAL_COUNT_MAX, Cuid

__all__ = ["Cuid", "CUID", "DEFAULT_LENGTH", "INITIAL_COUNT_MAX"]
__all__ = ["Cuid", "DEFAULT_LENGTH", "INITIAL_COUNT_MAX", "cuid_wrapper"]
8 changes: 8 additions & 0 deletions src/cuid2/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from cuid2.generator import cuid_wrapper

generate_cuid = cuid_wrapper()


def main() -> None:
"""Print out a CUID generated string. Used by the CLI console script."""
print(generate_cuid()) # noqa: T201 (print statement)
Loading