Skip to content
Open
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
3 changes: 3 additions & 0 deletions python-stdlib/uuid/manifest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
metadata(version="0.1.0")

module("uuid.py")
29 changes: 29 additions & 0 deletions python-stdlib/uuid/test_uuid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import unittest
import uuid


class TestUUID(unittest.TestCase):
def test_unique(self):
u1 = uuid.uuid4()
u2 = uuid.uuid4()
self.assertNotEqual(u1.hex, u2.hex)
self.assertNotEqual(str(u1), str(u2))

def test_len(self):
u = uuid.uuid4()
self.assertEqual(len(str(u)), 36)
self.assertEqual(len(u.hex), 32)

def test_repr(self):
u = str(repr(uuid.uuid4()))
self.assertTrue(u.startswith("<UUID"))
self.assertTrue(u.endswith(">"))

def test_constructor(self):
u1 = uuid.uuid4()
u2 = uuid.UUID(bytes.fromhex(u1.hex))
self.assertEqual(u1.hex, u2.hex)


if __name__ == "__main__":
unittest.main()
27 changes: 27 additions & 0 deletions python-stdlib/uuid/uuid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import os


class UUID:
def __init__(self, bytes):
if len(bytes) != 16:
raise ValueError("bytes arg must be 16 bytes long")
self._bytes = bytes

@property
def hex(self):
return self._bytes.hex()

def __str__(self):
h = self.hex
return "-".join((h[0:8], h[8:12], h[12:16], h[16:20], h[20:32]))

def __repr__(self):
return "<UUID: %s>" % str(self)


def uuid4():
"""Generates a random UUID compliant to RFC 4122 pg.14"""
random = bytearray(os.urandom(16))
random[6] = (random[6] & 0x0F) | 0x40
random[8] = (random[8] & 0x3F) | 0x80
return UUID(bytes=random)
1 change: 1 addition & 0 deletions tools/ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ function ci_package_tests_run {
python-stdlib/time \
python-stdlib/unittest/tests \
python-stdlib/unittest-discover/tests \
python-stdlib/uuid \
; do
(cd $path && "${MICROPYTHON}" -m unittest)
if [ $? -ne 0 ]; then false; return; fi
Expand Down
Loading