Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Class or Dictionary?

Reply
Thread Tools

Class or Dictionary?

 
 
Martin De Kauwe
Guest
Posts: n/a
 
      02-12-2011
On Feb 12, 7:21*pm, Andrea Crotti <andrea.crott...@gmail.com> wrote:
> Il giorno 12/feb/2011, alle ore 00.45, Martin De Kauwe ha scritto:
>
> > Hi,

>
> > yes I read a .INI file using ConfigParser, just similar sections (in
> > my opinion) to make one object which i can then pass to different
> > classes. E.G.

>
> Ok then I suggest configobj, less buggy and much more powerful than ConfigParser:http://www.voidspace.org.uk/python/configobj.html
>
> (and included from python 2.7).
> In this way you can also simply just carry around that dictionary, and it will be correctly
> typed if you validate the input.


That is interesting however I am using python 2.6 so I guess I shall
have to stick as I am for the moment. I think the way I used it above
was quite straight forward? It seemed OK? What are the issues?
 
Reply With Quote
 
 
 
 
Martin De Kauwe
Guest
Posts: n/a
 
      02-12-2011
On Feb 12, 8:06*pm, Martin De Kauwe <mdeka...@gmail.com> wrote:
> On Feb 12, 7:21*pm, Andrea Crotti <andrea.crott...@gmail.com> wrote:
>
> > Il giorno 12/feb/2011, alle ore 00.45, Martin De Kauwe ha scritto:

>
> > > Hi,

>
> > > yes I read a .INI file using ConfigParser, just similar sections (in
> > > my opinion) to make one object which i can then pass to different
> > > classes. E.G.

>
> > Ok then I suggest configobj, less buggy and much more powerful than ConfigParser:http://www.voidspace.org.uk/python/configobj.html

>
> > (and included from python 2.7).
> > In this way you can also simply just carry around that dictionary, and it will be correctly
> > typed if you validate the input.

>
> That is interesting however I am using python 2.6 so I guess I shall
> have to stick as I am for the moment. I think the way I used it above
> was quite straight forward? It seemed OK? What are the issues?


Ignore that I have just tested it (it works in 2.6), much better!
Thanks!!
 
Reply With Quote
 
 
 
 
Terry Reedy
Guest
Posts: n/a
 
      02-12-2011
On 2/12/2011 1:24 AM, Martin De Kauwe wrote:

> The point of this posting was just to ask those that know, whether it
> was a bad idea to use the class object in the way I had or was that
> OK? And if I should have just used a dictionary, why?


Did you miss my suggestion to use a module rather than a class?
Modules do not have some of the disadvantages of classes listed by J. Nagle.

--
Terry Jan Reedy

 
Reply With Quote
 
Martin De Kauwe
Guest
Posts: n/a
 
      02-13-2011
On Feb 13, 5:12*am, Terry Reedy <tjre...@udel.edu> wrote:
> On 2/12/2011 1:24 AM, Martin De Kauwe wrote:
>
> > The point of this posting was just to ask those that know, whether it
> > was a bad idea to use the class object in the way I had or was that
> > OK? And if I should have just used a dictionary, why?

>
> Did you miss my suggestion to use a module rather than a class?
> Modules do not have some of the disadvantages of classes listed by J. Nagle.
>
> --
> Terry Jan Reedy


Hi, sorry I did. I just re-read it, could you provide an example?
Sorry i am having a major mind blank this morning (I think this is
obvious!?). And it would meet all of the criteria outlined by John
Nagle?
 
Reply With Quote
 
Dan Stromberg
Guest
Posts: n/a
 
      02-13-2011
On Fri, Feb 11, 2011 at 10:20 PM, Steven D'Aprano
<steve+> wrote:
> On Fri, 11 Feb 2011 21:47:32 -0800, Dan Stromberg wrote:
>
>> Did you have some sort of bad experience with pylint? *Do you resent the
>> 20 minutes it takes to set it up?

>
> If you read my post more carefully and less defensively, you'll see that
> nothing I said was *opposed* to the use of pylint, merely that pylint is
> not a substitute of a good, thorough test suite.


Actually, I mentioned the value of pylint. Then you asked me if my
unit tests were broken.

What would you infer from such a question?
 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      02-13-2011
On 2/12/2011 9:20 PM, Martin De Kauwe wrote:
> On Feb 13, 5:12 am, Terry Reedy<tjre...@udel.edu> wrote:
>> On 2/12/2011 1:24 AM, Martin De Kauwe wrote:
>>
>>> The point of this posting was just to ask those that know, whether it
>>> was a bad idea to use the class object in the way I had or was that
>>> OK? And if I should have just used a dictionary, why?

>>
>> Did you miss my suggestion to use a module rather than a class?
>> Modules do not have some of the disadvantages of classes listed by J. Nagle.
>>
>> --
>> Terry Jan Reedy

>
> Hi, sorry I did. I just re-read it, could you provide an example?


I am not sure what you are asking. tkinter.contants is one example.

> Sorry i am having a major mind blank this morning (I think this is
> obvious!?). And it would meet all of the criteria outlined by John
> Nagle?


A module will work fine if but only if you commit yourself to having all
keys be legal Python identifiers. But that is usually not a problem with
configuration values.


--
Terry Jan Reedy

 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      02-13-2011
On Sat, 12 Feb 2011 19:45:55 -0800, Dan Stromberg wrote:

> On Fri, Feb 11, 2011 at 10:20 PM, Steven D'Aprano
> <steve+> wrote:
>> On Fri, 11 Feb 2011 21:47:32 -0800, Dan Stromberg wrote:
>>
>>> Did you have some sort of bad experience with pylint? Â*Do you resent
>>> the 20 minutes it takes to set it up?

>>
>> If you read my post more carefully and less defensively, you'll see
>> that nothing I said was *opposed* to the use of pylint, merely that
>> pylint is not a substitute of a good, thorough test suite.

>
> Actually, I mentioned the value of pylint. Then you asked me if my unit
> tests were broken.
>
> What would you infer from such a question?



That I believed that you are using pylint as a substitute for unit tests,
instead of in conjunction with unit tests.



--
Steven
 
Reply With Quote
 
Martin De Kauwe
Guest
Posts: n/a
 
      02-13-2011
On Feb 13, 6:35*pm, Terry Reedy <tjre...@udel.edu> wrote:
> On 2/12/2011 9:20 PM, Martin De Kauwe wrote:
>
> > On Feb 13, 5:12 am, Terry Reedy<tjre...@udel.edu> *wrote:
> >> On 2/12/2011 1:24 AM, Martin De Kauwe wrote:

>
> >>> The point of this posting was just to ask those that know, whether it
> >>> was a bad idea to use the class object in the way I had or was that
> >>> OK? And if I should have just used a dictionary, why?

>
> >> Did you miss my suggestion to use a module rather than a class?
> >> Modules do not have some of the disadvantages of classes listed by J. Nagle.

>
> >> --
> >> Terry Jan Reedy

>
> > Hi, sorry I did. I just re-read it, could you provide an example?

>
> I am not sure what you are asking. tkinter.contants is one example.
>
> > Sorry i am having a major mind blank this morning (I think this is
> > obvious!?). And it would meet all of the criteria outlined by John
> > Nagle?

>
> A module will work fine if but only if you commit yourself to having all
> keys be legal Python identifiers. But that is usually not a problem with
> configuration values.
>
> --
> Terry Jan Reedy


I think I got it, did you mean something like this?


class Constants:

radius_of_earth = 6.37122E+6
days_as_yrs = 1.0 / 365.25
m2_as_ha = 1E-4 # metres squared as hectares
g_as_tonnes = 1E-6 # grammes as tonnes
kg_as_tonnes = 1E-3 # kg as tonnes
kg_as_g = 1E+3

def __init__(self):

self.radius_of_earth = self.__class__.radius_of_earth
self.days_as_yrs = self.__class__.days_as_yrs
self.m2_as_ha = self.__class__.m2_as_ha
self.g_as_tonnes = self.__class__.g_as_tonnes
self.kg_as_tonnes = self.__class__.kg_as_tonnes
self.kg_as_g = self.__class__.kg_as_g


usage something like

>>>from constants import Constants
>>>Constants.kg_as_g
>>>1000.0


Something similar for the params?

thanks

 
Reply With Quote
 
Martin De Kauwe
Guest
Posts: n/a
 
      02-14-2011
On Feb 14, 10:16*am, Martin De Kauwe <mdeka...@gmail.com> wrote:
> On Feb 13, 6:35*pm, Terry Reedy <tjre...@udel.edu> wrote:
>
>
>
>
>
> > On 2/12/2011 9:20 PM, Martin De Kauwe wrote:

>
> > > On Feb 13, 5:12 am, Terry Reedy<tjre...@udel.edu> *wrote:
> > >> On 2/12/2011 1:24 AM, Martin De Kauwe wrote:

>
> > >>> The point of this posting was just to ask those that know, whether it
> > >>> was a bad idea to use the class object in the way I had or was that
> > >>> OK? And if I should have just used a dictionary, why?

>
> > >> Did you miss my suggestion to use a module rather than a class?
> > >> Modules do not have some of the disadvantages of classes listed by J.. Nagle.

>
> > >> --
> > >> Terry Jan Reedy

>
> > > Hi, sorry I did. I just re-read it, could you provide an example?

>
> > I am not sure what you are asking. tkinter.contants is one example.

>
> > > Sorry i am having a major mind blank this morning (I think this is
> > > obvious!?). And it would meet all of the criteria outlined by John
> > > Nagle?

>
> > A module will work fine if but only if you commit yourself to having all
> > keys be legal Python identifiers. But that is usually not a problem with
> > configuration values.

>
> > --
> > Terry Jan Reedy

>
> I think I got it, did you mean something like this?
>
> class Constants:
>
> * * radius_of_earth = 6.37122E+6
> * * days_as_yrs = 1.0 / 365.25
> * * m2_as_ha = 1E-4 *# metres squared as hectares
> * * g_as_tonnes = 1E-6 *# grammes as tonnes
> * * kg_as_tonnes = 1E-3 *# kg as tonnes
> * * kg_as_g = 1E+3
>
> * * def __init__(self):
>
> * * * * self.radius_of_earth = self.__class__.radius_of_earth
> * * * * self.days_as_yrs = self.__class__.days_as_yrs
> * * * * self.m2_as_ha = self.__class__.m2_as_ha
> * * * * self.g_as_tonnes = self.__class__.g_as_tonnes
> * * * * self.kg_as_tonnes = self.__class__.kg_as_tonnes
> * * * * self.kg_as_g = self.__class__.kg_as_g
>
> usage something like
>
> >>>from constants import Constants
> >>>Constants.kg_as_g
> >>>1000.0

>
> Something similar for the params?
>
> thanks


And then I guess define some default parameters and then use configobj
to parse a .INI file. Then adjust those parameters change in the .INI
file?

e.g.

from parameters import DefaultParameters as params
# d -> comes from .INI file
p = params()
for key, value in d.iteritems():
# change anything read from file
p.__dict__[key] = value

?
 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      02-14-2011
On 2/13/2011 6:16 PM, Martin De Kauwe wrote:

> I think I got it, did you mean something like this?
>
>
> class Constants:
>
> radius_of_earth = 6.37122E+6
> days_as_yrs = 1.0 / 365.25
> m2_as_ha = 1E-4 # metres squared as hectares
> g_as_tonnes = 1E-6 # grammes as tonnes
> kg_as_tonnes = 1E-3 # kg as tonnes
> kg_as_g = 1E+3
>
> def __init__(self):
>
> self.radius_of_earth = self.__class__.radius_of_earth
> self.days_as_yrs = self.__class__.days_as_yrs
> self.m2_as_ha = self.__class__.m2_as_ha
> self.g_as_tonnes = self.__class__.g_as_tonnes
> self.kg_as_tonnes = self.__class__.kg_as_tonnes
> self.kg_as_g = self.__class__.kg_as_g
>
>
> usage something like
>
>>> >from constants import Constants
>>>> Constants.kg_as_g
>>>> 1000.0


No, simpler. No class statememt necessarily needed.

module constants
----------------

radius_of_earth = 6.37122E+6
days_as_yrs = 1.0 / 365.25
m2_as_ha = 1E-4 # metres squared as hectares
g_as_tonnes = 1E-6 # grammes as tonnes
kg_as_tonnes = 1E-3 # kg as tonnes
kg_as_g = 1E+3
# could also have code to load stuff from other files


usage
=====
>>> import constants
>>> constants.kg_as_g

1000.0

Real example
>>> import math
>>> math.pi, math.e

(3.141592653589793, 2.718281828459045)


--
Terry Jan Reedy

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Class A contains class B, class B points to class A Joseph Turian C++ 5 12-30-2005 03:24 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
A parameterized class (i.e. template class / class template) is not a class? christopher diggins C++ 16 05-04-2005 12:26 AM
Generic class literals - e.g,, Class<Map<String, Integer>>.class Purush Java 4 04-13-2005 08:40 PM
instanciate a class in a jar file with class.forname, while my main class is in another jar cyril Java 2 08-25-2004 06:55 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57