@@ -45,8 +45,13 @@ def __init__(self):
4545 This object's UID would be "ice_cream-chocolate"
4646 """
4747
48- @staticmethod
49- def _render_uid (self , uid_def ):
48+ @property
49+ def uid_definition (self ) -> dict :
50+ if not hasattr (self .__class__ , "_uid_definition" ):
51+ raise AttributeError ("classes with HasUid must define _uid_definition" )
52+ return self .__class__ ._uid_definition
53+
54+ def _as_string (self , k , o ):
5055 def clean_string (s ):
5156 if isinstance (s , Enum ):
5257 s = s .value .lower ()
@@ -56,38 +61,25 @@ def clean_string(s):
5661 else :
5762 return s
5863
59- def as_string (k , o ):
60- if k == "class" and o == "self" :
61- return clean_string (self .__class__ .__name__ )
62- if isinstance (o , type ):
63- return clean_string (o .__name__ )
64- if isinstance (o , classmethod ):
65- return clean_string (str (o .__wrapped__ (self .__class__ )))
66- if callable (o ):
67- return clean_string (str (o (self )))
68- if o .startswith ("self." ):
69- return clean_string (self .__getattribute__ (o [5 :]))
70- return clean_string (str (o ))
71-
72- return "-" .join (as_string (k , v ) for k , v in uid_def .items ())
64+ if k == "class" and o == "self" :
65+ return clean_string (self .__class__ .__name__ )
66+ if isinstance (o , type ):
67+ return clean_string (o .__name__ )
68+ if isinstance (o , classmethod ):
69+ return clean_string (str (o .__wrapped__ (self .__class__ )))
70+ if callable (o ):
71+ return clean_string (str (o (self )))
72+ if o .startswith ("self." ):
73+ return clean_string (self .__getattribute__ (o [5 :]))
74+ return clean_string (str (o ))
7375
7476 @property
75- def uid (self ):
76- if not hasattr (self .__class__ , "_uid_definition" ):
77- raise AttributeError ("classes with HasUid must define _uid_definition" )
77+ def uid (self ) -> str :
78+ return "-" .join (self ._as_string (k , v ) for k , v in self .uid_definition .items ())
7879
79- return HasUid ._render_uid (self , self .__class__ ._uid_definition )
80-
81- def get_uid_part (self , part_name : str ) -> str :
82- """Gets string-rendered value of a specific part of the UID."""
83- if not hasattr (self .__class__ , "_uid_definition" ):
84- raise AttributeError ("classes with HasUid must define _uid_definition" )
85-
86- uid_def = self .__class__ ._uid_definition
87- if part_name not in uid_def :
88- raise KeyError (f"part name { part_name } not found in _uid_definition" )
89-
90- return HasUid ._render_uid (self , {part_name : uid_def [part_name ]})
80+ @property
81+ def uid_dict (self ) -> dict :
82+ return {k : self ._as_string (k , v ) for k , v in self .uid_definition .items ()}
9183
9284 def __str__ (self ):
9385 return f"{ self .__class__ .__name__ } ({ self .uid } )"
0 commit comments