Skip to content

Commit

Permalink
feat: add uuid (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
HHongSeungWoo authored Dec 27, 2023
1 parent ee88841 commit 8b7977d
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 4 deletions.
32 changes: 31 additions & 1 deletion Cargo.lock

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

10 changes: 9 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "fastid"
version = "0.0.2"
version = "0.0.3"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand All @@ -15,3 +15,11 @@ ulid="1.1.0"
pyo3="0.20.0"
rs-snowflake = "0.6.0"
once_cell = "1.19.0"

[dependencies.uuid]
version = "1.6.1"
features = [
"v7",
"fast-rng", # Use a faster (but still sufficiently random) RNG
"macro-diagnostics", # Enable better diagnostics for compile-time UUIDs
]
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ Created for the easy and fast generation of various IDs

# Supported IDs
- ulid
- snowflake
- snowflake
- uuid v7 (If you intend to use uuid in your database, this can be a great choice)
6 changes: 6 additions & 0 deletions fastid.pyi
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
def ulid() -> str:
...


def snowflake_str(machine_id: int = 1, node_id: int = 1) -> str:
...


def snowflake_int(machine_id: int = 1, node_id: int = 1) -> int:
...


def uuid_v7() -> str:
...
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ name = "fastid"
description = "This is a library for quickly generating unique IDs in Python."
authors = [{ name = "Seungwoo Hong", email = "[email protected]" }]
requires-python = ">=3.7"
keywords = ["unique-id", "id-generator", "ulid", "snowflake"]
keywords = ["unique-id", "id-generator", "ulid", "snowflake", "uuid"]
classifiers = [
"Programming Language :: Rust",
"Programming Language :: Python",
Expand Down
7 changes: 7 additions & 0 deletions python/test/test_fastid.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import timeit
import uuid

import fastid
from ulid import microsecond
Expand All @@ -14,3 +15,9 @@ def test_snowflake():
print()
print("fastid.snowflake_int", timeit.timeit(lambda: fastid.snowflake_int(), number=10000))
print("fastid.snowflake_str", timeit.timeit(lambda: fastid.snowflake_str(), number=10000))


def test_uuid():
print()
print("uuid.uuid4 ", timeit.timeit(lambda: uuid.uuid4(), number=10000))
print("fastid.uuid_v7", timeit.timeit(lambda: fastid.uuid_v7(), number=10000))
10 changes: 10 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ use once_cell::sync::Lazy;
use pyo3::prelude::*;
use snowflake::SnowflakeIdGenerator;
use ulid::Ulid;
use uuid::Uuid;

static SNOWFLAKE_GENERATOR: Lazy<Mutex<SnowflakeIdGenerator>> = Lazy::new(|| {
Mutex::new(SnowflakeIdGenerator::new(1, 1))
});

#[pyfunction]
#[pyo3(name = "ulid")]
fn get_ulid() -> PyResult<String> {
Expand All @@ -20,6 +22,7 @@ fn get_snowflake(machine_id: Option<i32>, node_id: Option<i32>) -> i64 {
sg.machine_id = machine_id.unwrap_or(1);
sg.real_time_generate()
}

#[pyfunction]
#[pyo3(name = "snowflake_str")]
fn get_snowflake_str(machine_id: Option<i32>, node_id: Option<i32>) -> PyResult<String> {
Expand All @@ -32,6 +35,11 @@ fn get_snowflake_int(machine_id: Option<i32>, node_id: Option<i32>) -> PyResult<
Ok(get_snowflake(machine_id, node_id))
}

#[pyfunction]
fn uuid_v7() -> PyResult<String> {
Ok(Uuid::now_v7().to_string())
}


#[pymodule]
fn fastid(_py: Python, m: &PyModule) -> PyResult<()> {
Expand All @@ -40,5 +48,7 @@ fn fastid(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(get_snowflake_int, m)?)?;
m.add_function(wrap_pyfunction!(get_snowflake_str, m)?)?;

m.add_function(wrap_pyfunction!(uuid_v7, m)?)?;

Ok(())
}

0 comments on commit 8b7977d

Please sign in to comment.