Re: [Numpy-discussion] how to use the name of a ndarray as a string
![](https://secure.gravatar.com/avatar/4f98becee709da3e88f193e87a24c18b.jpg?s=120&d=mm&r=g)
Thanks to all people for this very nice discussions. the solutions are more that what I want!! and help me to clarify some concepts, and really begin to use class as a beginner :) I would say either Olivier or denis's solution can solve my problem completely. cheers, Chao 2011/11/18 denis <denis-bz-gg@t-online.de>
as Olivier and Chris say, use a dict from the start. To make this a bit easier / more readable, use this tiny class Dotdict:
class Dotdict( dict ): """ d = dotdict(), d.key same as d["key"] """ # aka Bag, Bunch # cPickle.dumps( d, -1 ): PicklingError in 2.6.4 def __init__(self, **kwargs): dict.__init__(self, kwargs) self.__dict__ = self
d = Dotdict() d.a = np.arange(10) d.b = np.ones((3,4)) d.b[1,2] += 1 print d.b
One can also save all the d.* arrays like this:
def savedotdict( d, prefix="d.", save=np.savetxt, **kwargs ): """ save all d.* to files """ for name, val in d.items(): out = prefix + name if isinstance( val, basestring ): with open( out, "w" ) as f: f.write( val.rstrip() ) f.write( "\n" ) else: print "saving %s to %s" % (val.shape, out) save( out, val, **kwargs )
d.info = "2011-11-18 nov kilroy" # From() savedotdict( d, "d.", fmt="%.3g" )
cheers -- denis
(If you use this, could you post it to the numpy-discussion group please ? It rejects me, grr.)
On Nov 10, 11:17 am, Chao YUE <chaoyue...@gmail.com> wrote:
Hi,
Does anyone know how I can quickly use the name of a ndarray as a string?
-- *********************************************************************************** Chao YUE Laboratoire des Sciences du Climat et de l'Environnement (LSCE-IPSL) UMR 1572 CEA-CNRS-UVSQ Batiment 712 - Pe 119 91191 GIF Sur YVETTE Cedex Tel: (33) 01 69 08 29 02; Fax:01.69.08.77.16 ************************************************************************************
![](https://secure.gravatar.com/avatar/b0f62d137f9ea1d0b6cc4e7e6f61b119.jpg?s=120&d=mm&r=g)
On Fri, Nov 18, 2011 at 10:40 AM, Chao YUE <chaoyuejoy@gmail.com> wrote:
Thanks to all people for this very nice discussions.
the solutions are more that what I want!! and help me to clarify some concepts, and really begin to use class as a beginner :)
FYI: Just a day or so ago, I stumbled across the same question in the Python FAQ: http://docs.python.org/faq/programming.html#how-can-my-code-discover-the-nam... Warren
I would say either Olivier or denis's solution can solve my problem completely.
cheers,
Chao
2011/11/18 denis <denis-bz-gg@t-online.de>
as Olivier and Chris say, use a dict from the start. To make this a bit easier / more readable, use this tiny class Dotdict:
class Dotdict( dict ): """ d = dotdict(), d.key same as d["key"] """ # aka Bag, Bunch # cPickle.dumps( d, -1 ): PicklingError in 2.6.4 def __init__(self, **kwargs): dict.__init__(self, kwargs) self.__dict__ = self
d = Dotdict() d.a = np.arange(10) d.b = np.ones((3,4)) d.b[1,2] += 1 print d.b
One can also save all the d.* arrays like this:
def savedotdict( d, prefix="d.", save=np.savetxt, **kwargs ): """ save all d.* to files """ for name, val in d.items(): out = prefix + name if isinstance( val, basestring ): with open( out, "w" ) as f: f.write( val.rstrip() ) f.write( "\n" ) else: print "saving %s to %s" % (val.shape, out) save( out, val, **kwargs )
d.info = "2011-11-18 nov kilroy" # From() savedotdict( d, "d.", fmt="%.3g" )
cheers -- denis
(If you use this, could you post it to the numpy-discussion group please ? It rejects me, grr.)
On Nov 10, 11:17 am, Chao YUE <chaoyue...@gmail.com> wrote:
Hi,
Does anyone know how I can quickly use the name of a ndarray as a string?
--
*********************************************************************************** Chao YUE Laboratoire des Sciences du Climat et de l'Environnement (LSCE-IPSL) UMR 1572 CEA-CNRS-UVSQ Batiment 712 - Pe 119 91191 GIF Sur YVETTE Cedex Tel: (33) 01 69 08 29 02; Fax:01.69.08.77.16
************************************************************************************
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
participants (2)
-
Chao YUE
-
Warren Weckesser