1
1
from typing import Any , Literal , Optional , Union
2
2
3
3
import more_itertools
4
- from more_itertools import first_true
5
4
6
5
from annofabapi .models import Lang
7
6
@@ -70,21 +69,24 @@ def get_choice(choices: list[dict[str, Any]], *, choice_id: Optional[str] = None
70
69
choice_name: 選択肢名(英語)
71
70
72
71
Raises:
73
- ValueError: 'choice_id'か'choice_name'の指定方法が間違っている。または引数に合致する選択肢情報が見つからない。
72
+ ValueError: 'choice_id'か'choice_name'の指定方法が間違っている。または引数に合致する選択肢情報が見つからない。または複数見つかった。
74
73
75
74
"""
76
75
if choice_id is not None and choice_name is not None :
77
76
raise ValueError ("'choice_id'か'choice_name'のどちらかはNoneにしてください。" )
78
77
79
78
if choice_id is not None :
80
- result = first_true ( choices , pred = lambda e : e ["choice_id" ] == choice_id )
79
+ result = [ e for e in choices if e ["choice_id" ] == choice_id ]
81
80
elif choice_name is not None :
82
- result = first_true ( choices , pred = lambda e : get_english_message (e ["name" ]) == choice_name )
81
+ result = [ e for e in choices if get_english_message (e ["name" ]) == choice_name ]
83
82
else :
84
83
raise ValueError ("'choice_id'か'choice_name'のどちらかはNone以外にしてください。" )
85
- if result is None :
84
+
85
+ if len (result ) == 0 :
86
86
raise ValueError (f"選択肢情報が見つかりませんでした。 :: choice_id='{ choice_id } ', choice_name='{ choice_name } '" )
87
- return result
87
+ if len (result ) > 1 :
88
+ raise ValueError (f"選択肢情報が複数({ len (result )} 件)見つかりました。 :: choice_id='{ choice_id } ', choice_name='{ choice_name } '" )
89
+ return result [0 ]
88
90
89
91
90
92
def get_attribute (additionals : list [dict [str , Any ]], * , attribute_id : Optional [str ] = None , attribute_name : Optional [str ] = None ) -> dict [str , Any ]:
@@ -96,20 +98,23 @@ def get_attribute(additionals: list[dict[str, Any]], *, attribute_id: Optional[s
96
98
attribute_name: 属性名(英語)
97
99
98
100
Raises:
99
- ValueError: 'attribute_id'か'attribute_name'の指定方法が間違っている。または引数に合致する属性情報が見つからない。
101
+ ValueError: 'attribute_id'か'attribute_name'の指定方法が間違っている。または引数に合致する属性情報が見つからない。または複数見つかった。
100
102
"""
101
103
if attribute_id is not None and attribute_name is not None :
102
104
raise ValueError ("'attribute_id'か'attribute_name'のどちらかはNoneにしてください。" )
103
105
104
106
if attribute_id is not None :
105
- result = first_true ( additionals , pred = lambda e : e ["additional_data_definition_id" ] == attribute_id )
107
+ result = [ e for e in additionals if e ["additional_data_definition_id" ] == attribute_id ]
106
108
elif attribute_name is not None :
107
- result = first_true ( additionals , pred = lambda e : get_english_message (e ["name" ]) == attribute_name )
109
+ result = [ e for e in additionals if get_english_message (e ["name" ]) == attribute_name ]
108
110
else :
109
111
raise ValueError ("'attribute_id'か'attribute_name'のどちらかはNone以外にしてください。" )
110
- if result is None :
112
+
113
+ if len (result ) == 0 :
111
114
raise ValueError (f"属性情報が見つかりませんでした。 :: attribute_id='{ attribute_id } ', attribute_name='{ attribute_name } '" )
112
- return result
115
+ if len (result ) > 1 :
116
+ raise ValueError (f"属性情報が複数({ len (result )} 件)見つかりました。 :: attribute_id='{ attribute_id } ', attribute_name='{ attribute_name } '" )
117
+ return result [0 ]
113
118
114
119
115
120
def get_label (labels : list [dict [str , Any ]], * , label_id : Optional [str ] = None , label_name : Optional [str ] = None ) -> dict [str , Any ]:
@@ -121,21 +126,24 @@ def get_label(labels: list[dict[str, Any]], *, label_id: Optional[str] = None, l
121
126
label_name: ラベル名(英語)
122
127
123
128
Raises:
124
- ValueError: 'label_id'か'label_name'の指定方法が間違っている。または引数に合致するラベル情報が見つからない。
129
+ ValueError: 'label_id'か'label_name'の指定方法が間違っている。または引数に合致するラベル情報が見つからない。または複数見つかった。
125
130
126
131
"""
127
132
if label_id is not None and label_name is not None :
128
133
raise ValueError ("'label_id'か'label_name'のどちらかはNoneにしてください。" )
129
134
130
135
if label_id is not None :
131
- result = first_true ( labels , pred = lambda e : e ["label_id" ] == label_id )
136
+ result = [ e for e in labels if e ["label_id" ] == label_id ]
132
137
elif label_name is not None :
133
- result = first_true ( labels , pred = lambda e : get_english_message (e ["label_name" ]) == label_name )
138
+ result = [ e for e in labels if get_english_message (e ["label_name" ]) == label_name ]
134
139
else :
135
140
raise ValueError ("'label_id'か'label_name'のどちらかはNone以外にしてください。" )
136
- if result is None :
141
+
142
+ if len (result ) == 0 :
137
143
raise ValueError (f"ラベル情報が見つかりませんでした。 :: label_id='{ label_id } ', label_name='{ label_name } '" )
138
- return result
144
+ if len (result ) > 1 :
145
+ raise ValueError (f"ラベル情報が複数({ len (result )} 件)見つかりました。 :: label_id='{ label_id } ', label_name='{ label_name } '" )
146
+ return result [0 ]
139
147
140
148
141
149
class AnnotationSpecsAccessor :
0 commit comments