Hi,
On Jan 21, 9:22 am, Jonatan Hjul <[email protected]> wrote:
> I have been migrating from MATLAB over the last months, and spyder has
> really been beneficial in that process. It is really the best IDE for
> python!
Thanks for your support!
> I have noticed some strange behaviour of the variable editor. I have
> used the following class which emulates the struct data type in
> matlab. It's basically a dictionary which is easier to access than the
> normal one in python. When I create a struct and open it in the
> variable inspector it works fine, and if I close it by the x then
> everything is ok, but if i use ok, then it's suddenly converted to a
> dict. Is this a bug or a feature - I find it a bit strange that the
> variable inspector is able to change the data's type and not only it's
> content.
>
> class struct(dict):
> '''Matlab style struct. Wow I have been missing this'''
> def __getattr__(self, attr):
> return self.get(attr, None)
> __setattr__= dict.__setitem__
> __delattr__= dict.__delitem__
That is "normal".
Actually the variable explorer supports only the following types:
- builtin types: dict, list, tuple, float, int, bool, str, unicode
- datetime objects
- NumPy arrays
- PIL images
FYI, the editor widget which is behind the Variable Explorer plugin is
the DictEditor:
http://code.google.com/p/spyderlib/source/browse/spyderlib/widgets/dicteditor.py
(which may be used outside Spyder)
Outside Spyder, you may also use 'oedit' (GUI-based object editor
function):
from spyderlib.widgets.objecteditor import oedit
oedit(obj) # edit all kinds of objects
(but that's another story)
So, regarding your dictionnary conversion issue, because your custom
object is deriving from 'dict', the DictEditor thinks that it's a
dictionnary, so it treats like it. And the problem is that the
DictEditor must work on a copy of the object, otherwise clicking on
the "Cancel" button would not allow to cancel changes. The copy of the
object is obtained (for a dictionnary) by calling the 'copy' method,
here:
http://code.google.com/p/spyderlib/source/browse/spyderlib/widgets/dicteditor.py#1214
So what you need to do in your custom object is to reimplement the
'copy' method to return a real copy of your object and not a
dictionnary.
For example, you write the following (not tested):
class struct(dict):
'''Matlab style struct. Wow I have been missing this'''
def __getattr__(self, attr):
return self.get(attr, None)
__setattr__= dict.__setitem__
__delattr__= dict.__delitem__
def copy(self):
return struct(dict.copy(self))
> In general it would be a nice extension if the variable editor could
> support more general data types as objects.
This feature request has already been added to the 'todo list':
http://code.google.com/p/spyderlib/issues/detail?id=291
Cheers,
Pierre
--
You received this message because you are subscribed to the Google Groups
"spyder" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/spyderlib?hl=en.