-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3047556
commit 575f213
Showing
12 changed files
with
134 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,13 +7,11 @@ name = "fastnanoid" | |
description = "A tiny, secure URL-friendly, and fast unique string ID generator for Python, written in Rust." | ||
readme = "README.md" | ||
authors = [ | ||
{name = "Oliver Lambson", email = "[email protected]"}, | ||
{name = "Ochir Erkhembayar"}, | ||
{ name = "Oliver Lambson", email = "[email protected]" }, | ||
{ name = "Ochir Erkhembayar" }, | ||
] | ||
maintainers = [ | ||
{name = "Oliver Lambson", email = "[email protected]"}, | ||
] | ||
license = {file = "LICENSE"} | ||
maintainers = [{ name = "Oliver Lambson", email = "[email protected]" }] | ||
license = { file = "LICENSE" } | ||
requires-python = ">=3.8" | ||
keywords = ["nanoid"] | ||
classifiers = [ | ||
|
@@ -43,3 +41,18 @@ Issues = "https://github.com/oliverlambson/fastnanoid/issues" | |
features = ["pyo3/extension-module"] | ||
python-source = "python" | ||
module-name = "fastnanoid.fastnanoid" | ||
|
||
[tool.pytest.ini_options] | ||
addopts = "--doctest-modules" | ||
testpaths = ["python", "benchmarks", "tests"] | ||
|
||
[tool.mypy] | ||
files = ["python/**/*.py", "benchmarks/**/*.py", "tests/**/*.py"] | ||
|
||
[tool.ruff] | ||
include = [ | ||
"pyproject.toml", | ||
"python/**/*.py", | ||
"benchmarks/**/*.py", | ||
"tests/**/*.py", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from fastnanoid.fastnanoid import generate | ||
from fastnanoid.uuid import urlid_to_uuid, uuid_to_urlid | ||
|
||
__all__ = ["generate"] | ||
__all__ = ["generate", "urlid_to_uuid", "uuid_to_urlid"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import base64 | ||
import uuid | ||
|
||
|
||
def uuid_to_urlid(uuid_: uuid.UUID) -> str: | ||
"""Convert a UUID to a short URL-safe string. | ||
>>> import base64 | ||
>>> import uuid | ||
>>> id_ = uuid.UUID('5d98d578-2731-4a4d-b666-70ca16f10aa2') | ||
>>> url_id = uuid_to_urlid(id_) | ||
>>> print(url_id) | ||
XZjVeCcxSk22ZnDKFvEKog | ||
""" | ||
return base64.urlsafe_b64encode(uuid_.bytes).rstrip(b"=").decode("utf-8") | ||
|
||
|
||
def urlid_to_uuid(url: str) -> uuid.UUID: | ||
"""Convert a base64url encoded UUID string to a UUID. | ||
>>> import base64 | ||
>>> import uuid | ||
>>> url_id = 'XZjVeCcxSk22ZnDKFvEKog' | ||
>>> id_ = urlid_to_uuid(url_id) | ||
>>> print(id_) | ||
5d98d578-2731-4a4d-b666-70ca16f10aa2 | ||
""" | ||
return uuid.UUID(bytes=base64.urlsafe_b64decode(url + "=" * (len(url) % 4))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
-e . | ||
maturin | ||
nanoid==2.0.0 | ||
pytest | ||
mypy | ||
ruff | ||
types-nanoid |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from fastnanoid import generate | ||
|
||
|
||
def test_generate(): | ||
assert len(generate()) == 21 | ||
assert len(generate(size=10)) == 10 |