def list(slug, limit): try: new_url = url + '/' + slug + '/' conn = urllib.request.urlopen(new_url) the_page = conn.read().decode('utf8') response_code = conn.getcode() print(response_code) new_data = None if slug == 'feed': data = json.loads(the_page)['results'] if limit: new_data = (data[0:int(limit)]) data = new_data article_res = json.dumps(data, indent=4, sort_keys=True) Conversions.writeToJson('./', 'articles', article_res) Conversions.converToCsv('./', 'articles', article_res) print(article_res) else: data = json.loads(the_page) res = json.dumps(data, indent=4, sort_keys=True) Conversions.writeToJson('./', 'articles', res) print(res) except urllib.error.HTTPError as e: # Return code error (e.g. 404, 501, ...) # ... error = json.dumps({ 'message': 'Not Found!', 'HTTPError': e.code }, indent=4, sort_keys=True) print(error) except urllib.error.URLError as e: # Not an HTTP-specific error (e.g. connection refused) # ... print('URLError: {}'.format(e.reason))
def convert(self): oddsA, oddsB = False, False # load values from ui.odds if len(self.ui.oddsA.text()) != 0: oddsA = self.ui.oddsA.text() if Conversions().odds_type(oddsA) == "us": # Get decimal odds from US odds decOddsA = Conversions().us_to_dec(oddsA) # set usOdds usOddsA = int(oddsA) if Conversions().odds_type(oddsA) == "dec": # get us odds from dec usOddsA = Conversions().dec_to_us(oddsA) # set decOdds decOddsA = float(oddsA) if Conversions().odds_type(oddsA) == 'frac': # get dec odds from frac decOddsA = Conversions().frac_to_dec(oddsA) # get us odds from dec usOddsA = Conversions().dec_to_us(decOddsA) # Calculate net odds netDecOddsA = decOddsA - 1 # Get implied probability from US odds impliedPrA = Conversions().implied_pr(usOddsA) # get fractional odds fracA = Conversions().dec_to_frac(decOddsA) # get expected value ev_A = Conversions().expected_value(netDecOddsA, impliedPrA) self.ui.output_dec_odds_A.setText("{:.4f}".format(decOddsA)) self.ui.output_net_dec_odds_A.setText("{:.4f}".format(netDecOddsA)) self.ui.output_imp_pr_A.setText("{:.4f}".format(impliedPrA)) self.ui.output_us_odds_A.setText(f"{usOddsA}") self.ui.output_frac_odds_A.setText(f"{fracA}") if ev_A > 0: self.ui.output_ev_A.setText("+{:.4f}".format(ev_A)) else: self.ui.output_ev_A.setText("{:.4f}".format(ev_A)) if len(self.ui.oddsB.text()) != 0: oddsB = self.ui.oddsB.text() if Conversions().odds_type(oddsB) == "us": # Get decimal odds from US odds decOddsB = Conversions().us_to_dec(oddsB) # set usOdds usOddsB = int(oddsB) if Conversions().odds_type(oddsB) == "dec": # get us odds from dec usOddsB = Conversions().dec_to_us(oddsB) # set decOdds decOddsB = float(oddsB) if Conversions().odds_type(oddsB) == 'frac': # get dec odds from frac decOddsB = Conversions().frac_to_dec(oddsB) # get us odds from dec usOddsB = Conversions().dec_to_us(decOddsB) # Calculate net odds netDecOddsB = decOddsB - 1 # Get implied probability from US odds impliedPrB = Conversions().implied_pr(usOddsB) # get fractional odds fracB = Conversions().dec_to_frac(decOddsB) # get expected value ev_B = Conversions().expected_value(netDecOddsB, impliedPrB) self.ui.output_dec_odds_B.setText("{:.4f}".format(decOddsB)) self.ui.output_net_dec_odds_B.setText("{:.4f}".format(netDecOddsB)) self.ui.output_imp_pr_B.setText("{:.4f}".format(impliedPrB)) self.ui.output_us_odds_B.setText(f"{usOddsB}") self.ui.output_frac_odds_B.setText(f"{fracB}") if ev_B > 0: self.ui.output_ev_B.setText("+{:.4f}".format(ev_B)) else: self.ui.output_ev_B.setText("{:.4f}".format(ev_B)) else: oddsB = False if oddsA and oddsB: # NoVig is the ratio of 1 side to the sum of all sides noVigA = Conversions().no_vig(impliedPrA, impliedPrB) noVigB = Conversions().no_vig(impliedPrB, impliedPrA) # output to window self.ui.output_no_vig_A.setText("{:.4f}".format(noVigA)) self.ui.output_no_vig_B.setText("{:.4f}".format(noVigB)) if len(self.ui.input_betAmt_A.text()) != 0 and oddsA: betAmtA = self.ui.input_betAmt_A.text() self.ui.input_win_A.setText("${:.2f}".format(float(betAmtA) * netDecOddsA)) self.ui.input_payout_A.setText("${:.2f}".format(float(betAmtA) * decOddsA)) if len(self.ui.input_betAmt_B.text()) != 0 and oddsB: betAmtB = self.ui.input_betAmt_B.text() self.ui.input_win_B.setText("${:.2f}".format(float(betAmtB) * netDecOddsB)) self.ui.input_payout_B.setText("${:.2f}".format(float(betAmtB) * decOddsB))
def test_conversion_to_base2_2(self): test_conversions=Conversions() test_conversions.conversion_to_base2('2017,5603', 8) assert(test_conversions.get_result()=='10000001111,101110000011')
def test_conversion_to_base2(self): test_conversions=Conversions() test_conversions.conversion_to_base2('123,01', 4) assert(test_conversions.get_result()=='11011,0001')
def test_expected_value(): assert Conversions().expected_value(2.0, 0.57) == 0.7099999999999999 assert Conversions().expected_value(3.0, 0.34) == 0.3600000000000001
''' Tibre Diana Andreea, group 917 ''' from operations import Operations from conversions import Conversions from service import Service from ui import UI from tests import Tests from validations import Validation operations=Operations() conversions=Conversions() validations=Validation() service=Service(operations,conversions,validations) ui=UI(service) tests=Tests() tests.run() ui.run()
def test_frac_to_dec(): assert Conversions().frac_to_dec("4:1") == 5.0 assert Conversions().frac_to_dec("4/1") == 5.0 assert Conversions().frac_to_dec("1:4") == 1.25 assert Conversions().frac_to_dec("1/4") == 1.25
def test_odds_type(): assert Conversions().odds_type("-110") == "us" assert Conversions().odds_type("130") == "us" assert Conversions().odds_type("2.0") == "dec" assert Conversions().odds_type("21.033") == "dec" assert Conversions().odds_type("") == None
def test_noVig(): assert Conversions().no_vig(0.52, 0.58) == (0.4727272727272727272727272727) assert Conversions().no_vig(0.1739, 0.5238) == (0.2492475275906550093163250681)
def test_impliedPr(): assert Conversions().implied_pr(426) == 0.1901140684410646387832699620 assert (Conversions().implied_pr(-110) == 0.52380952380952383595769106250372715294361114501953125)
def test_dec_to_frac(): assert Conversions().dec_to_frac(1.9091) == "10/11" assert Conversions().dec_to_frac(1.16501650) == "50/303" assert Conversions().dec_to_frac(2.34) == "67/50"
def test_dec_to_us(): assert Conversions().dec_to_us("1.5") == -200 assert Conversions().dec_to_us("1.9091") == -110 assert Conversions().dec_to_us("4.75") == 375 assert Conversions().dec_to_us("1.9434") == -106 assert Conversions().dec_to_us("2.34") == 134
def test_us_to_dec(): assert Conversions().us_to_dec("-606") == 1.165016501650165016501650165 assert Conversions().us_to_dec("475") == 5.75 assert Conversions().us_to_dec("-106") == 1.943396226415094339622641509 assert Conversions().us_to_dec("234") == 3.34