From 0bc0f33f3e53e6b5d818ab0d36c4c35542f6fec5 Mon Sep 17 00:00:00 2001 From: Sourcery AI <> Date: Mon, 18 Sep 2023 03:32:17 +0000 Subject: [PATCH] 'Refactored by Sourcery' --- Solutions/1_10/mortgage.py | 4 ++-- Solutions/1_33/pcost.py | 6 +----- Solutions/1_5/bounce.py | 4 +--- 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 | 4 ++-- Solutions/3_16/report.py | 2 +- Solutions/3_18/fileparse.py | 5 +---- Solutions/3_18/pcost.py | 4 ++-- Solutions/3_18/report.py | 2 +- Solutions/3_7/fileparse.py | 5 +---- Solutions/4_10/fileparse.py | 5 +---- Solutions/4_10/pcost.py | 4 ++-- Solutions/4_10/report.py | 5 ++--- Solutions/4_4/fileparse.py | 5 +---- Solutions/4_4/pcost.py | 4 ++-- Solutions/4_4/report.py | 5 ++--- Solutions/5_8/fileparse.py | 5 +---- Solutions/5_8/pcost.py | 4 ++-- Solutions/5_8/report.py | 5 ++--- Solutions/6_12/fileparse.py | 5 +---- Solutions/6_12/pcost.py | 2 +- Solutions/6_12/portfolio.py | 4 ++-- Solutions/6_12/report.py | 2 +- Solutions/6_12/ticker.py | 2 +- Solutions/6_15/fileparse.py | 5 +---- Solutions/6_15/pcost.py | 2 +- Solutions/6_15/report.py | 2 +- Solutions/6_15/ticker.py | 2 +- Solutions/6_3/fileparse.py | 5 +---- Solutions/6_3/pcost.py | 2 +- Solutions/6_3/portfolio.py | 4 ++-- Solutions/6_3/report.py | 2 +- Solutions/6_7/fileparse.py | 5 +---- Solutions/6_7/pcost.py | 2 +- Solutions/6_7/portfolio.py | 4 ++-- Solutions/6_7/report.py | 2 +- Solutions/7_11/fileparse.py | 5 +---- Solutions/7_11/pcost.py | 2 +- Solutions/7_11/report.py | 2 +- Solutions/7_11/ticker.py | 2 +- Solutions/7_11/typedproperty.py | 2 +- Solutions/7_4/fileparse.py | 5 +---- Solutions/7_4/pcost.py | 2 +- Solutions/7_4/report.py | 2 +- Solutions/7_4/ticker.py | 2 +- Solutions/7_9/fileparse.py | 5 +---- Solutions/7_9/pcost.py | 2 +- Solutions/7_9/report.py | 2 +- Solutions/7_9/ticker.py | 2 +- Solutions/7_9/typedproperty.py | 2 +- Solutions/8_1/fileparse.py | 5 +---- Solutions/8_1/pcost.py | 2 +- Solutions/8_1/report.py | 2 +- Solutions/8_1/ticker.py | 2 +- Solutions/8_1/typedproperty.py | 2 +- Solutions/8_2/fileparse.py | 5 +---- Solutions/8_2/pcost.py | 2 +- Solutions/8_2/report.py | 2 +- Solutions/8_2/ticker.py | 2 +- Solutions/8_2/typedproperty.py | 2 +- Solutions/9_3/porty-app/porty/fileparse.py | 7 ++----- Solutions/9_3/porty-app/porty/pcost.py | 2 +- Solutions/9_3/porty-app/porty/report.py | 2 +- Solutions/9_3/porty-app/porty/ticker.py | 2 +- Solutions/9_3/porty-app/porty/typedproperty.py | 2 +- Solutions/9_5/porty-app/porty/fileparse.py | 7 ++----- Solutions/9_5/porty-app/porty/pcost.py | 2 +- Solutions/9_5/porty-app/porty/report.py | 2 +- Solutions/9_5/porty-app/porty/ticker.py | 2 +- Solutions/9_5/porty-app/porty/typedproperty.py | 2 +- Work/Data/stocksim.py | 3 +-- 75 files changed, 90 insertions(+), 165 deletions(-) diff --git a/Solutions/1_10/mortgage.py b/Solutions/1_10/mortgage.py index 51e477519..5398a06c3 100644 --- a/Solutions/1_10/mortgage.py +++ b/Solutions/1_10/mortgage.py @@ -16,11 +16,11 @@ total_paid = total_paid + payment if month >= extra_payment_start_month and month <= extra_payment_end_month: - principal = principal - extra_payment + principal -= extra_payment total_paid = 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..166a400e7 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: +for bounce in range(1, 11): height = 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..2968f9b78 100644 --- a/Solutions/3_16/pcost.py +++ b/Solutions/3_16/pcost.py @@ -7,11 +7,11 @@ 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: - raise SystemExit('Usage: %s portfoliofile' % args[0]) + raise SystemExit(f'Usage: {args[0]} portfoliofile') filename = args[1] print('Total cost:', portfolio_cost(filename)) diff --git a/Solutions/3_16/report.py b/Solutions/3_16/report.py index aba162bca..6eabb926c 100644 --- a/Solutions/3_16/report.py +++ b/Solutions/3_16/report.py @@ -54,7 +54,7 @@ def portfolio_report(portfoliofile, pricefile): def main(args): if len(args) != 3: - raise SystemExit('Usage: %s portfile pricefile' % args[0]) + raise SystemExit(f'Usage: {args[0]} portfile pricefile') portfolio_report(args[1], args[2]) if __name__ == '__main__': 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..2968f9b78 100644 --- a/Solutions/3_18/pcost.py +++ b/Solutions/3_18/pcost.py @@ -7,11 +7,11 @@ 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: - raise SystemExit('Usage: %s portfoliofile' % args[0]) + raise SystemExit(f'Usage: {args[0]} portfoliofile') filename = args[1] print('Total cost:', portfolio_cost(filename)) diff --git a/Solutions/3_18/report.py b/Solutions/3_18/report.py index fbf71bd10..09b090270 100644 --- a/Solutions/3_18/report.py +++ b/Solutions/3_18/report.py @@ -56,7 +56,7 @@ def portfolio_report(portfoliofile, pricefile): def main(args): if len(args) != 3: - raise SystemExit('Usage: %s portfile pricefile' % args[0]) + raise SystemExit(f'Usage: {args[0]} portfile pricefile') portfolio_report(args[1], args[2]) if __name__ == '__main__': 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..fff42be2e 100644 --- a/Solutions/4_10/pcost.py +++ b/Solutions/4_10/pcost.py @@ -7,11 +7,11 @@ 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: - raise SystemExit('Usage: %s portfoliofile' % args[0]) + raise SystemExit(f'Usage: {args[0]} portfoliofile') filename = args[1] print('Total cost:', portfolio_cost(filename)) diff --git a/Solutions/4_10/report.py b/Solutions/4_10/report.py index 9cf07b3ac..42096f44f 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): ''' @@ -63,7 +62,7 @@ def portfolio_report(portfoliofile, pricefile, fmt='txt'): def main(args): if len(args) != 4: - raise SystemExit('Usage: %s portfile pricefile format' % args[0]) + raise SystemExit(f'Usage: {args[0]} portfile pricefile format') portfolio_report(args[1], args[2], args[3]) if __name__ == '__main__': 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..fff42be2e 100644 --- a/Solutions/4_4/pcost.py +++ b/Solutions/4_4/pcost.py @@ -7,11 +7,11 @@ 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: - raise SystemExit('Usage: %s portfoliofile' % args[0]) + raise SystemExit(f'Usage: {args[0]} portfoliofile') filename = args[1] print('Total cost:', portfolio_cost(filename)) diff --git a/Solutions/4_4/report.py b/Solutions/4_4/report.py index 370badda0..c70b80dd1 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): ''' @@ -62,7 +61,7 @@ def portfolio_report(portfoliofile, pricefile): def main(args): if len(args) != 3: - raise SystemExit('Usage: %s portfile pricefile' % args[0]) + raise SystemExit(f'Usage: {args[0]} portfile pricefile') portfolio_report(args[1], args[2]) if __name__ == '__main__': 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..fff42be2e 100644 --- a/Solutions/5_8/pcost.py +++ b/Solutions/5_8/pcost.py @@ -7,11 +7,11 @@ 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: - raise SystemExit('Usage: %s portfoliofile' % args[0]) + raise SystemExit(f'Usage: {args[0]} portfoliofile') filename = args[1] print('Total cost:', portfolio_cost(filename)) diff --git a/Solutions/5_8/report.py b/Solutions/5_8/report.py index 9cf07b3ac..42096f44f 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): ''' @@ -63,7 +62,7 @@ def portfolio_report(portfoliofile, pricefile, fmt='txt'): def main(args): if len(args) != 4: - raise SystemExit('Usage: %s portfile pricefile format' % args[0]) + raise SystemExit(f'Usage: {args[0]} portfile pricefile format') portfolio_report(args[1], args[2], args[3]) if __name__ == '__main__': 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/pcost.py b/Solutions/6_12/pcost.py index 1a5aef3f0..dd8b01755 100644 --- a/Solutions/6_12/pcost.py +++ b/Solutions/6_12/pcost.py @@ -11,7 +11,7 @@ def portfolio_cost(filename): def main(args): if len(args) != 2: - raise SystemExit('Usage: %s portfoliofile' % args[0]) + raise SystemExit(f'Usage: {args[0]} portfoliofile') filename = args[1] print('Total cost:', portfolio_cost(filename)) 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_12/report.py b/Solutions/6_12/report.py index 7c16aad5d..9bc62a935 100644 --- a/Solutions/6_12/report.py +++ b/Solutions/6_12/report.py @@ -64,7 +64,7 @@ def portfolio_report(portfoliofile, pricefile, fmt='txt'): def main(args): if len(args) != 4: - raise SystemExit('Usage: %s portfile pricefile format' % args[0]) + raise SystemExit(f'Usage: {args[0]} portfile pricefile format') portfolio_report(args[1], args[2], args[3]) if __name__ == '__main__': diff --git a/Solutions/6_12/ticker.py b/Solutions/6_12/ticker.py index 6922632cc..b96c0d972 100644 --- a/Solutions/6_12/ticker.py +++ b/Solutions/6_12/ticker.py @@ -42,7 +42,7 @@ def ticker(portfile, logfile, fmt): def main(args): if len(args) != 4: - raise SystemExit('Usage: %s portfoliofile logfile fmt' % args[0]) + raise SystemExit(f'Usage: {args[0]} portfoliofile logfile fmt') ticker(args[1], args[2], args[3]) if __name__ == '__main__': 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_15/pcost.py b/Solutions/6_15/pcost.py index 1a5aef3f0..dd8b01755 100644 --- a/Solutions/6_15/pcost.py +++ b/Solutions/6_15/pcost.py @@ -11,7 +11,7 @@ def portfolio_cost(filename): def main(args): if len(args) != 2: - raise SystemExit('Usage: %s portfoliofile' % args[0]) + raise SystemExit(f'Usage: {args[0]} portfoliofile') filename = args[1] print('Total cost:', portfolio_cost(filename)) diff --git a/Solutions/6_15/report.py b/Solutions/6_15/report.py index 7c16aad5d..9bc62a935 100644 --- a/Solutions/6_15/report.py +++ b/Solutions/6_15/report.py @@ -64,7 +64,7 @@ def portfolio_report(portfoliofile, pricefile, fmt='txt'): def main(args): if len(args) != 4: - raise SystemExit('Usage: %s portfile pricefile format' % args[0]) + raise SystemExit(f'Usage: {args[0]} portfile pricefile format') portfolio_report(args[1], args[2], args[3]) if __name__ == '__main__': diff --git a/Solutions/6_15/ticker.py b/Solutions/6_15/ticker.py index 67171e092..c91353840 100644 --- a/Solutions/6_15/ticker.py +++ b/Solutions/6_15/ticker.py @@ -36,7 +36,7 @@ def ticker(portfile, logfile, fmt): def main(args): if len(args) != 4: - raise SystemExit('Usage: %s portfoliofile logfile fmt' % args[0]) + raise SystemExit(f'Usage: {args[0]} portfoliofile logfile fmt') ticker(args[1], args[2], args[3]) if __name__ == '__main__': 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/pcost.py b/Solutions/6_3/pcost.py index 1a5aef3f0..dd8b01755 100644 --- a/Solutions/6_3/pcost.py +++ b/Solutions/6_3/pcost.py @@ -11,7 +11,7 @@ def portfolio_cost(filename): def main(args): if len(args) != 2: - raise SystemExit('Usage: %s portfoliofile' % args[0]) + raise SystemExit(f'Usage: {args[0]} portfoliofile') filename = args[1] print('Total cost:', portfolio_cost(filename)) 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_3/report.py b/Solutions/6_3/report.py index 7c16aad5d..9bc62a935 100644 --- a/Solutions/6_3/report.py +++ b/Solutions/6_3/report.py @@ -64,7 +64,7 @@ def portfolio_report(portfoliofile, pricefile, fmt='txt'): def main(args): if len(args) != 4: - raise SystemExit('Usage: %s portfile pricefile format' % args[0]) + raise SystemExit(f'Usage: {args[0]} portfile pricefile format') portfolio_report(args[1], args[2], args[3]) if __name__ == '__main__': 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/pcost.py b/Solutions/6_7/pcost.py index 1a5aef3f0..dd8b01755 100644 --- a/Solutions/6_7/pcost.py +++ b/Solutions/6_7/pcost.py @@ -11,7 +11,7 @@ def portfolio_cost(filename): def main(args): if len(args) != 2: - raise SystemExit('Usage: %s portfoliofile' % args[0]) + raise SystemExit(f'Usage: {args[0]} portfoliofile') filename = args[1] print('Total cost:', portfolio_cost(filename)) 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/6_7/report.py b/Solutions/6_7/report.py index 7c16aad5d..9bc62a935 100644 --- a/Solutions/6_7/report.py +++ b/Solutions/6_7/report.py @@ -64,7 +64,7 @@ def portfolio_report(portfoliofile, pricefile, fmt='txt'): def main(args): if len(args) != 4: - raise SystemExit('Usage: %s portfile pricefile format' % args[0]) + raise SystemExit(f'Usage: {args[0]} portfile pricefile format') portfolio_report(args[1], args[2], args[3]) if __name__ == '__main__': 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_11/pcost.py b/Solutions/7_11/pcost.py index 1a5aef3f0..dd8b01755 100644 --- a/Solutions/7_11/pcost.py +++ b/Solutions/7_11/pcost.py @@ -11,7 +11,7 @@ def portfolio_cost(filename): def main(args): if len(args) != 2: - raise SystemExit('Usage: %s portfoliofile' % args[0]) + raise SystemExit(f'Usage: {args[0]} portfoliofile') filename = args[1] print('Total cost:', portfolio_cost(filename)) diff --git a/Solutions/7_11/report.py b/Solutions/7_11/report.py index 4c74f6e33..a1bfbd244 100644 --- a/Solutions/7_11/report.py +++ b/Solutions/7_11/report.py @@ -59,7 +59,7 @@ def portfolio_report(portfoliofile, pricefile, fmt='txt'): def main(args): if len(args) != 4: - raise SystemExit('Usage: %s portfile pricefile format' % args[0]) + raise SystemExit(f'Usage: {args[0]} portfile pricefile format') portfolio_report(args[1], args[2], args[3]) if __name__ == '__main__': diff --git a/Solutions/7_11/ticker.py b/Solutions/7_11/ticker.py index 31f8e6774..702272f05 100644 --- a/Solutions/7_11/ticker.py +++ b/Solutions/7_11/ticker.py @@ -35,7 +35,7 @@ def ticker(portfile, logfile, fmt): def main(args): if len(args) != 4: - raise SystemExit('Usage: %s portfoliofile logfile fmt' % args[0]) + raise SystemExit(f'Usage: {args[0]} portfoliofile logfile fmt') ticker(args[1], args[2], args[3]) if __name__ == '__main__': diff --git a/Solutions/7_11/typedproperty.py b/Solutions/7_11/typedproperty.py index e4119301a..2c29398e4 100644 --- a/Solutions/7_11/typedproperty.py +++ b/Solutions/7_11/typedproperty.py @@ -1,7 +1,7 @@ # typedproperty.py def typedproperty(name, expected_type): - private_name = '_' + name + private_name = f'_{name}' @property def prop(self): return getattr(self, private_name) 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_4/pcost.py b/Solutions/7_4/pcost.py index 1a5aef3f0..dd8b01755 100644 --- a/Solutions/7_4/pcost.py +++ b/Solutions/7_4/pcost.py @@ -11,7 +11,7 @@ def portfolio_cost(filename): def main(args): if len(args) != 2: - raise SystemExit('Usage: %s portfoliofile' % args[0]) + raise SystemExit(f'Usage: {args[0]} portfoliofile') filename = args[1] print('Total cost:', portfolio_cost(filename)) diff --git a/Solutions/7_4/report.py b/Solutions/7_4/report.py index 789a08e5b..b8daf6150 100644 --- a/Solutions/7_4/report.py +++ b/Solutions/7_4/report.py @@ -65,7 +65,7 @@ def portfolio_report(portfoliofile, pricefile, fmt='txt'): def main(args): if len(args) != 4: - raise SystemExit('Usage: %s portfile pricefile format' % args[0]) + raise SystemExit(f'Usage: {args[0]} portfile pricefile format') portfolio_report(args[1], args[2], args[3]) if __name__ == '__main__': diff --git a/Solutions/7_4/ticker.py b/Solutions/7_4/ticker.py index 67171e092..c91353840 100644 --- a/Solutions/7_4/ticker.py +++ b/Solutions/7_4/ticker.py @@ -36,7 +36,7 @@ def ticker(portfile, logfile, fmt): def main(args): if len(args) != 4: - raise SystemExit('Usage: %s portfoliofile logfile fmt' % args[0]) + raise SystemExit(f'Usage: {args[0]} portfoliofile logfile fmt') ticker(args[1], args[2], args[3]) if __name__ == '__main__': 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/7_9/pcost.py b/Solutions/7_9/pcost.py index 1a5aef3f0..dd8b01755 100644 --- a/Solutions/7_9/pcost.py +++ b/Solutions/7_9/pcost.py @@ -11,7 +11,7 @@ def portfolio_cost(filename): def main(args): if len(args) != 2: - raise SystemExit('Usage: %s portfoliofile' % args[0]) + raise SystemExit(f'Usage: {args[0]} portfoliofile') filename = args[1] print('Total cost:', portfolio_cost(filename)) diff --git a/Solutions/7_9/report.py b/Solutions/7_9/report.py index 789a08e5b..b8daf6150 100644 --- a/Solutions/7_9/report.py +++ b/Solutions/7_9/report.py @@ -65,7 +65,7 @@ def portfolio_report(portfoliofile, pricefile, fmt='txt'): def main(args): if len(args) != 4: - raise SystemExit('Usage: %s portfile pricefile format' % args[0]) + raise SystemExit(f'Usage: {args[0]} portfile pricefile format') portfolio_report(args[1], args[2], args[3]) if __name__ == '__main__': diff --git a/Solutions/7_9/ticker.py b/Solutions/7_9/ticker.py index 67171e092..c91353840 100644 --- a/Solutions/7_9/ticker.py +++ b/Solutions/7_9/ticker.py @@ -36,7 +36,7 @@ def ticker(portfile, logfile, fmt): def main(args): if len(args) != 4: - raise SystemExit('Usage: %s portfoliofile logfile fmt' % args[0]) + raise SystemExit(f'Usage: {args[0]} portfoliofile logfile fmt') ticker(args[1], args[2], args[3]) if __name__ == '__main__': diff --git a/Solutions/7_9/typedproperty.py b/Solutions/7_9/typedproperty.py index a6b4a59d1..124fa94d0 100644 --- a/Solutions/7_9/typedproperty.py +++ b/Solutions/7_9/typedproperty.py @@ -1,7 +1,7 @@ # typedproperty.py def typedproperty(name, expected_type): - private_name = '_' + name + private_name = f'_{name}' @property def prop(self): return getattr(self, private_name) 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_1/pcost.py b/Solutions/8_1/pcost.py index 1a5aef3f0..dd8b01755 100644 --- a/Solutions/8_1/pcost.py +++ b/Solutions/8_1/pcost.py @@ -11,7 +11,7 @@ def portfolio_cost(filename): def main(args): if len(args) != 2: - raise SystemExit('Usage: %s portfoliofile' % args[0]) + raise SystemExit(f'Usage: {args[0]} portfoliofile') filename = args[1] print('Total cost:', portfolio_cost(filename)) diff --git a/Solutions/8_1/report.py b/Solutions/8_1/report.py index 4c74f6e33..a1bfbd244 100644 --- a/Solutions/8_1/report.py +++ b/Solutions/8_1/report.py @@ -59,7 +59,7 @@ def portfolio_report(portfoliofile, pricefile, fmt='txt'): def main(args): if len(args) != 4: - raise SystemExit('Usage: %s portfile pricefile format' % args[0]) + raise SystemExit(f'Usage: {args[0]} portfile pricefile format') portfolio_report(args[1], args[2], args[3]) if __name__ == '__main__': diff --git a/Solutions/8_1/ticker.py b/Solutions/8_1/ticker.py index 31f8e6774..702272f05 100644 --- a/Solutions/8_1/ticker.py +++ b/Solutions/8_1/ticker.py @@ -35,7 +35,7 @@ def ticker(portfile, logfile, fmt): def main(args): if len(args) != 4: - raise SystemExit('Usage: %s portfoliofile logfile fmt' % args[0]) + raise SystemExit(f'Usage: {args[0]} portfoliofile logfile fmt') ticker(args[1], args[2], args[3]) if __name__ == '__main__': diff --git a/Solutions/8_1/typedproperty.py b/Solutions/8_1/typedproperty.py index a2e68833b..a8049164e 100644 --- a/Solutions/8_1/typedproperty.py +++ b/Solutions/8_1/typedproperty.py @@ -1,7 +1,7 @@ # typedproperty.py def typedproperty(name, expected_type): - private_name = '_' + name + private_name = f'_{name}' @property def prop(self): return getattr(self, private_name) 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/8_2/pcost.py b/Solutions/8_2/pcost.py index 1a5aef3f0..dd8b01755 100644 --- a/Solutions/8_2/pcost.py +++ b/Solutions/8_2/pcost.py @@ -11,7 +11,7 @@ def portfolio_cost(filename): def main(args): if len(args) != 2: - raise SystemExit('Usage: %s portfoliofile' % args[0]) + raise SystemExit(f'Usage: {args[0]} portfoliofile') filename = args[1] print('Total cost:', portfolio_cost(filename)) diff --git a/Solutions/8_2/report.py b/Solutions/8_2/report.py index 4c74f6e33..a1bfbd244 100644 --- a/Solutions/8_2/report.py +++ b/Solutions/8_2/report.py @@ -59,7 +59,7 @@ def portfolio_report(portfoliofile, pricefile, fmt='txt'): def main(args): if len(args) != 4: - raise SystemExit('Usage: %s portfile pricefile format' % args[0]) + raise SystemExit(f'Usage: {args[0]} portfile pricefile format') portfolio_report(args[1], args[2], args[3]) if __name__ == '__main__': diff --git a/Solutions/8_2/ticker.py b/Solutions/8_2/ticker.py index 31f8e6774..702272f05 100644 --- a/Solutions/8_2/ticker.py +++ b/Solutions/8_2/ticker.py @@ -35,7 +35,7 @@ def ticker(portfile, logfile, fmt): def main(args): if len(args) != 4: - raise SystemExit('Usage: %s portfoliofile logfile fmt' % args[0]) + raise SystemExit(f'Usage: {args[0]} portfoliofile logfile fmt') ticker(args[1], args[2], args[3]) if __name__ == '__main__': diff --git a/Solutions/8_2/typedproperty.py b/Solutions/8_2/typedproperty.py index a2e68833b..a8049164e 100644 --- a/Solutions/8_2/typedproperty.py +++ b/Solutions/8_2/typedproperty.py @@ -1,7 +1,7 @@ # typedproperty.py def typedproperty(name, expected_type): - private_name = '_' + name + private_name = f'_{name}' @property def prop(self): return getattr(self, private_name) diff --git a/Solutions/9_3/porty-app/porty/fileparse.py b/Solutions/9_3/porty-app/porty/fileparse.py index 8c6d5c199..d2ef606c8 100644 --- a/Solutions/9_3/porty-app/porty/fileparse.py +++ b/Solutions/9_3/porty-app/porty/fileparse.py @@ -7,7 +7,7 @@ def parse_csv(lines, select=None, types=None, has_headers=True, delimiter=',', s ''' Parse a CSV file into a list of records with type conversion. ''' - assert not (select and not has_headers), 'select requires column headers' + assert not select or has_headers, 'select requires column headers' rows = csv.reader(lines, delimiter=delimiter) # Read the file headers (if any) @@ -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_3/porty-app/porty/pcost.py b/Solutions/9_3/porty-app/porty/pcost.py index 8f43a3e06..20666673c 100644 --- a/Solutions/9_3/porty-app/porty/pcost.py +++ b/Solutions/9_3/porty-app/porty/pcost.py @@ -11,7 +11,7 @@ def portfolio_cost(filename): def main(args): if len(args) != 2: - raise SystemExit('Usage: %s portfoliofile' % args[0]) + raise SystemExit(f'Usage: {args[0]} portfoliofile') filename = args[1] print('Total cost:', portfolio_cost(filename)) diff --git a/Solutions/9_3/porty-app/porty/report.py b/Solutions/9_3/porty-app/porty/report.py index 0131aad4a..43d4a4cec 100644 --- a/Solutions/9_3/porty-app/porty/report.py +++ b/Solutions/9_3/porty-app/porty/report.py @@ -59,7 +59,7 @@ def portfolio_report(portfoliofile, pricefile, fmt='txt'): def main(args): if len(args) != 4: - raise SystemExit('Usage: %s portfile pricefile format' % args[0]) + raise SystemExit(f'Usage: {args[0]} portfile pricefile format') portfolio_report(args[1], args[2], args[3]) if __name__ == '__main__': diff --git a/Solutions/9_3/porty-app/porty/ticker.py b/Solutions/9_3/porty-app/porty/ticker.py index 4ac26bcdc..7d34160cf 100644 --- a/Solutions/9_3/porty-app/porty/ticker.py +++ b/Solutions/9_3/porty-app/porty/ticker.py @@ -35,7 +35,7 @@ def ticker(portfile, logfile, fmt): def main(args): if len(args) != 4: - raise SystemExit('Usage: %s portfoliofile logfile fmt' % args[0]) + raise SystemExit(f'Usage: {args[0]} portfoliofile logfile fmt') ticker(args[1], args[2], args[3]) if __name__ == '__main__': diff --git a/Solutions/9_3/porty-app/porty/typedproperty.py b/Solutions/9_3/porty-app/porty/typedproperty.py index a2e68833b..a8049164e 100644 --- a/Solutions/9_3/porty-app/porty/typedproperty.py +++ b/Solutions/9_3/porty-app/porty/typedproperty.py @@ -1,7 +1,7 @@ # typedproperty.py def typedproperty(name, expected_type): - private_name = '_' + name + private_name = f'_{name}' @property def prop(self): return getattr(self, private_name) diff --git a/Solutions/9_5/porty-app/porty/fileparse.py b/Solutions/9_5/porty-app/porty/fileparse.py index 8c6d5c199..d2ef606c8 100644 --- a/Solutions/9_5/porty-app/porty/fileparse.py +++ b/Solutions/9_5/porty-app/porty/fileparse.py @@ -7,7 +7,7 @@ def parse_csv(lines, select=None, types=None, has_headers=True, delimiter=',', s ''' Parse a CSV file into a list of records with type conversion. ''' - assert not (select and not has_headers), 'select requires column headers' + assert not select or has_headers, 'select requires column headers' rows = csv.reader(lines, delimiter=delimiter) # Read the file headers (if any) @@ -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/pcost.py b/Solutions/9_5/porty-app/porty/pcost.py index 8f43a3e06..20666673c 100644 --- a/Solutions/9_5/porty-app/porty/pcost.py +++ b/Solutions/9_5/porty-app/porty/pcost.py @@ -11,7 +11,7 @@ def portfolio_cost(filename): def main(args): if len(args) != 2: - raise SystemExit('Usage: %s portfoliofile' % args[0]) + raise SystemExit(f'Usage: {args[0]} portfoliofile') filename = args[1] print('Total cost:', portfolio_cost(filename)) diff --git a/Solutions/9_5/porty-app/porty/report.py b/Solutions/9_5/porty-app/porty/report.py index 0131aad4a..43d4a4cec 100644 --- a/Solutions/9_5/porty-app/porty/report.py +++ b/Solutions/9_5/porty-app/porty/report.py @@ -59,7 +59,7 @@ def portfolio_report(portfoliofile, pricefile, fmt='txt'): def main(args): if len(args) != 4: - raise SystemExit('Usage: %s portfile pricefile format' % args[0]) + raise SystemExit(f'Usage: {args[0]} portfile pricefile format') portfolio_report(args[1], args[2], args[3]) if __name__ == '__main__': diff --git a/Solutions/9_5/porty-app/porty/ticker.py b/Solutions/9_5/porty-app/porty/ticker.py index 4ac26bcdc..7d34160cf 100644 --- a/Solutions/9_5/porty-app/porty/ticker.py +++ b/Solutions/9_5/porty-app/porty/ticker.py @@ -35,7 +35,7 @@ def ticker(portfile, logfile, fmt): def main(args): if len(args) != 4: - raise SystemExit('Usage: %s portfoliofile logfile fmt' % args[0]) + raise SystemExit(f'Usage: {args[0]} portfoliofile logfile fmt') ticker(args[1], args[2], args[3]) if __name__ == '__main__': diff --git a/Solutions/9_5/porty-app/porty/typedproperty.py b/Solutions/9_5/porty-app/porty/typedproperty.py index a2e68833b..a8049164e 100644 --- a/Solutions/9_5/porty-app/porty/typedproperty.py +++ b/Solutions/9_5/porty-app/porty/typedproperty.py @@ -1,7 +1,7 @@ # typedproperty.py def typedproperty(name, expected_type): - private_name = '_' + name + private_name = f'_{name}' @property def prop(self): return getattr(self, private_name) 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):