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
+ """
1
12
import csv
2
13
import random
3
14
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
+ """
5
27
winlist = []
6
- with open (args . csv ) as csvfile :
28
+ with open (argcsv ) as csvfile :
7
29
reader = csv .DictReader (csvfile )
8
30
for row in reader :
9
31
username = row ['What username do you go by online?' ]
10
32
if row ['What username do you go by online?' ] == '-' :
11
33
username = row ['Ticket First Name' ]
12
34
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
+ """
14
48
slide = open ('winners.html' , 'w' )
15
49
slide .write ("""
16
50
<!doctype html>
@@ -24,7 +58,7 @@ def htmlwriter(winlist):
24
58
<h2>SMKmeetup Giveaway Winners</h2>
25
59
<ul class="multi-12">
26
60
""" )
27
- for i in random .sample (winlist , len (winlist ))[0 :totalWinners ]:
61
+ for i in random .sample (winlist , len (winlist ))[0 :total_winners ]:
28
62
winner = i [:12 ] + (i [12 :] and '...' )
29
63
slide .write ("<li>" + winner + "</li>\n " )
30
64
slide .write ("""
@@ -33,9 +67,26 @@ def htmlwriter(winlist):
33
67
</html>
34
68
""" )
35
69
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
+ """
37
81
parser = argparse .ArgumentParser ()
38
82
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