From 3ceed69b3dab4b1388888f2acf435de243636b98 Mon Sep 17 00:00:00 2001 From: mard3n Date: Sun, 31 May 2020 20:19:45 +0300 Subject: [PATCH 1/9] Please enter the commit message for your changes. Lines starting --- Work/bounce.py | 7 +++++-- Work/mortgage.py | 27 +++++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/Work/bounce.py b/Work/bounce.py index 3660ddd82..051a98460 100644 --- a/Work/bounce.py +++ b/Work/bounce.py @@ -1,3 +1,6 @@ # bounce.py -# -# Exercise 1.5 +x = 60 +for i in range (1,11): + print(i,round(x,4)) + x = x * 0.6 + diff --git a/Work/mortgage.py b/Work/mortgage.py index d527314e3..114fa1689 100644 --- a/Work/mortgage.py +++ b/Work/mortgage.py @@ -1,3 +1,26 @@ # mortgage.py -# -# Exercise 1.7 + +principal = 500000.0 +rate = 0.05 +payment = 2684.11 +total_paid = 0.0 +month = 0 +extra = 1000 +extra_payment_start_month = 60 +extra_payment_end_month = 108 +while principal > 0: + month += 1 + if month >= extra_payment_start_month and month <= extra_payment_end_month: + principal = (principal * (1+rate/12)) - (extra + payment) + total_paid = total_paid + (extra + payment) + else: + principal = principal * (1+rate/12) - payment + total_paid = total_paid + payment + if principal< 300: + qqq = principal + principal = 0 + total_paid = total_paid - qqq + print(month, round(total_paid,2), round(principal, 2)) + +print('Total paid', round(total_paid, 2)) +print('Months', month) \ No newline at end of file From a927ccf596bf772ad916905d5182f342dd272090 Mon Sep 17 00:00:00 2001 From: mard3n Date: Sun, 31 May 2020 21:43:02 +0300 Subject: [PATCH 2/9] sad --- Work/.vscode/settings.json | 3 +++ Work/sears.py | 15 +++++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 Work/.vscode/settings.json create mode 100644 Work/sears.py diff --git a/Work/.vscode/settings.json b/Work/.vscode/settings.json new file mode 100644 index 000000000..615aafb03 --- /dev/null +++ b/Work/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "python.pythonPath": "/usr/bin/python3" +} \ No newline at end of file diff --git a/Work/sears.py b/Work/sears.py new file mode 100644 index 000000000..8add59254 --- /dev/null +++ b/Work/sears.py @@ -0,0 +1,15 @@ +# sears.py + +bill_thickness = 0.11 * 0.001 # Meters (0.11 mm) +sears_height = 442 # Height (meters) +num_bills = 1 +day = 1 + +while num_bills * bill_thickness < sears_height: + print(day, num_bills, num_bills * bill_thickness) + day = day + 1 + num_bills = num_bills * 2 + +print('Number of days', day) +print('Number of bills', num_bills) +print('Final height', num_bills * bill_thickness) \ No newline at end of file From 1498fa91af2d3307ad702b2ce9d86a2570869a6e Mon Sep 17 00:00:00 2001 From: mard3n Date: Mon, 1 Jun 2020 19:16:53 +0300 Subject: [PATCH 3/9] 1.17 --- Work/mortgage.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Work/mortgage.py b/Work/mortgage.py index 114fa1689..e9fbafac9 100644 --- a/Work/mortgage.py +++ b/Work/mortgage.py @@ -20,7 +20,7 @@ qqq = principal principal = 0 total_paid = total_paid - qqq - print(month, round(total_paid,2), round(principal, 2)) + print(f'{month} | {round(total_paid,2)} {round(principal, 2)}') -print('Total paid', round(total_paid, 2)) -print('Months', month) \ No newline at end of file +print(f'Total paid: {round(total_paid, 2)}') +print(f'Months: {month}') \ No newline at end of file From 24e5eab9c3256e64be309be95818a5625c6b06fe Mon Sep 17 00:00:00 2001 From: mard3n Date: Thu, 4 Jun 2020 19:21:04 +0300 Subject: [PATCH 4/9] 01_introduction --- Work/pcost.py | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/Work/pcost.py b/Work/pcost.py index e68aa20b4..3e648187d 100644 --- a/Work/pcost.py +++ b/Work/pcost.py @@ -1,3 +1,21 @@ -# pcost.py -# -# Exercise 1.27 +import sys +import csv +if len(sys.argv) == 2: + filename = sys.argv[1] +else: + filename = 'Data/portfolio.csv' +def portfolio_cost(filename): + result = 0 + with open(filename, 'rt') as f: + rows = csv.reader(f) + _ = next(rows) + for line in f: + try: + row = line.split(',') + summ = float(row[1]) * float(row[2]) + result += summ + except ValueError: + print('Shit happens') + return result +cost = portfolio_cost(filename) +print('Total cost:', cost) \ No newline at end of file From f296937d779a3b3ce34e52bd3827c2c54a70aec7 Mon Sep 17 00:00:00 2001 From: mard3n Date: Sat, 6 Jun 2020 18:38:07 +0300 Subject: [PATCH 5/9] i have no idea --- Work/pcost.py | 15 +++++++------ Work/report.py | 59 +++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 64 insertions(+), 10 deletions(-) diff --git a/Work/pcost.py b/Work/pcost.py index 3e648187d..bd971ba8a 100644 --- a/Work/pcost.py +++ b/Work/pcost.py @@ -8,14 +8,15 @@ def portfolio_cost(filename): result = 0 with open(filename, 'rt') as f: rows = csv.reader(f) - _ = next(rows) - for line in f: - try: - row = line.split(',') - summ = float(row[1]) * float(row[2]) - result += summ + headers = next(rows) + for rowno, row in enumerate(rows, start=1): + record = dict(zip(headers, row)) + try: + nshares = int(record['shares']) + price = float(record['price']) + result += nshares * price except ValueError: - print('Shit happens') + print(f'Row {rowno}: Bad row: {row}') return result cost = portfolio_cost(filename) print('Total cost:', cost) \ No newline at end of file diff --git a/Work/report.py b/Work/report.py index 47d5da7b1..c657cc771 100644 --- a/Work/report.py +++ b/Work/report.py @@ -1,3 +1,56 @@ -# report.py -# -# Exercise 2.4 +import csv +import sys +if len(sys.argv) == 2: + filename = sys.argv[1] +else: + filename = 'Data/portfolio.csv' + +def read_portfolio(filename): + portfolio = [] + with open(filename, 'rt') as f: + rows = csv.reader(f) + headers = next(rows) + for rowno, row in enumerate(rows, start=1): + try: + record = dict(zip(headers, row)) + portfolio.append(record) + except: + print(f'Row {rowno}: Bad row: {row}') + return portfolio + +def read_prices(filename): + prices = {} + with open(filename) as f: + rows = csv.reader(f) + for row in rows: + try: + prices[row[0]] = float(row[1]) + except IndexError: + pass + return prices + +def make_report(portfolio, prices): + rows = [] + for d in portfolio: + cprice = prices[d['name']] + change = cprice - float(d['price']) + summary = (d['name'], int(d['shares']), cprice, change) + rows.append(summary) + return rows + +portfolio = read_portfolio(filename) +prices = read_prices('Data/prices.csv') +report = make_report(portfolio, prices) + +tcost = 0.0 +tvalue = 0.0 +for i in portfolio: + tcost += int(i['shares'])*float(i['price']) +for i in portfolio: + tvalue += float(i['shares'])*prices[i['name']] + +headers = ('Name', 'Shares', 'Price', 'Change') +print('%10s %10s %10s %10s' % headers) +print(('-' * 10 + ' ') * len(headers)) +for name, shares, price, change in report: + print(f'{name:>10s} {shares:>10d} {price:>10.2f} {change:>10.2f}') From ed723ebbe3b065476dbfbff8e4f3f76324b1f0b8 Mon Sep 17 00:00:00 2001 From: mard3n Date: Wed, 10 Jun 2020 20:48:53 +0300 Subject: [PATCH 6/9] aaa --- Work/pcost.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Work/pcost.py b/Work/pcost.py index bd971ba8a..5d6f30c8c 100644 --- a/Work/pcost.py +++ b/Work/pcost.py @@ -16,7 +16,7 @@ def portfolio_cost(filename): price = float(record['price']) result += nshares * price except ValueError: - print(f'Row {rowno}: Bad row: {row}') + print(f'Ro w {rowno}: Bad row: {row}') return result cost = portfolio_cost(filename) print('Total cost:', cost) \ No newline at end of file From 021fb1f7090383639b9e6a35eadba34cb3505105 Mon Sep 17 00:00:00 2001 From: mard3n Date: Thu, 25 Jun 2020 18:44:50 +0300 Subject: [PATCH 7/9] 25062020 --- Work/fileparse.py | 24 ++++++++++++++++++++++-- Work/pcost.py | 1 + Work/report.py | 46 ++++++++++++++++++++-------------------------- 3 files changed, 43 insertions(+), 28 deletions(-) diff --git a/Work/fileparse.py b/Work/fileparse.py index 1d499e733..98d1cbefb 100644 --- a/Work/fileparse.py +++ b/Work/fileparse.py @@ -1,3 +1,23 @@ -# fileparse.py -# # Exercise 3.3 +import csv + +def parse_csv(filename, select=None): + '''Parse CSV file into a list of records + ''' + with open (filename) as f: + rows = csv.reader(f) + headers = next(rows) + if select: + indices = [headers.index(colname) for colname in select] + headers = select + else: + indices = [] + records = [] + for row in rows: + if not row: + continue + if indices: + row = [row[index]for index in indices] + record = dict(zip(headers, row)) + records.append(record) + return records diff --git a/Work/pcost.py b/Work/pcost.py index 5d6f30c8c..16f733d51 100644 --- a/Work/pcost.py +++ b/Work/pcost.py @@ -4,6 +4,7 @@ filename = sys.argv[1] else: filename = 'Data/portfolio.csv' + def portfolio_cost(filename): result = 0 with open(filename, 'rt') as f: diff --git a/Work/report.py b/Work/report.py index c657cc771..6f6e65835 100644 --- a/Work/report.py +++ b/Work/report.py @@ -1,23 +1,20 @@ import csv import sys -if len(sys.argv) == 2: - filename = sys.argv[1] -else: - filename = 'Data/portfolio.csv' +#if len(sys.argv) == 2: +# filename = sys.argv[1] +#else: +# filename = 'Data/portfolio.csv' def read_portfolio(filename): portfolio = [] with open(filename, 'rt') as f: rows = csv.reader(f) headers = next(rows) - for rowno, row in enumerate(rows, start=1): - try: - record = dict(zip(headers, row)) - portfolio.append(record) - except: - print(f'Row {rowno}: Bad row: {row}') + select = ['name', 'shares', 'price'] + indices = [ headers.index(colname) for colname in select ] + portfolio = [ { colname: row[index] for colname, index in zip(select, indices) } for row in rows ] return portfolio - + def read_prices(filename): prices = {} with open(filename) as f: @@ -38,19 +35,16 @@ def make_report(portfolio, prices): rows.append(summary) return rows -portfolio = read_portfolio(filename) -prices = read_prices('Data/prices.csv') -report = make_report(portfolio, prices) - -tcost = 0.0 -tvalue = 0.0 -for i in portfolio: - tcost += int(i['shares'])*float(i['price']) -for i in portfolio: - tvalue += float(i['shares'])*prices[i['name']] +def print_report(reportdata): + headers = ('Name','Shares','Price','Change') + print('%10s %10s %10s %10s' % headers) + print(('-'*10 + ' ')*len(headers)) + for row in reportdata: + print('%10s %10d %10.2f %10.2f' % row) -headers = ('Name', 'Shares', 'Price', 'Change') -print('%10s %10s %10s %10s' % headers) -print(('-' * 10 + ' ') * len(headers)) -for name, shares, price, change in report: - print(f'{name:>10s} {shares:>10d} {price:>10.2f} {change:>10.2f}') +def portfolio_report(portfoliofile,pricefile): + portfolio = read_portfolio(portfoliofile) + prices = read_prices(pricefile) + report = make_report(portfolio,prices) + print_report(report) +portfolio_report('Data/portfolio.csv','Data/prices.csv') From 4b9ed9e50c07d5ce513f791ad42f1ccefba05aa6 Mon Sep 17 00:00:00 2001 From: mard3n Date: Mon, 29 Jun 2020 18:50:06 +0300 Subject: [PATCH 8/9] 2906 --- Work/fileparse.py | 58 ++++++++++++++++++++++++---------- Work/pcost.py | 38 ++++++++++------------ Work/report.py | 80 ++++++++++++++++++++++++++++------------------- 3 files changed, 104 insertions(+), 72 deletions(-) diff --git a/Work/fileparse.py b/Work/fileparse.py index 98d1cbefb..d464c2edc 100644 --- a/Work/fileparse.py +++ b/Work/fileparse.py @@ -1,23 +1,47 @@ -# Exercise 3.3 +# fileparse.py import csv -def parse_csv(filename, select=None): - '''Parse CSV file into a list of records +def parse_csv(lines, select=None, types=None, has_headers=True, delimiter=',', silence_errors=False): ''' - with open (filename) as f: - rows = csv.reader(f) - headers = next(rows) + Parse a CSV file into a list of records with type conversion. + ''' + if select and not has_headers: + raise RuntimeError('select requires column headers') + + rows = csv.reader(lines, delimiter=delimiter) + + # Read the file headers (if any) + headers = next(rows) if has_headers else [] + + # If specific columns have been selected, make indices for filtering and set output columns + if select: + indices = [ headers.index(colname) for colname in select ] + headers = select + + records = [] + for rowno, row in enumerate(rows, 1): + if not row: # Skip rows with no data + continue + + # If specific column indices are selected, pick them out if select: - indices = [headers.index(colname) for colname in select] - headers = select - else: - indices = [] - records = [] - for row in rows: - if not row: + row = [ row[index] for index in indices] + + # Apply type conversion to the row + if types: + try: + row = [func(val) for func, val in zip(types, row)] + except ValueError as e: + if not silence_errors: + print(f"Row {rowno}: Couldn't convert {row}") + print(f"Row {rowno}: Reason {e}") continue - if indices: - row = [row[index]for index in indices] + + # Make a dictionary or a tuple + if headers: record = dict(zip(headers, row)) - records.append(record) - return records + else: + record = tuple(row) + records.append(record) + + return records \ No newline at end of file diff --git a/Work/pcost.py b/Work/pcost.py index 16f733d51..a30d4e7c5 100644 --- a/Work/pcost.py +++ b/Work/pcost.py @@ -1,23 +1,17 @@ -import sys -import csv -if len(sys.argv) == 2: - filename = sys.argv[1] -else: - filename = 'Data/portfolio.csv' - +import report def portfolio_cost(filename): - result = 0 - with open(filename, 'rt') as f: - rows = csv.reader(f) - headers = next(rows) - for rowno, row in enumerate(rows, start=1): - record = dict(zip(headers, row)) - try: - nshares = int(record['shares']) - price = float(record['price']) - result += nshares * price - except ValueError: - print(f'Ro w {rowno}: Bad row: {row}') - return result -cost = portfolio_cost(filename) -print('Total cost:', cost) \ No newline at end of file + ''' + 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]) + +def main(args): + if len(args) != 2: + raise SystemExit('Usage: %s portfoliofile' % args[0]) + filename = args[1] + print('Total cost:', portfolio_cost(filename)) + +if __name__ == '__main__': + import sys + main(sys.argv) \ No newline at end of file diff --git a/Work/report.py b/Work/report.py index 6f6e65835..fbf71bd10 100644 --- a/Work/report.py +++ b/Work/report.py @@ -1,50 +1,64 @@ -import csv -import sys -#if len(sys.argv) == 2: -# filename = sys.argv[1] -#else: -# filename = 'Data/portfolio.csv' +# report.py + +import fileparse def read_portfolio(filename): - portfolio = [] - with open(filename, 'rt') as f: - rows = csv.reader(f) - headers = next(rows) - select = ['name', 'shares', 'price'] - indices = [ headers.index(colname) for colname in select ] - portfolio = [ { colname: row[index] for colname, index in zip(select, indices) } for row in rows ] - return portfolio - + ''' + Read a stock portfolio file into a list of dictionaries with keys + name, shares, and price. + ''' + with open(filename) as lines: + return fileparse.parse_csv(lines, select=['name','shares','price'], types=[str,int,float]) + def read_prices(filename): - prices = {} - with open(filename) as f: - rows = csv.reader(f) - for row in rows: - try: - prices[row[0]] = float(row[1]) - except IndexError: - pass - return prices - -def make_report(portfolio, prices): + ''' + Read a CSV file of price data into a dict mapping names to prices. + ''' + with open(filename) as lines: + return dict(fileparse.parse_csv(lines, types=[str,float], has_headers=False)) + +def make_report_data(portfolio,prices): + ''' + Make a list of (name, shares, price, change) tuples given a portfolio list + and prices dictionary. + ''' rows = [] - for d in portfolio: - cprice = prices[d['name']] - change = cprice - float(d['price']) - summary = (d['name'], int(d['shares']), cprice, change) + for stock in portfolio: + current_price = prices[stock['name']] + change = current_price - stock['price'] + summary = (stock['name'], stock['shares'], current_price, change) rows.append(summary) return rows def print_report(reportdata): + ''' + Print a nicely formated table from a list of (name, shares, price, change) tuples. + ''' headers = ('Name','Shares','Price','Change') print('%10s %10s %10s %10s' % headers) print(('-'*10 + ' ')*len(headers)) for row in reportdata: print('%10s %10d %10.2f %10.2f' % row) -def portfolio_report(portfoliofile,pricefile): +def portfolio_report(portfoliofile, pricefile): + ''' + Make a stock report given portfolio and price data files. + ''' + # Read data files portfolio = read_portfolio(portfoliofile) prices = read_prices(pricefile) - report = make_report(portfolio,prices) + + # Create the report data + report = make_report_data(portfolio, prices) + + # Print it out print_report(report) -portfolio_report('Data/portfolio.csv','Data/prices.csv') + +def main(args): + if len(args) != 3: + raise SystemExit('Usage: %s portfile pricefile' % args[0]) + portfolio_report(args[1], args[2]) + +if __name__ == '__main__': + import sys + main(sys.argv) From d50d287e6e1dd92a7d4048591aec7e0df53112a3 Mon Sep 17 00:00:00 2001 From: mard3n Date: Mon, 29 Jun 2020 18:50:33 +0300 Subject: [PATCH 9/9] asd --- Work/Data/stocklog.csv | 59 ++++++++++++++++++++++++++++++++++++++++++ Work/stock.py | 20 ++++++++++++++ Work/test.py | 56 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 135 insertions(+) create mode 100644 Work/Data/stocklog.csv create mode 100644 Work/stock.py create mode 100644 Work/test.py diff --git a/Work/Data/stocklog.csv b/Work/Data/stocklog.csv new file mode 100644 index 000000000..960acbacd --- /dev/null +++ b/Work/Data/stocklog.csv @@ -0,0 +1,59 @@ +"AA",39.31,"6/11/2007","09:30.00",-0.35,39.67,39.31,39.31,75600 +"AIG",71.26,"6/11/2007","09:30.00",-0.27,71.29,71.26,71.26,73400 +"AXP",62.38,"6/11/2007","09:30.00",-0.66,62.79,62.38,62.38,834350 +"BA",98.31,"6/11/2007","09:30.00",0.12,98.25,98.31,98.31,37450 +"C",52.99,"6/11/2007","09:30.00",-0.34,53.20,52.99,52.99,82500 +"CAT",77.99,"6/11/2007","09:30.00",-0.53,78.32,77.99,77.99,169282 +"DD",50.60,"6/11/2007","09:30.00",-0.53,51.13,50.60,50.60,9750 +"DIS",34.04,"6/11/2007","09:30.00",-0.16,34.28,34.04,34.04,105100 +"GE",37.12,"6/11/2007","09:30.00",-0.20,37.07,37.12,37.12,175900 +"GM",31.50,"6/11/2007","09:30.00",0.50,31.00,31.50,31.50,177454 +"HD",37.62,"6/11/2007","09:30.00",-0.33,37.78,37.62,37.62,114969 +"HON",57.02,"6/11/2007","09:30.00",-0.36,57.25,57.02,57.02,111800 +"HPQ",45.59,"6/11/2007","09:30.00",-0.11,45.80,45.59,45.59,121100 +"IBM",102.77,"6/11/2007","09:30.00",-0.30,102.87,102.77,102.77,73900 +"INTC",21.82,"6/11/2007","09:30.00",-0.01,21.70,21.82,21.82,1796393 +"JNJ",62.08,"6/11/2007","09:30.00",-0.05,62.89,62.08,62.08,253400 +"JPM",50.25,"6/11/2007","09:30.00",-0.16,50.41,50.25,50.25,185650 +"KO",51.63,"6/11/2007","09:30.00",-0.04,51.67,51.63,51.63,3952150 +"MCD",50.80,"6/11/2007","09:30.00",-0.61,51.47,50.80,50.80,92400 +"MMM",85.75,"6/11/2007","09:30.00",-0.19,85.94,85.75,85.75,156100 +"MO",70.30,"6/11/2007","09:30.00",0.00,70.25,70.30,70.30,362600 +"MRK",49.66,"6/11/2007","09:30.00",-0.48,50.30,49.66,49.66,1254100 +"MSFT",29.95,"6/11/2007","09:30.00",-0.10,30.05,29.95,29.95,4861715 +"PFE",26.31,"6/11/2007","09:30.00",-0.21,26.50,26.31,26.31,436150 +"PG",62.61,"6/11/2007","09:30.00",-0.46,62.80,62.61,62.61,80754 +"T",39.87,"6/11/2007","09:30.00",-0.39,40.20,39.87,39.87,508700 +"UTX",69.71,"6/11/2007","09:30.00",-0.52,69.85,69.71,69.71,97050 +"VZ",42.78,"6/11/2007","09:30.00",-0.29,42.95,42.78,42.78,119300 +"WMT",49.87,"6/11/2007","09:30.00",-0.21,49.90,49.87,49.87,456450 +"XOM",82.64,"6/11/2007","09:30.00",-0.04,82.68,82.64,82.64,144750 +"AA",39.32,"6/11/2007","09:30.01",-0.34,39.67,39.32,39.31,75894 +"AXP",62.39,"6/11/2007","09:30.01",-0.65,62.79,62.39,62.38,834629 +"GE",37.13,"6/11/2007","09:30.01",-0.19,37.07,37.13,37.12,177339 +"XOM",82.63,"6/11/2007","09:30.01",-0.05,82.68,82.64,82.63,145684 +"HPQ",45.60,"6/11/2007","09:30.02",-0.10,45.80,45.60,45.59,122111 +"MCD",50.81,"6/11/2007","09:30.05",-0.60,51.47,50.81,50.80,93678 +"CAT",78.00,"6/11/2007","09:30.06",-0.52,78.32,78.00,77.99,170217 +"MRK",49.67,"6/11/2007","09:30.07",-0.47,50.30,49.67,49.66,1257973 +"JNJ",62.09,"6/11/2007","09:30.09",-0.04,62.89,62.09,62.08,256102 +"MO",70.29,"6/11/2007","09:30.09",-0.01,70.25,70.30,70.29,365314 +"PG",62.62,"6/11/2007","09:30.09",-0.45,62.80,62.62,62.61,86011 +"DIS",34.05,"6/11/2007","09:30.10",-0.15,34.28,34.05,34.04,107963 +"MMM",85.74,"6/11/2007","09:30.10",-0.20,85.94,85.75,85.74,157256 +"T",39.88,"6/11/2007","09:30.10",-0.38,40.20,39.88,39.87,514789 +"VZ",42.79,"6/11/2007","09:30.11",-0.28,42.95,42.79,42.78,123028 +"DD",50.61,"6/11/2007","09:30.13",-0.52,51.13,50.61,50.60,12897 +"MRK",49.68,"6/11/2007","09:30.13",-0.46,50.30,49.68,49.66,1261293 +"MCD",50.82,"6/11/2007","09:30.15",-0.59,51.47,50.82,50.80,96234 +"AIG",71.27,"6/11/2007","09:30.16",-0.26,71.29,71.27,71.26,78826 +"HON",57.03,"6/11/2007","09:30.16",-0.35,57.25,57.03,57.02,112829 +"IBM",102.78,"6/11/2007","09:30.17",-0.29,102.87,102.78,102.77,78308 +"CAT",78.01,"6/11/2007","09:30.18",-0.51,78.32,78.01,77.99,172087 +"AXP",62.40,"6/11/2007","09:30.19",-0.64,62.79,62.40,62.38,839662 +"HPQ",45.61,"6/11/2007","09:30.19",-0.09,45.80,45.61,45.59,130710 +"MRK",49.69,"6/11/2007","09:30.20",-0.45,50.30,49.69,49.66,1265166 +"C",53.00,"6/11/2007","09:30.21",-0.33,53.20,53.00,52.99,98739 +"PFE",26.32,"6/11/2007","09:30.21",-0.20,26.50,26.32,26.31,459451 +"WMT",49.86,"6/11/2007","09:30.21",-0.22,49.90,49.87,49.86,469268 +"AA",39.33,"6/11/2007","09:30.22",-0.33,39.67,39.33,39.31,82089 diff --git a/Work/stock.py b/Work/stock.py new file mode 100644 index 000000000..141bd578d --- /dev/null +++ b/Work/stock.py @@ -0,0 +1,20 @@ +class Stock: + ''' + An instance of a stock holding consisting of name, shares, and price. + ''' + def __init__(self, name, shares, price): + self.name = name + self.shares = shares + self.price = price + + def cost(self): + ''' + Return the cost as shares*price + ''' + return self.shares * self.price + + def sell(self, nshares): + ''' + Sell a number of shares + ''' + self.shares -= nshares \ No newline at end of file diff --git a/Work/test.py b/Work/test.py new file mode 100644 index 000000000..c657cc771 --- /dev/null +++ b/Work/test.py @@ -0,0 +1,56 @@ +import csv +import sys +if len(sys.argv) == 2: + filename = sys.argv[1] +else: + filename = 'Data/portfolio.csv' + +def read_portfolio(filename): + portfolio = [] + with open(filename, 'rt') as f: + rows = csv.reader(f) + headers = next(rows) + for rowno, row in enumerate(rows, start=1): + try: + record = dict(zip(headers, row)) + portfolio.append(record) + except: + print(f'Row {rowno}: Bad row: {row}') + return portfolio + +def read_prices(filename): + prices = {} + with open(filename) as f: + rows = csv.reader(f) + for row in rows: + try: + prices[row[0]] = float(row[1]) + except IndexError: + pass + return prices + +def make_report(portfolio, prices): + rows = [] + for d in portfolio: + cprice = prices[d['name']] + change = cprice - float(d['price']) + summary = (d['name'], int(d['shares']), cprice, change) + rows.append(summary) + return rows + +portfolio = read_portfolio(filename) +prices = read_prices('Data/prices.csv') +report = make_report(portfolio, prices) + +tcost = 0.0 +tvalue = 0.0 +for i in portfolio: + tcost += int(i['shares'])*float(i['price']) +for i in portfolio: + tvalue += float(i['shares'])*prices[i['name']] + +headers = ('Name', 'Shares', 'Price', 'Change') +print('%10s %10s %10s %10s' % headers) +print(('-' * 10 + ' ') * len(headers)) +for name, shares, price, change in report: + print(f'{name:>10s} {shares:>10d} {price:>10.2f} {change:>10.2f}')