Skip to content

Instantly share code, notes, and snippets.

@sgibson91
Created April 1, 2020 18:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sgibson91/bd9c3c56a0e8b35f0009ea831b7df1ab to your computer and use it in GitHub Desktop.
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)
#!/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