-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add HasUid and apply it to Benchmark and Hazard. (#386)
* Add HasUid and apply it to Benchmark and Hazard. * Pleasing the formatting gods.
- Loading branch information
Showing
5 changed files
with
129 additions
and
2 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
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,64 @@ | ||
import re | ||
|
||
import casefy | ||
|
||
|
||
class HasUid: | ||
""" | ||
A mixin class that gives an object an AISafety UID. | ||
Add it to your object's parent class list and then add a _uid_definition | ||
class variable that specifies your UID. | ||
class MySimpleObject(ABC, HasUid): | ||
_uid_definition = {"name": "simple", "version": "0.5"} | ||
That will result in a uid of "simple-0.5". | ||
Your UID values can include literals, function references, or class references, | ||
all of which will get rendered automatically. Due to the specifics of python, | ||
you can't refer to a function or object before it exists, so make sure the | ||
UID definition is after the reference. For example: | ||
class MyDynamicObject(ABC, HasUid): | ||
def name(self): | ||
return "bob" | ||
_uid_definition = {"name": name, "version": "0.5"} | ||
Then calling MyDynamicObject().uid will return "bob-0.5". | ||
If you'd like to refer to the class currently being defined, you'll need to | ||
use the special value "class": "self", like this: | ||
class ClassyObject(ABC, HasUid): | ||
_uid_definition = {"class": "self", "version": "0.5"} | ||
This object's UID would be "classy_object-0.5". | ||
""" | ||
|
||
@property | ||
def uid(self): | ||
if not hasattr(self.__class__, "_uid_definition"): | ||
raise AttributeError("classes with HasUid must define _uid_definition") | ||
|
||
uid_def = self.__class__._uid_definition | ||
|
||
def clean_string(s): | ||
s = re.sub("[-]+", "_", s) | ||
if s.lower() != s: | ||
return casefy.snakecase(s) | ||
else: | ||
return s | ||
|
||
def as_string(k, o): | ||
if k == "class" and o == "self": | ||
return clean_string(self.__class__.__name__) | ||
if isinstance(o, type): | ||
return clean_string(o.__name__) | ||
if isinstance(o, classmethod): | ||
return clean_string(str(o.__wrapped__(self.__class__))) | ||
if callable(o): | ||
return clean_string(str(o(self))) | ||
return clean_string(str(o)) | ||
|
||
return "-".join(as_string(k, v) for k, v in uid_def.items()) |
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,55 @@ | ||
from modelbench.uid import HasUid | ||
|
||
|
||
class HasStaticUid(HasUid, object): | ||
_uid_definition = {"name": "static", "version": "1.1"} | ||
|
||
|
||
class HasPropertyInUid(HasUid, object): | ||
|
||
def __init__(self, name): | ||
super().__init__() | ||
self._name = name | ||
|
||
def name(self): | ||
return self._name | ||
|
||
_uid_definition = {"name": name} | ||
|
||
|
||
class HasClassMethodInUid(HasUid, object): | ||
|
||
@classmethod | ||
def name(cls): | ||
return "a_class_specific_name" | ||
|
||
_uid_definition = {"name": name} | ||
|
||
|
||
class HasOwnClassInUid(HasUid, object): | ||
_uid_definition = {"class": "self", "version": "1.2"} | ||
|
||
|
||
def test_mixin_static(): | ||
assert HasStaticUid().uid == "static-1.1" | ||
|
||
|
||
def test_mixin_property(): | ||
assert HasPropertyInUid("fnord").uid == "fnord" | ||
|
||
|
||
def test_mixin_class_method(): | ||
# class methods behave differently than normal methods | ||
assert HasClassMethodInUid().uid == "a_class_specific_name" | ||
|
||
|
||
def test_mixin_class(): | ||
assert HasOwnClassInUid().uid == "has_own_class_in_uid-1.2" | ||
|
||
|
||
def test_mixin_case(): | ||
assert HasPropertyInUid("lower").uid == "lower" | ||
assert HasPropertyInUid("lower_with_underscore").uid == "lower_with_underscore" | ||
assert HasPropertyInUid("lower-with-dash").uid == "lower_with_dash" | ||
assert HasPropertyInUid("UPPER").uid == "upper" | ||
assert HasPropertyInUid("MixedCase").uid == "mixed_case" |