From 4d09d6df4a0b94546db2915136bed416843855a4 Mon Sep 17 00:00:00 2001 From: Sourcery AI Date: Sun, 11 Oct 2020 10:28:30 +0000 Subject: [PATCH] 'Refactored by Sourcery' --- Solutions/1_10/mortgage.py | 10 +++++----- Solutions/1_33/pcost.py | 6 +----- Solutions/1_5/bounce.py | 6 ++---- Solutions/2_16/pcost.py | 6 +----- Solutions/3_10/fileparse.py | 5 +---- Solutions/3_14/fileparse.py | 5 +---- Solutions/3_14/pcost.py | 8 ++------ Solutions/3_16/fileparse.py | 5 +---- Solutions/3_16/pcost.py | 2 +- Solutions/3_18/fileparse.py | 5 +---- Solutions/3_18/pcost.py | 2 +- Solutions/3_7/fileparse.py | 5 +---- Solutions/4_10/fileparse.py | 5 +---- Solutions/4_10/pcost.py | 2 +- Solutions/4_10/report.py | 3 +-- Solutions/4_4/fileparse.py | 5 +---- Solutions/4_4/pcost.py | 2 +- Solutions/4_4/report.py | 3 +-- Solutions/5_8/fileparse.py | 5 +---- Solutions/5_8/pcost.py | 2 +- Solutions/5_8/report.py | 3 +-- Solutions/6_12/fileparse.py | 5 +---- Solutions/6_12/portfolio.py | 4 ++-- Solutions/6_15/fileparse.py | 5 +---- Solutions/6_3/fileparse.py | 5 +---- Solutions/6_3/portfolio.py | 4 ++-- Solutions/6_7/fileparse.py | 5 +---- Solutions/6_7/portfolio.py | 4 ++-- Solutions/7_11/fileparse.py | 5 +---- Solutions/7_4/fileparse.py | 5 +---- Solutions/7_9/fileparse.py | 5 +---- Solutions/8_1/fileparse.py | 5 +---- Solutions/8_2/fileparse.py | 5 +---- Solutions/9_3/porty-app/porty/fileparse.py | 5 +---- Solutions/9_5/porty-app/porty/fileparse.py | 5 +---- Work/Data/stocksim.py | 3 +-- 36 files changed, 45 insertions(+), 120 deletions(-) diff --git a/Solutions/1_10/mortgage.py b/Solutions/1_10/mortgage.py index 51e477519..d83b6718f 100644 --- a/Solutions/1_10/mortgage.py +++ b/Solutions/1_10/mortgage.py @@ -11,16 +11,16 @@ extra_payment_end_month = 108 while principal > 0: - month = month + 1 + month += 1 principal = principal * (1+rate/12) - payment - total_paid = total_paid + payment + total_paid += payment if month >= extra_payment_start_month and month <= extra_payment_end_month: - principal = principal - extra_payment - total_paid = total_paid + extra_payment + principal -= extra_payment + total_paid += extra_payment print(month, round(total_paid,2), round(principal, 2)) - + print('Total paid', round(total_paid, 2)) print('Months', month) diff --git a/Solutions/1_33/pcost.py b/Solutions/1_33/pcost.py index 8ce3a4043..2c1fcaf4a 100644 --- a/Solutions/1_33/pcost.py +++ b/Solutions/1_33/pcost.py @@ -22,10 +22,6 @@ def portfolio_cost(filename): return total_cost import sys -if len(sys.argv) == 2: - filename = sys.argv[1] -else: - filename = input('Enter a filename:') - +filename = sys.argv[1] if len(sys.argv) == 2 else input('Enter a filename:') cost = portfolio_cost(filename) print('Total cost:', cost) diff --git a/Solutions/1_5/bounce.py b/Solutions/1_5/bounce.py index 1f3fa5317..461aba6b6 100644 --- a/Solutions/1_5/bounce.py +++ b/Solutions/1_5/bounce.py @@ -1,8 +1,6 @@ # bounce.py height = 100 -bounce = 1 -while bounce <= 10: - height = height * (3/5) +for bounce in range(1, 11): + height *= 3/5 print(bounce, round(height, 4)) - bounce += 1 diff --git a/Solutions/2_16/pcost.py b/Solutions/2_16/pcost.py index bb0ede93d..5a01c1f27 100644 --- a/Solutions/2_16/pcost.py +++ b/Solutions/2_16/pcost.py @@ -23,10 +23,6 @@ def portfolio_cost(filename): return total_cost import sys -if len(sys.argv) == 2: - filename = sys.argv[1] -else: - filename = input('Enter a filename:') - +filename = sys.argv[1] if len(sys.argv) == 2 else input('Enter a filename:') cost = portfolio_cost(filename) print('Total cost:', cost) diff --git a/Solutions/3_10/fileparse.py b/Solutions/3_10/fileparse.py index 00a7c0230..62df09228 100644 --- a/Solutions/3_10/fileparse.py +++ b/Solutions/3_10/fileparse.py @@ -39,10 +39,7 @@ def parse_csv(filename, select=None, types=None, has_headers=True, delimiter=',' continue # Make a dictionary or a tuple - if headers: - record = dict(zip(headers, row)) - else: - record = tuple(row) + record = dict(zip(headers, row)) if headers else tuple(row) records.append(record) return records diff --git a/Solutions/3_14/fileparse.py b/Solutions/3_14/fileparse.py index 00a7c0230..62df09228 100644 --- a/Solutions/3_14/fileparse.py +++ b/Solutions/3_14/fileparse.py @@ -39,10 +39,7 @@ def parse_csv(filename, select=None, types=None, has_headers=True, delimiter=',' continue # Make a dictionary or a tuple - if headers: - record = dict(zip(headers, row)) - else: - record = tuple(row) + record = dict(zip(headers, row)) if headers else tuple(row) records.append(record) return records diff --git a/Solutions/3_14/pcost.py b/Solutions/3_14/pcost.py index 5685d0144..8667d21cf 100644 --- a/Solutions/3_14/pcost.py +++ b/Solutions/3_14/pcost.py @@ -7,13 +7,9 @@ def portfolio_cost(filename): Computes the total cost (shares*price) of a portfolio file ''' portfolio = report.read_portfolio(filename) - return sum([s['shares']*s['price'] for s in portfolio]) + return sum(s['shares']*s['price'] for s in portfolio) import sys -if len(sys.argv) == 2: - filename = sys.argv[1] -else: - filename = input('Enter a filename:') - +filename = sys.argv[1] if len(sys.argv) == 2 else input('Enter a filename:') cost = portfolio_cost(filename) print('Total cost:', cost) diff --git a/Solutions/3_16/fileparse.py b/Solutions/3_16/fileparse.py index 00a7c0230..62df09228 100644 --- a/Solutions/3_16/fileparse.py +++ b/Solutions/3_16/fileparse.py @@ -39,10 +39,7 @@ def parse_csv(filename, select=None, types=None, has_headers=True, delimiter=',' continue # Make a dictionary or a tuple - if headers: - record = dict(zip(headers, row)) - else: - record = tuple(row) + record = dict(zip(headers, row)) if headers else tuple(row) records.append(record) return records diff --git a/Solutions/3_16/pcost.py b/Solutions/3_16/pcost.py index b537a456b..a7b9cf949 100644 --- a/Solutions/3_16/pcost.py +++ b/Solutions/3_16/pcost.py @@ -7,7 +7,7 @@ def portfolio_cost(filename): Computes the total cost (shares*price) of a portfolio file ''' portfolio = report.read_portfolio(filename) - return sum([s['shares'] * s['price'] for s in portfolio]) + return sum(s['shares'] * s['price'] for s in portfolio) def main(args): if len(args) != 2: diff --git a/Solutions/3_18/fileparse.py b/Solutions/3_18/fileparse.py index 8ad4554d2..479d93f43 100644 --- a/Solutions/3_18/fileparse.py +++ b/Solutions/3_18/fileparse.py @@ -38,10 +38,7 @@ def parse_csv(lines, select=None, types=None, has_headers=True, delimiter=',', s continue # Make a dictionary or a tuple - if headers: - record = dict(zip(headers, row)) - else: - record = tuple(row) + record = dict(zip(headers, row)) if headers else tuple(row) records.append(record) return records diff --git a/Solutions/3_18/pcost.py b/Solutions/3_18/pcost.py index b537a456b..a7b9cf949 100644 --- a/Solutions/3_18/pcost.py +++ b/Solutions/3_18/pcost.py @@ -7,7 +7,7 @@ def portfolio_cost(filename): Computes the total cost (shares*price) of a portfolio file ''' portfolio = report.read_portfolio(filename) - return sum([s['shares'] * s['price'] for s in portfolio]) + return sum(s['shares'] * s['price'] for s in portfolio) def main(args): if len(args) != 2: diff --git a/Solutions/3_7/fileparse.py b/Solutions/3_7/fileparse.py index 56605d59a..b3aaec61f 100644 --- a/Solutions/3_7/fileparse.py +++ b/Solutions/3_7/fileparse.py @@ -30,10 +30,7 @@ def parse_csv(filename, select=None, types=None, has_headers=True, delimiter=',' row = [func(val) for func, val in zip(types, row)] # Make a dictionary or a tuple - if headers: - record = dict(zip(headers, row)) - else: - record = tuple(row) + record = dict(zip(headers, row)) if headers else tuple(row) records.append(record) return records diff --git a/Solutions/4_10/fileparse.py b/Solutions/4_10/fileparse.py index 8ad4554d2..479d93f43 100644 --- a/Solutions/4_10/fileparse.py +++ b/Solutions/4_10/fileparse.py @@ -38,10 +38,7 @@ def parse_csv(lines, select=None, types=None, has_headers=True, delimiter=',', s continue # Make a dictionary or a tuple - if headers: - record = dict(zip(headers, row)) - else: - record = tuple(row) + record = dict(zip(headers, row)) if headers else tuple(row) records.append(record) return records diff --git a/Solutions/4_10/pcost.py b/Solutions/4_10/pcost.py index a8e07399e..750be8d01 100644 --- a/Solutions/4_10/pcost.py +++ b/Solutions/4_10/pcost.py @@ -7,7 +7,7 @@ def portfolio_cost(filename): Computes the total cost (shares*price) of a portfolio file ''' portfolio = report.read_portfolio(filename) - return sum([s.cost() for s in portfolio]) + return sum(s.cost() for s in portfolio) def main(args): if len(args) != 2: diff --git a/Solutions/4_10/report.py b/Solutions/4_10/report.py index 9cf07b3ac..9eb9d635e 100644 --- a/Solutions/4_10/report.py +++ b/Solutions/4_10/report.py @@ -14,8 +14,7 @@ def read_portfolio(filename): select=['name','shares','price'], types=[str,int,float]) - portfolio = [ Stock(d['name'], d['shares'], d['price']) for d in portdicts ] - return portfolio + return [ Stock(d['name'], d['shares'], d['price']) for d in portdicts ] def read_prices(filename): ''' diff --git a/Solutions/4_4/fileparse.py b/Solutions/4_4/fileparse.py index 8ad4554d2..479d93f43 100644 --- a/Solutions/4_4/fileparse.py +++ b/Solutions/4_4/fileparse.py @@ -38,10 +38,7 @@ def parse_csv(lines, select=None, types=None, has_headers=True, delimiter=',', s continue # Make a dictionary or a tuple - if headers: - record = dict(zip(headers, row)) - else: - record = tuple(row) + record = dict(zip(headers, row)) if headers else tuple(row) records.append(record) return records diff --git a/Solutions/4_4/pcost.py b/Solutions/4_4/pcost.py index a8e07399e..750be8d01 100644 --- a/Solutions/4_4/pcost.py +++ b/Solutions/4_4/pcost.py @@ -7,7 +7,7 @@ def portfolio_cost(filename): Computes the total cost (shares*price) of a portfolio file ''' portfolio = report.read_portfolio(filename) - return sum([s.cost() for s in portfolio]) + return sum(s.cost() for s in portfolio) def main(args): if len(args) != 2: diff --git a/Solutions/4_4/report.py b/Solutions/4_4/report.py index 370badda0..2081d8683 100644 --- a/Solutions/4_4/report.py +++ b/Solutions/4_4/report.py @@ -13,8 +13,7 @@ def read_portfolio(filename): select=['name','shares','price'], types=[str,int,float]) - portfolio = [ Stock(d['name'], d['shares'], d['price']) for d in portdicts ] - return portfolio + return [ Stock(d['name'], d['shares'], d['price']) for d in portdicts ] def read_prices(filename): ''' diff --git a/Solutions/5_8/fileparse.py b/Solutions/5_8/fileparse.py index 8ad4554d2..479d93f43 100644 --- a/Solutions/5_8/fileparse.py +++ b/Solutions/5_8/fileparse.py @@ -38,10 +38,7 @@ def parse_csv(lines, select=None, types=None, has_headers=True, delimiter=',', s continue # Make a dictionary or a tuple - if headers: - record = dict(zip(headers, row)) - else: - record = tuple(row) + record = dict(zip(headers, row)) if headers else tuple(row) records.append(record) return records diff --git a/Solutions/5_8/pcost.py b/Solutions/5_8/pcost.py index a8e07399e..750be8d01 100644 --- a/Solutions/5_8/pcost.py +++ b/Solutions/5_8/pcost.py @@ -7,7 +7,7 @@ def portfolio_cost(filename): Computes the total cost (shares*price) of a portfolio file ''' portfolio = report.read_portfolio(filename) - return sum([s.cost() for s in portfolio]) + return sum(s.cost() for s in portfolio) def main(args): if len(args) != 2: diff --git a/Solutions/5_8/report.py b/Solutions/5_8/report.py index 9cf07b3ac..9eb9d635e 100644 --- a/Solutions/5_8/report.py +++ b/Solutions/5_8/report.py @@ -14,8 +14,7 @@ def read_portfolio(filename): select=['name','shares','price'], types=[str,int,float]) - portfolio = [ Stock(d['name'], d['shares'], d['price']) for d in portdicts ] - return portfolio + return [ Stock(d['name'], d['shares'], d['price']) for d in portdicts ] def read_prices(filename): ''' diff --git a/Solutions/6_12/fileparse.py b/Solutions/6_12/fileparse.py index 8ad4554d2..479d93f43 100644 --- a/Solutions/6_12/fileparse.py +++ b/Solutions/6_12/fileparse.py @@ -38,10 +38,7 @@ def parse_csv(lines, select=None, types=None, has_headers=True, delimiter=',', s continue # Make a dictionary or a tuple - if headers: - record = dict(zip(headers, row)) - else: - record = tuple(row) + record = dict(zip(headers, row)) if headers else tuple(row) records.append(record) return records diff --git a/Solutions/6_12/portfolio.py b/Solutions/6_12/portfolio.py index 9a79361d9..9b5322f25 100644 --- a/Solutions/6_12/portfolio.py +++ b/Solutions/6_12/portfolio.py @@ -14,11 +14,11 @@ def __getitem__(self, index): return self._holdings[index] def __contains__(self, name): - return any([s.name == name for s in self._holdings]) + return any(s.name == name for s in self._holdings) @property def total_cost(self): - return sum([s.shares * s.price for s in self._holdings]) + return sum(s.shares * s.price for s in self._holdings) def tabulate_shares(self): from collections import Counter diff --git a/Solutions/6_15/fileparse.py b/Solutions/6_15/fileparse.py index 8ad4554d2..479d93f43 100644 --- a/Solutions/6_15/fileparse.py +++ b/Solutions/6_15/fileparse.py @@ -38,10 +38,7 @@ def parse_csv(lines, select=None, types=None, has_headers=True, delimiter=',', s continue # Make a dictionary or a tuple - if headers: - record = dict(zip(headers, row)) - else: - record = tuple(row) + record = dict(zip(headers, row)) if headers else tuple(row) records.append(record) return records diff --git a/Solutions/6_3/fileparse.py b/Solutions/6_3/fileparse.py index 8ad4554d2..479d93f43 100644 --- a/Solutions/6_3/fileparse.py +++ b/Solutions/6_3/fileparse.py @@ -38,10 +38,7 @@ def parse_csv(lines, select=None, types=None, has_headers=True, delimiter=',', s continue # Make a dictionary or a tuple - if headers: - record = dict(zip(headers, row)) - else: - record = tuple(row) + record = dict(zip(headers, row)) if headers else tuple(row) records.append(record) return records diff --git a/Solutions/6_3/portfolio.py b/Solutions/6_3/portfolio.py index 9a79361d9..9b5322f25 100644 --- a/Solutions/6_3/portfolio.py +++ b/Solutions/6_3/portfolio.py @@ -14,11 +14,11 @@ def __getitem__(self, index): return self._holdings[index] def __contains__(self, name): - return any([s.name == name for s in self._holdings]) + return any(s.name == name for s in self._holdings) @property def total_cost(self): - return sum([s.shares * s.price for s in self._holdings]) + return sum(s.shares * s.price for s in self._holdings) def tabulate_shares(self): from collections import Counter diff --git a/Solutions/6_7/fileparse.py b/Solutions/6_7/fileparse.py index 8ad4554d2..479d93f43 100644 --- a/Solutions/6_7/fileparse.py +++ b/Solutions/6_7/fileparse.py @@ -38,10 +38,7 @@ def parse_csv(lines, select=None, types=None, has_headers=True, delimiter=',', s continue # Make a dictionary or a tuple - if headers: - record = dict(zip(headers, row)) - else: - record = tuple(row) + record = dict(zip(headers, row)) if headers else tuple(row) records.append(record) return records diff --git a/Solutions/6_7/portfolio.py b/Solutions/6_7/portfolio.py index 9a79361d9..9b5322f25 100644 --- a/Solutions/6_7/portfolio.py +++ b/Solutions/6_7/portfolio.py @@ -14,11 +14,11 @@ def __getitem__(self, index): return self._holdings[index] def __contains__(self, name): - return any([s.name == name for s in self._holdings]) + return any(s.name == name for s in self._holdings) @property def total_cost(self): - return sum([s.shares * s.price for s in self._holdings]) + return sum(s.shares * s.price for s in self._holdings) def tabulate_shares(self): from collections import Counter diff --git a/Solutions/7_11/fileparse.py b/Solutions/7_11/fileparse.py index 8ad4554d2..479d93f43 100644 --- a/Solutions/7_11/fileparse.py +++ b/Solutions/7_11/fileparse.py @@ -38,10 +38,7 @@ def parse_csv(lines, select=None, types=None, has_headers=True, delimiter=',', s continue # Make a dictionary or a tuple - if headers: - record = dict(zip(headers, row)) - else: - record = tuple(row) + record = dict(zip(headers, row)) if headers else tuple(row) records.append(record) return records diff --git a/Solutions/7_4/fileparse.py b/Solutions/7_4/fileparse.py index 8ad4554d2..479d93f43 100644 --- a/Solutions/7_4/fileparse.py +++ b/Solutions/7_4/fileparse.py @@ -38,10 +38,7 @@ def parse_csv(lines, select=None, types=None, has_headers=True, delimiter=',', s continue # Make a dictionary or a tuple - if headers: - record = dict(zip(headers, row)) - else: - record = tuple(row) + record = dict(zip(headers, row)) if headers else tuple(row) records.append(record) return records diff --git a/Solutions/7_9/fileparse.py b/Solutions/7_9/fileparse.py index 8ad4554d2..479d93f43 100644 --- a/Solutions/7_9/fileparse.py +++ b/Solutions/7_9/fileparse.py @@ -38,10 +38,7 @@ def parse_csv(lines, select=None, types=None, has_headers=True, delimiter=',', s continue # Make a dictionary or a tuple - if headers: - record = dict(zip(headers, row)) - else: - record = tuple(row) + record = dict(zip(headers, row)) if headers else tuple(row) records.append(record) return records diff --git a/Solutions/8_1/fileparse.py b/Solutions/8_1/fileparse.py index 8ad4554d2..479d93f43 100644 --- a/Solutions/8_1/fileparse.py +++ b/Solutions/8_1/fileparse.py @@ -38,10 +38,7 @@ def parse_csv(lines, select=None, types=None, has_headers=True, delimiter=',', s continue # Make a dictionary or a tuple - if headers: - record = dict(zip(headers, row)) - else: - record = tuple(row) + record = dict(zip(headers, row)) if headers else tuple(row) records.append(record) return records diff --git a/Solutions/8_2/fileparse.py b/Solutions/8_2/fileparse.py index 9cf0de9a4..25051bdf5 100644 --- a/Solutions/8_2/fileparse.py +++ b/Solutions/8_2/fileparse.py @@ -40,10 +40,7 @@ def parse_csv(lines, select=None, types=None, has_headers=True, delimiter=',', s continue # Make a dictionary or a tuple - if headers: - record = dict(zip(headers, row)) - else: - record = tuple(row) + record = dict(zip(headers, row)) if headers else tuple(row) records.append(record) return records diff --git a/Solutions/9_3/porty-app/porty/fileparse.py b/Solutions/9_3/porty-app/porty/fileparse.py index 8c6d5c199..c5db155a8 100644 --- a/Solutions/9_3/porty-app/porty/fileparse.py +++ b/Solutions/9_3/porty-app/porty/fileparse.py @@ -38,10 +38,7 @@ def parse_csv(lines, select=None, types=None, has_headers=True, delimiter=',', s continue # Make a dictionary or a tuple - if headers: - record = dict(zip(headers, row)) - else: - record = tuple(row) + record = dict(zip(headers, row)) if headers else tuple(row) records.append(record) return records diff --git a/Solutions/9_5/porty-app/porty/fileparse.py b/Solutions/9_5/porty-app/porty/fileparse.py index 8c6d5c199..c5db155a8 100644 --- a/Solutions/9_5/porty-app/porty/fileparse.py +++ b/Solutions/9_5/porty-app/porty/fileparse.py @@ -38,10 +38,7 @@ def parse_csv(lines, select=None, types=None, has_headers=True, delimiter=',', s continue # Make a dictionary or a tuple - if headers: - record = dict(zip(headers, row)) - else: - record = tuple(row) + record = dict(zip(headers, row)) if headers else tuple(row) records.append(record) return records diff --git a/Work/Data/stocksim.py b/Work/Data/stocksim.py index 52ebe4185..8cfd0a9d0 100755 --- a/Work/Data/stocksim.py +++ b/Work/Data/stocksim.py @@ -49,8 +49,7 @@ def read_history(filename): # Format CSV record def csv_record(fields): - s = '"%s",%0.2f,"%s","%s",%0.2f,%0.2f,%0.2f,%0.2f,%d' % tuple(fields) - return s + return '"%s",%0.2f,"%s","%s",%0.2f,%0.2f,%0.2f,%0.2f,%d' % tuple(fields) class StockTrack(object): def __init__(self,name):