parameter passing Question

Its not exactly a wxPython question but I got confused because I am using wxGlade as designer tool. Its about parameter passing. Here is my little confusion.

CODE BEGINS*********************

class MyFrame1(wx.Frame):
def init(self, *args, **kwds):


def OnEdit(self, event):
sim_name = ‘some_string’
win = MyFrame2(self, -1, “”)

     win.Show(True)
···

class MyFrame2(wx.Frame):
def init(self, args, kwds):


def onLaunch(self, event):
## This function needs to use the parameter sim_name which is in class MyFrame1–>OnEdit
CODE ENDS
******************

I want to pass sim_name parameter to MyFrame2 but I don’t know how to do it. I tried couple of things but always got some error. I have some parameter passing in normal Python code but *args and **kwds is a little confusing.

Could you please tell me how to send parameter “sim_name” from MyFrame1 and how to recieve it in MyFrame2.

Every help is appreciated.

Thanks

Daniel Johnson wrote:

***************CODE BEGINS************************************
class MyFrame1(wx.Frame):
   def __init__(self, *args, **kwds):
        .....
   def OnEdit(self, event):
        sim_name = 'some_string'
        win = MyFrame2(self, -1, "")
        win.Show(True)

sim_name only exists at this point in the local namespace of the OnEdit method -- you can't get at it from anywhere else at all.

If you put it in self.sim_name, then it will live in the namespace of the instance of MyFrame1.

class MyFrame2(wx.Frame):
   def __init__(self, *args, **kwds):
        .....
   def onLaunch(self, event):
        ## This function needs to use the parameter sim_name which is in
class MyFrame1-->OnEdit
***************CODE ENDS************************************

I want to pass sim_name parameter to MyFrame2 but I don't know how to do it.

Where is your MyFrame2 getting created? Unless it's created in MyFrame1.OnEdit, which would be weird, you can't really just pass it in.

In general, it looks like you need to have some kind of class that holds various information your app is working with. I'm guess that sim_name isn't the only piece of data that you'll need to do this kind of thing with.

Then you need to be able to reference that data object from both Frame1 and Frame2, and probably a lot of other places. There are a lot of way to do that. One of which is....

I tried couple of things but always got some error. I have some parameter
passing in normal Python code but *args and **kwds is a little confusing.

You could do something like:

class MyFrame2(wx.Frame):
     def __init__(self, MyData, *args, **kwds):

then you would create it like:

AFrame = MyFrame2(SomeData, parent, other, parameters, to, wx.Frame)

If wxGlade Doesn't like that, you can just set the MyData parameter after you create the object;

AFrame = MyFrame2(parent, other, parameters, to, wx.Frame)
AFrame.MyData = SomeData

You could also store this data in the wx.App object, in a module that you reference from anywhere you need it, etc, etc, etc.

Remember: Python is ALL about namespaces. Every Python object (a class, a module, an instance, etc, is really just a namespace)

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer
                                         
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

I tried your way…but again I get error.

I have created an instance of MyFrame2 in MyFrame1 (OnEdit function).

MyFrame2 accepts parameter in the following form
class MyFrame2(wx.Frame):
def init(self, *args, **kwds):

What I did was

From OnEdit function of MyFram1 I wrote

win = MyFrame2(self, sim_name, *args, **kwds)

and changed MyFrame2 to the following form
class MyFrame2(wx.Frame):
def init(self, sim_name_accept, *args, **kwds):

I get error.

Where am I going wrong ?

···

On 8/4/06, Christopher Barker < Chris.Barker@noaa.gov> wrote:

Daniel Johnson wrote:

CODE BEGINS*********************

class MyFrame1(wx.Frame):
def init(self, *args, **kwds):


def OnEdit(self, event):
sim_name = ‘some_string’
win = MyFrame2(self, -1, “”)

    win.Show(True)

sim_name only exists at this point in the local namespace of the OnEdit
method – you can’t get at it from anywhere else at all.

If you put it in self.sim_name, then it will live in the namespace of

the instance of MyFrame1.

class MyFrame2(wx.Frame):
def init(self, *args, **kwds):


def onLaunch(self, event):
## This function needs to use the parameter sim_name which is in

class MyFrame1–>OnEdit
CODE ENDS*********************

I want to pass sim_name parameter to MyFrame2 but I don’t know how to do
it.

Where is your MyFrame2 getting created? Unless it’s created in

MyFrame1.OnEdit, which would be weird, you can’t really just pass it in.

In general, it looks like you need to have some kind of class that holds
various information your app is working with. I’m guess that sim_name

isn’t the only piece of data that you’ll need to do this kind of thing with.

Then you need to be able to reference that data object from both Frame1
and Frame2, and probably a lot of other places. There are a lot of way

to do that. One of which is…

I tried couple of things but always got some error. I have some parameter
passing in normal Python code but *args and **kwds is a little confusing.

You could do something like:

class MyFrame2(wx.Frame):
def init(self, MyData, *args, **kwds):

then you would create it like:

AFrame = MyFrame2(SomeData, parent, other, parameters, to, wx.Frame)

If wxGlade Doesn’t like that, you can just set the MyData parameter

after you create the object;

AFrame = MyFrame2(parent, other, parameters, to, wx.Frame)
AFrame.MyData = SomeData

You could also store this data in the wx.App object, in a module that
you reference from anywhere you need it, etc, etc, etc.

Remember: Python is ALL about namespaces. Every Python object (a class,
a module, an instance, etc, is really just a namespace)

-Chris


Christopher Barker, Ph.D.
Oceanographer

NOAA/OR&R/HAZMAT (206) 526-6959 voice

7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov


To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail:
wxPython-users-help@lists.wxwidgets.org

Daniel Johnson wrote:

I tried your way....but again I get error.

What's the error?

From OnEdit function of MyFram1 I wrote

win = MyFrame2(self, sim_name, *args, **kwds)

This should be:
win = MyFrame2(sim_name, self, *args, **kwds)

And only that if you want to pass in the args and kwargs that are defined, and they may not be defined at all in that namespace.

In this case "self" is the parent of MyFrame (or you could pass None). You want that passed into the wx.Frame.__init__

and changed MyFrame2 to the following form
class MyFrame2(wx.Frame):
   def __init__(self, sim_name_accept, *args, **kwds):

In this case "self" is the instance. It alwys gets passed in as the first argument of any method.

*args means: put all the unamed arguments into a tuple called args.
**kwargs means: put all keyword arguments into a dict called kwargs

It's the "*" that is important, the names are jsut conventions.

That construction is used if you want to simppy pass all the arguemnts to a class __init__ on to the superclass constructor.

you can get a sense of how all that *args stuff works, by playing with it:
>>> def f(p, *args, **kwargs):
... print "p", p
... print "*args", args
... print "**kwargs", kwargs
...
>>>
>>> f(4)
p 4
*args ()
**kwargs {}
>>> f(4,5,6)
p 4
*args (5, 6)
**kwargs {}
>>> f(4,6,7,this=8)
p 4
*args (6, 7)
**kwargs {'this': 8}
>>>

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer
                                         
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

Chris,

The error I am getting is

Traceback (most recent call last):
File “NewProject.py”, line 87, in OnEdit
win = MyFrame4(self, -1, “”,sim_name=sim_name)
File "NewProject.py
", line 1098, in init
wx.Frame.init(self, *args, **kwds)
File “/usr/lib/python2.4/site-packages/wx-2.6-gtk2-unicode/wx/_windows.py”, line 476, in init
newobj = windows.new_Frame(*args, **kwargs)

TypeError: ‘sim_name’ is an invalid keyword argument for this function

My intial feeling is since I am using wxGlade for designing it inserts some **kwds from stylewx.DEFAULT_FRAME_STYLE etc

So the parameter are not being matched correctly.

Is there a simple way of taking one on one parameter give that there is *args and **kwds in such a case.

Its really getting confusing as I am stuck at simple problem for long time. I understand all the examples you have given. Thanks

I dont want to past the code as it is around 50KB.

Any other help would be greatly appreciated.

···

On 8/4/06, Christopher Barker < Chris.Barker@noaa.gov> wrote:

Daniel Johnson wrote:

I tried your way…but again I get error.

What’s the error?

From OnEdit function of MyFram1 I wrote

win = MyFrame2(self, sim_name, *args, **kwds)

This should be:
win = MyFrame2(sim_name, self, *args, **kwds)

And only that if you want to pass in the args and kwargs that are

defined, and they may not be defined at all in that namespace.

In this case “self” is the parent of MyFrame (or you could pass None).
You want that passed into the wx.Frame.init

and changed MyFrame2 to the following form

class MyFrame2(wx.Frame):
def init(self, sim_name_accept, *args, **kwds):

In this case “self” is the instance. It alwys gets passed in as the
first argument of any method.

*args means: put all the unamed arguments into a tuple called args.
**kwargs means: put all keyword arguments into a dict called kwargs

It’s the “*” that is important, the names are jsut conventions.

That construction is used if you want to simppy pass all the arguemnts
to a class init on to the superclass constructor.

you can get a sense of how all that *args stuff works, by playing with it:

def f(p, *args, **kwargs):

… print “p”, p
… print “*args”, args
… print “**kwargs”, kwargs

f(4)
p 4
*args ()
**kwargs {}

f(4,5,6)

p 4
*args (5, 6)
**kwargs {}

f(4,6,7,this=8)
p 4
*args (6, 7)
**kwargs {‘this’: 8}

-Chris


Christopher Barker, Ph.D.
Oceanographer

NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov


To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org

For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org

Daniel Johnson wrote:

Chris,

The error I am getting is

Traceback (most recent call last):
  File "NewProject.py", line 87, in OnEdit
    win = MyFrame4(self, -1, "",sim_name=sim_name)
  File "NewProject.py ", line 1098, in __init__
    wx.Frame.__init__(self, *args, **kwds)
  File "/usr/lib/python2.4/site-packages/wx-2.6-gtk2-unicode/wx/_windows.py", line 476, in __init__
    newobj = _windows_.new_Frame(*args, **kwargs)
TypeError: 'sim_name' is an invalid keyword argument for this function

You need to remove the sim_name value from the kwds dictionary before passing it to wx.Frame.__init__

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Daniel Johnson wrote:

The error I am getting is

Traceback (most recent call last):
File "NewProject.py", line 87, in OnEdit
   win = MyFrame4(self, -1, "",sim_name=sim_name)
File "NewProject.py", line 1098, in __init__
   wx.Frame.__init__(self, *args, **kwds)
File "/usr/lib/python2.4/site-packages/wx-2.6-gtk2-unicode/wx/_windows.py",
line 476, in __init__
   newobj = _windows_.new_Frame(*args, **kwargs)
TypeError: 'sim_name' is an invalid keyword argument for this function

It's hard to tell from just this error message, but it looks like you are passing the sim_name keyword argument into MyFrame4's __init__, then passing all the keyword args on to wx.Frame.__init__, but that doesn't know what to do with a sim_name keyword.

My intial feeling is since I am using wxGlade for designing it inserts some
**kwds from stylewx.DEFAULT_FRAME_STYLE etc

That shouldn't make a difference.

So the parameter are not being matched correctly.

That's what keyword arguments are for -- they are named, so you don't need to put them in any particular order.

Is there a simple way of taking one on one parameter give that there is
*args and **kwds in such a case.

I don't understand that question. Remember that args is a tuple, kwargs is a dict.:

When you call your _init__ like this:

MyFrame4(self, -1, "",sim_name=sim_name)

you get:

args = (self, -1, "")
kwargs = {"sim_name": sim_name}

Does it make sense to pass those on to wx.Frame.__init__ ?

one solution is to remove the "sim_name" from kwargs:

self.sim_name = kwargs["sim_name"]
del kwargs["sim_name"]

then

wx.Frame.__init__(self, *args, **kwds)

or what I wrote before, putting sim_name before the other arguments.

working code is worth a thousand words. I've enclosed a demo of both methods in use.

I don't want to past the code as it is around 50KB.

This is key: ALWAYS, when you are having a problem like this, break it out into a tiny sample that does just the little bit you are trying to figure out. Then either:

A) you'll figure it out yourself as you isolate the problem

or

B) You'll have a nifty little sample to post on the list here and get help with.

The code I've enclosed only took me about 10 minutes to write, but it would have been 1 minute to fix yours if you had written it.

-Chris

junk.py (984 Bytes)

···

--
Christopher Barker, Ph.D.
Oceanographer
                                         
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

[snip]

A more foolproof method is to use:
self.sim_name = kwargs.pop('sim_name', None)

Then a later check for the validity of self.sim_name .

- Josiah

···

Christopher Barker <Chris.Barker@noaa.gov> wrote:

Daniel Johnson wrote:
one solution is to remove the "sim_name" from kwargs:

self.sim_name = kwargs["sim_name"]
del kwargs["sim_name"]

Josiah Carlson wrote:

A more foolproof method is to use:
self.sim_name = kwargs.pop('sim_name', None)

As it happens, I figured out to use pop when I actually wrote the test code, but I didn't add the None parameter -- I guess it's a question of when you want to catch the error.

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer
                                         
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov