Skip to content
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

Adding more general function with better name #893

Merged
merged 12 commits into from
Sep 2, 2022
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