-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
84 additions
and
4 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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import random | ||
|
||
_hex_table = [f"{i:02x}" for i in range(256)] | ||
|
||
def is_obj_id(value: str): | ||
var_type = type(value) | ||
if var_type is Obj_Id: | ||
return True | ||
if var_type is str: | ||
if len(value) == 8: # todo: add efficient check if we only have hex values | ||
return True | ||
return False | ||
|
||
def new_obj_id(): | ||
return hex(random.getrandbits(32))[2:].zfill(8) # slice off '0x' and pad | ||
|
||
class Obj_Id(str): | ||
def __new__(cls, value: str=None): | ||
if value: | ||
if is_obj_id(value): | ||
obj_id = value | ||
else: | ||
raise ValueError(f'in Obj_Id: value provided was not a valid Obj_Id: {value}') | ||
else: | ||
obj_id = new_obj_id() | ||
return super().__new__(cls, obj_id) # Return a new instance of Guid initialized with the string version of the UUID | ||
|
||
def __str__(self): | ||
return self |
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
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,27 @@ | ||
from unittest import TestCase | ||
|
||
from osbot_utils.helpers.Obj_Id import new_obj_id, is_obj_id, Obj_Id | ||
from osbot_utils.testing.performance.Performance_Measure__Session import Performance_Measure__Session | ||
from osbot_utils.utils.Dev import pprint | ||
|
||
|
||
class test_Obj_Id(TestCase): | ||
|
||
def test__new__(self): | ||
obj_id = Obj_Id() | ||
assert type(obj_id ) is Obj_Id | ||
assert isinstance(obj_id, str) is True | ||
assert is_obj_id(obj_id ) is True | ||
|
||
def test_new_obj_id(self): | ||
obj_id = new_obj_id() | ||
assert is_obj_id(obj_id) | ||
|
||
def test__perf__new__(self): | ||
with Performance_Measure__Session() as _: | ||
_.measure(lambda: Obj_Id() ).assert_time__less_than(600) | ||
|
||
def test__perf__new_obj_id(self): | ||
with Performance_Measure__Session() as _: | ||
_.measure(lambda: new_obj_id() ).assert_time__less_than(300) | ||
_.measure(lambda: is_obj_id('abc')).assert_time__less_than(300) |