Here's My Problem:

I have three Python Modules (using 2.7.2)

TC_Config.py merely holds Common Variables used by both of the other two Modules.

pyTC.py is supposed to be a Test and Flow Control (Master) Module that once standardized will not be changed (much).

ptf.py is a module that will be unique to whatever test is being done.

pyTC.py:

import TC_Config
from wx import *
from time import *
from visa import *
from os import *

def MakeConstant(value):
    def GetIt():
        return value
    retutn GetIt

STTO = MakeConstant(False)

def Test_Control():
    UUT_Info()

ptf.py: (This is the Calling or Origination Module)

import TC_Config
from pyTC import *

TC_Config.Module_Count = 1

def UUT_Info():
    if __name__ == "__main__":
        app = wx.PySimpleApp(0)
        wx.InitAllImageHandlers()
        Display = InfoUUT(None, -1, "")
        Display.Man_Name.SetLabel(" Intellipower, Inc.")
        Display.ModNumber.SetLabel(" IHT2KGACDC-2184")
        Display.MilDesig.SetLabel(" ")
        Display.Nomen.SetLabel(" SWAN-D UPS")
        Display.PartNo.SetLabel(" ")
        Display.SerNo.SetLabel(" 2KCAGDCI09390606")
        app.SetTopWindow(Display)
        Display.Show()
        app.MainLoop()


Test_Control()

if I call UUT_Info from here (instead of calling Test_Control) it works fine...

When I call from pyTC it errors out as undefined global name...

if I pass it as a function:

Test_Control(UUT_Info)

Modifying -->
Test_Control(fn)
fn()

It works...

But, the idea is that the Standardized Classes for the Test Control and Display will be in pyTC and the Unique Data will be in the ptf.py file...

*AND* UUT_Info is only one of many functions I'll need to call back into ptf.py for.

Quite honestly I am a Python Newbie and I am in way over my head.

Is there anything I can do to accomplish my design goal?

I had thought about passing a dictionary of functions to Test_Control, but, I have no clue as to how to do that...

Thanks!

Does not make sense for me, maybe with some kind of design diagram I could find sane implementation. But from your verbal explanations I could not make it, maybe some native English speaker is able to.

Here is result of trying to catch the meaning of code and commenting out unused modules etc. You did not post InfoUUT source, so the app does not start without the mainframe:

pyTC.py

## Not used commented out
##import TC_Config
##from wx import *
##from time import *
##from visa import *
##from os import *
##
## 
##def MakeConstant(value):
##    def GetIt():
##        return value
##    return GetIt
##
##MakeConstant(False) # does not make Constant, but makes function
STTO = lambda: False 
## does not make sense UUT_Info not defined
##def Test_Control():
##    UUT_Info()

ptf.py

import wx # added as used in function

#import TC_Config # not available replaced by class
class TC_Config:
    pass

# imports the STTO, which actually is not used
from pyTC import STTO


TC_Config.Module_Count = 1

def UUT_Info():
    app = wx.PySimpleApp(0)
    wx.InitAllImageHandlers()
##    Display = InfoUUT(None, -1, "") # InfoUUT is defined where?
##    Display.Man_Name.SetLabel(" Intellipower, Inc.")
##    Display.ModNumber.SetLabel(" IHT2KGACDC-2184")
##    Display.MilDesig.SetLabel(" ")
##    Display.Nomen.SetLabel(" SWAN-D UPS")
##    Display.PartNo.SetLabel(" ")
##    Display.SerNo.SetLabel(" 2KCAGDCI09390606")
##    app.SetTopWindow(Display)
##    Display.Show()
    print('Entering mainloop')
    app.MainLoop()
    print('Finished')

## this test does not make sense inside function or at least is very strange
if __name__ == "__main__":
    UUT_Info()

Edited 5 Years Ago by pyTony: n/a

I too am not completely sure what you want to achieve.
Forget about the code you've written - try to make a post that states clearly and simply what you want us to help you with, along with a clear design.

Edited 5 Years Ago by Enalicho: n/a

As I see it, the function UUT_Info() does not know about __name__, wx, or InfoUUT() as they are all declared/defined somewhere else and not passed to the function, but without a complete error message there is no way to tell which name is undefined. You would use something like

from wx import *
import file_that_contains_InfoUUT

def UUT_Info():
    app = wx.PySimpleApp(0)
    wx.InitAllImageHandlers()
    Display = file_that_contains_InfoUUT.InfoUUT(None, -1, "")

I may have solved my problem:

I created a Dictionary ptf = {"UUT_Info":UUT_Info()}

And, I call Test_Control(ptf)

in the pyTC file I changed the

def Test_Control(fn):
    fn()

-to-

def Test_Control(ptf)
    ptf.get("UUT_Info")

I'll consider this "Solved" after I add a few more items to the Dictionary.

But, what I have here seems to work. (Whew!)


to -> pyTony sorry sometimes what is clear to me can't easily be put into words with brevity.

Basically program flow is as follows:

1. user starts/runs the ptf.py file

2. ptf.py file initializes a few Common variables and passes control to Test_Control() in the pyTC.py file.

3. The next step is to display the UUT Information to the user. The Class for the UUT Information Display is in the pyTC.py file.

But, it is "Frozen" and the programmer is not allowed to modify it.

So, UUT_Info in the ptf.py file Instantiates an instance of the UUT Information Display Class.

Loads up the Unique Information and then Shows the Frame.

Where it was failing was after control was passed to Test_Control() in the pyTC.py file, Python could no longer find UUT_Info() in the ptf.py file because of namespace issues.

Is this clear as mud? :)

For me you should have OO stuff, where basic settings are inherited from basic class, specialized functions are added to the base class.

Codified your text example in python you mean

# command prompt
$ python ptf.py
# in ptf.py
# lets not polute namespace
import TC_config
import pyTC
def UUT_info:
    print(TC_config.message)
pyTC.Test_Control(UUT_info)
# in TC_config
message = 'Hi'
# in pyTC
def Test_Control(f):
    f()

Output:

Microsoft Windows XP [versio 5.1.2600]
(C) Copyright 1985 - 2001 Microsoft Corp.

ke 24.08.2011  0:29:57,39 K:\test
>python ptf.py
Hi

ke 24.08.2011  0:30:06,31 K:\test
>

Edited 5 Years Ago by pyTony: n/a

Comments
Poster should utilize OO "stuff"

The Dictionary Method seems to be working with multiple entries so I am going to consider this one solved.

Some comments were indecipherable. :)

The whole point of moving to Python from VB6 is to be Cross-Platform and have a common set of Classes against which to build Test Programs.

DOES NOT WORK!!!!!!

Discovered a major flaw with using a Dictionary to pass the references to Functions to the Other Module...

As the References are added to the Dictionary they are acted upon/run...

Clearly this isn't what I had in mind. :)

So, I will pass a rather long list of arguments instead...

OK...

It doesn't work the way *I* was doing it... :)

Original Calling Module Code

ptf={"UUT_Info":UUT_Info(), "Load_Menu":Load_Menu()}
Test_Control(ptf)

Original Called Module Code

def Test_Control(ptf):
    # print "Module_Count: " + str(TC_Config.Module_Count)
    # print "I made it to Test Control"
    ptf.get("UUT_Info")
    if TC_Config.TPS_Status == "Continue":
        # print "I am Continuing..."
        ptf.get("Load_Menu")
        Main_Menu()
        
    if TC_Config.TPS_Status == "Exit":
        print "User Exit From UUT_Info Display"

According to my extremely busy elsewhere Python Guru the closing parens after the Function name in the Dictionary was telling Python to invoke the Function...

I had tried removing those parens, but, then lost the display...

The New Calling Code

ptf={"UUT_Info":UUT_Info, "Load_Menu":Load_Menu}
Test_Control(ptf)

Note the removal of the parens after UUT_Info and Load_Menu .

The New Called Code

def Test_Control(ptf):
    # print "Module_Count: " + str(TC_Config.Module_Count)
    # print "I made it to Test Control"
    ptf.get("UUT_Info")()
    if TC_Config.TPS_Status == "Continue":
        # print "I am Continuing..."
        ptf.get("Load_Menu")()
        Main_Menu()
        
    if TC_Config.TPS_Status == "Exit":
        print "User Exit From UUT_Info Display"

Note the addition of the parens after the ptf.get(xxxx)()

You can either pass a long list a arguments, or you can create a dictionary.

The choice is yours...

This question has already been answered. Start a new discussion instead.