[Tutor] Dynamic variables names to objects of the same class ?

K Dhanvi dhanvik at gmx.net
Mon Aug 25 13:12:13 EDT 2003


Hi Thanks for the reply...
It was exactly what I was looking for :)..

The AddKey function is basically for doing some input value validation once
the values are read. This would atleast remove some of the overhead of
validating the inputs.

Thanks for ur help
Dhanvi
----- Original Message ----- 
From: "Lloyd Kvam" <pythontutor at venix.com>
To: "K Dhanvi" <dhanvik at gmx.net>
Cc: <tutor at python.org>
Sent: Saturday, August 23, 2003 10:30 PM
Subject: Re: [Tutor] Dynamic variables names to objects of the same class ?


> The python function setattr should do what you want.  I expect you
> would write something like:
>
> class ConfigReader:
> # stuff
> def ReadConfigFile(self):
> # stuff to get key_names and key_value pairs so that
> # key_name = "MAX_THREADS and key_value = some_integer
> setattr( self, key_name, key_value)
>
> Then your line:
>  >         noOfThreads = config.MAX_THREADS
> should work.
>
> This example code does not make any use of whatever might have been
> done in the AddKey method.
>
> HTH
>
>
> K Dhanvi wrote:
>
> > Dear friends,
> >
> >  I have a query about giving names to variables inside classes and thus
to
> > the objects of the classes..
> >
> >  I have a class called configReader which reads a configuration file and
> > then puts the configuration data into a dictionary at prsent. Before
reading
> > the configuration file, the config object is told what keys to expect
and
> > their datatpyes for simple santiy checks on the corresonding values.. At
> > present the key and values are stored in a dictionary.. I would like to
make
> > each key a property of the respective configReader object.
> >   for example =>
> >  If one of my keys in the configuration file is MAX_THREADS..
> > I would like my code to be as follows
>
> --------------------------------------------------------------------------
----------- 
> >
> >         import ConfigReader
> >         config = ConfigReader( "./config.file")
> >         config.AddKey( "MAX_THREADS", INT )
> >         config.ReadConfigFile()
> >
> >         noOfThreads = config.MAX_THREADS
>
> --------------------------------------------------------------------------
-----------
> >
> > The first 3 lines of the sample code have been completed.. I would like
to
> > add the functionality similar to the final line of the sample code. Each
> > object willl have different properties in itself depending upon the keys
it
> > was initialized with. i.e there could be another config object which
will
> > access another property "REGISTRATION_CURVE"..
> >  config1.REGISTRATION_CURVE.
> >
> > please lemme  know if theres any way this can be achieved in Python ?
> >
> > Thank you for your time and help :)
> >
> > Dhanvi
> >
> >
> > ------------------------------------------------------------------------
> >
> > _______________________________________________
> > Tutor maillist  -  Tutor at python.org
> > http://mail.python.org/mailman/listinfo/tutor
>
> -- 
> Lloyd Kvam
> Venix Corp.
> 1 Court Street, Suite 378
> Lebanon, NH 03766-1358
>
> voice: 603-443-6155
> fax: 801-459-9582
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>




More information about the Tutor mailing list