Advertisement
krusader74

Pywerball

Nov 28th, 2016
420
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.33 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. # Please read the Markdown man page at http://pastebin.com/5MVbFNH1
  4. # To render the man page with pandoc use the command
  5. # pandoc Pywerball.md -s -t man | /usr/bin/man -l -
  6.  
  7. import sys, getopt, urllib, random, datetime
  8.  
  9. winnums = {}
  10.  
  11. 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}
  12.  
  13. 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}
  14.  
  15. g_dt = ""
  16.  
  17. def usage():
  18.     print """
  19. Pywerball - Powerball lottery utility
  20.  
  21.    -c, --check FILE
  22.        Read powerball numbers from the FILE and check them. The FILE is
  23.        formatted like so:
  24.  
  25.            Draw Date   WB1 WB2 WB3 WB4 WB5 PB
  26.            11/26/2016  01  20  37  51  67  18
  27.            11/26/2016  07  51  58  63  67  24
  28.            11/30/2016  17  19  21  37  44  16
  29.  
  30.    -d, --date DATE
  31.        If DATE is prev (or next), then show the previous (or next) draw date.
  32.        Otherwise set the date to DATE, which must be formatted as 'MM/DD/YYYY'
  33.  
  34.    -g, --generate NUMBER
  35.        Generate NUMBER random draws. The output format is the same as the
  36.        input format for --check FILE. The date is the next draw date,
  37.        unless overridden with the --date DATE option.
  38.  
  39.    -h, --help
  40.        Display this help and exit.
  41.  
  42.    -n, --numbers
  43.        Show the winning numbers. Use the previous draw date, unless over-
  44.        ridden with the --date DATE option.
  45.  
  46.    -o, --odds COMBO
  47.        Show the odds of getting the combination of white balls plus power
  48.        balls. COMBO must be in the format 'W+P' where W is 0 to 5 and
  49.        P is 0 or 1, e.g., -o '0+1' -o '5+0'
  50.  
  51.    -p, --prize COMBO
  52.        Show the prize for getting the combination of white balls plus power
  53.        balls. COMBO must be in the format 'W+P' where W is 0 to 5 and
  54.        P is 0 or 1, e.g., -p '0+1' -p '5+0'
  55.  
  56.    -v, --version
  57.        Display the version and exit.
  58. """
  59.     sys.exit()
  60.  
  61.  
  62. def get_winnums():
  63.     global winnums
  64.     link = "http://www.powerball.com/powerball/winnums-text.txt"
  65.     f = urllib.urlopen(link)
  66.     header = f.readline()
  67.     for line in f:
  68.         line = line.strip()
  69.         if line:
  70.             columns = line.split()
  71.             dt = columns[0]
  72.             wb = set(columns[1:6])
  73.             pb = columns[6]
  74.             winnum = (wb, pb)
  75.             winnums[dt] = winnum
  76.     f.close()
  77.  
  78.  
  79. def check(inputfile):
  80.     # Read file into array
  81.     f = open(inputfile, 'r')
  82.     header = f.readline()
  83.     draws = []
  84.     for line in f:
  85.         line = line.strip()
  86.         if line:
  87.             columns = line.split()
  88.             dt = columns[0]
  89.             wb = set(columns[1:6])
  90.             pb = columns[6]
  91.             draw = (dt, wb, pb)
  92.             draws.append(draw)
  93.     f.close()
  94.     # Return if nothing to do
  95.     if not draws:
  96.         return 0
  97.     # Make sure winnums is populated
  98.     if not winnums:
  99.         get_winnums()
  100.     # Print header
  101.     print "Draw Date   WB1 WB2 WB3 WB4 WB5 PB"
  102.     # Check each draw
  103.     for draw in draws:
  104.         dt = draw[0]
  105.         wb1 = draw[1]
  106.         wb = list(wb1)
  107.         wb.sort()
  108.         wbstr = '  '.join(str(i) for i in wb)
  109.         pb1 = draw[2]
  110.         if dt in winnums:
  111.             wb2 = winnums[dt][0]
  112.             pb2 = winnums[dt][1]
  113.             # They key to the prizes dictionary is
  114.             # {number of matching white balls}+{number of matching power balls}
  115.             # Use set intersection (&) to find matching white balls
  116.             # Count matching white balls (with len) and power balls
  117.             key = "{}+{}".format(len(wb1 & wb2), 1 if pb1 == pb2 else 0)
  118.             if key in prizes:
  119.                 print "{}  {}  {}  matches {} for ${}".format(dt, wbstr, pb1, key, str(prizes[key]))
  120.             else:
  121.                 print "{}  {}  {}  no match".format(dt, wbstr, pb1)
  122.         else:
  123.             print "{}  {}  {}  error: could not find matching date online!".format(dt, wbstr, pb1)
  124.  
  125.  
  126. def get_next_date():
  127.     today = datetime.date.today()
  128.     wkday = today.weekday()
  129.     if wkday == 2 or wkday == 5:
  130.         offset = 0
  131.     elif wkday == 0:
  132.         offset = 2
  133.     elif wkday == 1:
  134.         offset = 1
  135.     elif wkday == 3:
  136.         offset = 2
  137.     elif wkday == 4:
  138.         offset = 1
  139.     elif wkday == 6:
  140.         offset = 3
  141.     dt = today + datetime.timedelta(days=offset)
  142.     return "{:%m/%d/%Y}".format(dt)
  143.  
  144.  
  145. def get_prev_date():
  146.     today = datetime.date.today()
  147.     wkday = today.weekday()
  148.     if wkday == 0:
  149.         offset = -2
  150.     elif wkday == 1:
  151.         offset = -3
  152.     elif wkday == 2:
  153.         offset = -4
  154.     elif wkday == 3:
  155.         offset = -1
  156.     elif wkday == 4:
  157.         offset = -2
  158.     elif wkday == 5:
  159.         offset = -3
  160.     elif wkday == 6:
  161.         offset = -1
  162.     dt = today + datetime.timedelta(days=offset)
  163.     return "{:%m/%d/%Y}".format(dt)
  164.  
  165.  
  166. def get_date(prev):
  167.     if g_dt:
  168.         dt = g_dt
  169.     elif prev:
  170.         dt = get_prev_date()
  171.     else:
  172.         dt = get_next_date()
  173.     return dt
  174.  
  175.  
  176. def generate(num):
  177.     print "Draw Date   WB1 WB2 WB3 WB4 WB5 PB"
  178.     dt = get_date(0)
  179.     for draw in range(0,num):
  180.         wb = range(1,70)
  181.         pb = range(1,27)
  182.         random.shuffle(wb)
  183.         random.shuffle(pb)
  184.         wb5 = wb[0:5]
  185.         wb = list(wb5)
  186.         wb.sort()
  187.         pb1 = pb[0]
  188.         wbstr = '  '.join("{:02d}".format(i) for i in wb)
  189.         print "{}  {}  {:02d}".format(dt, wbstr, pb1)
  190.  
  191.  
  192. def get_nums():
  193.     # Make sure winnums is populated
  194.     if not winnums:
  195.         get_winnums()
  196.     dt = get_date(1)
  197.     if dt in winnums:
  198.         wb = winnums[dt][0]
  199.         pb = winnums[dt][1]
  200.         wb = list(wb)
  201.         wb.sort()
  202.         wbstr = '  '.join(str(i) for i in wb)
  203.         print "Draw Date   WB1 WB2 WB3 WB4 WB5 PB"
  204.         print "{}  {}  {}".format(dt, wbstr, pb)
  205.     else:
  206.         print "error: could not find date matching {} online!".format(dt)
  207.  
  208.  
  209. def main(argv):
  210.     global g_dt
  211.     try:
  212.         opts, args = getopt.getopt(argv,"c:d:g:hno:p:v",["check=", "date=", "generate=", "help", "numbers", "odds=", "prize=", "version"])
  213.     except getopt.GetoptError:
  214.         usage()
  215.     for opt, arg in opts:
  216.         if opt in ("-c", "--check"):
  217.             check(arg)
  218.         elif opt in ("-d", "--date"):
  219.             if arg == "prev" or arg == "previous":
  220.                 print get_prev_date()
  221.             elif arg == "next":
  222.                 print get_next_date()
  223.             else:
  224.                 g_dt = arg
  225.         elif opt in ("-g", "--generate"):
  226.             generate(int(arg))
  227.         elif opt in ("-h", "--help"):
  228.             usage()
  229.         elif opt in ("-n", "--numbers"):
  230.             get_nums()
  231.         elif opt in ("-o", "--odds"):
  232.             if arg in odds:
  233.                 print "1 in {}".format(str(odds[arg]))
  234.             else:
  235.                 print "error: could not find {} in table of odds.".format(arg)
  236.         elif opt in ("-p", "--prize"):
  237.             if arg in prizes:
  238.                 print "${}".format(prizes[arg])
  239.             else:
  240.                 print "error: could not find {} in table of prizes.".format(arg)
  241.         elif opt in ("-v", "--version"):
  242.             print "Pywerball version 0.2 (2016-11-28)"
  243.  
  244.  
  245. if __name__ == "__main__":
  246.    main(sys.argv[1:])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement