[Tutor] classes & required method not working

Dave S pythontut at pusspaws.net
Sat Jun 19 15:00:10 EDT 2004


I have a class problem. I have a script, ggScanStock.py which defines a 
class ScanData :

#!/usr/bin/env python

"""
AnalizeClass
"""

import os,ggdatacore3

class ScanData:
   
    def __init__(self,stockdir='/home/dave/gg/stockdata/'):

        self.datadir=stockdir
        self.cyclecount=0
       
        self.reset()

    def scan(self):
           
        datadirs=os.listdir(self.stockdir)  # Loop for stockdir days
        datadirs.sort()
       
        # ############################# debug temp #########################
        datadirs=datadirs[:3]
        # ###############################################################
       
        for datadir in datadirs:
           
            print 'Processing Directory : ',datadir
           
            ggdatacore3.adddata(stockdir+datadir)
            keys=ggdatacore3.readkeys()
           
            for key in keys:
               
                ftselist,yftselist,ftsestats=ggdatacore3.readdata(key)
               
                # A probing loop to checkout all possible combinations 
of buying and selling slots
                for offset in range(1,101):
                    for buy in range(0,101-offset):
                    # from ... start to start+offset = profit / loss
           
                        sell=buy+offset
                       
                        bought=float(ftselist[buy][1])
                        sold=float(ftselist[sell][1])
                        pcinc=((sold-bought)/bought)*100
                       
                        # buy = slot I bought from
                        # sell = slot I sold in
                        # bought = pence I bought shares at
                        # sold = pence I sold shares at
                        # pcinc = % increase of share value
                       
                        for pcprofit_min in xrange(5,31):   # scans each 
pcinc for varing profit levels
                           
                            
self.process(pcprofit_min,buy,sell,bought,sold,pcinc,ftselist,yftselist,ftsestats)
                               
                               
 It has two dummy 'stub' class methods, namely self.reset() & 
self.process(). Because different instances will have different
'stub' class methods I have defined these methods in a seperate script, 
ggpcprofit.py

#!/usr/bin/env python

import os,ggScanStock

class pcprofit(ScanData):
   
    def reset(self):
        self.totalcycles=self.profitcycles=float(0)

    def 
process(self,pcprofit_min,buy,sell,bought,sold,pcinc,ftselist,yftselist,ftsestats):

        self.totalcycles=+1
        if pcinc>0:
            self.profitcycles=+1
   
    def output(self):
       
        print '\n*** ggpcprofit module ***'
        print '\nTotal %% profitable transactions over full range : 
%0.2f' % self.profitcycles/self.totalcycles
       

inst=pcprofit
inst.scan()
inst.output()

It seemed like a good idea & a way to learn about classes filling in 
required methods.

However when I try & execute ggpcprofit.py I get :

bash-2.05b$ ./ggpcprofit.py
Traceback (most recent call last):
  File "./ggpcprofit.py", line 5, in ?
    class pcprofit(ScanData):
NameError: name 'ScanData' is not defined
bash-2.05b$

Can anyone explain where I am going wrong - I imported ggScanStock so 
why cant my class pcprofit
find class ScanData ?

Dave






More information about the Tutor mailing list