def choose_winner_cli(participants, date): """choose a winner from PARTICIPANTS while using DATE to count seed""" with open(participants) as fp: raw_usernames = load(fp) hashed_participants = process_usernames(raw_usernames) hashed_winner = choose_winner(hashed_participants, date) participants = [prepare_username(uname) for uname in raw_usernames] winner = find_winner(hashed_winner, participants) click.echo(winner)
def prepare_original_cli(source, destination): """Parse a file with a of participants, clean it up and save to json""" with open(source) as fp: lines = (line for line in fp) valid_lines = filter_usernames(lines) prepared_usernames = [prepare_username(uname) for uname in valid_lines] with open(destination, "w") as ofp: dump(prepared_usernames, ofp)
def choose_winner_cli(participants, date, n): """Choose a winner from PARTICIPANTS while using DATE to count seed""" if date is None: date = get_date_from_filename(participants) with open(participants) as fp: raw_usernames = load(fp) hashed_participants = process_usernames(raw_usernames) hashed_winners = choose_winners(hashed_participants, date, n=n) participants = [prepare_username(uname) for uname in raw_usernames] winners = find_winners(hashed_winners, participants) click.echo(" ".join(winners))
def verify_choice_cli(hashed_participants, username): """ Verify given USERNAME is present in a HASHED_PARTICIPANTS file. """ with open(hashed_participants) as fp: hashed_participants = load(fp) hashed_username = hash_username(prepare_username(username)) click.echo(f"Hashed username: {hashed_username}") is_in_participants = hashed_username in hashed_participants if is_in_participants: click.echo( f"{username} may not be a winner. But it is present in a list of participants." ) else: click.echo(f"{username} is a creep and does not belong here :(")
def verify_choice_cli(hashed_participants, date, username): """ Verify choice using a HASHED_PARTICIPANTS file and DATE. Optionally you can provide a username to verify that it was chosen. """ with open(hashed_participants) as fp: hashed_participants = load(fp) hashed_winner = choose_winner(hashed_participants, date) click.echo(f"Winner's hash is {hashed_winner}.") if username: prepared_username = prepare_username(username) is_winner = verify_winner(prepared_username, hashed_winner) if is_winner: click.echo(f"Yup! {username} is definitely a winner.") else: click.echo( f"Unfortunately, {username} is not a winner. But don't worry, better luck next time!" )
def verify_choice_cli(hashed_participants, date, username, n): """ Verify choice using a HASHED_PARTICIPANTS file and DATE. Optionally you can provide a username to verify that it was chosen. """ if date is None: date = get_date_from_filename(hashed_participants) with open(hashed_participants) as fp: hashed_participants = load(fp) hashed_winners = choose_winners(hashed_participants, date, n=n) click.echo(f"Winners are: {', '.join(hashed_winners)}.") if username: prepared_username = prepare_username(username) is_winner = any(verify_winner(prepared_username, hashed_winner) for hashed_winner in hashed_winners) if is_winner: click.echo(f"Yup! {username} is definitely a winner.") else: click.echo( f"Unfortunately, {username} is not a winner. But don't worry, better luck next time!" )