Last active
August 29, 2015 14:05
-
-
Save bkuberek/8526dedceb3446db5b6b to your computer and use it in GitHub Desktop.
This is a proof of concept in generating winning lottery numbers.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.DS_Store | |
*~ | |
._* | |
*.pyc | |
.idea/ | |
api-key* | |
apikey* | |
*.egg-info |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# !/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import argparse | |
import json | |
import random | |
import sys | |
import time | |
try: | |
import pyjsonrpc | |
RPC = True | |
except ImportError: | |
RPC = False | |
__version__ = '0.1.0' | |
games = { | |
'megamillions': [ | |
{'range': range(1, 76), 'picks': 5}, | |
{'range': range(1, 16), 'picks': 1} | |
], | |
'powerball': [ | |
{'range': range(1, 60), 'picks': 5}, | |
{'range': range(1, 36), 'picks': 1} | |
] | |
} | |
RANDOM_API = 'https://api.random.org/json-rpc/1/invoke' | |
random_api_usage = None | |
def remote_draw(game, key, count=1, interval=None): | |
global random_api_usage | |
rpc_client = pyjsonrpc.HttpClient(url=RANDOM_API) | |
for i in range(count): | |
sample = [] | |
for rules in game: | |
params = { | |
"apiKey": key, | |
"n": rules.get('picks'), | |
"min": min(rules.get('range')), | |
"max": max(rules.get('range')), | |
"replacement": False, | |
"base": 10 | |
} | |
rpc_response = rpc_client.call('generateIntegers', **params) | |
random_api_usage = rpc_response.copy() | |
del random_api_usage['random'] | |
sample.append(sorted(rpc_response.get('random').get('data'))) | |
yield sample | |
if interval: | |
time.sleep(interval) | |
def local_draw(game, count=1, seed=None, interval=None): | |
if seed is not None: | |
random.seed(seed) | |
for i in range(count): | |
sample = [] | |
for rules in game: | |
sample.append(sorted(random.sample(rules.get('range'), rules.get('picks')))) | |
yield sample | |
if interval: | |
time.sleep(interval) | |
def display(picks): | |
row_delimiter = '' | |
for i, pick in enumerate(picks): | |
rownum = '%s: ' % str(i + 1) | |
col = '{0:>3}' | |
row = ' | '.join([seq for seq in [' '.join([col.format(str(n)) for n in numbers]) for numbers in pick]]) | |
if i == 0: | |
row_delimiter = (' ' * len(rownum)) + '+-' + ('-' * len(row)) + '-+' | |
print row_delimiter | |
row = '| ' + row + ' |' | |
print '%s%s' % (rownum, row) | |
print row_delimiter | |
# noinspection PyShadowingNames | |
def play(args): | |
use_service = False | |
if not args.game in games: | |
sys.exit('Invalid game. Available games are: %s' % ', '.join(games.keys())) | |
if args.api_key and args.api_key_file: | |
sys.exit('One one of --api-key, --api-key-file can be specified at a time.') | |
if args.api_key or args.api_key_file: | |
if not RPC: | |
sys.exit("You must install python-jsonrpc before using random.org's service.") | |
if args.seed: | |
sys.exit('--seed is not available when using random.org service.') | |
if args.api_key_file: | |
with open(args.api_key_file, 'r') as key_file: | |
key = key_file.read().strip() | |
else: | |
key = args.api_key | |
picks = remote_draw(games.get(args.game), key, count=args.number, interval=args.interval) | |
use_service = True | |
else: | |
picks = local_draw(games.get(args.game), count=args.number, seed=args.seed, interval=args.interval) | |
if args.json: | |
print json.dumps([n for n in picks]) | |
else: | |
display(picks) | |
if use_service: | |
sys.stderr.write('\n\nrandom.org usage:\n') | |
col = max([len(k) for k in random_api_usage.keys()]) | |
for k in sorted(random_api_usage.keys()): | |
sys.stderr.write(('- {0:<%s}: {1}\n' % col).format(k, random_api_usage.get(k))) | |
def main(): | |
parser = argparse.ArgumentParser( | |
description='Lottery Number Generator. Uses random.org json-rpc API if API key is provided. ' | |
'Go to https://api.random.org/api-keys/beta to get an API key.') | |
parser.add_argument('game', metavar='GAME', | |
help='The name of the game. Available games:\n%s' % ', '.join(games.keys())) | |
parser.add_argument('-n', '--number', metavar='N', type=int, default=1, | |
help='The number of games to generate. Default: %(default)s') | |
parser.add_argument('-s', '--seed', metavar='SEED', | |
help='The seed to be used in random. Not available when using random.org.') | |
parser.add_argument('-i', '--interval', metavar='SEC', type=int, | |
help='The number of seconds to wait between draws.') | |
parser.add_argument('-k', '--api-key', metavar='KEY', | |
help='Random.org api key.') | |
parser.add_argument('-K', '--api-key-file', metavar='FILE', | |
help='Random.org api key filename.') | |
parser.add_argument('--json', default=False, action='store_true', | |
help='Use JSON formatter.') | |
args = parser.parse_args() | |
return play(args) | |
if __name__ == '__main__': | |
sys.exit(main() or 0) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
python-jsonrpc |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import os | |
import re | |
from setuptools import setup, find_packages | |
f = open(os.path.join(os.path.dirname(__file__), 'lottery.py')) | |
VERSION = re.compile(r".*__version__ = '(.*?)'", re.S).match(f.read()).group(1) | |
f.close() | |
here = os.path.abspath(os.path.dirname(__file__)) | |
# README = open(os.path.join(here, 'README.md')).read() | |
# CHANGES = open(os.path.join(here, 'CHANGES.md')).read() | |
REQUIRES = open(os.path.join(here, 'requirements.txt')).read() | |
setup( | |
name='lottery', | |
version=VERSION, | |
description='Lottery number generator', | |
# long_description=README + '\n\n' + CHANGES, | |
# : Classifiers: https://pypi.python.org/pypi?:action=list_classifiers | |
classifiers=[ | |
'Development Status :: 2 - Pre-Alpha', | |
'Intended Audience :: Other Audience', | |
'License :: MIT License', | |
'Natural Language :: English', | |
'Operating System :: POSIX :: Linux', | |
'Operating System :: MacOS :: MacOS X', | |
'Topic :: Utilities', | |
'Environment :: Console', | |
'Programming Language :: Python', | |
'Programming Language :: Python :: 2.6', | |
'Programming Language :: Python :: 2.7', | |
], | |
author='Bastian Kuberek', | |
author_email='[email protected]', | |
maintainer='Bastian Kuberek', | |
maintainer_email='[email protected]', | |
url='https://gist.github.com/bkuberek/8526dedceb3446db5b6b', | |
keywords='lottery number random generator', | |
license='MIT', | |
packages=find_packages(), | |
include_package_data=True, | |
zip_safe=False, | |
install_requires=REQUIRES, | |
entry_points={ | |
'console_scripts': [ | |
'lottery = lottery:main', | |
], | |
} | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment