From d43dd71bc7e3b9822501717510e8f0f6e4a77e9d Mon Sep 17 00:00:00 2001 From: lolPirate Date: Fri, 29 May 2020 23:23:13 +0530 Subject: [PATCH 01/12] completed exercise 1.5 --- Work/bounce.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Work/bounce.py b/Work/bounce.py index 3660ddd82..6da1b5144 100644 --- a/Work/bounce.py +++ b/Work/bounce.py @@ -1,3 +1,9 @@ # bounce.py # # Exercise 1.5 + +height = 100 #meters + +for i in range(1,11): + height = (3/5) * height + print (f'{i} {round(height, 4)}') \ No newline at end of file From 84548562cb9ca790e3b1b5afa9bee8fb9d1b1bae Mon Sep 17 00:00:00 2001 From: lolPirate Date: Fri, 29 May 2020 23:23:48 +0530 Subject: [PATCH 02/12] completed exercise 1.5 --- Work/bounce.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Work/bounce.py b/Work/bounce.py index 6da1b5144..b3dd54853 100644 --- a/Work/bounce.py +++ b/Work/bounce.py @@ -2,8 +2,8 @@ # # Exercise 1.5 -height = 100 #meters +height = 100 # meters -for i in range(1,11): +for i in range(1, 11): height = (3/5) * height - print (f'{i} {round(height, 4)}') \ No newline at end of file + print(f'{i} {round(height, 4)}') From e13f8530a2ddffbe3f2ad8d94419b868aa51ec54 Mon Sep 17 00:00:00 2001 From: lolPirate Date: Sat, 30 May 2020 00:11:36 +0530 Subject: [PATCH 03/12] completed exercise 1.11 --- Work/mortgage.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/Work/mortgage.py b/Work/mortgage.py index d527314e3..14deabb96 100644 --- a/Work/mortgage.py +++ b/Work/mortgage.py @@ -1,3 +1,25 @@ # mortgage.py # # Exercise 1.7 +principal = 500000.0 +rate = 0.05 +payment = 2684.11 +total_paid = 0.0 +months = 0 +extra_payment_start_month = 60 +extra_payment_end_month = 108 +extra_payment = 1000 + +while principal > 0: + months += 1 + interest = principal * (1+rate/12) + if interest < payment: + payment = interest + principal = interest - payment + total_paid = total_paid + payment + if extra_payment_start_month <= months <= extra_payment_end_month: + principal = principal - extra_payment + total_paid = total_paid + extra_payment + print(f'{months}\t{round(total_paid, 2)}\t{round(principal, 2)}') + +print(f'{round(total_paid, 2)} over {months} months') From 9b96c879ee197ce99c4a444d4bec9baf9b530f3f Mon Sep 17 00:00:00 2001 From: lolPirate Date: Sat, 30 May 2020 00:37:52 +0530 Subject: [PATCH 04/12] completed exercise 1.17 --- Work/mortgage.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Work/mortgage.py b/Work/mortgage.py index 14deabb96..e4cdf34b1 100644 --- a/Work/mortgage.py +++ b/Work/mortgage.py @@ -20,6 +20,6 @@ if extra_payment_start_month <= months <= extra_payment_end_month: principal = principal - extra_payment total_paid = total_paid + extra_payment - print(f'{months}\t{round(total_paid, 2)}\t{round(principal, 2)}') + print(f'{months}\t{total_paid:0.2f}\t{principal:10.2f}') -print(f'{round(total_paid, 2)} over {months} months') +print(f'{total_paid:0.2f} over {months} months') From 0aaddcaff8e4005e038c61bb78ceb46bbab481c0 Mon Sep 17 00:00:00 2001 From: lolPirate Date: Sat, 30 May 2020 01:39:08 +0530 Subject: [PATCH 05/12] completed exercise 1.27 --- Work/pcost.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Work/pcost.py b/Work/pcost.py index e68aa20b4..054aad6f5 100644 --- a/Work/pcost.py +++ b/Work/pcost.py @@ -1,3 +1,15 @@ # pcost.py # # Exercise 1.27 + +file = r'Work\Data\portfolio.csv' + +total_cost = 0.0 + +with open(file, 'rt') as f: + next(f) + for line in f: + name, shares, price = line.strip().split(',') + total_cost = total_cost + int(shares) * float(price) + +print(f'Total cost {total_cost:0.2f}') From 7c7c5563bcb07d43e7f77166dbb8af4684a5004e Mon Sep 17 00:00:00 2001 From: lolPirate Date: Sat, 30 May 2020 01:48:35 +0530 Subject: [PATCH 06/12] completed exercise 1.30 --- Work/pcost.py | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/Work/pcost.py b/Work/pcost.py index 054aad6f5..64b5ba9ce 100644 --- a/Work/pcost.py +++ b/Work/pcost.py @@ -2,14 +2,19 @@ # # Exercise 1.27 -file = r'Work\Data\portfolio.csv' - -total_cost = 0.0 - -with open(file, 'rt') as f: - next(f) - for line in f: - name, shares, price = line.strip().split(',') - total_cost = total_cost + int(shares) * float(price) +def portfolio_cost(file_name): + """ + Returns the total cost to buy all shares. + """ + total_cost = 0.0 + with open(file_name, 'rt') as f: + next(f) + for line in f: + _, shares, price = line.strip().split(',') + total_cost = total_cost + int(shares) * float(price) + return total_cost +file = r'Work\Data\portfolio.csv' +total_cost = portfolio_cost(file) print(f'Total cost {total_cost:0.2f}') + From cc41577bc94377f6b36bc7669c4fb9d792f0c4c5 Mon Sep 17 00:00:00 2001 From: lolPirate Date: Sat, 30 May 2020 02:01:37 +0530 Subject: [PATCH 07/12] completed exercise 1.33 --- Work/pcost.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Work/pcost.py b/Work/pcost.py index 64b5ba9ce..3feec6e53 100644 --- a/Work/pcost.py +++ b/Work/pcost.py @@ -1,6 +1,7 @@ # pcost.py # # Exercise 1.27 +import sys def portfolio_cost(file_name): """ @@ -14,7 +15,12 @@ def portfolio_cost(file_name): total_cost = total_cost + int(shares) * float(price) return total_cost -file = r'Work\Data\portfolio.csv' + +if len(sys.argv) == 2: + file = sys.argv[1] +else: + file = r'Work\Data\portfolio.csv' + total_cost = portfolio_cost(file) print(f'Total cost {total_cost:0.2f}') From 52e2d1f1be250d7a24b4fad5a1cb907a53b20124 Mon Sep 17 00:00:00 2001 From: lolPirate Date: Sat, 30 May 2020 02:59:20 +0530 Subject: [PATCH 08/12] completed exercise 2.7 --- Work/report.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/Work/report.py b/Work/report.py index 47d5da7b1..6cfacdf80 100644 --- a/Work/report.py +++ b/Work/report.py @@ -1,3 +1,50 @@ # report.py # # Exercise 2.4 +import csv +from pprint import pprint + +def read_portfolio(file_name): + """ + Opens portfolio file, reads it and returns a list. + """ + portfolio = [] + with open(file_name, 'rt') as f: + f = csv.reader(f) + next(f) + for line in f: + name, shares, price = line + portfolio.append( + { + 'name': name, + 'shares': int(shares), + 'price': float(price), + } + ) + return portfolio + +def read_prices(file_name): + """ + Reads prices from a csv file and returns a dictionary + """ + prices = {} + with open(file_name, 'rt') as f: + f = csv.reader(f) + for line in f: + if line: + prices[line[0]] = float(line[1]) + return prices + +portfolio = read_portfolio(r'Work\Data\portfolio.csv') +current_prices = read_prices(r'Work\Data\prices.csv') + +portfolio_price = 0.0 +current_price = 0.0 + +for i in portfolio: + portfolio_price += i['shares'] * i['price'] + current_price += i['shares'] * current_prices[i['name']] + +print(f'Original Value : {portfolio_price:0.2f}') +print(f'Current Value : {current_price:0.2f}') +print(f'Gain/Loss : {(current_price - portfolio_price)*100/portfolio_price:0.2f}%') \ No newline at end of file From 52f53c929a1878d74a535be956c487bbd51006f3 Mon Sep 17 00:00:00 2001 From: lolPirate Date: Sat, 30 May 2020 02:59:59 +0530 Subject: [PATCH 09/12] completed exercise 2.7 --- Work/report.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Work/report.py b/Work/report.py index 6cfacdf80..993ccb2cf 100644 --- a/Work/report.py +++ b/Work/report.py @@ -4,6 +4,7 @@ import csv from pprint import pprint + def read_portfolio(file_name): """ Opens portfolio file, reads it and returns a list. @@ -23,6 +24,7 @@ def read_portfolio(file_name): ) return portfolio + def read_prices(file_name): """ Reads prices from a csv file and returns a dictionary @@ -35,6 +37,7 @@ def read_prices(file_name): prices[line[0]] = float(line[1]) return prices + portfolio = read_portfolio(r'Work\Data\portfolio.csv') current_prices = read_prices(r'Work\Data\prices.csv') @@ -47,4 +50,5 @@ def read_prices(file_name): print(f'Original Value : {portfolio_price:0.2f}') print(f'Current Value : {current_price:0.2f}') -print(f'Gain/Loss : {(current_price - portfolio_price)*100/portfolio_price:0.2f}%') \ No newline at end of file +print( + f'Gain/Loss : {(current_price - portfolio_price)*100/portfolio_price:0.2f}%') From d681f0708a3db1bb23ccd50ec3c0854936d8d80f Mon Sep 17 00:00:00 2001 From: lolPirate Date: Sat, 30 May 2020 03:47:00 +0530 Subject: [PATCH 10/12] completed exercise 2.12 --- Work/report.py | 55 ++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 44 insertions(+), 11 deletions(-) diff --git a/Work/report.py b/Work/report.py index 993ccb2cf..1f1730d7b 100644 --- a/Work/report.py +++ b/Work/report.py @@ -38,17 +38,50 @@ def read_prices(file_name): return prices -portfolio = read_portfolio(r'Work\Data\portfolio.csv') -current_prices = read_prices(r'Work\Data\prices.csv') +def make_report(portfolio, current_prices): + """ + Makes a report from the supplied portfolio and current_prices + """ + report = [] + for i in portfolio: + name, shares, price = i.values() + current_price = current_prices[name] + change = current_price - price + report.append((name, shares, current_price, change)) + return report + + +def retire(portfolio, current_prices): + portfolio_price = 0.0 + current_price = 0.0 + + for i in portfolio: + portfolio_price += i['shares'] * i['price'] + current_price += i['shares'] * current_prices[i['name']] + + print(f'Original Value : {portfolio_price:0.2f}') + print(f'Current Value : {current_price:0.2f}') + print( + f'Gain/Loss : {(current_price - portfolio_price)*100/portfolio_price:0.2f}%') + + +def print_report(): + portfolio = read_portfolio(r'Work\Data\portfolio.csv') + current_prices = read_prices(r'Work\Data\prices.csv') + + report = make_report(portfolio, current_prices) + + headers = ('Name', 'Shares', 'Price', 'Change') + print( + f'{headers[0]:>10s} {headers[1]:>10s} {headers[2]:>10s} {headers[3]:>10s}') + + seperators = '-'*10 + print(f'{seperators} {seperators} {seperators} {seperators}') -portfolio_price = 0.0 -current_price = 0.0 + for name, shares, current_price, change in report: + current_price = round(current_price, 2) + current_price = '$'+str(current_price) + print(f'{name:>10s} {shares:>10n} {current_price:>10s} {change:>10.2f}') -for i in portfolio: - portfolio_price += i['shares'] * i['price'] - current_price += i['shares'] * current_prices[i['name']] -print(f'Original Value : {portfolio_price:0.2f}') -print(f'Current Value : {current_price:0.2f}') -print( - f'Gain/Loss : {(current_price - portfolio_price)*100/portfolio_price:0.2f}%') +print_report() From 09925c32aa489b9d8da76f86d9a66dc4601ef5ef Mon Sep 17 00:00:00 2001 From: lolPirate Date: Sat, 30 May 2020 20:09:04 +0530 Subject: [PATCH 11/12] completed exercise 2.16 --- Work/pcost.py | 16 +++++++++++----- Work/report.py | 10 +++++----- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/Work/pcost.py b/Work/pcost.py index 3feec6e53..0b1cc75fa 100644 --- a/Work/pcost.py +++ b/Work/pcost.py @@ -2,6 +2,7 @@ # # Exercise 1.27 import sys +import csv def portfolio_cost(file_name): """ @@ -9,10 +10,16 @@ def portfolio_cost(file_name): """ total_cost = 0.0 with open(file_name, 'rt') as f: - next(f) - for line in f: - _, shares, price = line.strip().split(',') - total_cost = total_cost + int(shares) * float(price) + f = csv.reader(f) + headers = next(f) + for indx, line in enumerate(f, start=1): + record = dict(zip(headers, line)) + try: + shares = int(record['shares']) + price = float(record['price']) + total_cost = total_cost + shares * price + except ValueError: + print(f'Row {indx}: Bad Row: {line}') return total_cost @@ -23,4 +30,3 @@ def portfolio_cost(file_name): total_cost = portfolio_cost(file) print(f'Total cost {total_cost:0.2f}') - diff --git a/Work/report.py b/Work/report.py index 1f1730d7b..6acc21b61 100644 --- a/Work/report.py +++ b/Work/report.py @@ -12,14 +12,14 @@ def read_portfolio(file_name): portfolio = [] with open(file_name, 'rt') as f: f = csv.reader(f) - next(f) + headers = next(f) for line in f: - name, shares, price = line + record = dict(zip(headers, line)) portfolio.append( { - 'name': name, - 'shares': int(shares), - 'price': float(price), + 'name': record['name'], + 'shares': int(record['shares']), + 'price': float(record['price']), } ) return portfolio From 75c6bbcc04560dbf02ba74ff631f5b61f424dfa3 Mon Sep 17 00:00:00 2001 From: lolPirate Date: Sat, 30 May 2020 22:49:17 +0530 Subject: [PATCH 12/12] completed exercise 3.2 --- Work/report.py | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/Work/report.py b/Work/report.py index 6acc21b61..a71b95697 100644 --- a/Work/report.py +++ b/Work/report.py @@ -65,11 +65,7 @@ def retire(portfolio, current_prices): f'Gain/Loss : {(current_price - portfolio_price)*100/portfolio_price:0.2f}%') -def print_report(): - portfolio = read_portfolio(r'Work\Data\portfolio.csv') - current_prices = read_prices(r'Work\Data\prices.csv') - - report = make_report(portfolio, current_prices) +def print_report(report): headers = ('Name', 'Shares', 'Price', 'Change') print( @@ -84,4 +80,20 @@ def print_report(): print(f'{name:>10s} {shares:>10n} {current_price:>10s} {change:>10.2f}') -print_report() +def portfolio_report(portfolio, prices): + portfolio = read_portfolio(portfolio) + prices = read_prices(prices) + report = make_report(portfolio, prices) + print_report(report) + + +if __name__ == '__main__': + portfolio = r'Work\Data\portfolio.csv' + prices = r'Work\Data\prices.csv' + portfolio_report(portfolio, prices) + +files = [r'Work\Data\portfolio.csv', r'Work\Data\portfolio2.csv'] +for f in files: + print(f'{f:-^43s}') + portfolio_report(f, r'Work\Data\prices.csv') + print()