Advertisement
shawdyy

PSET7 - check 50: 'NoneType' object is not subscriptable

Jan 21st, 2018
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.61 KB | None | 0 0
  1. def pulling_portfolio(rows, cash):
  2.     """Creating a searcher to rearrange the transactions into a portfolio"""
  3.     share_cache = []
  4.     """Creating a filtered copy of the list"""
  5.     for i in range(0, len(rows)):
  6.         if len(share_cache) < 1:
  7.             share_cache.append(rows[i].copy())
  8.  
  9.         else:
  10.             for j in range(0, len(share_cache)):
  11.                 if rows[i]["symbol"] == share_cache[j]["symbol"]:
  12.                     break
  13.             else:
  14.                 share_cache.append(rows[i].copy())
  15.  
  16.     '''Setting every quantity to 0 to avoid doubles'''
  17.     for i in range(0, len(share_cache)):
  18.         share_cache[i]["quantity"] = 0
  19.  
  20.     """Summing the quantity out of the transactions"""
  21.     for i in range(0, len(share_cache)):
  22.         cache = share_cache[i]["symbol"]
  23.         for j in range(0, len(rows)):
  24.             if rows[j]["symbol"] == cache:
  25.                 share_cache[i]["quantity"] += rows[j]["quantity"]
  26.     """Adding Price and Value to the list"""
  27.     for i in range(0, len(share_cache)):
  28.         cache = share_cache[i]["symbol"]
  29.         cache_dict = lookup(cache)
  30.         share_cache[i]["price"] = cache_dict["price"]
  31.         share_cache[i]["value"] = float(share_cache[i]["price"]) * float(share_cache[i]["quantity"])
  32.  
  33.     return share_cache
  34.  
  35. def buy():
  36.     """Buy shares of stock."""
  37.  
  38.     if request.method == "POST":
  39.         symbol = request.form.get("symbol")
  40.         try:
  41.             quantity_buy = int(request.form.get("shares"))
  42.         except:
  43.             try:
  44.                 quantity_buy = int(request.form.get("shares_buy"))
  45.             except:
  46.                 return apology("Please check your input: METHOD Error")
  47.         quote_buy = lookup(symbol)
  48.  
  49.         if not quote_buy:
  50.             return apology("Yo die Quote geht nicht")
  51.  
  52.         for key, value in quote_buy.items():
  53.             if isinstance(quote_buy[key], type(None)):
  54.                 return apology("Fehler ist im quote_buy")
  55.  
  56.         user_id = session.get("user_id")
  57.  
  58.         if not isinstance(quantity_buy, int) or len(symbol) < 1 or quantity_buy < 1:
  59.             return apology("Please check your input: INPUT ERROR")
  60.  
  61.         elif not quote:
  62.             return apology("Insert a valid symbol")
  63.  
  64.         try:
  65.             try:
  66.                 stock_price = quote_buy["price"] * quantity_buy
  67.                 cash_cache = db.execute("SELECT cash FROM users WHERE id = :id", id = user_id)
  68.  
  69.                 if (isinstance(cash_cache[0]["cash"], type(None))):
  70.                     return apology("Fehler ist im cash_cache")
  71.  
  72.                 old_cash = cash_cache[0]["cash"]
  73.                 new_cash = old_cash - stock_price
  74.             except:
  75.                 return apology("Here is the BUG")
  76.  
  77.             if new_cash < 0:
  78.                 return apology("Sorry you have not enough cash!")
  79.  
  80.             else:
  81.                 try:
  82.                     transaction_completed = db.execute("UPDATE users SET cash = :newcash WHERE id = :sessionid", newcash = new_cash, sessionid = user_id)
  83.                     result = db.execute("INSERT INTO transactions (id, type, symbol, quantity, current_price) VALUES (:id, :type, :quote, :quantity, :current_price)", id = user_id, type = "buy", quote = quote_buy["name"], quantity = quantity_buy, current_price = stock_price *-1)
  84.  
  85.                 except:
  86.                     return apology("Problem mit updaten der DBs")
  87.  
  88.                 if not transaction_completed or not result:
  89.                     return apology("Your transaction could not be finished.")
  90.  
  91.                 else:
  92.                     return redirect(url_for("index"))
  93.  
  94.         except:
  95.             return apology("Oops, something went wrong")
  96.  
  97.     else:
  98.         return render_template("buy.html")
  99.  
  100. @app.route("/index")
  101. @login_required
  102. def index():
  103.     """Show a Dashboard."""
  104.     user_id = session.get("user_id")
  105.     rows = db.execute("SELECT symbol, quantity FROM transactions WHERE id = :uid", uid = user_id)
  106.     cash = db.execute("SELECT cash FROM users WHERE id = :uid", uid = user_id)
  107.  
  108.     share_cache = pulling_portfolio(rows, cash)
  109.  
  110.     """Calculating the current value of all the shares"""
  111.     try:
  112.         total = 0
  113.         for i in range(0, len(share_cache)):
  114.             total += share_cache[i]["value"]
  115.             share_cache[i]["price"] = usd(share_cache[i]["price"])
  116.             share_cache[i]["value"] = usd(share_cache[i]["value"])
  117.  
  118.         total += cash[0]["cash"]
  119.  
  120.         return render_template("index.html", table_content = share_cache, total = usd(total), total_cash = usd(cash[0]["cash"]))
  121.  
  122.     except:
  123.         return apology("Your portfolio cannot be displayed")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement