Created
April 1, 2020 18:10
-
-
Save sgibson91/bd9c3c56a0e8b35f0009ea831b7df1ab to your computer and use it in GitHub Desktop.
A python script to generate a competition winner from an input file of entries (one entry per line)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import argparse | |
import numpy as np | |
def parse_args(): | |
"""Construct the command line arguments | |
Returns: | |
args -- Parsed arguments | |
""" | |
DESCRIPTION = "Script to choose a competition winner from a file containing entries" | |
parser = argparse.ArgumentParser(description=DESCRIPTION) | |
parser.add_argument("file", help="File containing competition entries") | |
return parser.parse_args() | |
def pick_winner(input_file): | |
"""Pick a competition winner from a file containing entries. | |
One entry per line. | |
Arguments: | |
input_file {filename} -- File containing competition entries | |
""" | |
print("And the winner is...!") | |
# Read in file | |
with open(input_file, "r") as f: | |
text = f.read() | |
# Split entries by newline character | |
entries = text.split("\n") | |
# Generate random number from length of entries | |
indx = np.random.randint(low=0, high=len(entries)) | |
print(entries[indx]) | |
def main(): | |
"""Main function""" | |
args = parse_args() | |
pick_winner(args.input_file) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment