Python Program

CClate
project.py

"""CSC 161 Project: Milestone 1 Ziyu Cao Lab Section M 7:40-8:55pm Fall 2020 """ # importing csv module import csv from collections import namedtuple def main(): pass # Do nothing, just passing through! if __name__ == '__main__': main() def test_data(filename, col, day): """A test function to query the data you loaded into your program. Args: filename: A string for the filename containing the stock data, in CSV format. col: A string of either "date", "open", "high", "low", "close", "volume", or "adj_close" for the column of stock market data to look into. The string arguments MUST be LOWERCASE! day: An integer reflecting the absolute number of the day in the data to look up, e.g. day 1, 15, or 1200 is row 1, 15, or 1200 in the file. Returns: A value selected for the stock on some particular day, in some column col. The returned value *must* be of the appropriate type, such as float, int or str. """ with open(filename, 'rU') as infile: # read the file as a dictionary for each row ({header : value}) reader = csv.DictReader(infile) data = {} for row in reader: for header, value in row.items(): try: data[header].append(value) except KeyError: data[header] = [value] infile.close() val = data[col][day-1] return val def transact(cash_balance, stocks_owned, qty,price, buy=False, sell=False): """A bookkeeping function to help make stock transactions. Args: cash_balance: An account balance, a float; it is a value of how much money you have,currently. stocks_owned: An int, representing the number of stock you currently own. qty: An int, representing how many stock you wish to buy or sell. price: An float reflecting a price of a single stock. buy: This option parameter, if set to true, will initiate a buy. sell: This option parameter, if set to true, will initiate a sell. Returns: Two values *must* be returned. The first (a float) is the new account balance (funds) as the transaction is completed. The second is the number of stock now owned (an int) after the transaction is complete. Error condition #1: If the `buy` and `sell` keyword parameters are both set to true, or both false. You *must* raise an ValueError exception with an appropriate error message since this is an ambiguous transaction. Error condition #2: If you buy, or sell without enough funds or stocks to sell, respectively. You *must* raise an ValueError exception with an appropriate error message since this is an ambiguous transaction. """ price = test_data("AAPL.csv", "Close", 42) # or, just set a random price yourself if buy ==True and sell ==True: print("Ambiguous transaction! Can't determine whether to buy or sell!") elif buy ==False and sell ==False: print("Ambiguous transaction! Can't determine whether to buy or sell!") elif buy==True and price>cash_balance: print("Insufficient funds to purchase {qty} stock at ${price:0.2f}!") elif sell==True and stocks_owned < qty : print("Insufficient stock owned to sell {qty} stocks!") elif sell==True : cash_balance = cash_balance + price stocks_owned = stocks_owned - qty return print(cash_balance, stocks_owned) elif buy==True : cash_balance = cash_balance - price stocks_owned = stocks_owned + qty return print(cash_balance, stocks_owned) else: return print(cash_balance, stocks_owned)