Skip to content

Commit

Permalink
Add all arguments from the original uuid class (#16)
Browse files Browse the repository at this point in the history
* Add all arguments from the original `uuid` class
  • Loading branch information
oittaa authored Jan 3, 2022
1 parent cc664c3 commit be4da65
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 9 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,10 @@ jobs:
coverage run -m unittest discover
coverage report -m
coverage xml
- name: Benchmark
run: ./bench.sh
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v2.1.0
if: matrix.python-version == '3.10'
with:
flags: unittests
- name: Benchmark
run: |
pip install pyperf
./bench.sh
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
coverage[toml]==6.2
flake8==4.0.1
mypy==0.930
pyperf==2.3.0
31 changes: 26 additions & 5 deletions src/uuid6/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,36 @@

import secrets
import time
from uuid import UUID
from typing import Tuple
from uuid import UUID, SafeUUID


class DraftUUID(UUID):
r"""UUID draft version objects"""

def __init__(self, int: int, version: int = None) -> None:
r"""Create a UUID from a single 128-bit integer as the 'int' argument."""

def __init__(
self,
hex: str = None,
bytes: bytes = None,
bytes_le: bytes = None,
fields: Tuple[int, int, int, int, int, int] = None,
int: int = None,
version: int = None,
*,
is_safe=SafeUUID.unknown
) -> None:
r"""Create a UUID."""

if int is None or [hex, bytes, bytes_le, fields].count(None) != 4:
return super().__init__(
hex=hex,
bytes=bytes,
bytes_le=bytes_le,
fields=fields,
int=int,
version=version,
is_safe=is_safe,
)
if not 0 <= int < 1 << 128:
raise ValueError("int is out of range (need a 128-bit value)")
if version is not None:
Expand All @@ -26,7 +47,7 @@ def __init__(self, int: int, version: int = None) -> None:
# Set the version number.
int &= ~(0xF000 << 64)
int |= version << 76
super().__init__(int=int)
super().__init__(int=int, is_safe=is_safe)

@property
def subsec(self) -> int:
Expand Down
8 changes: 8 additions & 0 deletions test/test_uuid6.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ def test_time(self):
uuid_7 = uuid7()
self.assertAlmostEqual(uuid_7.time / 10 ** 9, cur_time / 10 ** 9, 3)

def test_uuid7_from_hex(self):
uuid_7 = DraftUUID(hex="061d0edc-bea0-75cc-9892-f6295fd7d295")
self.assertEqual(uuid_7.time, 1641082315914150976)

def test_multiple_arguments(self):
with self.assertRaises(TypeError):
_ = DraftUUID(int=0, hex="061d0edc-bea0-75cc-9892-f6295fd7d295")


if __name__ == "__main__":
unittest.main()

0 comments on commit be4da65

Please sign in to comment.