Skip to content

Commit 7f829ad

Browse files
authored
annofab.util.annotation_specs.AnnotationSpecsAccessorget_attribute関数にlabel引数を追加して、指定したラベルに含まれる属性情報を取得できるようにしました。 (#717)
1 parent 658f3d3 commit 7f829ad

File tree

2 files changed

+45
-7
lines changed

2 files changed

+45
-7
lines changed

annofabapi/util/annotation_specs.py

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,20 @@ def get_choice(choices: list[dict[str, Any]], *, choice_id: Optional[str] = None
8989
return result[0]
9090

9191

92-
def get_attribute(additionals: list[dict[str, Any]], *, attribute_id: Optional[str] = None, attribute_name: Optional[str] = None) -> dict[str, Any]:
92+
def get_attribute(
93+
additionals: list[dict[str, Any]],
94+
*,
95+
attribute_id: Optional[str] = None,
96+
attribute_name: Optional[str] = None,
97+
label: Optional[dict[str, Any]] = None,
98+
) -> dict[str, Any]:
9399
"""
94100
属性情報を取得します。
95101
96102
Args:
97103
attribute_id: 属性ID
98104
attribute_name: 属性名(英語)
105+
label: Noneでなければ、指定したラベルに紐づく属性情報を取得します。
99106
100107
Raises:
101108
ValueError: 'attribute_id'か'attribute_name'の指定方法が間違っている。または引数に合致する属性情報が見つからない。または複数見つかった。
@@ -110,10 +117,19 @@ def get_attribute(additionals: list[dict[str, Any]], *, attribute_id: Optional[s
110117
else:
111118
raise ValueError("'attribute_id'か'attribute_name'のどちらかはNone以外にしてください。")
112119

120+
label_name = None
121+
if label is not None:
122+
result = [e for e in result if e["additional_data_definition_id"] in label["additional_data_definitions"]]
123+
label_name = get_english_message(label["label_name"])
124+
113125
if len(result) == 0:
114-
raise ValueError(f"属性情報が見つかりませんでした。 :: attribute_id='{attribute_id}', attribute_name='{attribute_name}'")
126+
raise ValueError(
127+
f"属性情報が見つかりませんでした。 :: attribute_id='{attribute_id}', attribute_name='{attribute_name}', label_name='{label_name}'"
128+
)
115129
if len(result) > 1:
116-
raise ValueError(f"属性情報が複数({len(result)}件)見つかりました。 :: attribute_id='{attribute_id}', attribute_name='{attribute_name}'")
130+
raise ValueError(
131+
f"属性情報が複数({len(result)}件)見つかりました。 :: attribute_id='{attribute_id}', attribute_name='{attribute_name}', label_name='{label_name}'" # noqa: E501
132+
)
117133
return result[0]
118134

119135

@@ -159,15 +175,22 @@ def __init__(self, annotation_specs: dict[str, Any]) -> None:
159175
self.labels = annotation_specs["labels"]
160176
self.additionals = annotation_specs["additionals"]
161177

162-
def get_attribute(self, *, attribute_id: Optional[str] = None, attribute_name: Optional[str] = None) -> dict[str, Any]:
178+
def get_attribute(
179+
self, *, attribute_id: Optional[str] = None, attribute_name: Optional[str] = None, label: Optional[dict[str, Any]] = None
180+
) -> dict[str, Any]:
163181
"""
164182
属性情報を取得します。
165183
166184
Args:
167185
attribute_id: 属性ID
168186
attribute_name: 属性名(英語)
187+
label: Noneでなければ、指定したラベルに紐づく属性情報を取得します。
188+
189+
Raises:
190+
ValueError: 'attribute_id'か'attribute_name'の指定方法が間違っている。または引数に合致する属性情報が見つからない。または複数見つかった。
191+
169192
"""
170-
return get_attribute(self.additionals, attribute_id=attribute_id, attribute_name=attribute_name)
193+
return get_attribute(self.additionals, attribute_id=attribute_id, attribute_name=attribute_name, label=label)
171194

172195
def get_label(self, *, label_id: Optional[str] = None, label_name: Optional[str] = None) -> dict[str, Any]:
173196
"""
@@ -176,5 +199,9 @@ def get_label(self, *, label_id: Optional[str] = None, label_name: Optional[str]
176199
Args:
177200
label_id: ラベルID
178201
label_name: ラベル名(英語)
202+
203+
Raises:
204+
ValueError: 'label_id'か'label_name'の指定方法が間違っている。または引数に合致するラベル情報が見つからない。または複数見つかった。
205+
179206
"""
180207
return get_label(self.labels, label_id=label_id, label_name=label_name)

tests/util/test_annotation_specs.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ class Test__AnnotationSpecsAccessor:
3535
def setup_method(self):
3636
self.annotation_specs = {
3737
"labels": [
38-
{"label_id": "1", "label_name": {"messages": [{"lang": "en-US", "message": "Car"}]}},
39-
{"label_id": "2", "label_name": {"messages": [{"lang": "en-US", "message": "Bike"}]}},
38+
{"label_id": "1", "label_name": {"messages": [{"lang": "en-US", "message": "Car"}]}, "additional_data_definitions": ["1", "2"]},
39+
{"label_id": "2", "label_name": {"messages": [{"lang": "en-US", "message": "Bike"}]}, "additional_data_definitions": []},
4040
],
4141
"additionals": [
4242
{"additional_data_definition_id": "1", "name": {"messages": [{"lang": "en-US", "message": "Color"}]}},
@@ -73,6 +73,17 @@ def test_get_attribute_not_found(self):
7373
with pytest.raises(ValueError):
7474
self.accessor.get_attribute(attribute_id="3")
7575

76+
def test_get_attribute_by_id_and_label(self):
77+
label = self.accessor.get_label(label_id="1")
78+
attribute = self.accessor.get_attribute(attribute_id="1", label=label)
79+
assert attribute["additional_data_definition_id"] == "1"
80+
assert get_english_message(attribute["name"]) == "Color"
81+
82+
def test_get_attribute_by_id_and_label__not_found(self):
83+
label = self.accessor.get_label(label_id="2")
84+
with pytest.raises(ValueError):
85+
self.accessor.get_attribute(attribute_id="1", label=label)
86+
7687

7788
class Test__get_choice:
7889
def setup_method(self):

0 commit comments

Comments
 (0)