[Numpy-discussion] how to use the name of a ndarray as a string
Warren Weckesser
warren.weckesser@enthought....
Fri Nov 18 10:48:28 CST 2011
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-name-of-an-object
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
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20111118/bab12c55/attachment.html
More information about the NumPy-Discussion
mailing list