-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add simple UUID module with uuid4() #1137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
edc8b7c
91b10e6
0651951
2fe05af
b9aed47
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| metadata(version="0.1.0") | ||
|
|
||
| module("uuid.py") |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import unittest | ||
| import uuid | ||
|
|
||
|
|
||
| class TestUUID(unittest.TestCase): | ||
| def test_unique(self): | ||
| n = 10 | ||
| us = set(uuid.uuid4().bytes for _ in range(n)) | ||
| self.assertEqual(len(us), n) | ||
|
|
||
| def test_len(self): | ||
| u = uuid.uuid4() | ||
| self.assertEqual(len(u.bytes), 16) | ||
| self.assertEqual(len(u.hex), 32) | ||
| self.assertEqual(len(str(u)), 36) | ||
|
|
||
| def test_repr(self): | ||
| u = repr(uuid.uuid4()) | ||
| self.assertEqual(len(u), 44) | ||
| 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() |
| 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") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried to test if cpython gave the save error here but it gave a much messier exception. Possibly not worth worrying about, this should be user friendly enough.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, indeed CPython does give a confusing error. |
||
| 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('{}')".format(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)) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if other methods/properties would make porting CPython code easier, it's hard to know which are in common use.
versionandvariantseem like they would be high on the list - particularly if we decide to add support for UUIDv7 in the future - but it's probably best to hold off for now and keep it as minimal as possible.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also don't really know what users are going to need...