B: Objects

Quinn Dunkan quinn at lira.ugcs.caltech.edu
Sun May 19 18:34:54 EDT 2002


On 17 May 2002 07:07:30 -0700, Terje Johan Abrahamsen <terjej at mailandnews.com>
wrote:
>I have just started to learn Python after writing more of a spagetti
>style code in other languages with plenty of goto's. After what I have
>understood, you use different objects in Python to get the same
>effect.

No, not really.  Goto is a very generic feature.  Modern languages generally
have a whole host of more specialized things to replace them, like loops,
if-then, functions, etc.

Sounds like FORTRAN or something experience is tripping you.

>        So, I took my task, to transfer Excel data over to an AS400
>display, and divided the task into 4 separate parts. Basically what I
>want the four parts to do is:
>Accountcurrentstest:
>     Run the other tasks and stop when it gets the stop command from
>finnxlpol. (When stopper = 5).
>
>Finnxlpol:
>     Find the next policynumber in Excel. These are located in the A
>collumn. When there are no more policynumbers, set stopper = 5, so
>accountcurrentstest can stop the whole thing.
>
>finnpolicy:
>     Find the policy that finnxlpol returned in the As400 display. 
>
>skriv:
>     When finnpolicy has found the correct line, get the amount info
>from Excel, and paste it into the AS400 display. When this is done,
>the whole process should start again.
>
>The 3 last tasks works ok. I can run them, and they do what I want
>them to do.

Run them how?  When you call them from the prompt?  I wouldn't expect the code
below to work at all.

>them to do. My problem is the first one. How can I get the
>accountcurrentstest to run the others? Well, actually it runs the
>others, but there always ends up being errors. Like xlrow is not
>defined. Even thought I tried with a config.py file that contained
>'pass' and then wrote config.xlrow and so forth it didn't work. I have
>tried writing global in front of all variables, and it didn't work
>either. I have rewritten the thing probably 20 times, but it will not
>work. (I know that the accountcurrentstest will not work with the code
>I have now, but I have tried to take one step at a time, and include
>more and more of what I want. But, it stops in the start, and this is
>where I am now.)

If you want to pass values from one place to the next, use function arguments.

Also, rewriting something N times is not going to help you as much as figuring
out what you're doing.  Random guessing is usually not efficient (and is
frustrating besides).

>Could anyone give me a little hint about how I can fix this problem?
>Thanks a lot in advance......

Um, well, I don't know anything about win32 stuff, but it sounds like you need
to spend some time picking up the basics of "structured" programming.  Check
out some of the "beginning programmer" python tutorials on the web with an open
mind, forgetting what you know about your previous language.  Pay special
attention to the parts about 'functions' and 'variable scope', since I suspect
these concepts were absent from your previous language.

>-----------
>from win32com.client import Dispatch
>import finnxlpol
>import skriv
>import finnpolicy
>import config
>
>class accountcurrentstest:

Umm, I don't think 'class' is doing what you think it's doing here, but I
don't really know what you think it's doing.  Anyway, it's wrong :)

maybe:

def xls_test(fn): # then write ...Open(Filename=fn) below

>        xl = Dispatch("Excel.Application")
>        ex = Dispatch("Extra.System")
>        stopper = 0
>        xlrow = 1
>        xlcol = 1
>        global exrow
>        exrow = 15
>        excol = 4
>        polchar = 10

One of the things functions give you is scoping, which means if you don't use
those variables before the end of the indent, they'll never get used for
anything.  If you want someone to see it, pass it as an argument.

>        xl.Workbooks.Open(Filename='H:\\My documents\\account currents
>test.xls')
>        xlBook = xl.Workbooks(1)
>        xlSheet = xl.Sheets(1)
>        
>        while stopper != 5:
>                xlpol2 = finnxlpol.xlpol()
>                print xlpol2
>                stopper = 5      

This while loop is functionally identical to:
        print finnxlpol.xlpol()

>
>-------------------
>
>from win32com.client import Dispatch
>
>class finnxlpol():

Something tells me you haven't actually tried this code... maybe because the
above is a syntax error, and the function call in the above file was xlpol(),
and you define 'finnxlpol'.  In any case, I'm hoping 'finnxlpol' is a really
meaningful name in Finnish or whatever.  Aztec?

>        xl = Dispatch("Excel.Application")
>        ex = Dispatch("Extra.System")
>        xlBook = xl.Workbooks(1)
>        xlSheet = xl.Sheets(1)
>        xlrow = 1 + xlrow
>        xlpol = xlSheet.Cells(xlrow, 1).Value
>        if xlSheet.Cells(xlrow, 1).Value == None:
>            stopper = 5

This function uses 'xlrow' 'xlpol' and xlSheet', so they need to be passed as
an argument.  I randomly suggest replacing the syntax error with something
like

def finnxlpol(xlSheet, xlrow, xlpol):

(and then calling it properly)

>--------------------
>
>from win32com.client import Dispatch
>
>def finnpolicy():
>
>    ex = Dispatch("Extra.System")
>    expol = ex.ActiveSession.Screen.GetString(exrow, excol, polchar)
>    exrow = 13
>        
>    while expol != xlpol:
>        exrow = exrow + 1
>        expol = ex.ActiveSession.Screen.GetString(exrow, excol,
>polchar)
>        print expol, exrow
>        if exrow > 21:
>            ex.ActiveSession.Screen.SendKeys("<RollUp>")
>            exrow = 13

Likewise, you need to define 'polchar', either by assigning it within
finnpolicy() or passing it as an argument.

>from win32com.client import Dispatch
>    
>    
>def skriv():
>    xl = Dispatch("Excel.Application")
>    ex = Dispatch("Extra.System")
>    xlBook = xl.Workbooks(1)
>    xlSheet = xl.Sheets(1)
>    xlamount = xlSheet.Cells(xlrow, 3).Value
>    ex.ActiveSession.Screen.Select(exrow, 66, exrow, 76)
>    ex.ActiveSession.Screen.Paste()

Ditto.

Is there any particular reason you have these each in their own file?  All that
importing is a lot of work for not much gain.


So.  Go read some tutorials.  You're missing some fundamentals.  Once you've
got that stuff down, try again, and hopefully you'll be pleasantly surprised by
how easy it actually is.



More information about the Python-list mailing list