Skip to content

Commit e93e546

Browse files
committed
Put everything into methods and added docstrings.
1 parent fc08f3c commit e93e546

File tree

1 file changed

+59
-8
lines changed

1 file changed

+59
-8
lines changed

raffle.py

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,50 @@
1+
"""
2+
This program is used to generate an html file with a list of randomly-selected winners.
3+
It can be used for giveaways, raffles, etc. so long as the input csv is properly
4+
formatted (for now).
5+
6+
Todo:
7+
* Add args to select a specified column.
8+
* Improve docstrings. Some of the phrasing used in the docstrings are...bad.
9+
* Write a readme for easier usage.
10+
11+
"""
112
import csv
213
import random
314
import argparse
4-
def asfhkjljafsk():
15+
def gen_list(argcsv):
16+
"""
17+
This function generates the list of possible winners to be passed into html_writer().
18+
19+
Args:
20+
argcsv - csv file from parse_args() to read and pull the list of possible winners from
21+
Returns:
22+
winlist - the list of possible winners. It takes in the ticketholder's
23+
username and ticketnumber and appends them before inserting them into
24+
winlist to be passed to html_writer()
25+
26+
"""
527
winlist = []
6-
with open(args.csv) as csvfile:
28+
with open(argcsv) as csvfile:
729
reader = csv.DictReader(csvfile)
830
for row in reader:
931
username = row['What username do you go by online?']
1032
if row['What username do you go by online?'] == '-':
1133
username = row['Ticket First Name']
1234
winlist.append(row['Number'] + " " + username)
13-
def htmlwriter(winlist):
35+
return winlist
36+
def html_writer(winlist, total_winners):
37+
"""
38+
This function writes the html file to display the winners.
39+
40+
Args:
41+
winlist - list of winners generated by gen_list()
42+
total_winners - total number of specified winners taken from command line args
43+
44+
Returns:
45+
None. Writes an html file.
46+
47+
"""
1448
slide = open('winners.html', 'w')
1549
slide.write("""
1650
<!doctype html>
@@ -24,7 +58,7 @@ def htmlwriter(winlist):
2458
<h2>SMKmeetup Giveaway Winners</h2>
2559
<ul class="multi-12">
2660
""")
27-
for i in random.sample(winlist, len(winlist))[0:totalWinners]:
61+
for i in random.sample(winlist, len(winlist))[0:total_winners]:
2862
winner = i[:12] + (i[12:] and '...')
2963
slide.write("<li>"+winner+"</li>\n")
3064
slide.write("""
@@ -33,9 +67,26 @@ def htmlwriter(winlist):
3367
</html>
3468
""")
3569
slide.close()
36-
def main():
70+
def parse_args():
71+
"""
72+
This function parses the arguments from the command line.
73+
74+
Args:
75+
None
76+
77+
Returns:
78+
args object to be passed to other functions
79+
80+
"""
3781
parser = argparse.ArgumentParser()
3882
parser.add_argument("csv", help="the location of the csv input")
39-
parser.add_argument("totalWinners", type=int, help="total number of selected winners")
40-
args = parser.parse_args()
41-
totalWinners = args.totalWinners
83+
parser.add_argument("total_winners", type=int, help="total number of selected winners")
84+
return parser.parse_args()
85+
def main():
86+
"""
87+
This function calls all the necessary functions needed for the program to run.
88+
"""
89+
args = parse_args()
90+
html_writer(gen_list(args.csv), args.total_winners)
91+
if __name__ == "__main__":
92+
main()

0 commit comments

Comments
 (0)