[Tutor] OOPs?
Kirby Urner
urnerk@qwest.net
Fri, 29 Mar 2002 09:49:05 -0800
At 08:27 AM 3/29/2002 -0600, Bryce Embry wrote:
>Howdy,
>I'm trying to get the hang of classes and would like to tap into the wealth
>of this tutorial for some guidance.
You could have a stock class and then stuff stock objects made
from it into a dictionary (similar to a stock list -- but just
for looking up by ticker code).
Here's a simple module, for pedagogical purposes only, as you
say (not to make a mint).
Note that I use a dictionary-like file to store my stock objects,
in order to gain persistence from one Python session to the next.
=============
import time, shelve
class Stock:
"Simple stock class, keeps history using current time"
def __init__(self,code,price,datetime):
self.code = code
self.history = [(price,datetime)]
self.price = price
def newprice(self,price,datetime):
self.history.append((price,datetime))
def showhist(self):
return self.history
def getcode():
"Handle cases where user inputs code we don't have"
code = raw_input("Code? > ")
try:
stockobj = stockdict[code]
except:
print "Not a legal code"
return 0
return stockobj
def addstock():
"Add new stock, any code OK"
code = raw_input("Code? > ")
price = raw_input("Price? > ")
stockdict[code] = Stock(code,price,time.asctime())
def update():
obj = getcode()
if obj:
price = raw_input("Price? > ")
obj.newprice(price,time.asctime())
print "%s prices on file" % len(obj.history)
# would have to do next step if using real dictionary
stockdict[obj.code]=obj # overwrites old copy
def dumphist():
obj = getcode()
if obj:
hist = obj.showhist()
print hist
def quit():
stockdict.close()
print "Stocks saved"
def mainmenu():
"Using dictionary-like syntax to do file i/o"
global stockdict
stockdict = shelve.open("mystocks")
callables = [quit,addstock,update,dumphist]
while 1:
print \
"""
(1) add new stock
(2) update price of stock
(3) dump history of stock
(0) save/quit
"""
sel = raw_input("yr choice: > ")
try:
sel = int(sel)
assert int(sel) <= 3 and int(sel) >= 0
callables[int(sel)]()
if int(sel)==0: break
except:
print "Not a legal choice"
=============
Usage (in shell mode):
>>> import stocks
>>> stocks.mainmenu()
(1) add new stock
(2) update price of stock
(3) dump history of stock
(0) save/quit
yr choice: > 1
Code? > CISCO
Price? > 23.12
(1) add new stock
(2) update price of stock
(3) dump history of stock
(0) save/quit
yr choice: > 3
Code? > IBM [saved in an earlier session ]
[('12.75', 'Fri Mar 29 09:40:45 2002'), ('12.10', 'Fri Mar 29 09:40:54
2002'),
('45', 'Fri Mar 29 09:41:50 2002')]
(1) add new stock
(2) update price of stock
(3) dump history of stock
(0) save/quit
yr choice: > 2
Code? > CISCO
Price? > 14.10
2 prices on file
(1) add new stock
(2) update price of stock
(3) dump history of stock
(0) save/quit
yr choice: > 3
Code? > CISCO
[('23.12', 'Fri Mar 29 09:44:16 2002'), ('14.10', 'Fri Mar 29 09:44:35
2002')]
(1) add new stock
(2) update price of stock
(3) dump history of stock
(0) save/quit
yr choice: > 0
Stocks saved
Kirby