From 48c0c23f79f4ea0d94d500831b44b39c7d168b17 Mon Sep 17 00:00:00 2001 From: Mojtaba <11262163+mojtaba-komeili@users.noreply.github.com> Date: Thu, 1 Sep 2022 23:27:36 -0400 Subject: [PATCH] Adding more general function with better name (#893) * adding the new function name * pre-commit install While retrying this step, realized that we need `pip install pre-commit` if it is not already installed. * lint * Removing threadsafety as we already lock connections behind threads * testing cypress fix * testing cypress fix * testing cypress fix * adding the new function name * lint Co-authored-by: Jack Urbanek --- .../providers/mturk/utils/script_utils.py | 46 +++++++++++++------ 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/mephisto/abstractions/providers/mturk/utils/script_utils.py b/mephisto/abstractions/providers/mturk/utils/script_utils.py index 6de93c5e0..7836b2119 100644 --- a/mephisto/abstractions/providers/mturk/utils/script_utils.py +++ b/mephisto/abstractions/providers/mturk/utils/script_utils.py @@ -11,22 +11,24 @@ from mephisto.data_model.requester import Requester from mephisto.data_model.unit import Unit from tqdm import tqdm # type: ignore +from mephisto.utils.logger_core import get_logger + +logging = get_logger(name=__name__) if TYPE_CHECKING: from mephisto.abstractions.database import MephistoDB -def direct_soft_block_mturk_workers( +def direct_assign_qual_mturk_workers( db: "MephistoDB", worker_list: List[str], - soft_block_qual_name: str, + qual_name: str, requester_name: Optional[str] = None, ): """ - Directly assign the soft blocking MTurk qualification that Mephisto - associates with soft_block_qual_name to all of the MTurk worker ids - in worker_list. If requester_name is not provided, it will use the - most recently registered mturk requester in the database. + Directly assign MTurk qualification that Mephisto associates with qual_name + to all of the MTurk worker ids in worker_list. If requester_name is not provided, + it will use the most recently registered mturk requester in the database. """ reqs = db.find_requesters(requester_name=requester_name, provider_type="mturk") requester = reqs[-1] @@ -35,21 +37,17 @@ def direct_soft_block_mturk_workers( requester, MTurkRequester ), "Can only direct soft block mturk workers from mturk requester" - mturk_qual_details = requester.datastore.get_qualification_mapping( - soft_block_qual_name - ) + mturk_qual_details = requester.datastore.get_qualification_mapping(qual_name) if mturk_qual_details is not None: # Overrule the requester, as this qualification already exists requester = Requester.get(db, mturk_qual_details["requester_id"]) qualification_id = mturk_qual_details["mturk_qualification_id"] else: - qualification_id = requester._create_new_mturk_qualification( - soft_block_qual_name - ) + qualification_id = requester._create_new_mturk_qualification(qual_name) assert isinstance( requester, MTurkRequester - ), "Can only direct soft block mturk workers from mturk requester" + ), "Can only direct assign qualification (soft block) mturk workers from mturk requester" mturk_client = requester._get_client(requester._requester_name) for worker_id in tqdm(worker_list): try: @@ -57,11 +55,31 @@ def direct_soft_block_mturk_workers( mturk_client, worker_id, qualification_id, value=1 ) except Exception as e: - print( + logging.exception( f'Failed to give worker with ID: "{worker_id}" qualification with error: {e}. Skipping.' ) +def direct_soft_block_mturk_workers( + db: "MephistoDB", + worker_list: List[str], + soft_block_qual_name: str, + requester_name: Optional[str] = None, +): + """ + Directly assign the soft blocking MTurk qualification that Mephisto + associates with soft_block_qual_name to all of the MTurk worker ids + in worker_list. If requester_name is not provided, it will use the + most recently registered mturk requester in the database. + """ + direct_assign_qual_mturk_workers( + db=db, + worker_list=worker_list, + qual_name=soft_block_qual_name, + requester_name=requester_name, + ) + + def get_mturk_ids_from_unit_id(db, unit_id: str) -> Dict[str, Optional[str]]: """ Find the relevant mturk ids from the given mephisto unit id