Making sense of a traceback from py2exe

2012-08-30 Thread Jonno
Not sure where the best place to post this is. My app uses wxpython,
matplotlib.
I'm running Python 2.7 on Windows 7.
I have a script app.py that I'm trying to turn into app.exe using py2exe.
The exe runs fine on the pc that it was compiled on but on another Win7
machine I get something like the following in the app.exe.log:

Traceback (most recent call last):
  File "app.py", line 1951, in 
  File "wx\_core.pyo", line 7981, in __init__
  File "wx\_core.pyo", line 7555, in _BootstrapApp
  File "app.py", line 1944, in OnInit
  File "app.py", line 1811, in __init__
  File "matplotlib\backends\backend_wxagg.pyo", line 59, in draw
  File "matplotlib\backends\backend_agg.pyo", line 401, in draw
  File "matplotlib\artist.pyo", line 55, in draw_wrapper
  File "matplotlib\figure.pyo", line 884, in draw
  File "matplotlib\artist.pyo", line 55, in draw_wrapper
  File "matplotlib\axes.pyo", line 1983, in draw
  File "matplotlib\artist.pyo", line 55, in draw_wrapper
  File "matplotlib\text.pyo", line 526, in draw
  File "matplotlib\text.pyo", line 309, in _get_layout
  File "matplotlib\backends\backend_agg.pyo", line 179, in
get_text_width_height_descent
  File "matplotlib\mathtext.pyo", line 2974, in parse
  File "matplotlib\mathtext.pyo", line 2352, in parse
  File "matplotlib\pyparsing.pyo", line 1048, in parseString
  File "matplotlib\pyparsing.pyo", line 981, in _parseCache
  File "matplotlib\pyparsing.pyo", line 924, in _parseNoCache
  File "matplotlib\pyparsing.pyo", line 2559, in parseImpl
  File "matplotlib\pyparsing.pyo", line 981, in _parseCache
  File "matplotlib\pyparsing.pyo", line 924, in _parseNoCache
  File "matplotlib\pyparsing.pyo", line 2307, in parseImpl
  File "matplotlib\pyparsing.pyo", line 981, in _parseCache
  File "matplotlib\pyparsing.pyo", line 924, in _parseNoCache
  File "matplotlib\pyparsing.pyo", line 2679, in parseImpl
  File "matplotlib\pyparsing.pyo", line 981, in _parseCache
  File "matplotlib\pyparsing.pyo", line 924, in _parseNoCache
  File "matplotlib\pyparsing.pyo", line 2307, in parseImpl
  File "matplotlib\pyparsing.pyo", line 981, in _parseCache
  File "matplotlib\pyparsing.pyo", line 924, in _parseNoCache
  File "matplotlib\pyparsing.pyo", line 2756, in parseImpl
  File "matplotlib\pyparsing.pyo", line 981, in _parseCache
  File "matplotlib\pyparsing.pyo", line 924, in _parseNoCache
  File "matplotlib\pyparsing.pyo", line 2714, in parseImpl
  File "matplotlib\pyparsing.pyo", line 981, in _parseCache
  File "matplotlib\pyparsing.pyo", line 924, in _parseNoCache
  File "matplotlib\pyparsing.pyo", line 2373, in parseImpl
  File "matplotlib\pyparsing.pyo", line 981, in _parseCache
  File "matplotlib\pyparsing.pyo", line 924, in _parseNoCache
  File "matplotlib\pyparsing.pyo", line 2559, in parseImpl
  File "matplotlib\pyparsing.pyo", line 981, in _parseCache
  File "matplotlib\pyparsing.pyo", line 924, in _parseNoCache
  File "matplotlib\pyparsing.pyo", line 2416, in parseImpl
  File "matplotlib\pyparsing.pyo", line 981, in _parseCache
  File "matplotlib\pyparsing.pyo", line 924, in _parseNoCache
  File "matplotlib\pyparsing.pyo", line 2559, in parseImpl
  File "matplotlib\pyparsing.pyo", line 981, in _parseCache
  File "matplotlib\pyparsing.pyo", line 924, in _parseNoCache
  File "matplotlib\pyparsing.pyo", line 2559, in parseImpl
  File "matplotlib\pyparsing.pyo", line 981, in _parseCache
  File "matplotlib\pyparsing.pyo", line 924, in _parseNoCache
  File "matplotlib\pyparsing.pyo", line 2416, in parseImpl
  File "matplotlib\pyparsing.pyo", line 981, in _parseCache
  File "matplotlib\pyparsing.pyo", line 924, in _parseNoCache
  File "matplotlib\pyparsing.pyo", line 2293, in parseImpl
  File "matplotlib\pyparsing.pyo", line 981, in _parseCache
  File "matplotlib\pyparsing.pyo", line 924, in _parseNoCache
  File "matplotlib\pyparsing.pyo", line 2756, in parseImpl
  File "matplotlib\pyparsing.pyo", line 981, in _parseCache
  File "matplotlib\pyparsing.pyo", line 924, in _parseNoCache
  File "matplotlib\pyparsing.pyo", line 2559, in parseImpl
  File "matplotlib\pyparsing.pyo", line 981, in _parseCache
  File "matplotlib\pyparsing.pyo", line 924, in _parseNoCache
  File "matplotlib\pyparsing.pyo", line 2373, in parseImpl
  File "matplotlib\pyparsing.pyo", line 981, in _parseCache
  File "matplotlib\pyparsing.pyo", line 924, in _parseNoCache
  File "matplotlib\pyparsing.pyo", line 2416, in parseImpl
  File "matplotlib\pyparsing.pyo", line 981, in _parseCache
  File "matplotlib\pyparsing.pyo", line 950, in _parseNoCache
  File "matplotlib\mathtext.pyo", line 2469, in symbol
  File "matplotlib\mathtext.pyo", line 1312, in __init__
  File "matplotlib\mathtext.pyo", line 1319, in _update_metrics
  File "matplotlib\mathtext.pyo", line 485, in get_metrics
  File "matplotlib\mathtext.pyo", line 618, in _get_info
  File "matplotlib\mathtext.pyo", line 720, in _get_glyph
KeyError: 98
Traceback (most recent call last):
  File "wx\_core.pyo", line 14669, in 
  File "app.py", line 826, in _in

Re: Making sense of a traceback from py2exe

2012-08-30 Thread Jonno
On Thu, Aug 30, 2012 at 3:11 PM, Jonno  wrote:

> Not sure where the best place to post this is. My app uses wxpython,
> matplotlib.
> I'm running Python 2.7 on Windows 7.
> I have a script app.py that I'm trying to turn into app.exe using py2exe.
> The exe runs fine on the pc that it was compiled on but on another Win7
> machine I get something like the following in the app.exe.log:
>
> Traceback (most recent call last):
>   File "app.py", line 1951, in 
>   File "wx\_core.pyo", line 7981, in __init__
>   File "wx\_core.pyo", line 7555, in _BootstrapApp
>   File "app.py", line 1944, in OnInit
>   File "app.py", line 1811, in __init__
>   File "matplotlib\backends\backend_wxagg.pyo", line 59, in draw
>   File "matplotlib\backends\backend_agg.pyo", line 401, in draw
>   File "matplotlib\artist.pyo", line 55, in draw_wrapper
>   File "matplotlib\figure.pyo", line 884, in draw
>   File "matplotlib\artist.pyo", line 55, in draw_wrapper
>   File "matplotlib\axes.pyo", line 1983, in draw
>   File "matplotlib\artist.pyo", line 55, in draw_wrapper
>   File "matplotlib\text.pyo", line 526, in draw
>   File "matplotlib\text.pyo", line 309, in _get_layout
>   File "matplotlib\backends\backend_agg.pyo", line 179, in
> get_text_width_height_descent
>   File "matplotlib\mathtext.pyo", line 2974, in parse
>   File "matplotlib\mathtext.pyo", line 2352, in parse
>   File "matplotlib\pyparsing.pyo", line 1048, in parseString
>   File "matplotlib\pyparsing.pyo", line 981, in _parseCache
>   File "matplotlib\pyparsing.pyo", line 924, in _parseNoCache
>   File "matplotlib\pyparsing.pyo", line 2559, in parseImpl
>   File "matplotlib\pyparsing.pyo", line 981, in _parseCache
>   File "matplotlib\pyparsing.pyo", line 924, in _parseNoCache
>   File "matplotlib\pyparsing.pyo", line 2307, in parseImpl
>   File "matplotlib\pyparsing.pyo", line 981, in _parseCache
>   File "matplotlib\pyparsing.pyo", line 924, in _parseNoCache
>   File "matplotlib\pyparsing.pyo", line 2679, in parseImpl
>   File "matplotlib\pyparsing.pyo", line 981, in _parseCache
>   File "matplotlib\pyparsing.pyo", line 924, in _parseNoCache
>   File "matplotlib\pyparsing.pyo", line 2307, in parseImpl
>   File "matplotlib\pyparsing.pyo", line 981, in _parseCache
>   File "matplotlib\pyparsing.pyo", line 924, in _parseNoCache
>   File "matplotlib\pyparsing.pyo", line 2756, in parseImpl
>   File "matplotlib\pyparsing.pyo", line 981, in _parseCache
>   File "matplotlib\pyparsing.pyo", line 924, in _parseNoCache
>   File "matplotlib\pyparsing.pyo", line 2714, in parseImpl
>   File "matplotlib\pyparsing.pyo", line 981, in _parseCache
>   File "matplotlib\pyparsing.pyo", line 924, in _parseNoCache
>   File "matplotlib\pyparsing.pyo", line 2373, in parseImpl
>   File "matplotlib\pyparsing.pyo", line 981, in _parseCache
>   File "matplotlib\pyparsing.pyo", line 924, in _parseNoCache
>   File "matplotlib\pyparsing.pyo", line 2559, in parseImpl
>   File "matplotlib\pyparsing.pyo", line 981, in _parseCache
>   File "matplotlib\pyparsing.pyo", line 924, in _parseNoCache
>   File "matplotlib\pyparsing.pyo", line 2416, in parseImpl
>   File "matplotlib\pyparsing.pyo", line 981, in _parseCache
>   File "matplotlib\pyparsing.pyo", line 924, in _parseNoCache
>   File "matplotlib\pyparsing.pyo", line 2559, in parseImpl
>   File "matplotlib\pyparsing.pyo", line 981, in _parseCache
>   File "matplotlib\pyparsing.pyo", line 924, in _parseNoCache
>   File "matplotlib\pyparsing.pyo", line 2559, in parseImpl
>   File "matplotlib\pyparsing.pyo", line 981, in _parseCache
>   File "matplotlib\pyparsing.pyo", line 924, in _parseNoCache
>   File "matplotlib\pyparsing.pyo", line 2416, in parseImpl
>   File "matplotlib\pyparsing.pyo", line 981, in _parseCache
>   File "matplotlib\pyparsing.pyo", line 924, in _parseNoCache
>   File "matplotlib\pyparsing.pyo", line 2293, in parseImpl
>   File "matplotlib\pyparsing.pyo", line 981, in _parseCache
>   File "matplotlib\pyparsing.pyo", line 924, in _parseNoCache
>   File "matplotlib\pyparsing.pyo", line 2756, in parseImpl
>   File "matplotlib\pyparsing.pyo", line 981, in _parseCache
>   File "matplotlib\pyparsing.pyo&quo

Re: Making sense of a traceback from py2exe

2012-08-30 Thread Jonno
On Thu, Aug 30, 2012 at 4:02 PM, Jonno  wrote:

>
> Well I managed to figure out that the first traceback is the one causing
> the problem and that matplotlib/mathtext in my app is the problem.
> Now to figure out how to get mathtext working.
>

Bit more information:
I am also seeing warnings in app.exe.log:
C:\Users\Administrator\Desktop\dist\library.zip\matplotlib\font_manager.py:1216:
UserWarning: findfont: Font family ['STIXGeneral'] not found. Falling back
to Bitstream Vera Sans
C:\Users\Administrator\Desktop\dist\library.zip\matplotlib\font_manager.py:1226:
UserWarning: findfont: Could not match :family=Bitstream Vera
Sans:style=normal:variant=normal:weight=normal:stretch=normal:size=12.
Returning c:\windows\fonts\browai.ttf
C:\Users\Administrator\Desktop\dist\library.zip\matplotlib\font_manager.py:1216:
UserWarning: findfont: Font family ['STIXSizeOneSym'] not found. Falling
back to Bitstream Vera Sans
C:\Users\Administrator\Desktop\dist\library.zip\matplotlib\font_manager.py:1226:
UserWarning: findfont: Could not match :family=Bitstream Vera
Sans:style=normal:variant=normal:weight=bold:stretch=normal:size=12.
Returning c:\windows\fonts\browai.ttf
C:\Users\Administrator\Desktop\dist\library.zip\matplotlib\font_manager.py:1216:
UserWarning: findfont: Font family ['STIXSizeThreeSym'] not found. Falling
back to Bitstream Vera Sans
C:\Users\Administrator\Desktop\dist\library.zip\matplotlib\font_manager.py:1216:
UserWarning: findfont: Font family ['STIXSizeFourSym'] not found. Falling
back to Bitstream Vera Sans
C:\Users\Administrator\Desktop\dist\library.zip\matplotlib\font_manager.py:1216:
UserWarning: findfont: Font family ['STIXSizeFiveSym'] not found. Falling
back to Bitstream Vera Sans
C:\Users\Administrator\Desktop\dist\library.zip\matplotlib\font_manager.py:1216:
UserWarning: findfont: Font family ['STIXSizeTwoSym'] not found. Falling
back to Bitstream Vera Sans
C:\Users\Administrator\Desktop\dist\library.zip\matplotlib\font_manager.py:1226:
UserWarning: findfont: Could not match :family=Bitstream Vera
Sans:style=italic:variant=normal:weight=normal:stretch=normal:size=12.
Returning c:\windows\fonts\browai.ttf
C:\Users\Administrator\Desktop\dist\library.zip\matplotlib\font_manager.py:1216:
UserWarning: findfont: Font family ['STIXNonUnicode'] not found. Falling
back to Bitstream Vera Sans
C:\Users\Administrator\Desktop\dist\library.zip\matplotlib\font_manager.py:1216:
UserWarning: findfont: Font family ['cmb10'] not found. Falling back to
Bitstream Vera Sans
C:\Users\Administrator\Desktop\dist\library.zip\matplotlib\font_manager.py:1216:
UserWarning: findfont: Font family ['cmtt10'] not found. Falling back to
Bitstream Vera Sans
C:\Users\Administrator\Desktop\dist\library.zip\matplotlib\font_manager.py:1216:
UserWarning: findfont: Font family ['cmmi10'] not found. Falling back to
Bitstream Vera Sans
C:\Users\Administrator\Desktop\dist\library.zip\matplotlib\font_manager.py:1216:
UserWarning: findfont: Font family ['cmex10'] not found. Falling back to
Bitstream Vera Sans
C:\Users\Administrator\Desktop\dist\library.zip\matplotlib\font_manager.py:1216:
UserWarning: findfont: Font family ['cmsy10'] not found. Falling back to
Bitstream Vera Sans
C:\Users\Administrator\Desktop\dist\library.zip\matplotlib\font_manager.py:1216:
UserWarning: findfont: Font family ['cmr10'] not found. Falling back to
Bitstream Vera Sans
C:\Users\Administrator\Desktop\dist\library.zip\matplotlib\font_manager.py:1216:
UserWarning: findfont: Font family ['cmss10'] not found. Falling back to
Bitstream Vera Sans

I think these are all fonts that matplotlib/mathtext would use so it makes
sense that the traceback ends with:
File "matplotlib\mathtext.pyo", line 720, in _get_glyph
KeyError: 98

I then discovered that both the methods I tried for getting data_files
(using glob and matplotlib.get_py2exe_datafiles) were not working and I had
nothing in my mpl-data/fonts directory. This directory should contain 3
folders: afm, pdfcorefonts & ttf. The ttf folder is where the mathtext
fonts mentioned above are located.

I then tried manually copying the entire mpl-data folder into the dist
folder after running py2exe on setup.py but still I get the same error.

Does data_files do anything other than include files and directories in the
dist folder?
-- 
http://mail.python.org/mailman/listinfo/python-list


Using an object inside a class

2012-01-23 Thread Jonno
I have a pretty complicated bit of code that I'm trying to convert to more
clean OOP.

Without getting too heavy into the details I have an object which I am
trying to make available inside another class. The reference to the object
is rather long and convoluted but what I find is that within my class
definition this works:

class Class1:
def __init__(self):

def method1(self):
 foo.bar.object

But this tells me "global name foo is not defined":

class Class1:
 def __init__(self):
   foo.bar.object

Obviously I want the object to be available throughout the class (I left
out the self.object = etc for simplicity).

Any ideas why I can reference foo inside the method but not in __init__?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using an object inside a class

2012-01-23 Thread Jonno
On Mon, Jan 23, 2012 at 1:44 PM, Jonno  wrote:

> I have a pretty complicated bit of code that I'm trying to convert to more
> clean OOP.
>
> Without getting too heavy into the details I have an object which I am
> trying to make available inside another class. The reference to the object
> is rather long and convoluted but what I find is that within my class
> definition this works:
>
> class Class1:
> def __init__(self):
>
> def method1(self):
>  foo.bar.object
>
> But this tells me "global name foo is not defined":
>
> class Class1:
>  def __init__(self):
>foo.bar.object
>
> Obviously I want the object to be available throughout the class (I left
> out the self.object = etc for simplicity).
>
> Any ideas why I can reference foo inside the method but not in __init__?
>

If it matters, foo is an instance of wx.App and bar is a wx.Frame within
the app.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using an object inside a class

2012-01-23 Thread Jonno
On Mon, Jan 23, 2012 at 2:09 PM, Ian Kelly  wrote:

> On Mon, Jan 23, 2012 at 12:44 PM, Jonno  wrote:
> > I have a pretty complicated bit of code that I'm trying to convert to
> more
> > clean OOP.
>
> Then you probably should not be using globals.
>

I'm trying to rewrite the whole thing to get rid of my globals.

>
> > Without getting too heavy into the details I have an object which I am
> > trying to make available inside another class. The reference to the
> object
> > is rather long and convoluted but what I find is that within my class
> > definition this works:
> >
> > class Class1:
> > def __init__(self):
> >
> > def method1(self):
> >  foo.bar.object
> >
> > But this tells me "global name foo is not defined":
> >
> > class Class1:
> >  def __init__(self):
> >foo.bar.object
>
> Where is foo actually stored?  Is it in fact a global, or is it
> somewhere else?  Please post the actual code.  I suspect that what's
> going on here is that you're assigning foo somewhere inside method1
> and so it is actually a local variable to that method, but there is no
> way to know that for certain from the minimal snippet provided.
>
> The whole code is complex but here is where I define foo and bar:

class MyApp(wx.App):
def OnInit(self):
self.bar = MyFrame(None, -1, 'App Name')
self.bar.Show(True)
return True

foo = MyApp(0)
app.MainLoop()

There is nothing inside method1 except the foo.bar.object reference.


> > Obviously I want the object to be available throughout the class (I left
> out
> > the self.object = etc for simplicity).
>
> Do you mean that you want the same object to be available to all
> instances of Class1, or that you just want the object to be available
> to all methods within a single instance (and other instances might
> access other objects)?  In the first case, I would recommend storing
> foo in a class attribute; in the second case, an instance attribute.
> Either way, it would then be accessed simply as "self.foo".
>

Either way would work but the main issue is I can't seem to use foo or
foo.bar or foo.bar.object anywhere in __init__ or even before that in the
main class area.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using an object inside a class

2012-01-23 Thread Jonno
On Mon, Jan 23, 2012 at 2:25 PM, Terry Reedy  wrote:

> On 1/23/2012 2:44 PM, Jonno wrote:
>
>> I have a pretty complicated bit of code that I'm trying to convert to
>> more clean OOP.
>>
>> Without getting too heavy into the details I have an object which I am
>> trying to make available inside another class. The reference to the
>> object is rather long and convoluted but what I find is that within my
>> class definition this works:
>>
>> class Class1:
>> def __init__(self):
>>
>> def method1(self):
>>  foo.bar.object
>>
>> But this tells me "global name foo is not defined":
>>
>> class Class1:
>>  def __init__(self):
>>foo.bar.object
>>
>> Obviously I want the object to be available throughout the class (I left
>> out the self.object = etc for simplicity).
>>
>
> Perhaps you left out some relevant details.
>
> I'm sure I did. Part of the reason I'm not posting the whole code is that
I'm trying to teach myself OOP as part of this process. I want to figure
out what is wrong as much as possible by myself. I really appreciate the
pointers and suggestions though.


>
>  Any ideas why I can reference foo inside the method but not in __init__?
>>
>
> References inside functions are resolved when the function is called. So
> purely from what you have presented above, it would seem that 'foo' is
> defined between the call to __init__ and a later call to method1.


I have a strong suspicion that this is what's happening.

Method1 is called on a button push when MainLoop is running so obviously
foo (the main wx.App) exists by then.
I must have somehow be initializing Class1 before foo = MyApp() happens.
Is there a good reference on the order that things happen in python when a
single script is run?

In the meantime here is my stripped down script (foo = app, bar = frame,
object = graph_panel). I'd welcome all suggestions to reorganize it.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using an object inside a class

2012-01-23 Thread Jonno
Script...

import wx
import wx.aui
import matplotlib as mpl
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as Canvas

class Class1(wx.Panel):
def __init__(self, parent, id = -1, dpi = None, **kwargs):
wx.Panel.__init__(self, parent, id=id, **kwargs)
self.figure = mpl.figure.Figure(dpi=dpi, figsize=(2,2))
self.canvas = Canvas(self, -1, self.figure)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.canvas,1,wx.EXPAND)
test_button = wx.Button(self, wx.ID_ANY, 'Test')
sizer.Add(test_button)
self.SetSizer(sizer)
self.Bind(wx.EVT_BUTTON, self.OnTest, id=wx.ID_ANY)

# This doesn't work
#app.frame.graph_panel.plot([1,2,3,4,5],[3,4,3,4,3])
#app.frame.graph_panel.figure.canvas.draw()

def OnTest(self, event):
# This works
app.frame.graph_panel.plot([1,2,3,4,5],[3,4,3,4,3])
app.frame.graph_panel.figure.canvas.draw()


class Tab(wx.Panel):
def __init__(self, parent, id = -1):
wx.Panel.__init__(self, parent, id=id)
self.nb = wx.aui.AuiNotebook(self)
sizer = wx.BoxSizer()
sizer.Add(self.nb, 1, wx.EXPAND)
self.SetSizer(sizer)

def add_axes(self,name="plot"):
   page = Class1(self.nb)
   self.nb.AddPage(page,name)
   return page.figure


class MyFrame(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition,
wx.Size(1000, 800))
tab_panel = Tab(self)
self.graph_panel = tab_panel.add_axes('Graph').gca()
self.graph_panel.plot([1,2,3,4,5],[2,1,4,2,3])


class MyApp(wx.App):
def OnInit(self):
self.frame = MyFrame(None, -1, 'App')
self.frame.Show(True)
return True

app = MyApp(0)
app.MainLoop()

On Mon, Jan 23, 2012 at 3:22 PM, Jonno  wrote:

>
>
> On Mon, Jan 23, 2012 at 2:25 PM, Terry Reedy  wrote:
>
>> On 1/23/2012 2:44 PM, Jonno wrote:
>>
>>> I have a pretty complicated bit of code that I'm trying to convert to
>>> more clean OOP.
>>>
>>> Without getting too heavy into the details I have an object which I am
>>> trying to make available inside another class. The reference to the
>>> object is rather long and convoluted but what I find is that within my
>>> class definition this works:
>>>
>>> class Class1:
>>> def __init__(self):
>>>
>>> def method1(self):
>>>  foo.bar.object
>>>
>>> But this tells me "global name foo is not defined":
>>>
>>> class Class1:
>>>  def __init__(self):
>>>foo.bar.object
>>>
>>> Obviously I want the object to be available throughout the class (I left
>>> out the self.object = etc for simplicity).
>>>
>>
>> Perhaps you left out some relevant details.
>>
>> I'm sure I did. Part of the reason I'm not posting the whole code is that
> I'm trying to teach myself OOP as part of this process. I want to figure
> out what is wrong as much as possible by myself. I really appreciate the
> pointers and suggestions though.
>
>
>>
>>  Any ideas why I can reference foo inside the method but not in __init__?
>>>
>>
>> References inside functions are resolved when the function is called. So
>> purely from what you have presented above, it would seem that 'foo' is
>> defined between the call to __init__ and a later call to method1.
>
>
> I have a strong suspicion that this is what's happening.
>
> Method1 is called on a button push when MainLoop is running so obviously
> foo (the main wx.App) exists by then.
> I must have somehow be initializing Class1 before foo = MyApp() happens.
> Is there a good reference on the order that things happen in python when a
> single script is run?
>
> In the meantime here is my stripped down script (foo = app, bar = frame,
> object = graph_panel). I'd welcome all suggestions to reorganize it.
>
>
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using an object inside a class

2012-01-23 Thread Jonno
On Mon, Jan 23, 2012 at 3:42 PM, Ian Kelly  wrote:
>
> Exactly.  The line "app = MyApp(0)" creates a MyApp instance and then
> assigns it to "app".  As part of the MyApp creation process, it
> creates a MyFrame, which creates a Tab, which creates a Class1, which
> attempts to reference "app".  All of this happens before that
> "MyApp(0)" call has returned, so the result of that call has not
> actually been assigned to "app" yet.
>
> I suggest using wx.GetApp() instead.
>
> That totally makes sense. However I'm not sure I understand your
suggestion how to use wx.GetApp()
Isn't the wxApp still not created before Class1 is instantiated so I still
can't call wx.GetApp() in __init__ of Class1 can I?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using an object inside a class

2012-01-23 Thread Jonno
On Mon, Jan 23, 2012 at 4:20 PM, Ian Kelly  wrote:
>
> The App object is created and the wx framework already knows about it.
>  It's just not assigned to the app global yet, and the OnInit call has
> not completed yet.  See:
>
> Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
> (Intel)] on win32
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import wx
> >>> class MyApp(wx.App):
> ... def OnInit(self):
> ... print "wx.GetApp() =", wx.GetApp()
> ... print "app =", app
> ... return True
> ...
> >>> app = MyApp(0)
> wx.GetApp() = <__main__.MyApp; proxy of  *' at 0x18d8fc0> >
> app =
> Traceback (most recent call last):
>  File "", line 1, in 
>  File "C:\Python25\lib\site-packages\wx-2.8-msw-unicode\wx\_core.py",
> line 7823, in __init__
>self._BootstrapApp()
>  File "C:\Python25\lib\site-packages\wx-2.8-msw-unicode\wx\_core.py",
> line 7420, in _BootstrapApp
>return _core_.PyApp__BootstrapApp(*args, **kwargs)
>  File "", line 4, in OnInit
> NameError: global name 'app' is not defined
>
> I see, so that would get me access to the app instance during init of
Class1 but if I can't access frame or the object as they still aren't
created yet. I can only do that in attributes that I know won't be called
until the app is created.
I'd have to do something like this:

class Class1:
def __init__(self):
self.app = wx.GetApp()

def Method1(self):
self.app.frame.object

This doesn't seem that pythonic. Is there a better way to do what I'm
trying to do? Is there a better way to create my object for example?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using an object inside a class

2012-01-23 Thread Jonno
On Mon, Jan 23, 2012 at 4:57 PM, Ian Kelly  wrote:

>
> By the way, looking at your object hierarchy more closely, isn't
> "app.frame.graph_panel" going to end up being the same thing as just
> "self.figure"?  Why not just use the latter and remove the reliance on
> finding the correct frame?
>

I could do that however the app ultimately has more than one notebook tab
that I need to be able to access the figure object from.

Perhaps I should step back and ask the question how to create an app with
multiple notebook pages with a figure in one and have that figure object be
accessible to all the notebook pages.
I created this architecture following the example here:
http://matplotlib.sourceforge.net/examples/user_interfaces/embedding_in_wx5.html
-- 
http://mail.python.org/mailman/listinfo/python-list


split string but ignore sep inside double quotes

2011-04-13 Thread Jonno
All,

I have the following unicode object:
u'3,"Some, text",more text'

and I want to split it into a list like this:
[3,"Some, text", more text]

In other words I want to split on the comma but not if it's inside a
double-quote.

Thanks.
-- 
http://mail.python.org/mailman/listinfo/python-list


Help creating new module which inherits existing class from another module.

2014-02-18 Thread Jonno
I'm not sure if this list is a suitable place to ask for this kind of help
so if it's not please just suggest another forum which might be more
suitable.

I'm looking for help/suggestions how to architect a module (perhaps just a
class).

There is an existing module I want to use which has a class we'll call
*Existing
Class*.

I want to create a python module which allows me to create *new_objects* with
the following properties:

   - The new_objects have all the attributes of the Existing_Class (simply
   create a class that inherits from Existing_Class)
   - I then want to create a nested structure under the new_objects
   something like:

new_object.foo

new_object.foo.bar

new_object.foo.bar.baz

Where foo, bar, baz have the following properties:

   - All have *docstrings*
   - All are available for *tab completion* tools upon new_object creation.
   -

   Some of which will have *new methods* which act in the following way:
   - new_object.foo.bar()

   calls
   - new_object.existing_method("foo.bar", *args)

I believe I'll need to use metaclasses and have an idea that
types.MethodType will help but I keep getting stuck. Any pointers would be
greatly appreciated.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Help creating new module which inherits existing class from another module.

2014-02-18 Thread Jonno
Ben,

Thanks for your reply. I'll try to ellaborate a little more in the comments
below.


On Tue, Feb 18, 2014 at 2:47 PM, Ben Finney wrote:

> Jonno  writes:
>
> > I'm not sure if this list is a suitable place to ask for this kind of
> > help so if it's not please just suggest another forum which might be
> > more suitable.
>
> Welcome! Asking for help with writing Python code is definitely suitable
> here.
>
> > I'm looking for help/suggestions how to architect a module (perhaps
> > just a class).
>
> Right, I don't see anything in your request that indicates why a new
> module would be needed.
>

Only that eventually I might want to distribute this and include some other
features.

>
> > There is an existing module I want to use which has a class we'll call
> > *Existing Class*.
> >
> > I want to create a python module which allows me to create
> > *new_objects*
>
> This is all rather abstract. Can you explain what the existing class is
> for? What the new class is for?
>
> Well I was trying to keep it as generic as possible and only introduce the
necessary features. It's possible I've left out something important though.


> > with the following properties:
> >
> >- The new_objects have all the attributes of the Existing_Class
> (simply
> >create a class that inherits from Existing_Class)
>
> The syntax for that is::
>
> class Bar(Foo):
> ...
>
> where "Foo" is the existing class, "Bar" is the class you're defining.
>
> It's not strictly true to say that Bar will thereby "have all the
> attributes of" the existing class. Rather, the resolution chain will
> mean that accessing attributes on Bar will fall-back to looking at Foo
> for attributes not found in Bar.
>
> Point taken.


> >- I then want to create a nested structure under the new_objects
> >something like:
> >
> > new_object.foo
> > new_object.foo.bar
> > new_object.foo.bar.baz
>
> Again, it's not clear why you're doing this. What are these attributes for?
>

I tried to explain the necessary properties in the requirements below.
Really the main function of creating this new module is to add the
docstring & tab completion capabilities. Essentially it's a way to create
an explorable structure of elements with documentation. If you have other
suggestions how to achieve that I'd be interested.
.

>
> > Where foo, bar, baz have the following properties:
> >
> >- All have *docstrings*
>
> Docstrings are only for code objects: modules, classes, functions. Will
> each of those attributes be one of those?
>
> Only because I'd like them to have docstring attributes.


> >- All are available for *tab completion* tools upon new_object
> creation.
>
> Tab completion is up to the interactive tool you're using. Which tool is
> that?
>
>
For me ipython but I'd like it to work in other tools so the more general
the better.


> >-
> >Some of which will have *new methods* which act in the following way:
> >- new_object.foo.bar()
> >
> >calls
> >- new_object.existing_method("foo.bar", *args)
>
> This would be an unusual semantic. Why not call the existing method?
>
> Basically right now the user has to look up external documentation to
figure out which arguments (foo, bar etc) are necessary and there are many
of them in a nested structure. I'd like to create a module that they can
explore within an enhanced python editor/interpreter.


> > Any pointers would be greatly appreciated.
>
> I am smelling the likelihood that you have a strange design that needs
> to be examined and changed. Can you describe what your purpose is that
> you think needs this strange architecture?
>
>
Hopefully my comments help some.
I'm definitely not opposed to changing the architecture but the main
objectives of documentation & explorability (yes I know that's not a real
word) are top priority.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Help creating new module which inherits existing class from another module.

2014-02-19 Thread Jonno
On Tue, Feb 18, 2014 at 11:48 PM, Ben Finney wrote:

> Jonno  writes:
>
> > I tried to explain the necessary properties in the requirements below.
>
> What you've described is a bunch of abstract behaviour.
>
> But as I said, I'm suspecting this is a poor design; and I can't know
> better until you explain what all this is *for*.
>
> What is the purpose of the code you're writing? What is special about it
> that makes you think it needs the specific properties you've described?
>
> I ask all this because I suspect there are better and/or simpler ways to
> achieve your *actual* goals. Maybe not; but to rule that out, I'd need
> to know more about the purpose.
>
>  Let me try to give an example then.
Let's say the existing module provides a class RoboCom() which allows the
user to communicate with robots (it doesn't). It handles all the
communication hooks to send commands to & receive updates from many kinds
of robots.

RoboCom has attributes like:
send_cmd()
receive_cmd()
as well as other attributes like robot_name etc which are created when an
instance of RoboCom is created. But a RoboCom object doesn't have any
attributes specific to the type of robot it will be communicating with.

Let's say the robots can be humanoid or canine.
A command that can be sent to the humanoid robot might look like:
1. send_cmd("left_leg.knee.raise 1.1")# 1.1 being a height in units of m
A command that can be sent to the canine robot might look like:
2. send_cmd("tail.wag 3") # where 3 = number of times to wag.

To be able to use RoboCom the user must currently look up the documentation
for the kind of robot they want to communicate with which is stored
somewhere in a text file and then enter it into the arguments of the
RoboCom methods which is a pain.

What I want to do is create a RoboCom-like object, specific to the type of
robot it will communicate with, which allows the user of that object to
discover which commands can & can't be sent to that type of robot.

The actual code executed to send the commands should be 1. & 2. above but
it would be more convenient to the user if they could type something like:
1A. humanoid_robocom1.left_leg.knee.raise.send(1.1)
2A. canine_robocom1.tail.wag.send(3)


Does that help explain things better?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Help creating new module which inherits existing class from another module.

2014-02-19 Thread Jonno
>
>
>
> The idea is that when you have a NewClass instance, calling
> "newobject.foo" will automatically call the descriptor's __getmethod__,
> passing it the class and instance. That descriptor will create and
> populate the FooClass instance, which does the real work.
>
> Descriptors are how methods, classmethods, staticmethods and properties
> work, so they are a fundamental, powerful way of implementing things like
> this.
>

Thanks for the suggestion Steven. Hopefully my last email on the subject
helped explain the intention a little better.
I'll look into descriptors some more. I had the feeling I needed to use one
(or more) of the following:
descriptors
decorators
metaclasses
None of which I know much about yet. I will do some reading & test out your
suggestion. Thanks again.
-- 
https://mail.python.org/mailman/listinfo/python-list


Convert string to array of floats

2008-06-19 Thread Jonno
Hi,

I'm very new to programming and python.

I need to convert a string like this:
'   0.906366 2.276152   0.01336980.773141
0.002836  -107.335197   0.01146286.846290\n'
to an array of floats.

Any pointers to the simplest way of doing this?

Thanks.

-- 
"If a theory can't produce hypotheses, can't be tested, can't be disproven,
and can't make predictions, then it's not a theory and certainly not
science." by spisska on Slashdot, Monday April 21, 2008
--
http://mail.python.org/mailman/listinfo/python-list

Re: Convert string to array of floats

2008-06-19 Thread Jonno
Brilliant! Thanks.

On Thu, Jun 19, 2008 at 10:00 AM, Cédric Lucantis <[EMAIL PROTECTED]> wrote:

> Le Thursday 19 June 2008 17:12:08 Jonno, vous avez écrit :
> > Hi,
> >
> > I'm very new to programming and python.
> >
> > I need to convert a string like this:
> > '   0.906366 2.276152   0.01336980.773141
> > 0.002836  -107.335197   0.01146286.846290\n'
> > to an array of floats.
> >
>
> string = '0.906366 2.276152   0.01336980.773141'
> array = [float(s) for s in string.split()]
>
> --
> Cédric Lucantis
> --
> http://mail.python.org/mailman/listinfo/python-list




-- 
"If a theory can't produce hypotheses, can't be tested, can't be disproven,
and can't make predictions, then it's not a theory and certainly not
science." by spisska on Slashdot, Monday April 21, 2008
--
http://mail.python.org/mailman/listinfo/python-list

Combination of element-wise and matrix multiplication

2008-07-01 Thread Jonno
I have two 2x2 arrays a & b.
a=(([c,d],[e,f]))
b=(([g,h],[i,j]))
Each of c,d,e,f,g,h,i,j are all 1xN arrays
I want to matrix multiply a & b to create a 2x2 array x, where the elements
of x are created with element-wise math and result in the following:
x[0,0] = c*g+d*i
x[0,1] = c*h+d*j
x[1,0] = e*g+f*i
x[1,1] = e*h+f*j

What is the simplest way to do this? I ended up doing the matrix
multiplication manually as above but this doesn't scale very nicely if a & b
become larger size.

Cheers,

Jonno.
-- 
"If a theory can't produce hypotheses, can't be tested, can't be disproven,
and can't make predictions, then it's not a theory and certainly not
science." by spisska on Slashdot, Monday April 21, 2008
--
http://mail.python.org/mailman/listinfo/python-list

Slice a list of lists?

2010-09-08 Thread Jonno
I know that I can index into a list of lists like this:
a=[[1,2,3],[4,5,6],[7,8,9]]
a[0][2]=3
a[2][0]=7

but when I try to use fancy indexing to select the first item in each
list I get:
a[0][:]=[1,2,3]
a[:][0]=[1,2,3]

Why is this and is there a way to select [1,4,7]?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Slice a list of lists?

2010-09-08 Thread Jonno
On Wed, Sep 8, 2010 at 2:11 PM, Benjamin Kaplan
 wrote:
> On Wed, Sep 8, 2010 at 2:55 PM, Jonno  wrote:
>> I know that I can index into a list of lists like this:
>> a=[[1,2,3],[4,5,6],[7,8,9]]
>> a[0][2]=3
>> a[2][0]=7
>>
>> but when I try to use fancy indexing to select the first item in each
>> list I get:
>> a[0][:]=[1,2,3]
>> a[:][0]=[1,2,3]
>>
>> Why is this and is there a way to select [1,4,7]?
>> --
>
> It's not fancy indexing. It's called taking a slice of the existing
> list. Look at it this way
> a[0] means take the first element of a. The first element of a is [1,2,3]
> a[0][:] means take all the elements in that first element of a. All
> the elements of [1,2,3] are [1,2,3].
>
> a[:] means take all the elements of a. So a[:] is [[1,2,3],[4,5,6],[7,8,9]].
> a[:][0] means take the first element of all the elements of a. The
> first element of a[:] is [1,2,3].
>
> There is no simple way to get [1,4,7] because it is just a list of
> lists and not an actual matrix. You have to extract the elements
> yourself.
>
> col = []
> for row in a:
>    col.append(row[0])
>
>
> You can do this in one line using a list comprehension:
> [ row[0] for row in a ]
>
Thanks! (to Andreas too). Totally makes sense now.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Slice a list of lists?

2010-09-08 Thread Jonno
On Wed, Sep 8, 2010 at 3:06 PM, Jonno  wrote:
> On Wed, Sep 8, 2010 at 2:11 PM, Benjamin Kaplan
>  wrote:
>> On Wed, Sep 8, 2010 at 2:55 PM, Jonno  wrote:
>>> I know that I can index into a list of lists like this:
>>> a=[[1,2,3],[4,5,6],[7,8,9]]
>>> a[0][2]=3
>>> a[2][0]=7
>>>
>>> but when I try to use fancy indexing to select the first item in each
>>> list I get:
>>> a[0][:]=[1,2,3]
>>> a[:][0]=[1,2,3]
>>>
>>> Why is this and is there a way to select [1,4,7]?
>>> --
>>
>> It's not fancy indexing. It's called taking a slice of the existing
>> list. Look at it this way
>> a[0] means take the first element of a. The first element of a is [1,2,3]
>> a[0][:] means take all the elements in that first element of a. All
>> the elements of [1,2,3] are [1,2,3].
>>
>> a[:] means take all the elements of a. So a[:] is [[1,2,3],[4,5,6],[7,8,9]].
>> a[:][0] means take the first element of all the elements of a. The
>> first element of a[:] is [1,2,3].
>>
>> There is no simple way to get [1,4,7] because it is just a list of
>> lists and not an actual matrix. You have to extract the elements
>> yourself.
>>
>> col = []
>> for row in a:
>>    col.append(row[0])
>>
>>
>> You can do this in one line using a list comprehension:
>> [ row[0] for row in a ]
>>
> Thanks! (to Andreas too). Totally makes sense now.
>

Now if I want to select the first item in every 2nd item of list a
(ie: [1,7]) can I use ::2 anywhere or do I need to create a list of
indices to use in a more complex for loop?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Slice a list of lists?

2010-09-08 Thread Jonno
On Wed, Sep 8, 2010 at 3:18 PM, Jonno  wrote:
> On Wed, Sep 8, 2010 at 3:06 PM, Jonno  wrote:
>> On Wed, Sep 8, 2010 at 2:11 PM, Benjamin Kaplan
>>  wrote:
>>> On Wed, Sep 8, 2010 at 2:55 PM, Jonno  wrote:
>>>> I know that I can index into a list of lists like this:
>>>> a=[[1,2,3],[4,5,6],[7,8,9]]
>>>> a[0][2]=3
>>>> a[2][0]=7
>>>>
>>>> but when I try to use fancy indexing to select the first item in each
>>>> list I get:
>>>> a[0][:]=[1,2,3]
>>>> a[:][0]=[1,2,3]
>>>>
>>>> Why is this and is there a way to select [1,4,7]?
>>>> --
>>>
>>> It's not fancy indexing. It's called taking a slice of the existing
>>> list. Look at it this way
>>> a[0] means take the first element of a. The first element of a is [1,2,3]
>>> a[0][:] means take all the elements in that first element of a. All
>>> the elements of [1,2,3] are [1,2,3].
>>>
>>> a[:] means take all the elements of a. So a[:] is [[1,2,3],[4,5,6],[7,8,9]].
>>> a[:][0] means take the first element of all the elements of a. The
>>> first element of a[:] is [1,2,3].
>>>
>>> There is no simple way to get [1,4,7] because it is just a list of
>>> lists and not an actual matrix. You have to extract the elements
>>> yourself.
>>>
>>> col = []
>>> for row in a:
>>>    col.append(row[0])
>>>
>>>
>>> You can do this in one line using a list comprehension:
>>> [ row[0] for row in a ]
>>>
>> Thanks! (to Andreas too). Totally makes sense now.
>>
>
> Now if I want to select the first item in every 2nd item of list a
> (ie: [1,7]) can I use ::2 anywhere or do I need to create a list of
> indices to use in a more complex for loop?
>
Seems like the simplest way would be:
[row[0] for row in a][::2]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Slice a list of lists?

2010-09-08 Thread Jonno
On Wed, Sep 8, 2010 at 3:44 PM, Andreas Waldenburger
 wrote:
> On Wed, 8 Sep 2010 15:23:35 -0500 Jonno  wrote:
>
>> On Wed, Sep 8, 2010 at 3:18 PM, Jonno  wrote:
>> [snip]
>> > Now if I want to select the first item in every 2nd item of list a
>> > (ie: [1,7]) can I use ::2 anywhere or do I need to create a list of
>> > indices to use in a more complex for loop?
>> >
>> Seems like the simplest way would be:
>> [row[0] for row in a][::2]
>
> What you're doing here is selecting every second item of the list of
> first items of the items in a, not the first items of every second item
> in a (head spinning yet?).
>
> If I'm not completely mindbent right now, these are logically
> equivalent, but not computationally.
>
> Compare
>    [row[0] for row in a][::2]  # (your Python code)
> with
>    [row[0] for row in a[::2]]  # (as per your description)
>
> The first one is more work for your computer, because it'll pick out
> the first elements of *all* of the items in a, whereas the second only
> picks out the first elements of every second item in a (which is only
> half the amount of "picks" compared to the former).
>
> I just thought I'd mention it. Because it might make a difference in
> one of your programs some day. And because I'm a pedant ;).

Thanks again. It is nice to know how to do things properly even though
in my case it probably won't make much difference.

Terry, I would have used numpy arrays (I actually use them later in
the code) except the lists in my list aren't all of the same length.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Slice a list of lists?

2010-09-09 Thread Jonno
On Thu, Sep 9, 2010 at 10:58 AM, Robert Kern  wrote:
> Please keep responses on the mailing list. However, I will reply below
> this one time.
>
> On Thu, Sep 9, 2010 at 10:35, Jonno  wrote:
>> On Wed, Sep 8, 2010 at 4:26 PM, Robert Kern  wrote:
>>> A motivating example:
>>>
>>> [~]
>>> |1> import numpy
>>>
>>> [~]
>>> |2> a = numpy.array([[1,2,3],[4,5,6],[7,8,9]])
>>>
>>> [~]
>>> |3> a
>>>
>>> array([[1, 2, 3],
>>>       [4, 5, 6],
>>>       [7, 8, 9]])
>>>
>>> [~]
>>> |4> a[0,2]
>>> 3
>>>
>>> [~]
>>> |5> a[2,0]
>>> 7
>>>
>>> [~]
>>> |6> a[0,:]
>>> array([1, 2, 3])
>>>
>>> [~]
>>> |7> a[:,0]
>>> array([1, 4, 7])
>>>
>>>
>>> --
>>> Robert Kern
>>
>> Thanks Robert. I'm actually much more familiar and comfortable with
>> arrays so I know how to slice them. The problem is that the data comes
>> from lines in a file which are not all of equal length.
>> My understanding is that I need to manipulate the data as lists until
>> I can get all the lists of equal length at which point I can make an
>> array from it.
>
> If you have ragged arrays, then you do not want to slice down columns
> to begin with. You might run into a row that does not have a value at
> that column. If you need to slice down columns (whether the object
> remains a list of lists or a numpy array), then you need to create a
> rectangular array regardless. Do that first, then I recommend using
> numpy arrays. Slicing down columns (probably) won't help you with
> that.

Understood. My data actually has 2 features which make it
non-rectangular in most circumstances. One is the extra element at the
start of every nth line (I now know how to remove that) and the second
is that some lines contain more data than others. My data is
essentially a m,m,n array but data from each row is shifted to the
next line when the line is full (4 columns). A new row then starts a
new line.

I want to pick out the data and assign it to an array but I'm
wondering whether I should first try to take the data on the extra
lines, remove it and stick it on the end of the original line (the
data from one "row" could extend onto multiple lines). Or whether I'm
better trying to create the array using loops to index my way through
the lines the way they already are laid out. Hope this makes sense.
-- 
http://mail.python.org/mailman/listinfo/python-list