From 24e0fa76af0859acac6f82a94e145346921014b3 Mon Sep 17 00:00:00 2001 From: Ryan Burrow Date: Fri, 23 Feb 2018 08:08:44 -0500 Subject: [PATCH 01/12] Updated README with reference --- python/README.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/python/README.md b/python/README.md index 0ae18b9..99147ef 100644 --- a/python/README.md +++ b/python/README.md @@ -7,8 +7,6 @@ Makefile: Check the script section in this readme to see more about the script cryptocheck.py - Checks the current price of bitcoin for you! + Checks the current price of bitcoin for you, using a similar strategy to that of [bogosort](https://en.wikipedia.org/wiki/Bogosort) - *TODO* - Add other crypto currencies - Add arguments such as range + From 9125912215597e3e4821a5bb1780d6723fe4f001 Mon Sep 17 00:00:00 2001 From: Ryan Burrow Date: Fri, 23 Feb 2018 09:12:41 -0500 Subject: [PATCH 02/12] Added basic command line options, updated README.md --- python/README.md | 24 +++++++++++++++------ python/cryptocheck/cryptocheck.py | 36 +++++++++++++++++++++++++------ 2 files changed, 48 insertions(+), 12 deletions(-) diff --git a/python/README.md b/python/README.md index 99147ef..869960c 100644 --- a/python/README.md +++ b/python/README.md @@ -2,11 +2,23 @@ This directory contains various python scripts to make your life easier Makefile: - Use "make install-(script)" to install a specific script - Use "make install" to install all scripts - Check the script section in this readme to see more about the script -cryptocheck.py - Checks the current price of bitcoin for you, using a similar strategy to that of [bogosort](https://en.wikipedia.org/wiki/Bogosort) +- Use "make install-(script)" to install a specific script +- Use "make install" to install all scripts +- Check the script section in this readme to see more about the script - +cryptocheck.py: +Checks the current price of bitcoin for you, using a similar strategy to that of [bogosort](https://en.wikipedia.org/wiki/Bogosort) + +``` +usage: cryptocheck.py [-h] [-c {BTC,LTC,ETH,BCH}] [-r {EU,UK,US}] + +Determine crypto currency exchange rate following a bogosort approach! + +optional arguments: + -h, --help show this help message and exit + -c {BTC,LTC,ETH,BCH}, --crypto {BTC,LTC,ETH,BCH} + Crypto currency to be exchanged + -r {EU,UK,US}, --region {EU,UK,US} + Region of exchange +``` diff --git a/python/cryptocheck/cryptocheck.py b/python/cryptocheck/cryptocheck.py index 0c0e2d0..fbe9906 100755 --- a/python/cryptocheck/cryptocheck.py +++ b/python/cryptocheck/cryptocheck.py @@ -1,13 +1,18 @@ #!/usr/bin/python3 +#coding: + +# A python script for (possibly) determining the current price of a crypto +# currency, following a similar approach to bogosort +import argparse import requests import random import time import sys API_URL = "https://api.gdax.com" -crypto_list = ['BTC', 'LTC', 'ETH', 'BTH'] +crypto_list = ['BTC', 'LTC', 'ETH', 'BCH'] region_dict = { 'US' : {'symbol' : '$', 'string' : 'USD'}, \ - 'UK' : {'symbol' : '£', 'string' : 'GPB'}, \ + 'UK' : {'symbol' : '£', 'string' : 'GBP'}, \ 'EU' : {'symbol' : '€', 'string' : 'EUR'} } def _get_price(crypto='BTC', region='US'): @@ -17,8 +22,12 @@ def _get_price(crypto='BTC', region='US'): r = requests.get(API_URL + '/products/' + exchange_symbol + '/book') data = r.json() + try: + retVal = (float(data['bids'][0][0]), exchange_symbol, region_symbol) + return retVal + except: + return (None, exchange_symbol, region_symbol) - return (float(data['bids'][0][0]), exchange_symbol, region_symbol) def _guess_price(): guess_dollar = random.randint(0,20000) @@ -26,8 +35,20 @@ def _guess_price(): return float(guess_dollar) + (float(guess_cents) / 100.00) +def _parse_args(): + parser = argparse.ArgumentParser(description='Determine crypto currency exchange rate '\ + 'following a bogosort approach!') + parser.add_argument('-c', '--crypto', choices=crypto_list, default='BTC', + help='Crypto currency to be exchanged') + parser.add_argument('-r', '--region', choices=region_dict.keys(), default='US', + help='Region of exchange') + + return parser.parse_args() + def __main__(): + args = _parse_args() + # seed a guess random.seed(int(time.time() * 100)) @@ -35,10 +56,13 @@ def __main__(): guess = _guess_price() # get price as well as exchange_symbol and region_symbol for given currency and region - price, exchange_symbol, region_symbol = _get_price() + price, exchange_symbol, region_symbol = _get_price(region=args.region, crypto=args.crypto) - print("The current exchange of %s is%s: %s%.2f" % \ - (exchange_symbol, "" if guess == price else " not", region_symbol, guess)) + if (price != None): + print("The current exchange of %s is%s: %s%.2f" % \ + (exchange_symbol, "" if guess == price else " not", region_symbol, guess)) + else: + print("That combination is not currently available for comparison") if __name__ == '__main__': __main__() From 0e5d1bce320552df11735aadfde685820c5efe8d Mon Sep 17 00:00:00 2001 From: Ryan Burrow Date: Fri, 23 Feb 2018 11:43:14 -0500 Subject: [PATCH 03/12] Added --guess-again option and updated README.md --- python/README.md | 8 +++-- python/cryptocheck/cryptocheck.py | 50 ++++++++++++++++++++++++------- 2 files changed, 44 insertions(+), 14 deletions(-) diff --git a/python/README.md b/python/README.md index 869960c..d038e3a 100644 --- a/python/README.md +++ b/python/README.md @@ -11,7 +11,8 @@ cryptocheck.py: Checks the current price of bitcoin for you, using a similar strategy to that of [bogosort](https://en.wikipedia.org/wiki/Bogosort) ``` -usage: cryptocheck.py [-h] [-c {BTC,LTC,ETH,BCH}] [-r {EU,UK,US}] +usage: cryptocheck.py [-h] [-c {BTC,LTC,ETH,BCH}] [-r {US,EU,UK}] + [--guess-again] Determine crypto currency exchange rate following a bogosort approach! @@ -19,6 +20,7 @@ optional arguments: -h, --help show this help message and exit -c {BTC,LTC,ETH,BCH}, --crypto {BTC,LTC,ETH,BCH} Crypto currency to be exchanged - -r {EU,UK,US}, --region {EU,UK,US} - Region of exchange + -r {US,EU,UK}, --region {US,EU,UK} + Region of exchange + --guess-again Keep running until the correct price is guessed ``` diff --git a/python/cryptocheck/cryptocheck.py b/python/cryptocheck/cryptocheck.py index fbe9906..5444336 100755 --- a/python/cryptocheck/cryptocheck.py +++ b/python/cryptocheck/cryptocheck.py @@ -28,10 +28,21 @@ def _get_price(crypto='BTC', region='US'): except: return (None, exchange_symbol, region_symbol) +def _get_cent_diff(a, b): + tmp = str('%.2f' % (a - int(a),)) + tmp = tmp[2:] + return int(tmp) -def _guess_price(): - guess_dollar = random.randint(0,20000) - guess_cents = random.randint(0,100) +def _guess_price(minPrice=0, maxPrice=20000.00): + # guess a dollar range within the given range + guess_dollar = random.randint(int(minPrice),int(maxPrice)) + + # if same dollar amount, set the max cent value at the max + maxCents = _get_cent_diff(maxPrice, int(maxPrice)) if guess_dollar == int(maxPrice) else 100 + # if same dollar amount, set the min cent value at the min + minCents = _get_cent_diff(minPrice, int(minPrice)) if guess_dollar == int(minPrice) else 0 + + guess_cents = random.randint(minCents, maxCents) return float(guess_dollar) + (float(guess_cents) / 100.00) @@ -42,6 +53,8 @@ def _parse_args(): help='Crypto currency to be exchanged') parser.add_argument('-r', '--region', choices=region_dict.keys(), default='US', help='Region of exchange') + parser.add_argument('--guess-again', action='store_true', dest='guess_again', + help='Keep running until the correct price is guessed') return parser.parse_args() @@ -51,18 +64,33 @@ def __main__(): # seed a guess random.seed(int(time.time() * 100)) - - # guess a price - guess = _guess_price() # get price as well as exchange_symbol and region_symbol for given currency and region price, exchange_symbol, region_symbol = _get_price(region=args.region, crypto=args.crypto) + numGuesses = 0 + minPrice = 0 + maxPrice = 20000.00 + + while (args.guess_again or numGuesses == 0): - if (price != None): - print("The current exchange of %s is%s: %s%.2f" % \ - (exchange_symbol, "" if guess == price else " not", region_symbol, guess)) - else: - print("That combination is not currently available for comparison") + # guess a price + guess = _guess_price(minPrice, maxPrice) + numGuesses = numGuesses + 1 + + if (price != None): + found = (str('%.2f' % (guess,)) == str('%.2f' % (price,))) + print("The current exchange of %s is%s: %s%.2f" % \ + (exchange_symbol, "" if found else " not", region_symbol, guess)) + if (found): + print("This program took %d guesses to determine the exchange rate!" % (numGuesses,)) + break + elif (guess < price): + minPrice = guess + else: + maxPrice = guess + else: + print("That combination is not currently available for comparison") + break if __name__ == '__main__': __main__() From a7d518a698e35d3db0a5ec43ba73bb250e01b547 Mon Sep 17 00:00:00 2001 From: Ryan Burrow Date: Fri, 23 Feb 2018 12:09:18 -0500 Subject: [PATCH 04/12] changed filename and added protection for out of range potential infinite loops --- python/cryptocheck/{cryptocheck.py => bogoexchange.py} | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) rename python/cryptocheck/{cryptocheck.py => bogoexchange.py} (94%) mode change 100755 => 100644 diff --git a/python/cryptocheck/cryptocheck.py b/python/cryptocheck/bogoexchange.py old mode 100755 new mode 100644 similarity index 94% rename from python/cryptocheck/cryptocheck.py rename to python/cryptocheck/bogoexchange.py index 5444336..318dbba --- a/python/cryptocheck/cryptocheck.py +++ b/python/cryptocheck/bogoexchange.py @@ -77,7 +77,7 @@ def __main__(): guess = _guess_price(minPrice, maxPrice) numGuesses = numGuesses + 1 - if (price != None): + if (price != None and price > minPrice and price < maxPrice): found = (str('%.2f' % (guess,)) == str('%.2f' % (price,))) print("The current exchange of %s is%s: %s%.2f" % \ (exchange_symbol, "" if found else " not", region_symbol, guess)) @@ -88,9 +88,12 @@ def __main__(): minPrice = guess else: maxPrice = guess - else: + elif (price == None): print("That combination is not currently available for comparison") break + else: + print("Sorry, %s is not within the provided price range" % (exchange_symbol,)) + break if __name__ == '__main__': __main__() From 318bafa640013ced41797afe29917c0bfcd7ea1a Mon Sep 17 00:00:00 2001 From: Ryan Burrow Date: Fri, 23 Feb 2018 17:33:14 -0500 Subject: [PATCH 05/12] Renamed files, updated README and makefile, started adding next commandline option --- python/README.md | 2 +- python/{cryptocheck => bogoexchange}/Makefile | 4 ++-- .../{cryptocheck => bogoexchange}/bogoexchange.py | 13 +++++++++++++ 3 files changed, 16 insertions(+), 3 deletions(-) mode change 100644 => 100755 python/README.md rename python/{cryptocheck => bogoexchange}/Makefile (61%) rename python/{cryptocheck => bogoexchange}/bogoexchange.py (87%) diff --git a/python/README.md b/python/README.md old mode 100644 new mode 100755 index d038e3a..d2d1a22 --- a/python/README.md +++ b/python/README.md @@ -11,7 +11,7 @@ cryptocheck.py: Checks the current price of bitcoin for you, using a similar strategy to that of [bogosort](https://en.wikipedia.org/wiki/Bogosort) ``` -usage: cryptocheck.py [-h] [-c {BTC,LTC,ETH,BCH}] [-r {US,EU,UK}] +usage: bogoexchange.py [-h] [-c {BTC,LTC,ETH,BCH}] [-r {US,EU,UK}] [--guess-again] Determine crypto currency exchange rate following a bogosort approach! diff --git a/python/cryptocheck/Makefile b/python/bogoexchange/Makefile similarity index 61% rename from python/cryptocheck/Makefile rename to python/bogoexchange/Makefile index ba0db8c..bdf2486 100644 --- a/python/cryptocheck/Makefile +++ b/python/bogoexchange/Makefile @@ -8,7 +8,7 @@ all: @echo "Invalid use of make, please use one of the given options" install: - install -m 557 btccheck.py $(BINDIR)/cryptocheck + install -m 557 bogoexchange.py $(BINDIR)/bogoexchange uninstall: - rm -f $(BINDIR)/cryptocheck \ No newline at end of file + rm -f $(BINDIR)/bogoexchange diff --git a/python/cryptocheck/bogoexchange.py b/python/bogoexchange/bogoexchange.py similarity index 87% rename from python/cryptocheck/bogoexchange.py rename to python/bogoexchange/bogoexchange.py index 318dbba..df9ba73 100644 --- a/python/cryptocheck/bogoexchange.py +++ b/python/bogoexchange/bogoexchange.py @@ -46,6 +46,17 @@ def _guess_price(minPrice=0, maxPrice=20000.00): return float(guess_dollar) + (float(guess_cents) / 100.00) +class BAction(argparse.Action): + def __call__(self, parser, args, values, option_string=None): + # print 'values: {v!r}'.format(v=values) + if values==None: + values='1' + try: + values=int(values) + except ValueError: + values=values.count('b')+1 + setattr(args, self.dest, values) + def _parse_args(): parser = argparse.ArgumentParser(description='Determine crypto currency exchange rate '\ 'following a bogosort approach!') @@ -55,6 +66,8 @@ def _parse_args(): help='Region of exchange') parser.add_argument('--guess-again', action='store_true', dest='guess_again', help='Keep running until the correct price is guessed') + parser.add_argument('-b', nargs='?', action=BAction, dest='bound', + help='Bound the bogo algorithm: -b: minor bounding, -bb: major bounding') return parser.parse_args() From a271f8397b0b99c423aebaa1ee736c39b6336bb2 Mon Sep 17 00:00:00 2001 From: Ryan Burrow Date: Fri, 23 Feb 2018 22:19:24 -0500 Subject: [PATCH 06/12] Added bounding, updated the README.md, help command still needs to be prettied --- python/README.md | 9 +- python/bogoexchange/bogoexchange.py | 135 ++++++++++++++++++++-------- 2 files changed, 106 insertions(+), 38 deletions(-) mode change 100644 => 100755 python/bogoexchange/bogoexchange.py diff --git a/python/README.md b/python/README.md index d2d1a22..db670d8 100755 --- a/python/README.md +++ b/python/README.md @@ -11,8 +11,8 @@ cryptocheck.py: Checks the current price of bitcoin for you, using a similar strategy to that of [bogosort](https://en.wikipedia.org/wiki/Bogosort) ``` -usage: bogoexchange.py [-h] [-c {BTC,LTC,ETH,BCH}] [-r {US,EU,UK}] - [--guess-again] +usage: bogoexchange.py [-h] [-c {BTC,LTC,ETH,BCH}] [-r {EU,US,UK}] + [--guess-again] [-b] Determine crypto currency exchange rate following a bogosort approach! @@ -20,7 +20,10 @@ optional arguments: -h, --help show this help message and exit -c {BTC,LTC,ETH,BCH}, --crypto {BTC,LTC,ETH,BCH} Crypto currency to be exchanged - -r {US,EU,UK}, --region {US,EU,UK} + -r {EU,US,UK}, --region {EU,US,UK} Region of exchange --guess-again Keep running until the correct price is guessed + -b Bound the bogo algorithm: -b: minor bounding, -bb: + major bounding (Warning, this will reduce the bogoness + of this program... you don't want to do that, do you? ``` diff --git a/python/bogoexchange/bogoexchange.py b/python/bogoexchange/bogoexchange.py old mode 100644 new mode 100755 index df9ba73..22251b2 --- a/python/bogoexchange/bogoexchange.py +++ b/python/bogoexchange/bogoexchange.py @@ -28,24 +28,8 @@ def _get_price(crypto='BTC', region='US'): except: return (None, exchange_symbol, region_symbol) -def _get_cent_diff(a, b): - tmp = str('%.2f' % (a - int(a),)) - tmp = tmp[2:] - return int(tmp) - -def _guess_price(minPrice=0, maxPrice=20000.00): - # guess a dollar range within the given range - guess_dollar = random.randint(int(minPrice),int(maxPrice)) - - # if same dollar amount, set the max cent value at the max - maxCents = _get_cent_diff(maxPrice, int(maxPrice)) if guess_dollar == int(maxPrice) else 100 - # if same dollar amount, set the min cent value at the min - minCents = _get_cent_diff(minPrice, int(minPrice)) if guess_dollar == int(minPrice) else 0 - - guess_cents = random.randint(minCents, maxCents) - - return float(guess_dollar) + (float(guess_cents) / 100.00) - +''' +# Class for bounding argument of argparse class BAction(argparse.Action): def __call__(self, parser, args, values, option_string=None): # print 'values: {v!r}'.format(v=values) @@ -56,6 +40,7 @@ def __call__(self, parser, args, values, option_string=None): except ValueError: values=values.count('b')+1 setattr(args, self.dest, values) +''' def _parse_args(): parser = argparse.ArgumentParser(description='Determine crypto currency exchange rate '\ @@ -66,11 +51,95 @@ def _parse_args(): help='Region of exchange') parser.add_argument('--guess-again', action='store_true', dest='guess_again', help='Keep running until the correct price is guessed') - parser.add_argument('-b', nargs='?', action=BAction, dest='bound', - help='Bound the bogo algorithm: -b: minor bounding, -bb: major bounding') + parser.add_argument('-b', action='count', dest='bound', + help='Bound the bogo algorithm: -b: minor bounding, -bb: major bounding '\ + '(Warning, this will reduce the bogoness of this program... you don\'t '\ + 'want to do that, do you?') return parser.parse_args() + +# Used for maintaining price range and adjusting it accordingly +class priceCheck: + __minPrice = 0.00 + __maxPrice = 20000.00 + __boundingLevel = 0 + __price = 0.00 + # upper and lower bound increments for -b + __upperBoundDec = 0 + __lowerBoundInc = 0 + + # Set the range of allowable guesses + def __init__(self, minPrice=0.00, maxPrice=20000.00, bound=0, actual=0.00): + self.__minPrice=minPrice + self.__maxPrice=maxPrice + self.__upperBoundDec=int((maxPrice-actual)/10) + self.__lowerBoundInc=int((actual-minPrice)/10) + self.__bound=bound + self.__price=actual + + # Update the range after a guess + def _update(self, guess): + if (self.__boundingLevel == 1): + # Do something + self.__minPrice = self.__minPrice + self.__lowerBoundInc + self.__maxPrice = self.__maxPrice - self.__upperBoundDec + elif (self.__bound == 2): + if (guess < self.__price): + self.__minPrice = guess + else: + self.__maxPrice = guess + + # Check if a given price less than greater than or equal + # Return 1 if greater, 0 if equal, -1 if less + def check(self, guess): + ret = (str('%.2f' % (guess,)) == str('%.2f' % (self.__price,))) + if (ret): + return 0 + elif (guess < self.__price): + ret = -1 + else: + ret = 1 + self._update(guess) + return ret + + # Check if the given price is in the range + # return True if valid, False if not + def valid(self): + return True if (self.__price < self.__maxPrice and \ + self.__price > self.__minPrice) else False + + # Generate a guess for the price of the exchange + def guess_price(self): + # guess a dollar range within the given range + guess_dollar = random.randint(int(self.__minPrice),int(self.__maxPrice)) + + # if same dollar amount, set the max cent value at the max + maxCents = self._get_cent_diff('max') if guess_dollar == int(self.__maxPrice) else 100 + # if same dollar amount, set the min cent value at the min + minCents = self._get_cent_diff('min') if guess_dollar == int(self.__minPrice) else 0 + + guess_cents = random.randint(minCents, maxCents) + + return float(guess_dollar) + (float(guess_cents) / 100.00) + + # Get the difference in cents between two floats + def _get_cent_diff(self, val='max'): + if (val == 'max'): + a = self.__maxPrice + else: + a = self.__minPrice + + tmp = str('%.2f' % (a - int(a),)) + tmp = tmp[2:] + return int(tmp) + + # Print the current range (Debuggin purposes) + def __repr__(self): + return "" % (self.__minPrice, self.__maxPrice) + + +# main, you know, the function that always runs! def __main__(): args = _parse_args() @@ -80,30 +149,26 @@ def __main__(): # get price as well as exchange_symbol and region_symbol for given currency and region price, exchange_symbol, region_symbol = _get_price(region=args.region, crypto=args.crypto) + if (price == None): + print("That combination is not currently available for comparison") + return + + PR = priceCheck(minPrice=0, maxPrice=20000.00, bound=args.bound, actual=price) numGuesses = 0 - minPrice = 0 - maxPrice = 20000.00 while (args.guess_again or numGuesses == 0): # guess a price - guess = _guess_price(minPrice, maxPrice) + guess = PR.guess_price() numGuesses = numGuesses + 1 - if (price != None and price > minPrice and price < maxPrice): - found = (str('%.2f' % (guess,)) == str('%.2f' % (price,))) + if (PR.valid()): + found = PR.check(guess) print("The current exchange of %s is%s: %s%.2f" % \ - (exchange_symbol, "" if found else " not", region_symbol, guess)) - if (found): + (exchange_symbol, " not" if found else "", region_symbol, guess)) + if (found == 0): print("This program took %d guesses to determine the exchange rate!" % (numGuesses,)) - break - elif (guess < price): - minPrice = guess - else: - maxPrice = guess - elif (price == None): - print("That combination is not currently available for comparison") - break + break else: print("Sorry, %s is not within the provided price range" % (exchange_symbol,)) break From 11195639b8af84ccc3b83d0d0c809ab95d56c2f6 Mon Sep 17 00:00:00 2001 From: Ryan Burrow Date: Fri, 23 Feb 2018 23:21:37 -0500 Subject: [PATCH 07/12] Removed some out of range errors, some testing might be warranted, but I think this version is pretty stable --- python/README.md | 13 ++--- python/bogoexchange/bogoexchange.py | 82 ++++++++++++++++++----------- 2 files changed, 58 insertions(+), 37 deletions(-) diff --git a/python/README.md b/python/README.md index db670d8..52b521b 100755 --- a/python/README.md +++ b/python/README.md @@ -11,8 +11,8 @@ cryptocheck.py: Checks the current price of bitcoin for you, using a similar strategy to that of [bogosort](https://en.wikipedia.org/wiki/Bogosort) ``` -usage: bogoexchange.py [-h] [-c {BTC,LTC,ETH,BCH}] [-r {EU,US,UK}] - [--guess-again] [-b] +usage: bogoexchange [-h] [-c {BTC,LTC,ETH,BCH}] [-r {US,EU,UK}] + [--guess-again] [-b] Determine crypto currency exchange rate following a bogosort approach! @@ -20,10 +20,11 @@ optional arguments: -h, --help show this help message and exit -c {BTC,LTC,ETH,BCH}, --crypto {BTC,LTC,ETH,BCH} Crypto currency to be exchanged - -r {EU,US,UK}, --region {EU,US,UK} + -r {US,EU,UK}, --region {US,EU,UK} Region of exchange --guess-again Keep running until the correct price is guessed - -b Bound the bogo algorithm: -b: minor bounding, -bb: - major bounding (Warning, this will reduce the bogoness - of this program... you don't want to do that, do you? + -b Bound the bogo algorithm: -b: minor bounding -bb: + slightly more bounding -bbb: Arguably too much + bounding (Warning, this will reduce the bogoness of + this program... you don't want to do that, do you? ``` diff --git a/python/bogoexchange/bogoexchange.py b/python/bogoexchange/bogoexchange.py index 22251b2..64b0c5d 100755 --- a/python/bogoexchange/bogoexchange.py +++ b/python/bogoexchange/bogoexchange.py @@ -28,20 +28,7 @@ def _get_price(crypto='BTC', region='US'): except: return (None, exchange_symbol, region_symbol) -''' -# Class for bounding argument of argparse -class BAction(argparse.Action): - def __call__(self, parser, args, values, option_string=None): - # print 'values: {v!r}'.format(v=values) - if values==None: - values='1' - try: - values=int(values) - except ValueError: - values=values.count('b')+1 - setattr(args, self.dest, values) -''' - +# Parse the command line arguments and return them def _parse_args(): parser = argparse.ArgumentParser(description='Determine crypto currency exchange rate '\ 'following a bogosort approach!') @@ -52,7 +39,10 @@ def _parse_args(): parser.add_argument('--guess-again', action='store_true', dest='guess_again', help='Keep running until the correct price is guessed') parser.add_argument('-b', action='count', dest='bound', - help='Bound the bogo algorithm: -b: minor bounding, -bb: major bounding '\ + help='Bound the bogo algorithm: '\ + '-b: minor bounding\n'\ + '-bb: slightly more bounding\n'\ + '-bbb: Arguably too much bounding\n'\ '(Warning, this will reduce the bogoness of this program... you don\'t '\ 'want to do that, do you?') @@ -73,22 +63,42 @@ class priceCheck: def __init__(self, minPrice=0.00, maxPrice=20000.00, bound=0, actual=0.00): self.__minPrice=minPrice self.__maxPrice=maxPrice - self.__upperBoundDec=int((maxPrice-actual)/10) - self.__lowerBoundInc=int((actual-minPrice)/10) + self.__upperBoundDec=int((maxPrice-actual)/1000) + self.__lowerBoundInc=int((actual-minPrice)/1000) self.__bound=bound self.__price=actual # Update the range after a guess + # Return if update was successful or if needs to be redone + # False indicates no rerun needed, True means run again def _update(self, guess): - if (self.__boundingLevel == 1): - # Do something - self.__minPrice = self.__minPrice + self.__lowerBoundInc - self.__maxPrice = self.__maxPrice - self.__upperBoundDec - elif (self.__bound == 2): + retval = False + # Basic bounding + if (self.__bound > 0 and self.__bound < 3): + tempMin = self.__minPrice + self.__minPrice = self.__minPrice + (self.__lowerBoundInc * pow(10, self.__bound - 1)) + tempMax = self.__maxPrice + self.__maxPrice = self.__maxPrice - (self.__upperBoundDec * pow(10, self.__bound - 1)) + # Reduce bounding range as we get closer to + if (self.valid() == -1): + self.__lowerBoundInc = self.__lowerBoundInc / 2 + retval = True + + if (self.valid() == 1): + self.__upperBoundDec = self.__upperBoundDec / 2 + retval = True + + if (retval): + self.__minPrice = tempMin + self.__maxPrice = tempMax + + elif (self.__bound >= 3): if (guess < self.__price): self.__minPrice = guess else: self.__maxPrice = guess + return retval + # Check if a given price less than greater than or equal # Return 1 if greater, 0 if equal, -1 if less @@ -100,19 +110,28 @@ def check(self, guess): ret = -1 else: ret = 1 - self._update(guess) + while(self._update(guess)): + pass return ret # Check if the given price is in the range - # return True if valid, False if not + # return 1 if price above range, 0 if in range, -1 below range def valid(self): - return True if (self.__price < self.__maxPrice and \ - self.__price > self.__minPrice) else False + # check for equal values to prevent infinite loops + retMin = (str('%.2f' % (self.__minPrice,)) == str('%.2f' % (self.__price,))) + retMax = (str('%.2f' % (self.__maxPrice,)) == str('%.2f' % (self.__price,))) + if (self.__price > self.__maxPrice and retMax == False): + return 1 + elif (self.__price < self.__minPrice and retMin == False): + return -1 + else: + return 0 # Generate a guess for the price of the exchange def guess_price(self): # guess a dollar range within the given range - guess_dollar = random.randint(int(self.__minPrice),int(self.__maxPrice)) + guess_dollar = random.randint(int(self.__minPrice),int(self.__maxPrice)) if \ + (int(self.__minPrice) != int(self.__maxPrice)) else self.__maxPrice # if same dollar amount, set the max cent value at the max maxCents = self._get_cent_diff('max') if guess_dollar == int(self.__maxPrice) else 100 @@ -136,7 +155,8 @@ def _get_cent_diff(self, val='max'): # Print the current range (Debuggin purposes) def __repr__(self): - return "" % (self.__minPrice, self.__maxPrice) + return "\n"\ + % (self.__minPrice, self.__maxPrice, self.__price) # main, you know, the function that always runs! @@ -162,16 +182,16 @@ def __main__(): guess = PR.guess_price() numGuesses = numGuesses + 1 - if (PR.valid()): + if (PR.valid() == 0): found = PR.check(guess) print("The current exchange of %s is%s: %s%.2f" % \ (exchange_symbol, " not" if found else "", region_symbol, guess)) if (found == 0): print("This program took %d guesses to determine the exchange rate!" % (numGuesses,)) - break + exit(0) else: print("Sorry, %s is not within the provided price range" % (exchange_symbol,)) - break + exit(1) if __name__ == '__main__': __main__() From 5015d38959f1d62e98dab6f84e58d398d31ba69e Mon Sep 17 00:00:00 2001 From: Ryan Burrow Date: Fri, 23 Feb 2018 23:24:20 -0500 Subject: [PATCH 08/12] Added version parameter, set it to 1.0 --- python/README.md | 8 +++++--- python/bogoexchange/bogoexchange.py | 1 + 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/python/README.md b/python/README.md index 52b521b..8a5cf8f 100755 --- a/python/README.md +++ b/python/README.md @@ -7,12 +7,13 @@ Makefile: - Use "make install" to install all scripts - Check the script section in this readme to see more about the script -cryptocheck.py: +bogoexchange.py: Checks the current price of bitcoin for you, using a similar strategy to that of [bogosort](https://en.wikipedia.org/wiki/Bogosort) +Version: 1.0 ``` -usage: bogoexchange [-h] [-c {BTC,LTC,ETH,BCH}] [-r {US,EU,UK}] - [--guess-again] [-b] +usage: bogoexchange.py [-h] [-c {BTC,LTC,ETH,BCH}] [-r {US,EU,UK}] + [--guess-again] [-b] [--version] Determine crypto currency exchange rate following a bogosort approach! @@ -27,4 +28,5 @@ optional arguments: slightly more bounding -bbb: Arguably too much bounding (Warning, this will reduce the bogoness of this program... you don't want to do that, do you? + --version show program's version number and exit ``` diff --git a/python/bogoexchange/bogoexchange.py b/python/bogoexchange/bogoexchange.py index 64b0c5d..89e09f5 100755 --- a/python/bogoexchange/bogoexchange.py +++ b/python/bogoexchange/bogoexchange.py @@ -45,6 +45,7 @@ def _parse_args(): '-bbb: Arguably too much bounding\n'\ '(Warning, this will reduce the bogoness of this program... you don\'t '\ 'want to do that, do you?') + parser.add_argument('--version', action='version', version='%(prog)s 1.0') return parser.parse_args() From 1525b978548d4892932bdd4aac5f975f9142fe19 Mon Sep 17 00:00:00 2001 From: Ryan Burrow Date: Fri, 23 Feb 2018 23:34:03 -0500 Subject: [PATCH 09/12] Minor aesthetic updates to README.md --- python/README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) mode change 100755 => 100644 python/README.md diff --git a/python/README.md b/python/README.md old mode 100755 new mode 100644 index 8a5cf8f..83131f8 --- a/python/README.md +++ b/python/README.md @@ -9,7 +9,8 @@ Makefile: bogoexchange.py: Checks the current price of bitcoin for you, using a similar strategy to that of [bogosort](https://en.wikipedia.org/wiki/Bogosort) -Version: 1.0 + +version: 1.0 ``` usage: bogoexchange.py [-h] [-c {BTC,LTC,ETH,BCH}] [-r {US,EU,UK}] From 0a749ed873a6974cfebe55ca2f2102bac1a33e43 Mon Sep 17 00:00:00 2001 From: Ryan Burrow Date: Sat, 24 Feb 2018 11:42:23 -0500 Subject: [PATCH 10/12] Fixed Nonetype error for no bounding option --- python/bogoexchange/bogoexchange.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/python/bogoexchange/bogoexchange.py b/python/bogoexchange/bogoexchange.py index 89e09f5..c11c3d3 100755 --- a/python/bogoexchange/bogoexchange.py +++ b/python/bogoexchange/bogoexchange.py @@ -74,8 +74,10 @@ def __init__(self, minPrice=0.00, maxPrice=20000.00, bound=0, actual=0.00): # False indicates no rerun needed, True means run again def _update(self, guess): retval = False + if (self.__bound == None): + return False # Basic bounding - if (self.__bound > 0 and self.__bound < 3): + elif (self.__bound < 3): tempMin = self.__minPrice self.__minPrice = self.__minPrice + (self.__lowerBoundInc * pow(10, self.__bound - 1)) tempMax = self.__maxPrice From 8c84d107dfa37bfe192b49a9d2ea42300ab3a925 Mon Sep 17 00:00:00 2001 From: Ryan Burrow Date: Sun, 25 Feb 2018 23:34:23 -0500 Subject: [PATCH 11/12] Added pretty colors and delay + argument --- python/bogoexchange/bogoexchange.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/python/bogoexchange/bogoexchange.py b/python/bogoexchange/bogoexchange.py index c11c3d3..ab48fb6 100755 --- a/python/bogoexchange/bogoexchange.py +++ b/python/bogoexchange/bogoexchange.py @@ -15,6 +15,10 @@ 'UK' : {'symbol' : '£', 'string' : 'GBP'}, \ 'EU' : {'symbol' : '€', 'string' : 'EUR'} } +RED_TEXT = "\x1b[1;31;40m" +GREEN_TEXT = "\x1b[1;32;40m" +END_COLOR = "\x1b[0m" + def _get_price(crypto='BTC', region='US'): # determine exchange symbol for currency and region exchange_symbol = crypto + '-' + region_dict[region]['string'] @@ -38,6 +42,8 @@ def _parse_args(): help='Region of exchange') parser.add_argument('--guess-again', action='store_true', dest='guess_again', help='Keep running until the correct price is guessed') + parser.add_argument('--nodelay', action='store_true', help='Do not delay in between printing out '\ + 'the next guess at the exchange rate.') parser.add_argument('-b', action='count', dest='bound', help='Bound the bogo algorithm: '\ '-b: minor bounding\n'\ @@ -47,6 +53,7 @@ def _parse_args(): 'want to do that, do you?') parser.add_argument('--version', action='version', version='%(prog)s 1.0') + return parser.parse_args() @@ -187,11 +194,19 @@ def __main__(): if (PR.valid() == 0): found = PR.check(guess) - print("The current exchange of %s is%s: %s%.2f" % \ - (exchange_symbol, " not" if found else "", region_symbol, guess)) + print("%sThe current exchange of %s is%s: %s%.2f%s" % \ + (RED_TEXT if found else GREEN_TEXT,\ + exchange_symbol, " not" if found else "", region_symbol, guess, + END_COLOR)) if (found == 0): print("This program took %d guesses to determine the exchange rate!" % (numGuesses,)) exit(0) + if (not args.nodelay): + for i in range(random.randint(1,20)): + print(".", end='', flush=True) + time.sleep(.1) + print("") + else: print("Sorry, %s is not within the provided price range" % (exchange_symbol,)) exit(1) From 5ab3e3a6f4b045726c33a612a03128d9df49c72f Mon Sep 17 00:00:00 2001 From: Ryan Burrow Date: Sun, 25 Feb 2018 23:37:14 -0500 Subject: [PATCH 12/12] updated README --- python/README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/python/README.md b/python/README.md index 83131f8..c5bc734 100644 --- a/python/README.md +++ b/python/README.md @@ -13,8 +13,8 @@ Checks the current price of bitcoin for you, using a similar strategy to that of version: 1.0 ``` -usage: bogoexchange.py [-h] [-c {BTC,LTC,ETH,BCH}] [-r {US,EU,UK}] - [--guess-again] [-b] [--version] +usage: bogoexchange [-h] [-c {BTC,LTC,ETH,BCH}] [-r {US,EU,UK}] + [--guess-again] [--nodelay] [-b] [--version] Determine crypto currency exchange rate following a bogosort approach! @@ -25,6 +25,8 @@ optional arguments: -r {US,EU,UK}, --region {US,EU,UK} Region of exchange --guess-again Keep running until the correct price is guessed + --nodelay Do not delay in between printing out the next guess at + the exchange rate. -b Bound the bogo algorithm: -b: minor bounding -bb: slightly more bounding -bbb: Arguably too much bounding (Warning, this will reduce the bogoness of