Skip to content

Commit c570276

Browse files
committed
making pep8 format
1 parent 5e7180e commit c570276

File tree

3 files changed

+234
-192
lines changed

3 files changed

+234
-192
lines changed

labelbox/exceptions.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
class LabelboxError(Exception):
22
"""Base class for exceptions."""
3-
43
def __init__(self, message, cause=None):
54
"""
65
Args:
@@ -28,7 +27,6 @@ class AuthorizationError(LabelboxError):
2827

2928
class ResourceNotFoundError(LabelboxError):
3029
"""Exception raised when a given resource is not found. """
31-
3230
def __init__(self, db_object_type, params):
3331
""" Constructor.
3432
@@ -68,7 +66,6 @@ class InvalidQueryError(LabelboxError):
6866

6967
class NetworkError(LabelboxError):
7068
"""Raised when an HTTPError occurs."""
71-
7269
def __init__(self, cause):
7370
super().__init__(str(cause), cause)
7471
self.cause = cause
@@ -82,7 +79,6 @@ class TimeoutError(LabelboxError):
8279
class InvalidAttributeError(LabelboxError):
8380
""" Raised when a field (name or Field instance) is not valid or found
8481
for a specific DB object type. """
85-
8682
def __init__(self, db_object_type, field):
8783
super().__init__("Field(s) '%r' not valid on DB type '%s'" %
8884
(field, db_object_type.type_name()))
@@ -104,3 +100,7 @@ class MalformedQueryException(Exception):
104100
class UuidError(LabelboxError):
105101
""" Raised when there are repeat Uuid's in bulk import request."""
106102
pass
103+
104+
105+
class InconsistentOntologyException(Exception):
106+
pass

labelbox/schema/ontology_generator.py

Lines changed: 75 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -6,47 +6,46 @@
66
from labelbox import Client, Project, Dataset, LabelingFrontend
77

88

9-
class InconsistentOntologyException(Exception):
10-
pass
11-
129
@dataclass
1310
class Option:
14-
value: str
11+
value: str
1512
schema_id: Optional[str] = None
1613
feature_schema_id: Optional[str] = None
1714
options: List["Classification"] = field(default_factory=list)
1815

1916
@property
2017
def label(self):
2118
return self.value
22-
19+
20+
@classmethod
21+
def from_dict(cls, dictionary: Dict[str, Any]):
22+
return Option(value=dictionary["value"],
23+
schema_id=dictionary["schemaNodeId"],
24+
feature_schema_id=dictionary["featureSchemaId"],
25+
options=[
26+
Classification.from_dict(o)
27+
for o in dictionary.get("options", [])
28+
])
29+
2330
def asdict(self) -> Dict[str, Any]:
2431
return {
2532
"schemaNodeId": self.schema_id,
2633
"featureSchemaId": self.feature_schema_id,
2734
"label": self.label,
2835
"value": self.value,
29-
"options": [o.asdict() for o in self.options]}
30-
31-
@classmethod
32-
def from_dict(cls, dictionary: Dict[str,Any]):
33-
return Option(
34-
value = dictionary["value"],
35-
schema_id = dictionary["schemaNodeId"],
36-
feature_schema_id = dictionary["featureSchemaId"],
37-
options = [Classification.from_dict(o)
38-
for o in dictionary.get("options", [])])
36+
"options": [o.asdict() for o in self.options]
37+
}
3938

4039
def add_option(self, option: 'Classification') -> 'Classification':
4140
if option.instructions in (o.instructions for o in self.options):
4241
raise InconsistentOntologyException(
4342
f"Duplicate nested classification '{option.instructions}' "
4443
f"for option '{self.label}'")
4544
self.options.append(option)
46-
47-
@dataclass
48-
class Classification:
4945

46+
47+
@dataclass
48+
class Classification:
5049
class Type(Enum):
5150
TEXT = "text"
5251
CHECKLIST = "checklist"
@@ -66,9 +65,19 @@ class Type(Enum):
6665
def name(self):
6766
return self.instructions
6867

69-
def asdict(self) -> Dict[str,Any]:
68+
@classmethod
69+
def from_dict(cls, dictionary: Dict[str, Any]):
70+
return Classification(
71+
class_type=Classification.Type(dictionary["type"]),
72+
instructions=dictionary["instructions"],
73+
required=dictionary["required"],
74+
options=[Option.from_dict(o) for o in dictionary["options"]],
75+
schema_id=dictionary["schemaNodeId"],
76+
feature_schema_id=dictionary["schemaNodeId"])
77+
78+
def asdict(self) -> Dict[str, Any]:
7079
if self.class_type in Classification._REQUIRES_OPTIONS \
71-
and len(self.options) < 1:
80+
and len(self.options) < 1:
7281
raise InconsistentOntologyException(
7382
f"Classification '{self.instructions}' requires options.")
7483
return {
@@ -78,17 +87,8 @@ def asdict(self) -> Dict[str,Any]:
7887
"required": self.required,
7988
"options": [o.asdict() for o in self.options],
8089
"schemaNodeId": self.schema_id,
81-
"featureSchemaId": self.feature_schema_id}
82-
83-
@classmethod
84-
def from_dict(cls, dictionary: Dict[str,Any]):
85-
return Classification(
86-
class_type = Classification.Type(dictionary["type"]),
87-
instructions = dictionary["instructions"],
88-
required = dictionary["required"],
89-
options = [Option.from_dict(o) for o in dictionary["options"]],
90-
schema_id = dictionary["schemaNodeId"],
91-
feature_schema_id = dictionary["schemaNodeId"])
90+
"featureSchemaId": self.feature_schema_id
91+
}
9292

9393
def add_option(self, option: Option):
9494
if option.value in (o.value for o in self.options):
@@ -97,9 +97,9 @@ def add_option(self, option: Option):
9797
f"for classification '{self.name}'.")
9898
self.options.append(option)
9999

100+
100101
@dataclass
101102
class Tool:
102-
103103
class Type(Enum):
104104
POLYGON = "polygon"
105105
SEGMENTATION = "superpixel"
@@ -108,75 +108,83 @@ class Type(Enum):
108108
LINE = "line"
109109
NER = "named-entity"
110110

111-
tool: Type
112-
name: str
111+
tool: Type
112+
name: str
113113
required: bool = False
114114
color: str = "#000000"
115115
classifications: List[Classification] = field(default_factory=list)
116116
schema_id: Optional[str] = None
117117
feature_schema_id: Optional[str] = None
118118

119-
def asdict(self) -> Dict[str,Any]:
119+
@classmethod
120+
def from_dict(cls, dictionary: Dict[str, Any]):
121+
return Tool(name=dictionary['name'],
122+
schema_id=dictionary["schemaNodeId"],
123+
feature_schema_id=dictionary["featureSchemaId"],
124+
required=dictionary["required"],
125+
tool=Tool.Type(dictionary["tool"]),
126+
classifications=[
127+
Classification.from_dict(c)
128+
for c in dictionary["classifications"]
129+
],
130+
color=dictionary["color"])
131+
132+
def asdict(self) -> Dict[str, Any]:
120133
return {
121134
"tool": self.tool.value,
122135
"name": self.name,
123136
"required": self.required,
124137
"color": self.color,
125138
"classifications": [c.asdict() for c in self.classifications],
126139
"schemaNodeId": self.schema_id,
127-
"featureSchemaId": self.feature_schema_id}
128-
129-
@classmethod
130-
def from_dict(cls, dictionary: Dict[str,Any]):
131-
return Tool(
132-
name = dictionary['name'],
133-
schema_id = dictionary["schemaNodeId"],
134-
feature_schema_id = dictionary["featureSchemaId"],
135-
required = dictionary["required"],
136-
tool = Tool.Type(dictionary["tool"]),
137-
classifications = [Classification.from_dict(c)
138-
for c in dictionary["classifications"]],
139-
color = dictionary["color"])
140+
"featureSchemaId": self.feature_schema_id
141+
}
140142

141143
def add_classification(self, classification: Classification):
142-
if classification.instructions in (c.instructions
144+
if classification.instructions in (c.instructions
143145
for c in self.classifications):
144146
raise InconsistentOntologyException(
145147
f"Duplicate nested classification '{classification.instructions}' "
146148
f"for tool '{self.name}'")
147-
self.classifications.append(classification)
149+
self.classifications.append(classification)
150+
148151

149152
@dataclass
150153
class Ontology:
151-
154+
152155
tools: List[Tool] = field(default_factory=list)
153156
classifications: List[Classification] = field(default_factory=list)
154157

158+
@classmethod
159+
def from_dict(cls, dictionary: Dict[str, Any]):
160+
return Ontology(tools=[Tool.from_dict(t) for t in dictionary["tools"]],
161+
classifications=[
162+
Classification.from_dict(c)
163+
for c in dictionary["classifications"]
164+
])
165+
166+
def asdict(self):
167+
return {
168+
"tools": [t.asdict() for t in self.tools],
169+
"classifications": [c.asdict() for c in self.classifications]
170+
}
171+
155172
@classmethod
156173
def from_project(cls, project: Project):
157174
ontology = project.ontology().normalized
158175
return Ontology.from_dict(ontology)
159176

160-
def add_tool(self, tool: Tool) -> Tool:
177+
def add_tool(self, tool: Tool) -> Tool:
161178
if tool.name in (t.name for t in self.tools):
162179
raise InconsistentOntologyException(
163180
f"Duplicate tool name '{tool.name}'. ")
164181
self.tools.append(tool)
165-
166-
def add_classification(self, classification: Classification) -> Classification:
167-
if classification.instructions in (c.instructions
182+
183+
def add_classification(self,
184+
classification: Classification) -> Classification:
185+
if classification.instructions in (c.instructions
168186
for c in self.classifications):
169187
raise InconsistentOntologyException(
170-
f"Duplicate classification instructions '{classification.instructions}'. ")
188+
f"Duplicate classification instructions '{classification.instructions}'. "
189+
)
171190
self.classifications.append(classification)
172-
173-
def asdict(self):
174-
return {
175-
"tools": [t.asdict() for t in self.tools],
176-
"classifications": [c.asdict() for c in self.classifications]}
177-
178-
@classmethod
179-
def from_dict(cls, dictionary: Dict[str,Any]):
180-
return Ontology(
181-
tools = [Tool.from_dict(t) for t in dictionary["tools"]],
182-
classifications = [Classification.from_dict(c) for c in dictionary["classifications"]])

0 commit comments

Comments
 (0)