473,387 Members | 1,575 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Advice/Criticism on Python App

1
I have made a Python App(really script) that will check a stocks current values from a website & save that data to a SQLite 3 database.

I am looking for any suggestions & criticisms on what I should do better or anything at all but mainly in these areas:
- Correct Python Layout of code
- Correct error checking: Am I catching all my errors or are my exceptions not specific enough? Should I be using more try excepts inside my functions?
- Are there any areas where huge errors, bugs etc could occur that I have not compensated for?
- I am also looking for suggestions on how to write a function better, so if you think that function is really bad & should be rewritten completely or something I would really like to hear it.
- Anything else that you see
- Is python meant to be used in the way I have used it? To make a 'semi detailed' app?
Any advice criticism would be really helpful.

App:
Expand|Select|Wrap|Line Numbers
  1. """
  2.  *Stock Data Builder*
  3.    Algorithm:
  4.       - Search website for stock
  5.       - Get website HTML source code
  6.       - Search code for target stock data(price,dividends,etc..)
  7.       - Add data to text file
  8. """
  9.  
  10. import sys
  11. import os
  12. import sqlite3
  13. import datetime
  14. import time
  15. import urllib2
  16.  
  17. ### Global Variables ###
  18. menu = "***Stock Program*** \n\n1. Add a Stock to track \n2. Get Todays Tracking Data \n3. Exit \nEnter decision: "
  19. target = '<th scope="row" class="row"><a href="/asx/research/companyInfo.do?by=asxCode&asxCode=%s">%s</a>'
  20. ASXurl = 'http://www.asx.com.au/asx/markets/priceLookup.do?by=asxCodes&asxCodes='
  21.  
  22. class stock:
  23.     code             = ""
  24.     purchasePrice    = 0
  25.     purchaseQuantity = 0
  26.     price            = []  # list of recent prices
  27.     recentBid        = []  # list of recent bids for stock
  28.     recentOffer      = []  # list of recent offers for stock
  29.     stockVol         = []  # list of stock quantity available on market
  30.     def __init__(self):
  31.         """ Default Constructor """
  32.         self.code             = ""
  33.         self.purchasePrice    = 0
  34.         self.purchaseQuantity = 0
  35.  
  36.     def constructor(self, stockCode, purPrice, purQuant):
  37.         """ Constructor """
  38.         self.code             = stockCode
  39.         self.purchasePrice    = purPrice
  40.         self.purchaseQuantity = purQuant
  41.  
  42.     def setData(self, stockCode, purPrice, purQuant, priceList, reBidList, reOffList, popList):
  43.         """ Defines & implements the objects' public variables """
  44.         self.code             = stockCode     
  45.         self.purchasePrice    = purPrice
  46.         self.purchaseQuantity = purQuant
  47.         self.price            = priceList
  48.         self.recentBid        = reBidList
  49.         self.recentOffer      = reOffList
  50.         self.stockVol         = popList
  51.  
  52.         self.printStats()
  53.  
  54.     def updateData(self, priceEle, bidEle, offerEle, populEle):
  55.         """ Adds data to stock object's lists """
  56.         self.price.append(priceEle)
  57.         self.recentBid.append(bidEle)
  58.         self.recentOffer.append(offerEle)
  59.         self.stockVol.append(populEle)
  60.  
  61.     def printStats(self):
  62.         """ Output Stock attributes """
  63.  
  64.         print("Stock Code: "+self.code)
  65.         print("Stock Purchase Price: "+str(self.purchasePrice))
  66.         print("Stock Quantity Owned: "+str(self.purchaseQuantity))
  67.         print("***Initial Investment Value: "+str(self.purchasePrice*self.purchaseQuantity))
  68.         if not(len(self.price) <= 0):
  69.             print("Stock Current Price: "+str(self.price[-1]))
  70.             print("Recent Bid: "+str(self.recentBid[-1]))
  71.             print("Recent Offer: "+str(self.recentOffer[-1]))
  72.             print("Total Stock Volume in market: "+str(self.stockVol[-1]))
  73.             print("***Present Investment Value: "+str(self.price[-1]*self.purchaseQuantity))        
  74.         print("\n")
  75.  
  76.  
  77. ### Functions ###
  78. def connectDatabase(dbLocation, dbName, tableName):
  79.     """ Establish & Return connection to SQLite Database """
  80.  
  81.     try:
  82.         if not (os.path.exists(dbLocation)):
  83.             os.mkdir(dbLocation) # create folder/dir
  84.  
  85.         os.chdir(dbLocation)        # change directory focus to dbLocation
  86.         conn = sqlite3.connect(dbLocation+dbName)
  87.         cur = conn.cursor()
  88.         try:
  89.             createTableQ = "CREATE TABLE IF NOT EXISTS "+tableName+" (code varchar PRIMARY KEY, purchase_price float, purchase_quantity float, purchase_date varchar);"
  90.             cur.execute(createTableQ)
  91.             conn.commit()
  92.         except:
  93.             pass
  94.         return conn
  95.     except IOError or OSError:
  96.         print "Connection to database failed"
  97.         return False
  98.  
  99. def retrieveStockDatabase(conn, tableName):
  100.     """ Read SQLite3 database & extract stock data into StockList """
  101.  
  102.     stockList  = []
  103.     stockQuery = "select recent_price, recent_offer, recent_bid, stock_volume from ? ;"
  104.     cur = conn.cursor()
  105.     cur.execute("select code, purchase_price, purchase_quantity from "+tableName+";")
  106.  
  107.     for row in cur.fetchall():
  108.         newStock = stock()
  109.         newStock.code             = row[0]
  110.         newStock.purchasePrice    = row[1]
  111.         newStock.purchaseQuantity = row[2]
  112.         cur.execute(stockQuery,[newStock.code])
  113.         for rw in cur.fetchall():
  114.             newStock.price.append(rw[0])
  115.             newStock.recentOffer.append(rw[1])
  116.             newStock.recentBid.append(rw[2])
  117.             newStock.stockVol.append(rw[3])
  118.         stockList.append(newStock)
  119.  
  120.     return stockList
  121.  
  122. def getDate():
  123.     """ Return todays date in format DD:MM:YYYY """
  124.     time = datetime.datetime.now()
  125.     date = time.strftime("%d:%m:%Y") # string format time (%y)
  126.     return date
  127.  
  128. def newStockDatabase(conn, stockTable, stock):
  129.     """ Add a new stock to SQLite database if not already there
  130.         We save the stocks code, purchase price, quantity purchased
  131.         & date of purchase.                                       """
  132.     cur = conn.cursor()
  133.     try:
  134.         createTableQ = "create table "+stock.code+" (date varchar PRIMARY KEY, recent_price float, recent_offer float, recent_bid float, stock_volume double);"
  135.         stockQuery   = "insert into "+stockTable+" values(?, ?, ?, ?);"
  136.         cur.execute(createTableQ)
  137.         cur.execute(stockQuery,[stock.code,stock.purchasePrice,stock.purchaseQuant,getDate()])
  138.         conn.commit()
  139.     except IOError or OSError:
  140.         print "Table may already exist or bad SQLite connection."
  141.         return False
  142.  
  143. def webFormat(URL):
  144.  
  145.     if (URL.startswith("http://")==False):
  146.         URL = "http://"+URL
  147.  
  148.     return URL
  149.  
  150. def getSource(URL):
  151.     """ Retrieve HTML source code from website URL &
  152.         save in sourceBuffer                       """
  153.  
  154.     try:
  155.         URL = webFormat(URL) # make sure URL contains essential "http://"
  156.         sourceBuffer = urllib2.urlopen(URL)
  157.         print '\nResponse code = ',sourceBuffer.code
  158.         print 'Response headers = ',sourceBuffer.info()
  159.         print 'Actual URL = ',sourceBuffer.geturl()
  160.         sourceCode = sourceBuffer.read()
  161.         sourceBuffer.close()
  162.         return sourceCode
  163.  
  164.     except IOError:  # URLError
  165.         print "Function Failed: Reasons could be invalid URL name \nOR \nHTML protocol message transfer failure."
  166.         return False # function failed
  167.  
  168. def getTargetText(targetStrtData, targetEndData, dataBuffer):
  169.     """ Grabs target text that lies inside 'dataBuffer' string
  170.         between targetStrtData & targetEndData                """
  171.  
  172.     try:
  173.         result = dataBuffer.split(targetStrtData)
  174.         result.pop(0)
  175.         result = result[0].split(targetEndData)
  176.         result.pop(1)
  177.         print result
  178.         return result
  179.     except IOError:
  180.         print "Function Failed: Reasons could be targetStrtData and/or targetEndData is not present in dataBuffer."
  181.  
  182. def getStockData(htmlText, selectedStock):
  183.     """ Extract stock data(stock code,price,etc) from htmlText """
  184.     try:
  185.         # Here I extract my number data from HTML text
  186.         tempList = []
  187.         for string in htmlText:
  188.             for i in string.split('>'): 
  189.                 for e in i.split(): 
  190.                         if ('.' in e and e[0].isdigit()):
  191.                             tempList.append(float(e))
  192.                         elif (',' in e and e[0].isdigit() ):
  193.                             # remove ',' chars
  194.                             e = e.replace(',','')
  195.                             tempList.append(float(e))
  196.  
  197.         selectedStock.updateData(tempList[0],tempList[2],tempList[3],tempList[7])
  198.  
  199.     except IOError:  # is this the correct error I should be trying to catch here??
  200.         print "Function Failed: Reasons could be: sites HTML data has changed. Consult author of program."
  201.         return False
  202.  
  203. def createStockTracker(stockCode,stockPrice,stockQuant, stockList):
  204.     """ Add a new stock to the database to track """
  205.     newStock = stock()
  206.     newStock.constructor(stockCode,stockPrice,stockQuant)
  207.     stockList.append(newStock)
  208.     return stockList
  209.  
  210. def writeStockToDatabase(conn, stock):
  211.     """ Write ONLY this Stock's attributes to SQLite Database """
  212.  
  213.     cur = conn.cursor()
  214.     date = getDate()
  215.  
  216.     tableName = stock.code
  217.     stockquery = "insert into "+tableName+" values(?, ?, ?, ?, ?);"
  218.     cur.execute(query,[date,stock.price[-1], stock.recentOffer[-1], stock.recentBid[-1], stock.stockVol[-1]])
  219.     conn.commit()
  220.  
  221. def writeAllToDatabase(conn, stockList):
  222.     """ Enter recent Stock attributes into SQLite Database """
  223.  
  224.     cur = conn.cursor()
  225.     date = getDate()
  226.  
  227.     for stock in stockList:
  228.         tableName = stock.code
  229.         stockquery = "insert into "+tableName+" values(?, ?, ?, ?, ?);"
  230.         cur.execute(query,[date,stock.price[-1], stock.recentOffer[-1], stock.recentBid[-1], stock.stockVol[-1]])
  231.         conn.commit()
  232.  
  233. ### Input Functions ###
  234. def inputNewStock():
  235.     """ """
  236.     print "*Please note only an Australian Securities Exchange(ASX) listed stock can be tracked in Version 1.0."
  237.     badInput = True
  238.  
  239.     while (badInput == True):
  240.         try:
  241.             code     = raw_input("Please enter the ASX code for the stock you wish to track: ")
  242.             price    = input("Please enter the individual stock value for "+code+": ")
  243.             quantity = input("Please enter the number/quantity of stocks purchased: ")
  244.             if (len(code)>3):
  245.                 badInput = True
  246.             else : badInput = False
  247.             return True
  248.         except IOError:
  249.             if (raw_input("Incorrect input. Note: ASX code cannot be more than 3 chars. Press 'x' to exit or anything else to try again")=='x'):
  250.                 return False
  251.         badInput = True # this I am not sure if necessary to loop correctly
  252.  
  253.  
  254. ### Main program loop ###
  255. def main():
  256.     programEnd = False;
  257.     dbLocation = "C:\Users\Sam\Desktop\StockApp/"
  258.     dbName     = "stockData.db"
  259.     stockTable = "stocks"
  260.  
  261.     conn = connectDatabase(dbLocation,dbName,stockTable)
  262.     stockList = retrieveStockDatabase(conn,stockTable)
  263.  
  264.     for s in stockList:
  265.         s.printStats()
  266.  
  267.     while (programEnd == False):
  268.  
  269.         decision = input(menu) # Print Menu
  270.  
  271.         if (decision==1):
  272.  
  273.             if not(inputNewStock()==False):
  274.                 stockList = createStockTracker(acode,price,quantity,stockList)
  275.                 newStockDatabase(conn,stockTable,stockList[-1])
  276.                 print("\n** New Stock **")
  277.                 stockList[-1].printStats()
  278.                 print "The stock "+code+" was successfully added to our database. \nNow every time the program runs it will automatically track this stock & obtain its stock attributes\n\n"
  279.                 # TO DO:
  280.                 # get new stock recent Data from internet etc.
  281.                 # add stock data to data base;
  282.         elif (decision==2):
  283.             if (len(stockList)>0):
  284.                 for Stock in stockList:
  285.                     URL = ASXurl+Stock.code
  286.                     sourceCode = getSource(URL)
  287.                     targetData = getTargetText(target %(Stock.code,Stock.code),"</tr>",sourceCode)
  288.                     getStockData(targetData,Stock)
  289.                     Stock.printStats()
  290.                     #writeStockToDatabase(conn,Stock)
  291.             else:
  292.                 print "You must first identify a stock to follow. \nPlease select option 1."
  293.         elif (decision==3):
  294.             print "Thank you for using Stock Program. Goodbye.\n"
  295.             programEnd = True; # Exit program         
  296.  
  297.     conn.close()
  298.     return 0 # End program
  299.  
  300. main()
Mar 24 '10 #1
0 1151

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Sam G | last post by:
Hello all. As of last month, I have had some web design, Flash, basic C and basic Linux experience. Nothing too extensive, but enough that I could build a solid website with some logic. However,...
75
by: Howard Nease | last post by:
Hello, everyone. I would appreciate any advice that someone could give me on my future career path. Here is my situation: I am a bright Junior in a very well-respected private high school, taking...
4
by: Socheat Sou | last post by:
After a brief, but informative, discussion on Freenode's #zope chatroom, I was advised to consult the gurus on c.l.p. I'm working for a small company who is in desperate need to rewrite it's...
3
by: KWilliams | last post by:
I'd like to get some good advice about our old ASP site. You can see our home page at: http://www.douglas-county.com/ ....and an example application page at:...
1
by: Ron Davis | last post by:
I have recently discovered Python and like it quite a bit. I would like to use it on a new project I am starting. The project will gather data from several web services and present the collected...
62
by: Xah Lee | last post by:
Criticism versus Constructive Criticism Xah Lee, 2003-01 A lot intelligent people are rather confused about criticism, especially in our “free-speech” free-for-all internet age. When they...
9
by: Duncan Smith | last post by:
Hello, I find myself in the, for me, unusual (and at the moment unique) position of having to write a web application. I have quite a lot of existing Python code that will form part of the...
84
by: aarklon | last post by:
Hi all, I found an interesting article here:- http://en.wikipedia.org/wiki/Criticism_of_the_C_programming_language well what do you guys think of this article....??? Is it constructive...
3
by: bsagert | last post by:
This simple script writes html color codes that can be viewed in a browser. I used short form hex codes (fff or 000, etc) and my list has only six hex numbers otherwise the results get rather...
28
by: Hussein B | last post by:
Hey, I'm a Java/Java EE developer and I'm playing with Python these days. I like the Python language so much and I like its communities and the Django framework. My friends are about to open a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.