xml marshal of general (but non Python standard) class
Hello all, In my project, I have container classes holding lists of item classes. For example, a container class myLibrary might hold a list of item classes myNation and associated variables like myNation.name='USA' and myNation.continent='North America'. Bottom line, I was hoping to use this structure to marshal the classes to xml. However, I've got dozens of unique classes (all subclassing the container and item classes) with unique variables attached, and I do not want to write rules for each. I was looking at the source for generic in xml.marshal (from xml.marshal import generic) which will dump to xml any configuration of standard Python data types, for example, a tuple of dictionaries containing lists of strings. This source just writes a rule for each data type. Naively, I would hope that there'd be something where the marshaller could just look at my data class, see what variables were associated, and just go from there. I'm moderately experienced with Python, but by no means an expert, and I'm not an xml pro, either. Would this project (xml marshal of a new class) be worth my time? If so, what would be best way to proceed? Any other thoughts? >>> import xml.marshal >>> from xml.marshal import generic >>> generic.dumps(['thank you','comp.lang.python']) 'thank youcomp.lang.python -- http://mail.python.org/mailman/listinfo/python-list
Re: xml marshal of general (but non Python standard) class
Thank you Martin. I had not considered pickle, and I've done my research. However, I'm still having problems: Your foo class (for pickle and xml dumps) works fine for me. >>> f=Foo() >>> f.thanksTo='Martin' >>> f.howMany=100 >>> pickle.dumps(f) "(i__main__\nFoo\np0\n(dp1\nS'thanksTo'\np2\nS'Martin'\np3\nsS'howMany'\np4\nI100\nsb." But for my identifiedPeaks class (for instance), it has trouble. This class contains a list of "peak" classes IdentifiedPeaks.Peak... >>> from IdentifiedPeaks import IdentifiedPeaks >>> identifiedPeaks=IdentifiedPeaks() >>> identifiedPeaks.read(open('input/analysisReport.txt')) >>> pickle.dumps(identifiedPeaks) Traceback (most recent call last): File "", line 1, in ? File "/usr/lib/python2.3/pickle.py", line 1386, in dumps Pickler(file, protocol, bin).dump(obj) File "/usr/lib/python2.3/pickle.py", line 231, in dump self.save(obj) File "/usr/lib/python2.3/pickle.py", line 338, in save self.save_reduce(obj=obj, *rv) File "/usr/lib/python2.3/pickle.py", line 433, in save_reduce save(state) File "/usr/lib/python2.3/pickle.py", line 293, in save f(self, obj) # Call unbound method with explicit self File "/usr/lib/python2.3/pickle.py", line 663, in save_dict self._batch_setitems(obj.iteritems()) File "/usr/lib/python2.3/pickle.py", line 677, in _batch_setitems save(v) File "/usr/lib/python2.3/pickle.py", line 293, in save f(self, obj) # Call unbound method with explicit self File "/usr/lib/python2.3/pickle.py", line 614, in save_list self._batch_appends(iter(obj)) File "/usr/lib/python2.3/pickle.py", line 629, in _batch_appends save(x) File "/usr/lib/python2.3/pickle.py", line 338, in save self.save_reduce(obj=obj, *rv) File "/usr/lib/python2.3/pickle.py", line 415, in save_reduce save(args) File "/usr/lib/python2.3/pickle.py", line 293, in save f(self, obj) # Call unbound method with explicit self File "/usr/lib/python2.3/pickle.py", line 576, in save_tuple save(element) File "/usr/lib/python2.3/pickle.py", line 293, in save f(self, obj) # Call unbound method with explicit self File "/usr/lib/python2.3/pickle.py", line 760, in save_global raise PicklingError( pickle.PicklingError: Can't pickle : it's not found as IdentifiedPeaks.Peak xml.marshal has the problem I mentioned before... >>> xml.marshal.generic.dumps(identifiedPeaks) Traceback (most recent call last): File "", line 1, in ? File "/usr/lib/python2.3/site-packages/_xmlplus/marshal/generic.py", line 59, in dumps L = [self.PROLOGUE + self.DTD] + self.m_root(value, dict) File "/usr/lib/python2.3/site-packages/_xmlplus/marshal/generic.py", line 104, in m_root L = ['<%s>' % name] + self._marshal(value,dict) + ['' % name] File "/usr/lib/python2.3/site-packages/_xmlplus/marshal/generic.py", line 92, in _marshal return getattr(self, meth)(value, dict) AttributeError: Marshaller instance has no attribute 'm_IdentifiedPeaks' Help would be hugely appreciated. -- http://mail.python.org/mailman/listinfo/python-list
Gnuplot.py and, _by far_, the weirdest thing I've ever seen on my computer
I don't even know where to begin. This is just bizarre. I just picked up the Gnuplot.py module (a light interface to gnuplot commands) and was messing around with it today. I've got a tiny script, but it only works from the command line about half the time! In the python interpreter, 100%. Ipython, 100%. I'm not kidding. #!/bin/env python import Gnuplot g = Gnuplot.Gnuplot(debug=1) g.title('A simple example') g('set data style linespoints') g('set terminal png small color') g('set output "myGraph.png"') g.plot([[0,1.1], [1,5.8], [2,3.3], [3,100]]) Here's just one example -- it does not work, then it works. It seems totally random. It will work a few times, then it won't for a few times... bash-2.05b$ ./myGnu.py gnuplot> set title "A simple example" gnuplot> set data style linespoints gnuplot> set terminal png small color gnuplot> set output "myGraph.png" gnuplot> plot '/tmp/tmp5LXAow' notitle gnuplot> plot '/tmp/tmp5LXAow' notitle ^ can't read data file "/tmp/tmp5LXAow" line 0: util.c: No such file or directory bash-2.05b$ ./myGnu.py gnuplot> set title "A simple example" gnuplot> set data style linespoints gnuplot> set terminal png small color gnuplot> set output "myGraph.png" gnuplot> plot '/tmp/tmpHMTkpL' notitle (and it makes the graph image just fine) I mean what the hell is going on? My permissions on /tmp are wide open (drwxrwxrwt). It does the same thing when I run as root. And it _always_ works when I use the interpreter or interactive python. Any clues would be greatly appreciated. I'm baffled. -- http://mail.python.org/mailman/listinfo/python-list
Re: Gnuplot.py and, _by far_, the weirdest thing I've ever seen on my computer
Thanks for all the help, guys! The affected computer is at home, but I'll address all these issues before I post again with my fix. James: It's on my personal box, and I've got reiserfs. David: OS: GNU/Linux 2.4 (Gentoo) Python 2.3 GNUplot 1.6 I just checked the Gentoo Portage tree and 1.7 is listed "~x86" (unstable), so unwittingly I had gotten 1.6 when I emerged gnuplot-py. Definitely, the first thing I'm going to do is upgrade to 1.7. Before I do, though, I'll try to see if this works: Gnuplot.GnuplotOpts.prefer_fifo_data = 0 Gnuplot.GnuplotOpts.prefer_inline_data = 1 It sounds like Paul had the same problem. I'll also try the "binary = 1" before I upgrade. Fernando, that's a creative solution, I'll try it as well... while 1: if os.path.isfile(your_plot_filename): break time.sleep(1) Thanks guys! -- http://mail.python.org/mailman/listinfo/python-list
Re: Gnuplot.py and, _by far_, the weirdest thing I've ever seen on my computer
Ok yall, here's my summary. Thanks again for all the help. Most importantly, upgrading to gnuplot-py 1.7 fixes this problem. In gnuplot-py 1.6, this is the bottom line... Does not fix: Gnuplot.GnuplotOpts.prefer_fifo_data = 0 Fixes: Gnuplot.GnuplotOpts.prefer_inline_data = 1 Fixes: (placed between setting output and plot) while 1: if os.path.isfile("myGraph.png"): break time.sleep(1) As for matplotlib, I checked it out. Looks amazing! I really, really like what demos I tried. HOWEVER, I could not find a good way to do smoothing. I like the gnuplot bezier smoothing. This wouldn't be the hardest thing in the world to code, but I was a little disappointed. If yall have any ideas, bring em on! :) -- http://mail.python.org/mailman/listinfo/python-list
within a class, redefining self with pickled file
def unpickle(self): self = pickle.load(open(self.getFilePath('pickle'))) This evidently does not work. Any idea why? I'd like to be able to replace a lightly populated class (enough to identify the pickled version correctly) with it's full version that's sitting pickled in a file. As of right now, I need to just return self and redefine the class. Thanks! -- http://mail.python.org/mailman/listinfo/python-list