Skip to content

Commit

Permalink
Adding more general function with better name (#893)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
mojtaba-komeili and JackUrb committed Sep 2, 2022
1 parent d8f5c3b commit 48c0c23
Showing 1 changed file with 32 additions and 14 deletions.
46 changes: 32 additions & 14 deletions mephisto/abstractions/providers/mturk/utils/script_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -35,33 +37,49 @@ 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:
give_worker_qualification(
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
Expand Down

0 comments on commit 48c0c23

Please sign in to comment.