-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
uuid: Provide UUID class and uuid4() implementation using os.urandom().
Includes unit test.
- Loading branch information
1 parent
6c6346b
commit 6ba4564
Showing
4 changed files
with
56 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
srctype=dummy | ||
type=module | ||
version = 0.0.1 | ||
srctype = micropython-lib | ||
type = module | ||
version = 0.1 | ||
author = Andrew Leech | ||
author_email = andrew@alelec.net | ||
depends = os |
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import uuid | ||
|
||
u1 = uuid.uuid4() | ||
u2 = uuid.uuid4() | ||
|
||
assert str(u1) != str(u2), "Two uuid4 should not match" | ||
|
||
assert len(str(u1)) == len(str(u1)) == 36 | ||
|
||
assert str(repr(u1)).startswith("<UUID") | ||
assert str(repr(u1)).endswith(">") | ||
|
||
assert len(u1.hex) == 32 | ||
|
||
print("OK") |
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 os | ||
import ubinascii | ||
|
||
|
||
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 ubinascii.hexlify(self._bytes).decode() | ||
|
||
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) |