[Numpy-discussion] save a matrix
Charles R Harris
charlesr.harris at gmail.com
Fri Dec 1 01:02:19 CST 2006
On 11/30/06, Charles R Harris <charlesr.harris at gmail.com> wrote:
>
>
>
> On 11/30/06, Keith Goodman <kwgoodman at gmail.com> wrote:
> >
> > On 11/30/06, Charles R Harris <charlesr.harris at gmail.com> wrote:
> > >
> > > It is also possible to put the variables of interest in a dictionary,
> > then
> > > pickle the dictionary. That way you can also store the variable names.
> >
> > >
> > > In [27]: f = open(' dump.pkl','w')
> > >
> > > In [28]: pickle.dump( {'a':a,'b':b}, f)
> > >
> > > In [29]: f.close()
> > >
> > > In [30]: f = open('dump.pkl','r')
> > >
> > > In [31]: mystuff = pickle.load(f)
> > >
> > > In [32]: f.close()
> > >
> > > In [34]: mystuff
> > > Out[34]:
> > > {'a': matrix([[2, 3],
> > > [4, 5]]), 'b': matrix([[2, 3],
> > > [4, 5]])}
> >
> > I think I could use that to write a function savematrix(filename, a, b,
> > c,...)
> >
> > Is there a way to write a loadmatrix(filename) that doesn't return
> > anything but makes the matrices a, b, c, ... available?
>
>
> I think there is, that is why I mentioned the saving the environment
> thingee. IIRC, I saw code for something like that a couple of years back but
> I don't recall the details. Maybe something like:
>
> In [80]: globals()['x'] = [1,2]
>
> In [81]: x
> Out[81]: [1, 2]
>
> Then you just have to merge the pickled dictionary with globals(). Like
> this:
>
> >>> globals().update(mystuff)
>
> where mystuff is the dictionary where you have your stuff. This could
> probably also go something like
>
> >>> globals().update(load(f))
>
> where f contains the pickled dictionary.
>
You could probably dump the entire environment from a subroutine,
cPickle.dump(globals(), f), which might be a good way to save everything
off, but not very efficient.
Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20061201/e230ea92/attachment.html
More information about the Numpy-discussion
mailing list