11from dataclasses import dataclass , field
22from enum import Enum , auto
33import os
4- from typing import List , Optional , Dict
4+ from typing import List , Optional , Dict , Any
55
66from labelbox import Client , Project , Dataset , LabelingFrontend
77
@@ -20,7 +20,7 @@ class Option:
2020 def label (self ):
2121 return self .value
2222
23- def asdict (self ) -> Dict [str , str ]:
23+ def asdict (self ) -> Dict [str , Any ]:
2424 return {
2525 "schemaNodeId" : self .schema_id ,
2626 "featureSchemaId" : self .feature_schema_id ,
@@ -30,20 +30,21 @@ def asdict(self) -> Dict[str, str]:
3030 }
3131
3232 @classmethod
33- def from_dict (cls , dictionary : Dict [str ,str ]):
33+ def from_dict (cls , dictionary : Dict [str ,Any ]):
3434 return Option (
3535 value = dictionary ["value" ],
3636 schema_id = dictionary ["schemaNodeId" ],
3737 feature_schema_id = dictionary ["featureSchemaId" ],
38- options = [Classification .from_dict (o ) for o in dictionary .get ("options" , [])]
38+ options = [Classification .from_dict (o )
39+ for o in dictionary .get ("options" , [])]
3940 )
4041
41- def add_option (self , new_o : 'Classification' ) -> 'Classification' :
42- if new_o .instructions in (c .instructions for c in self .options ):
43- #what is the best way to shorten exceptions?
44- raise InconsistentOntologyException ( f"Duplicate nested classification '{ new_o .instructions } ' for option ' { self . label } '" )
45- self .options . append ( new_o )
46- return new_o
42+ def add_option (self , option : 'Classification' ) -> 'Classification' :
43+ if option .instructions in (c .instructions for c in self .options ):
44+ raise InconsistentOntologyException (
45+ f"Duplicate nested classification '{ option .instructions } ' "
46+ f"for option ' { self .label } '" )
47+ self . options . append ( option )
4748
4849@dataclass
4950class Classification :
@@ -67,10 +68,11 @@ class Type(Enum):
6768 def name (self ):
6869 return self .instructions
6970
70- def asdict (self ) -> Dict [str ,str ]:
71- #unsure how to shorten this specification
72- if self .class_type in Classification ._REQUIRES_OPTIONS and len (self .options ) < 1 :
73- raise InconsistentOntologyException (f"Classification '{ self .instructions } ' requires options." )
71+ def asdict (self ) -> Dict [str ,Any ]:
72+ if self .class_type in Classification ._REQUIRES_OPTIONS \
73+ and len (self .options ) < 1 :
74+ raise InconsistentOntologyException (
75+ f"Classification '{ self .instructions } ' requires options." )
7476 return {
7577 "type" : self .class_type .value ,
7678 "instructions" : self .instructions ,
@@ -82,7 +84,7 @@ def asdict(self) -> Dict[str,str]:
8284 }
8385
8486 @classmethod
85- def from_dict (cls , dictionary : Dict [str ,str ]):
87+ def from_dict (cls , dictionary : Dict [str ,Any ]):
8688 return Classification (
8789 class_type = Classification .Type (dictionary ["type" ]),
8890 instructions = dictionary ["instructions" ],
@@ -92,11 +94,12 @@ def from_dict(cls, dictionary: Dict[str,str]):
9294 feature_schema_id = dictionary ["schemaNodeId" ]
9395 )
9496
95- def add_option (self , new_o : Option ):
96- if new_o .value in (o .value for o in self .options ):
97- raise InconsistentOntologyException (f"Duplicate option '{ new_o .value } ' for classification '{ self .name } '." )
98- self .options .append (new_o )
99- return new_o
97+ def add_option (self , option : Option ):
98+ if option .value in (o .value for o in self .options ):
99+ raise InconsistentOntologyException (
100+ f"Duplicate option '{ option .value } ' "
101+ f"for classification '{ self .name } '." )
102+ self .options .append (option )
100103
101104@dataclass
102105class Tool :
@@ -117,7 +120,7 @@ class Type(Enum):
117120 schema_id : Optional [str ] = None
118121 feature_schema_id : Optional [str ] = None
119122
120- def asdict (self ) -> Dict [str ,str ]:
123+ def asdict (self ) -> Dict [str ,Any ]:
121124 return {
122125 "tool" : self .tool .value ,
123126 "name" : self .name ,
@@ -129,23 +132,25 @@ def asdict(self) -> Dict[str,str]:
129132 }
130133
131134 @classmethod
132- def from_dict (cls , dictionary : Dict [str ,str ]):
135+ def from_dict (cls , dictionary : Dict [str ,Any ]):
133136 return Tool (
134137 name = dictionary ['name' ],
135138 schema_id = dictionary ["schemaNodeId" ],
136139 feature_schema_id = dictionary ["featureSchemaId" ],
137140 required = dictionary ["required" ],
138141 tool = Tool .Type (dictionary ["tool" ]),
139- #is there a way to shorten this classifications line at 140?
140- classifications = [ Classification . from_dict ( c ) for c in dictionary ["classifications" ]],
142+ classifications = [ Classification . from_dict ( c )
143+ for c in dictionary ["classifications" ]],
141144 color = dictionary ["color" ]
142145 )
143146
144- def add_nested_class (self , new_c : Classification ) -> Classification :
145- if new_c .instructions in (c .instructions for c in self .classifications ):
146- raise InconsistentOntologyException (f"Duplicate nested classification '{ new_c .instructions } ' for option '{ self .label } '" )
147- self .classifications .append (new_c )
148- return new_c
147+ def add_classification (self , classification : Classification ):
148+ if classification .instructions in (c .instructions
149+ for c in self .classifications ):
150+ raise InconsistentOntologyException (
151+ f"Duplicate nested classification '{ classification .instructions } ' "
152+ f"for option '{ self .label } '" )
153+ self .classifications .append (classification )
149154
150155@dataclass
151156class Ontology :
@@ -166,23 +171,21 @@ def from_project(cls, project: Project):
166171
167172 return return_ontology
168173
169- def add_tool (self , new_tool : Tool ) -> Tool :
170- if new_tool .name in (t .name for t in self .tools ):
171- raise InconsistentOntologyException (f"Duplicate tool name ' { new_tool . name } '. " )
172- self . tools . append ( new_tool )
173- return new_tool
174+ def add_tool (self , tool : Tool ) -> Tool :
175+ if tool .name in (t .name for t in self .tools ):
176+ raise InconsistentOntologyException (
177+ f"Duplicate tool name ' { tool . name } '. " )
178+ self . tools . append ( tool )
174179
175- def add_classification (self , new_c : Classification ) -> Classification :
176- if new_c .instructions in (c .instructions for c in self .classifications ):
177- raise InconsistentOntologyException (f"Duplicate classifications instructions '{ new_c .instructions } '. " )
178- self .classifications .append (new_c )
179- return new_c
180+ def add_classification (self , classification : Classification ) -> Classification :
181+ if classification .instructions in (c .instructions
182+ for c in self .classifications ):
183+ raise InconsistentOntologyException (
184+ f"Duplicate classifications instructions '{ classification .instructions } '. " )
185+ self .classifications .append (classification )
180186
181187 def asdict (self ):
182188 return {
183189 "tools" : [t .asdict () for t in self .tools ],
184190 "classifications" : [c .asdict () for c in self .classifications ]
185- }
186-
187- if __name__ == "__main__" :
188- pass
191+ }
0 commit comments