Skip to content

Commit 76dfc14

Browse files
committed
some cleanup on length
1 parent f74d027 commit 76dfc14

File tree

1 file changed

+33
-67
lines changed

1 file changed

+33
-67
lines changed

labelbox/schema/ontology_generator.py

Lines changed: 33 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,3 @@
1-
'''
2-
TODO
3-
Option.add_option() currently creates a new Classification object. however, this does not work for certain Classification options.
4-
Example:
5-
Classification.Type.DROPDOWN -> the options for this class_type should only generate more nested dropdowns
6-
-> Dropdowns are supposed to be removed moving forward, but this is a current problem
7-
-> This is the most major issue because going to the doubly nested class will break the UI
8-
Classification.Type.CHECKLIST & Classification.Type.TEXT-> the option cannot have a nested Classification.
9-
-> this reflects accurately in the UI without issues, but when you query via graphql, it shows what was input
10-
-> this is a lesser issue because the UI will not reflect the unavailable fields
11-
Is there an effective way to enforce limitations on Option.add_option()?
12-
-> Maybe a way to check if the Option itself has options when adding it to a Classification?
13-
14-
Classification.add_..
15-
Tool.add_..
16-
Tool.add_..
17-
Currently the above will let you input values to generate a new object, but they do not play well with already made objects
18-
Example:
19-
Classification.add_option(value=...) will work fine
20-
21-
option = Option(value=...)
22-
Classification.add_option(option) will not work
23-
24-
What is the best way to allow both the creation of both an object but also accept an already existing object?
25-
'''
26-
271
from dataclasses import dataclass, field
282
from enum import Enum, auto
293
import os
@@ -61,15 +35,15 @@ def from_dict(cls, dictionary: Dict[str,str]):
6135
value = dictionary["value"],
6236
schema_id = dictionary["schemaNodeId"],
6337
feature_schema_id = dictionary["featureSchemaId"],
64-
options = [Classification.from_dict(nested_class) for nested_class in dictionary.get("options", [])]
38+
options = [Classification.from_dict(o) for o in dictionary.get("options", [])]
6539
)
6640

67-
def add_option(self, *args, **kwargs):
68-
new_option = Classification(*args, **kwargs)
69-
if new_option.instructions in (c.instructions for c in self.options):
70-
raise InconsistentOntologyException(f"Duplicate nested classification '{new_option.instructions}' for option '{self.label}'")
71-
self.options.append(new_option)
72-
return new_option
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
7347

7448
@dataclass
7549
class Classification:
@@ -80,7 +54,7 @@ class Type(Enum):
8054
RADIO = "radio"
8155
DROPDOWN = "dropdown"
8256

83-
_REQUIRES_OPTIONS = set((Type.CHECKLIST, Type.RADIO, Type.DROPDOWN))
57+
_REQUIRES_OPTIONS = {Type.CHECKLIST, Type.RADIO, Type.DROPDOWN}
8458

8559
class_type: Type
8660
instructions: str
@@ -94,7 +68,8 @@ def name(self):
9468
return self.instructions
9569

9670
def asdict(self) -> Dict[str,str]:
97-
if self.class_type in Classification._REQUIRES_OPTIONS and len(self.options) < 1:
71+
#unsure how to shorten this specification
72+
if self.class_type in Classification._REQUIRES_OPTIONS and len(self.options) < 1:
9873
raise InconsistentOntologyException(f"Classification '{self.instructions}' requires options.")
9974
return {
10075
"type": self.class_type.value,
@@ -117,12 +92,11 @@ def from_dict(cls, dictionary: Dict[str,str]):
11792
feature_schema_id = dictionary["schemaNodeId"]
11893
)
11994

120-
def add_option(self, *args, **kwargs):
121-
new_option = Option(*args, **kwargs)
122-
if new_option.value in (o.value for o in self.options):
123-
raise InconsistentOntologyException(f"Duplicate option '{new_option.value}' for classification '{self.name}'.")
124-
self.options.append(new_option)
125-
return new_option
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
126100

127101
@dataclass
128102
class Tool:
@@ -161,17 +135,17 @@ def from_dict(cls, dictionary: Dict[str,str]):
161135
schema_id = dictionary["schemaNodeId"],
162136
feature_schema_id = dictionary["featureSchemaId"],
163137
required = dictionary["required"],
164-
tool = Tool.Type(dictionary["tool"]),
138+
tool = Tool.Type(dictionary["tool"]),
139+
#is there a way to shorten this classifications line at 140?
165140
classifications = [Classification.from_dict(c) for c in dictionary["classifications"]],
166141
color = dictionary["color"]
167142
)
168143

169-
def add_nested_class(self, *args, **kwargs):
170-
new_classification = Classification(*args, **kwargs)
171-
if new_classification.instructions in (c.instructions for c in self.classifications):
172-
raise InconsistentOntologyException(f"Duplicate nested classification '{new_classification.instructions}' for option '{self.label}'")
173-
self.classifications.append(new_classification)
174-
return new_classification
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
175149

176150
@dataclass
177151
class Ontology:
@@ -192,31 +166,23 @@ def from_project(cls, project: Project):
192166

193167
return return_ontology
194168

195-
def add_tool(self, *args, **kwargs) -> Tool:
196-
new_tool = Tool(*args, **kwargs)
169+
def add_tool(self, new_tool: Tool) -> Tool:
197170
if new_tool.name in (t.name for t in self.tools):
198171
raise InconsistentOntologyException(f"Duplicate tool name '{new_tool.name}'. ")
199172
self.tools.append(new_tool)
200173
return new_tool
201-
202-
def add_classification(self, *args, **kwargs) -> Classification:
203-
new_classification = Classification(*args, **kwargs)
204-
if new_classification.instructions in (c.instructions for c in self.classifications):
205-
raise InconsistentOntologyException(f"Duplicate classifications instructions '{new_classification.instructions}'. ")
206-
self.classifications.append(new_classification)
207-
return new_classification
174+
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
208180

209181
def asdict(self):
210-
all_tools = []
211-
all_classifications = []
212-
213-
for tool in self.tools:
214-
all_tools.append(tool.asdict())
215-
216-
for classification in self.classifications:
217-
all_classifications.append(classification.asdict())
218-
219-
return {"tools": all_tools, "classifications": all_classifications}
182+
return {
183+
"tools": [t.asdict() for t in self.tools],
184+
"classifications": [c.asdict() for c in self.classifications]
185+
}
220186

221187
if __name__ == "__main__":
222188
pass

0 commit comments

Comments
 (0)