Skip to content

Commit

Permalink
Add UUID properties
Browse files Browse the repository at this point in the history
  • Loading branch information
aminalaee committed Mar 28, 2023
1 parent 994c232 commit 0cd8231
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 9 deletions.
49 changes: 49 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,55 @@ impl UUID {
}
}

#[getter]
fn node(&self) -> u128 {
self.int() & 0xFFFFFFFFFFFF
}

#[getter]
fn time_low(&self) -> u128 {
self.int() >> 96
}

#[getter]
fn time_mid(&self) -> u128 {
(self.int() >> 80) & 0xFFFF
}

#[getter]
fn time_hi_version(&self) -> u128 {
(self.int() >> 64) & 0xFFFF
}

#[getter]
fn clock_seq_hi_variant(&self) -> u128 {
(self.int() >> 56) & 0xFF
}

#[getter]
fn clock_seq_low(&self) -> u128 {
(self.int() >> 48) & 0xFF
}

#[getter]
fn clock_seq(&self) -> u128 {
((self.clock_seq_hi_variant() & 0x3f) << 8) |
self.clock_seq_low()
}

#[getter]
fn time(&self) -> u128 {
((self.time_hi_version() & 0x0fff) << 48) |
(self.time_mid() << 32) | self.time_low()
}

#[getter]
fn fields(&self) -> PyResult<(u128, u128, u128, u128, u128, u128)>{
Ok((self.time_low(), self.time_mid(), self.time_hi_version(),
self.clock_seq_hi_variant(), self.clock_seq_low(), self.node()))
}


#[staticmethod]
fn from_hex(hex: &str) -> PyResult<UUID> {
match Uuid::parse_str(hex) {
Expand Down
29 changes: 20 additions & 9 deletions tests/test_uuid.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from uuid import getnode
from uuid import UUID, getnode

import pytest

Expand Down Expand Up @@ -133,11 +133,22 @@ def test_uuid_illegal_version() -> None:


def test_uuid_properties() -> None:
uuid = uuid_utils.UUID("a8098c1a-f86e-11da-bd1a-00112444be1e")

assert uuid.bytes == b"\xa8\t\x8c\x1a\xf8n\x11\xda\xbd\x1a\x00\x11$D\xbe\x1e"
assert uuid.bytes_le == b"\x1a\x8c\t\xa8n\xf8\xda\x11\xbd\x1a\x00\x11$D\xbe\x1e"
assert uuid.hex == "a8098c1af86e11dabd1a00112444be1e"
assert uuid.int == 223359875637754765292326297443183672862
assert uuid.urn == "urn:uuid:a8098c1a-f86e-11da-bd1a-00112444be1e"
assert uuid.variant == uuid_utils.RFC_4122
uuid_1 = uuid_utils.UUID("a8098c1a-f86e-11da-bd1a-00112444be1e")
uuid_2 = UUID("a8098c1a-f86e-11da-bd1a-00112444be1e")

assert uuid_1.bytes == uuid_2.bytes
assert uuid_1.bytes_le == uuid_2.bytes_le
assert uuid_1.clock_seq_hi_variant == uuid_2.clock_seq_hi_variant
assert uuid_1.clock_seq_low == uuid_2.clock_seq_low
assert uuid_1.clock_seq == uuid_2.clock_seq
assert uuid_1.fields == uuid_2.fields
assert uuid_1.hex == uuid_2.hex
assert uuid_1.int == uuid_2.int
assert uuid_1.node == uuid_2.node
assert uuid_1.time == uuid_2.time
assert uuid_1.time_low == uuid_2.time_low
assert uuid_1.time_mid == uuid_2.time_mid
assert uuid_1.time_hi_version == uuid_2.time_hi_version
assert uuid_1.urn == uuid_2.urn
assert uuid_1.variant == uuid_2.variant
assert uuid_1.version == uuid_2.version

0 comments on commit 0cd8231

Please sign in to comment.