Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # Please read the Markdown man page at http://pastebin.com/5MVbFNH1
- # To render the man page with pandoc use the command
- # pandoc Pywerball.md -s -t man | /usr/bin/man -l -
- import sys, getopt, urllib, random, datetime
- winnums = {}
- prizes = {"5+1": "Jackpot", "5+0": 1000000, "4+1": 50000, "4+0": 100, "3+1": 100, "3+0": 7, "2+1": 7, "1+1": 4, "0+1": 4}
- odds = {"5+1": 292201338.0, "5+0": 11688053.52, "4+1": 913129.18, "4+0": 36525.17, "3+1": 14494.11, "3+0": 579.76, "2+1": 701.33, "1+1": 91.98, "0+1": 38.32}
- g_dt = ""
- def usage():
- print """
- Pywerball - Powerball lottery utility
- -c, --check FILE
- Read powerball numbers from the FILE and check them. The FILE is
- formatted like so:
- Draw Date WB1 WB2 WB3 WB4 WB5 PB
- 11/26/2016 01 20 37 51 67 18
- 11/26/2016 07 51 58 63 67 24
- 11/30/2016 17 19 21 37 44 16
- -d, --date DATE
- If DATE is prev (or next), then show the previous (or next) draw date.
- Otherwise set the date to DATE, which must be formatted as 'MM/DD/YYYY'
- -g, --generate NUMBER
- Generate NUMBER random draws. The output format is the same as the
- input format for --check FILE. The date is the next draw date,
- unless overridden with the --date DATE option.
- -h, --help
- Display this help and exit.
- -n, --numbers
- Show the winning numbers. Use the previous draw date, unless over-
- ridden with the --date DATE option.
- -o, --odds COMBO
- Show the odds of getting the combination of white balls plus power
- balls. COMBO must be in the format 'W+P' where W is 0 to 5 and
- P is 0 or 1, e.g., -o '0+1' -o '5+0'
- -p, --prize COMBO
- Show the prize for getting the combination of white balls plus power
- balls. COMBO must be in the format 'W+P' where W is 0 to 5 and
- P is 0 or 1, e.g., -p '0+1' -p '5+0'
- -v, --version
- Display the version and exit.
- """
- sys.exit()
- def get_winnums():
- global winnums
- link = "http://www.powerball.com/powerball/winnums-text.txt"
- f = urllib.urlopen(link)
- header = f.readline()
- for line in f:
- line = line.strip()
- if line:
- columns = line.split()
- dt = columns[0]
- wb = set(columns[1:6])
- pb = columns[6]
- winnum = (wb, pb)
- winnums[dt] = winnum
- f.close()
- def check(inputfile):
- # Read file into array
- f = open(inputfile, 'r')
- header = f.readline()
- draws = []
- for line in f:
- line = line.strip()
- if line:
- columns = line.split()
- dt = columns[0]
- wb = set(columns[1:6])
- pb = columns[6]
- draw = (dt, wb, pb)
- draws.append(draw)
- f.close()
- # Return if nothing to do
- if not draws:
- return 0
- # Make sure winnums is populated
- if not winnums:
- get_winnums()
- # Print header
- print "Draw Date WB1 WB2 WB3 WB4 WB5 PB"
- # Check each draw
- for draw in draws:
- dt = draw[0]
- wb1 = draw[1]
- wb = list(wb1)
- wb.sort()
- wbstr = ' '.join(str(i) for i in wb)
- pb1 = draw[2]
- if dt in winnums:
- wb2 = winnums[dt][0]
- pb2 = winnums[dt][1]
- # They key to the prizes dictionary is
- # {number of matching white balls}+{number of matching power balls}
- # Use set intersection (&) to find matching white balls
- # Count matching white balls (with len) and power balls
- key = "{}+{}".format(len(wb1 & wb2), 1 if pb1 == pb2 else 0)
- if key in prizes:
- print "{} {} {} matches {} for ${}".format(dt, wbstr, pb1, key, str(prizes[key]))
- else:
- print "{} {} {} no match".format(dt, wbstr, pb1)
- else:
- print "{} {} {} error: could not find matching date online!".format(dt, wbstr, pb1)
- def get_next_date():
- today = datetime.date.today()
- wkday = today.weekday()
- if wkday == 2 or wkday == 5:
- offset = 0
- elif wkday == 0:
- offset = 2
- elif wkday == 1:
- offset = 1
- elif wkday == 3:
- offset = 2
- elif wkday == 4:
- offset = 1
- elif wkday == 6:
- offset = 3
- dt = today + datetime.timedelta(days=offset)
- return "{:%m/%d/%Y}".format(dt)
- def get_prev_date():
- today = datetime.date.today()
- wkday = today.weekday()
- if wkday == 0:
- offset = -2
- elif wkday == 1:
- offset = -3
- elif wkday == 2:
- offset = -4
- elif wkday == 3:
- offset = -1
- elif wkday == 4:
- offset = -2
- elif wkday == 5:
- offset = -3
- elif wkday == 6:
- offset = -1
- dt = today + datetime.timedelta(days=offset)
- return "{:%m/%d/%Y}".format(dt)
- def get_date(prev):
- if g_dt:
- dt = g_dt
- elif prev:
- dt = get_prev_date()
- else:
- dt = get_next_date()
- return dt
- def generate(num):
- print "Draw Date WB1 WB2 WB3 WB4 WB5 PB"
- dt = get_date(0)
- for draw in range(0,num):
- wb = range(1,70)
- pb = range(1,27)
- random.shuffle(wb)
- random.shuffle(pb)
- wb5 = wb[0:5]
- wb = list(wb5)
- wb.sort()
- pb1 = pb[0]
- wbstr = ' '.join("{:02d}".format(i) for i in wb)
- print "{} {} {:02d}".format(dt, wbstr, pb1)
- def get_nums():
- # Make sure winnums is populated
- if not winnums:
- get_winnums()
- dt = get_date(1)
- if dt in winnums:
- wb = winnums[dt][0]
- pb = winnums[dt][1]
- wb = list(wb)
- wb.sort()
- wbstr = ' '.join(str(i) for i in wb)
- print "Draw Date WB1 WB2 WB3 WB4 WB5 PB"
- print "{} {} {}".format(dt, wbstr, pb)
- else:
- print "error: could not find date matching {} online!".format(dt)
- def main(argv):
- global g_dt
- try:
- opts, args = getopt.getopt(argv,"c:d:g:hno:p:v",["check=", "date=", "generate=", "help", "numbers", "odds=", "prize=", "version"])
- except getopt.GetoptError:
- usage()
- for opt, arg in opts:
- if opt in ("-c", "--check"):
- check(arg)
- elif opt in ("-d", "--date"):
- if arg == "prev" or arg == "previous":
- print get_prev_date()
- elif arg == "next":
- print get_next_date()
- else:
- g_dt = arg
- elif opt in ("-g", "--generate"):
- generate(int(arg))
- elif opt in ("-h", "--help"):
- usage()
- elif opt in ("-n", "--numbers"):
- get_nums()
- elif opt in ("-o", "--odds"):
- if arg in odds:
- print "1 in {}".format(str(odds[arg]))
- else:
- print "error: could not find {} in table of odds.".format(arg)
- elif opt in ("-p", "--prize"):
- if arg in prizes:
- print "${}".format(prizes[arg])
- else:
- print "error: could not find {} in table of prizes.".format(arg)
- elif opt in ("-v", "--version"):
- print "Pywerball version 0.2 (2016-11-28)"
- if __name__ == "__main__":
- main(sys.argv[1:])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement