Skip to content

utils.pyを整理 #145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,15 @@
* 現在、APIは開発途上版です。予告なく互換性のない変更がある可能性をご了承ください。
* put, post, delete系のメソッドを間違えて実行してしまわないよう、注意してください。特に「プロジェクト削除」や「アノテーション仕様更新」のAPIには十分注意してください。

## 廃止予定
# 廃止予定

## 2020-05-01以降 utilsのいくつかのメソッドを非公開
以下のメソッドを非公開にします。以下のメソッドは本来非公開用でして、外部で利用することを想定していなかったためです。
* utils.raise_for_status
* utils.log_error_response
* utils.download



# Features
cURLやPostmanなどよりも簡単にAnnoFab Web APIにアクセスできます。
Expand Down
2 changes: 1 addition & 1 deletion annofabapi/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.29.2'
__version__ = '0.29.3'
26 changes: 11 additions & 15 deletions annofabapi/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import requests
from requests.structures import CaseInsensitiveDict

from annofabapi.models import TaskHistory, TaskPhase
from annofabapi.models import Task, TaskHistory, TaskPhase


def raise_for_status(response: requests.Response):
Expand Down Expand Up @@ -222,21 +222,17 @@ def get_task_history_index_skipped_inspection(task_history_list: List[TaskHistor
return index_list


def first_true(iterable, default=None, pred=None):
def can_put_annotation(task: Task, my_account_id: str) -> bool:
"""
Returns the first true value in the iterable.
対象タスクが、`put_annotation` APIで、アノテーションを更新できる状態かどうか。
過去に担当者が割り当たっている場合、または現在の担当者自分自身の場合は、アノテーションを更新できる。

If no true value is found, returns *default*

If *pred* is not None, returns the first item for which
``pred(item) == True`` .

>>> first_true(range(10))
1
>>> first_true(range(10), pred=lambda x: x > 5)
6
>>> first_true(range(10), default='missing', pred=lambda x: x > 9)
'missing'
Args:
task: 対象タスク
my_account_id: 自分(ログインしているユーザ)のアカウントID

Returns:
Trueならば、タスクの状態を変更せずに`put_annotation` APIを実行できる。
"""
return next(filter(pred, iterable), default)
# ログインユーザはプロジェクトオーナであること前提
return len(task["histories_by_phase"]) == 0 or task["account_id"] == my_account_id
14 changes: 9 additions & 5 deletions annofabapi/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from annofabapi.models import (AnnotationDataHoldingType, AnnotationSpecsV1, InputData, Inspection, InspectionStatus,
Instruction, JobInfo, JobStatus, JobType, MyOrganization, Organization,
OrganizationMember, Project, ProjectMember, SupplementaryData, Task)
from annofabapi.utils import allow_404_error, first_true
from annofabapi.utils import allow_404_error

logger = logging.getLogger(__name__)

Expand All @@ -41,6 +41,10 @@ class AnnotationSpecsRelation:
choice_id: Dict[ChoiceKey, ChoiceKey]


def __first_true(iterable, default=None, pred=None):
return next(filter(pred, iterable), default)


class Wrapper:
"""
AnnofabApiのラッパー.
Expand Down Expand Up @@ -389,15 +393,15 @@ def get_annotation_specs_relation(self, src_project_id: str, dest_project_id: st
dict_label_id: Dict[str, str] = {}
for src_label in src_annotation_specs["labels"]:
src_label_name_en = self.__get_label_name_en(src_label)
dest_label = first_true(dest_labels, pred=lambda e, f=src_label_name_en: self.__get_label_name_en(e) == f)
dest_label = __first_true(dest_labels, pred=lambda e, f=src_label_name_en: self.__get_label_name_en(e) == f)
if dest_label is not None:
dict_label_id[src_label["label_id"]] = dest_label["label_id"]

dict_additional_data_definition_id: Dict[str, str] = {}
dict_choice_id: Dict[ChoiceKey, ChoiceKey] = {}
for src_additional in src_annotation_specs["additionals"]:
src_additional_name_en = self.__get_additional_data_definition_name_en(src_additional)
dest_additional = first_true(
dest_additional = __first_true(
dest_additionals,
pred=lambda e, f=src_additional_name_en: self.__get_additional_data_definition_name_en(e) == f)
if dest_additional is None:
Expand All @@ -409,8 +413,8 @@ def get_annotation_specs_relation(self, src_project_id: str, dest_project_id: st
dest_choices = dest_additional["choices"]
for src_choice in src_additional["choices"]:
src_choice_name_en = self.__get_choice_name_en(src_choice)
dest_choice = first_true(dest_choices,
pred=lambda e, f=src_choice_name_en: self.__get_choice_name_en(e) == f)
dest_choice = __first_true(dest_choices,
pred=lambda e, f=src_choice_name_en: self.__get_choice_name_en(e) == f)
if dest_choice is not None:
dict_choice_id[ChoiceKey(src_additional["additional_data_definition_id"],
src_choice["choice_id"])] = ChoiceKey(
Expand Down
2 changes: 1 addition & 1 deletion generate/swagger/swagger.v2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ info:
WebhookID | プロジェクト内で一意
入力データセットID | 組織内で一意

version: 0.78.0
version: 0.78.2
title: AnnoFab Web API
x-logo:
url: "https://annofab.com/images/logo_landscape.png"
Expand Down
2 changes: 1 addition & 1 deletion generate/swagger/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ info:
WebhookID | プロジェクト内で一意
入力データセットID | 組織内で一意

version: 0.78.0
version: 0.78.2
title: AnnoFab Web API
x-logo:
url: "https://annofab.com/images/logo_landscape.png"
Expand Down