Skip to content

Commit

Permalink
uuid: Provide UUID class and uuid4() implementation using os.urandom().
Browse files Browse the repository at this point in the history
Includes unit test.
  • Loading branch information
andrewleech authored and pfalcon committed Oct 30, 2018
1 parent 6c6346b commit 6ba4564
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 9 deletions.
9 changes: 6 additions & 3 deletions uuid/metadata.txt
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
13 changes: 7 additions & 6 deletions uuid/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
import sdist_upip

setup(name='micropython-uuid',
version='0.0.1',
description='Dummy uuid module for MicroPython',
long_description='This is a dummy implementation of a module for MicroPython standard library.\nIt contains zero or very little functionality, and primarily intended to\navoid import errors (using idea that even if an application imports a\nmodule, it may be not using it onevery code path, so may work at least\npartially). It is expected that more complete implementation of the module\nwill be provided later. Please help with the development if you are\ninterested in this module.',
version='0.1',
description='uuid module for MicroPython',
long_description="This is a module reimplemented specifically for MicroPython standard library,\nwith efficient and lean design in mind. Note that this module is likely work\nin progress and likely supports just a subset of CPython's corresponding\nmodule. Please help with the development if you are interested in this\nmodule.",
url='https://github.com/pfalcon/micropython-lib',
author='Paul Sokolovsky',
author_email='micropython-lib@googlegroups.com',
author='Andrew Leech',
author_email='andrew@alelec.net',
maintainer='Paul Sokolovsky',
maintainer_email='micropython-lib@googlegroups.com',
license='MIT',
cmdclass={'sdist': sdist_upip.sdist},
py_modules=['uuid'])
py_modules=['uuid'],
install_requires=['micropython-os'])
15 changes: 15 additions & 0 deletions uuid/test_uuid.py
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")
28 changes: 28 additions & 0 deletions uuid/uuid.py
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)

0 comments on commit 6ba4564

Please sign in to comment.