Skip to content

Instantly share code, notes, and snippets.

@wesslen
Last active December 24, 2023 16:28
Show Gist options
  • Save wesslen/4b343f7be94cfbbae98318016c9a7c3c to your computer and use it in GitHub Desktop.
Save wesslen/4b343f7be94cfbbae98318016c9a7c3c to your computer and use it in GitHub Desktop.
Python function to map random user ID's (e.g., crowdsource platform) to a unique Prodigy session name
import random
import string
def generate_random_userids(num_ids):
passwords = []
for _ in range(num_ids):
password = ''.join(random.choices(string.ascii_letters + string.digits, k=12))
passwords.append(password)
return passwords
def assign_session_id(new_user_id, session_ids):
# assumes session_ids are in order, hence next would be 1 more than length
next_session_number = len(session_ids) + 1
next_session_id = "user" + str(next_session_number)
return {new_user_id: next_session_id}
## Test example
# new_user_id = "gxG1SyMBJb6I"
# assigned_sessions = [
# {"aASB1k10tYmA": "user1"},
# {"6rSei5t0sb8f": "user2"},
# {"GiFTnff72opR": "user3"},
# {"gzFuSyEuIJwh": "user4"}
# ]
# result = assign_session_id(new_user_id, assigned_sessions)
# print(result)
# {"gxG1SyMBJb6I": "user5"}
# General example
user_ids = generate_random_userids(10)
assigned_sessions = []
for new_user_id in user_ids:
result = assign_session_id(new_user_id, assigned_sessions)
assigned_sessions.append(result)
for key,val in result.items():
print(f"For user {key}, send them to https://my-prodigy-url.com?session={val}".format(key,val))
# For user aASB1k10tYmA, send them to https://my-prodigy-url.com?session=user1
# For user 6rSei5t0sb8f, send them to https://my-prodigy-url.com?session=user2
# For user GiFTnff72opR, send them to https://my-prodigy-url.com?session=user3
# For user gzFuSyEuIJwh, send them to https://my-prodigy-url.com?session=user4
# For user gxG1SyMBJb6I, send them to https://my-prodigy-url.com?session=user5
# For user wHV0a8xbqskh, send them to https://my-prodigy-url.com?session=user6
# For user TgNjgNBr0wxt, send them to https://my-prodigy-url.com?session=user7
# For user EL7nHI9nMY5b, send them to https://my-prodigy-url.com?session=user8
# For user mrCMIbRqwGTJ, send them to https://my-prodigy-url.com?session=user9
# For user AvZqcq7WsOaH, send them to https://my-prodigy-url.com?session=user10
@wesslen
Copy link
Author

wesslen commented Dec 24, 2023

LLM alternative recommendation:

import random
import string

def generate_random_string(num_strings, length=12):
    """Generates a list of random strings with specified length composed of letters and digits."""
    return [''.join(random.choices(string.ascii_letters + string.digits, k=length)) for _ in range(num_strings)]

def create_session_mapping(new_user_id, session_ids):
    """Creates a session ID mapping for a new user ID based on the current session IDs count."""
    next_session_number = len(session_ids) + 1
    next_session_id = f"user{next_session_number}"
    return {new_user_id: next_session_id}

# Main procedure
num_user_ids = 10
user_ids = generate_random_string(num_user_ids)

assigned_sessions = []
for new_user_id in user_ids:
    result = create_session_mapping(new_user_id, assigned_sessions)
    assigned_sessions.append(result)
    for key, val in result.items():
        print(f"For user {key}, send them to https://my-prodigy-url.com?session={val}")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment