473,396 Members | 2,038 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How can I use __setitem__ method of dict object?

Please excuse me if this is obvious to others, but I can't figure it
out. I am subclassing dict, but want to prevent direct changing of
some key/value pairs. For this I thought I should override the
__setitem__ method as such:
class xs(dict):
"""
XS is a container object to hold information about cross sections.
"""

def __new__(cls, xS=1.0, xF=1.0, xG=1.0, nu=1.0, debug=0):
"""
"""
x = {}
x['xS'] = xS
x['xF'] = xF
x['nu'] = nu
x['xG'] = xG
x['xA'] = x['xG'] + x['xF']
x['xT'] = x['xA'] + x['xS']

return x

def __setitem__(self, key, value):
"""
I have overridden this method to prevent setting xT or xA
outside the
class.
"""
print "I am in __setitem__"
if key == 'xT':
raise AttributeError("""Can't change xT. Please change,
xF, xS, or xG""")
But I can't even get __setitem__ to run. Example:
Python 2.5 (r25:51918, Sep 19 2006, 08:49:13)
[GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>import xs
cs = xs.xs()
cs
{'xA': 2.0, 'xF': 1.0, 'xG': 1.0, 'xS': 1.0, 'nu': 1.0, 'xT': 3.0}
>>cs['xT'] = 3.1415
cs
{'xA': 2.0, 'xF': 1.0, 'xG': 1.0, 'xS': 1.0, 'nu': 1.0, 'xT':
3.1415000000000002}
Is this what the __setitem__ method is for? If not, how can I do what
I want to do?
Thanks in advance,
Jeremy

Feb 6 '07 #1
12 9698
jeremito wrote:
Please excuse me if this is obvious to others, but I can't figure it
out. I am subclassing dict, but want to prevent direct changing of
some key/value pairs. For this I thought I should override the
__setitem__ method as such:
class xs(dict):
"""
XS is a container object to hold information about cross sections.
"""

def __new__(cls, xS=1.0, xF=1.0, xG=1.0, nu=1.0, debug=0):
"""
"""
x = {}
x['xS'] = xS
x['xF'] = xF
x['nu'] = nu
x['xG'] = xG
x['xA'] = x['xG'] + x['xF']
x['xT'] = x['xA'] + x['xS']

return x

def __setitem__(self, key, value):
"""
I have overridden this method to prevent setting xT or xA
outside the
class.
"""
print "I am in __setitem__"
if key == 'xT':
raise AttributeError("""Can't change xT. Please change,
xF, xS, or xG""")
But I can't even get __setitem__ to run. Example:
Python 2.5 (r25:51918, Sep 19 2006, 08:49:13)
[GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>import xs
cs = xs.xs()
cs
{'xA': 2.0, 'xF': 1.0, 'xG': 1.0, 'xS': 1.0, 'nu': 1.0, 'xT': 3.0}
>>>cs['xT'] = 3.1415
cs
{'xA': 2.0, 'xF': 1.0, 'xG': 1.0, 'xS': 1.0, 'nu': 1.0, 'xT':
3.1415000000000002}
Is this what the __setitem__ method is for? If not, how can I do what
I want to do?
>>class d(dict):
... def __setitem__(self, k, v):
... print "Setting", k
... dict.__setitem__(self, k, v)
...
>>dd = d()
dd['steve'] = 'holden'
Setting steve
>>dd['steve']
'holden'
>>>
I believe the problem is that your __new__ method does not return an
object of type xs but a dict, so it does not inherit the __getitem__
method from xs but instead from dict.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Blog of Note: http://holdenweb.blogspot.com
See you at PyCon? http://us.pycon.org/TX2007

Feb 6 '07 #2
On 6 fév, 16:23, "jeremito" <jerem...@gmail.comwrote:
Please excuse me if this is obvious to others, but I can't figure it
out. I am subclassing dict, but want to prevent direct changing of
some key/value pairs. For this I thought I should override the
__setitem__ method as such:

class xs(dict):
"""
XS is a container object to hold information about cross sections.
"""

def __new__(cls, xS=1.0, xF=1.0, xG=1.0, nu=1.0, debug=0):
"""
"""
x = {}
x['xS'] = xS
x['xF'] = xF
x['nu'] = nu
x['xG'] = xG
x['xA'] = x['xG'] + x['xF']
x['xT'] = x['xA'] + x['xS']

return x
replace this with:
def __init__(self, xS=1.0, xF=1.0, xG=1.0, nu=1.0, debug=0):
dict.__init__(
self,
xS=xS,
xF=xF,
xG=xG,
nu=nu,
xA=xG + xF,
xT=xG + xF + xS
)
def __setitem__(self, key, value):
"""
I have overridden this method to prevent setting xT or xA
outside the
class.
"""
print "I am in __setitem__"
if key == 'xT':
raise AttributeError(
"Can't change xT. Please change, xF, xS, or xG"
)
dict.__setitem__(self, key, value)
But I can't even get __setitem__ to run.
of course, since your __new__ method returns a dict instance, not a xs
instance...
There are very few cases where you really need to override the __new__
method.
Example:
Python 2.5 (r25:51918, Sep 19 2006, 08:49:13)
[GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin
Type "help", "copyright", "credits" or "license" for more information.>>>import xs
>cs = xs.xs()
cs

{'xA': 2.0, 'xF': 1.0, 'xG': 1.0, 'xS': 1.0, 'nu': 1.0, 'xT': 3.0}>>cs['xT'] = 3.1415
>cs

{'xA': 2.0, 'xF': 1.0, 'xG': 1.0, 'xS': 1.0, 'nu': 1.0, 'xT':
3.1415000000000002}

Is this what the __setitem__ method is for?
Yes. But note that you you need to manually call the superclass's
overriden method - unless you
really want to replace it with your own, which is obviously not the
case here...

Note that if someone manually changes the values of xG, xF, or xS, the
computed values of xA and/or xT
won't reflect this change. Is that what you want ?

Finally, and if I may ask, what is your use-case for subclassing
dict ? You don't need this to implement a dict-like object,
and it might be simpler in your case to write an ordinary class, then
add support for the required subset of the dict interface.

My 2 cents...

Feb 6 '07 #3
On Feb 6, 10:59 am, "bruno.desthuilli...@gmail.com"
<bruno.desthuilli...@gmail.comwrote:
On 6 fév, 16:23, "jeremito" <jerem...@gmail.comwrote:
Please excuse me if this is obvious to others, but I can't figure it
out. I am subclassing dict, but want to prevent direct changing of
some key/value pairs. For this I thought I should override the
__setitem__ method as such:
class xs(dict):
"""
XS is a container object to hold information about cross sections.
"""
def __new__(cls, xS=1.0, xF=1.0, xG=1.0, nu=1.0, debug=0):
"""
"""
x = {}
x['xS'] = xS
x['xF'] = xF
x['nu'] = nu
x['xG'] = xG
x['xA'] = x['xG'] + x['xF']
x['xT'] = x['xA'] + x['xS']
return x

replace this with:
def __init__(self, xS=1.0, xF=1.0, xG=1.0, nu=1.0, debug=0):
dict.__init__(
self,
xS=xS,
xF=xF,
xG=xG,
nu=nu,
xA=xG + xF,
xT=xG + xF + xS
)
def __setitem__(self, key, value):
"""
I have overridden this method to prevent setting xT or xA
outside the
class.
"""
print "I am in __setitem__"
if key == 'xT':
raise AttributeError(

"Can't change xT. Please change, xF, xS, or xG"
)
dict.__setitem__(self, key, value)
But I can't even get __setitem__ to run.

of course, since your __new__ method returns a dict instance, not a xs
instance...
There are very few cases where you really need to override the __new__
method.
The reason I create my object with __new__ instead of __init__ is
because when I use __init__ when a value is set it calls __setitem__.
This is what I want to happen, but not inside of __init__. Does this
make sense? I'm sure there is a better/more pythonic way to do this,
but I'm unsure of what it is. Can someone show me an example of how
this should work?

>
Example:
Python 2.5 (r25:51918, Sep 19 2006, 08:49:13)
[GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin
Type "help", "copyright", "credits" or "license" for more information.>>import xs
>>cs = xs.xs()
>>cs
{'xA': 2.0, 'xF': 1.0, 'xG': 1.0, 'xS': 1.0, 'nu': 1.0, 'xT': 3.0}>>cs['xT'] = 3.1415
>>cs
{'xA': 2.0, 'xF': 1.0, 'xG': 1.0, 'xS': 1.0, 'nu': 1.0, 'xT':
3.1415000000000002}
Is this what the __setitem__ method is for?

Yes. But note that you you need to manually call the superclass's
overriden method - unless you
really want to replace it with your own, which is obviously not the
case here...

Note that if someone manually changes the values of xG, xF, or xS, the
computed values of xA and/or xT
won't reflect this change. Is that what you want ?
Eventually (when I figure out how to use __setitem__) I will change
what happens when xG, xF, or xS are changed so that it also changes xA
and xT.
Finally, and if I may ask, what is your use-case for subclassing
dict ? You don't need this to implement a dict-like object,
and it might be simpler in your case to write an ordinary class, then
add support for the required subset of the dict interface.
Eventually I am going to add other features to my class (as I have
mentioned) so I can't simply use a dict object.
>
My 2 cents...
Thanks again,
Jeremy

Feb 6 '07 #4
"jeremito" <je******@gmail.comescribió en el mensaje
news:11**********************@k78g2000cwa.googlegr oups.com...
Please excuse me if this is obvious to others, but I can't figure it
out. I am subclassing dict, but want to prevent direct changing of
some key/value pairs. For this I thought I should override the
__setitem__ method as such:
if key == 'xT':
raise AttributeError("""Can't change xT. Please change,
xF, xS, or xG""")
Why using a dictionary? I'd use a simple class with properties:

pyclass Xs(object): # class names should be Uppercase
.... def __init__(self, xS=1.0, xF=1.0, xG=1.0, nu=1.0, debug=0):
.... self.xS = xS
.... self.xF = xF
.... self.nu = nu
.... self.xG = xG
.... xA = property(fget=lambda self: self.xG + self.xF)
.... xT = property(fget=lambda self: self.xA + self.xS)
....
pyxs = Xs(1.0, 0.95, 0.80, 0.70)
pyprint xs.xG
0.8
pyprint xs.xA
1.75
pyprint xs.xT
2.75
pyxs.xG = 0.5
pyprint xs.xA
1.45
pyprint xs.xT
2.45
pyxs.xA = 1.5
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: can't set attribute
pyxs.xT = 1.2
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: can't set attribute
py>

--
Gabriel Genellina
Feb 6 '07 #5
jeremito a écrit :
On Feb 6, 10:59 am, "bruno.desthuilli...@gmail.com"
<bruno.desthuilli...@gmail.comwrote:
>>On 6 fév, 16:23, "jeremito" <jerem...@gmail.comwrote:
(snip)
>>>But I can't even get __setitem__ to run.

of course, since your __new__ method returns a dict instance, not a xs
instance...
There are very few cases where you really need to override the __new__
method.


The reason I create my object with __new__ instead of __init__ is
because when I use __init__ when a value is set it calls __setitem__.
This is what I want to happen, but not inside of __init__. Does this
make sense?
It would make sens - if you couldn't call dict.__setitem__ directly.
I'm sure there is a better/more pythonic way to do this,
but I'm unsure of what it is. Can someone show me an example of how
this should work?

(snip)
>>>Is this what the __setitem__ method is for?

Yes. But note that you you need to manually call the superclass's
overriden method - unless you
really want to replace it with your own, which is obviously not the
case here...

Note that if someone manually changes the values of xG, xF, or xS, the
computed values of xA and/or xT
won't reflect this change. Is that what you want ?


Eventually (when I figure out how to use __setitem__) I will change
what happens when xG, xF, or xS are changed so that it also changes xA
and xT.
Which is not the best way to go IMHO. Unless the computation is very
intensive (which doesn't seem to be the case here) or it's heavily used
in big loops *and* the perfs are not good enough, it's better to
recompute on the fly at read time. And if one of the above cases arises,
then it will be time to use memoization (ie: cache the result of
computation, invalidating the cache when needed).
>
>>Finally, and if I may ask, what is your use-case for subclassing
dict ? You don't need this to implement a dict-like object,
and it might be simpler in your case to write an ordinary class, then
add support for the required subset of the dict interface.


Eventually I am going to add other features to my class (as I have
mentioned) so I can't simply use a dict object.
I already understood this. My question is : why do you want to
*subclass* dict. In Python, inheritence is only about implementation,
it's *not* needed for polymorphism to work. So you don't have to
subclass dict to have an object behaving (more or less, that's up to
you) like a dict.

Here's an alternative implementation, so you get the idea. Note that it
behaves mostly like a dict (well, not totally, but since we don't know
which subset of the dict interface you need...), but also like a
'standard' object, so you can use either cs.['xT'] or cs.xT with the
same result.

class Xs(dict):
"""
Xs is a container object to hold information about cross sections.
"""
_computedkeys = 'xA', 'xT'

def __init__(self, xS=1.0, xF=1.0, xG=1.0, nu=1.0, debug=0):
self.xS = xS
self.xF = xF
self.xG = xG
self.nu = nu

# xA and xT as properties (AKA computed attributes)
def _get_xA(self):
return self.xG + self.xF
def _set_xA(self, dummy):
raise AttributeError(
"%s.xA is read-only" % self.__class__.__name__
)
xA = property(fset=_set_xA, fget=_get_xA)

def _get_xT(self):
return self.xA + self.xS
def _set_xT(self, dummy):
raise AttributeError(
"%s.xT is read-only" % self.__class__.__name__
)
xT = property(fset=_set_xT, fget=_get_xT)

# dict interface support, to be extended if needed
def __setitem__(self, key, value):
setattr(self, key, value)

def __getitem__(self, key):
return getattr(self, key)

def keys(self):
return self.__dict__.keys() + list(self._computedkeys)

def values(self):
return self.__dict__.values() \
+ [getattr(self, key) for key in self._computedkeys]

def items(self):
return zip(self.keys(), self.values())

def __iter__(self):
for k in self.keys():
yield k
raise StopIteration

def __contains__(self, key):
return key in self.keys()

def __repr__(self):
return repr(dict(self.items()))

Feb 6 '07 #6
On Feb 6, 2:36 pm, Bruno Desthuilliers
<bdesth.quelquech...@free.quelquepart.frwrote:
jeremito a écrit :
On Feb 6, 10:59 am, "bruno.desthuilli...@gmail.com"
<bruno.desthuilli...@gmail.comwrote:
>On 6 fév, 16:23, "jeremito" <jerem...@gmail.comwrote:

(snip)
>>But I can't even get __setitem__ to run.
>of course, since your __new__ method returns a dict instance, not a xs
instance...
There are very few cases where you really need to override the __new__
method.
The reason I create my object with __new__ instead of __init__ is
because when I use __init__ when a value is set it calls __setitem__.
This is what I want to happen, but not inside of __init__. Does this
make sense?

It would make sens - if you couldn't call dict.__setitem__ directly.


I'm sure there is a better/more pythonic way to do this,
but I'm unsure of what it is. Can someone show me an example of how
this should work?

(snip)
>>Is this what the __setitem__ method is for?
>Yes. But note that you you need to manually call the superclass's
overriden method - unless you
really want to replace it with your own, which is obviously not the
case here...
>Note that if someone manually changes the values of xG, xF, or xS, the
computed values of xA and/or xT
won't reflect this change. Is that what you want ?
Eventually (when I figure out how to use __setitem__) I will change
what happens when xG, xF, or xS are changed so that it also changes xA
and xT.

Which is not the best way to go IMHO. Unless the computation is very
intensive (which doesn't seem to be the case here) or it's heavily used
in big loops *and* the perfs are not good enough, it's better to
recompute on the fly at read time. And if one of the above cases arises,
then it will be time to use memoization (ie: cache the result of
computation, invalidating the cache when needed).
>Finally, and if I may ask, what is your use-case for subclassing
dict ? You don't need this to implement a dict-like object,
and it might be simpler in your case to write an ordinary class, then
add support for the required subset of the dict interface.
Eventually I am going to add other features to my class (as I have
mentioned) so I can't simply use a dict object.

I already understood this. My question is : why do you want to
*subclass* dict. In Python, inheritence is only about implementation,
it's *not* needed for polymorphism to work. So you don't have to
subclass dict to have an object behaving (more or less, that's up to
you) like a dict.

Here's an alternative implementation, so you get the idea. Note that it
behaves mostly like a dict (well, not totally, but since we don't know
which subset of the dict interface you need...), but also like a
'standard' object, so you can use either cs.['xT'] or cs.xT with the
same result.

class Xs(dict):
"""
Xs is a container object to hold information about cross sections.
"""
_computedkeys = 'xA', 'xT'

def __init__(self, xS=1.0, xF=1.0, xG=1.0, nu=1.0, debug=0):
self.xS = xS
self.xF = xF
self.xG = xG
self.nu = nu

# xA and xT as properties (AKA computed attributes)
def _get_xA(self):
return self.xG + self.xF
def _set_xA(self, dummy):
raise AttributeError(
"%s.xA is read-only" % self.__class__.__name__
)
xA = property(fset=_set_xA, fget=_get_xA)

def _get_xT(self):
return self.xA + self.xS
def _set_xT(self, dummy):
raise AttributeError(
"%s.xT is read-only" % self.__class__.__name__
)
xT = property(fset=_set_xT, fget=_get_xT)

# dict interface support, to be extended if needed
def __setitem__(self, key, value):
setattr(self, key, value)

def __getitem__(self, key):
return getattr(self, key)

def keys(self):
return self.__dict__.keys() + list(self._computedkeys)

def values(self):
return self.__dict__.values() \
+ [getattr(self, key) for key in self._computedkeys]

def items(self):
return zip(self.keys(), self.values())

def __iter__(self):
for k in self.keys():
yield k
raise StopIteration

def __contains__(self, key):
return key in self.keys()

def __repr__(self):
return repr(dict(self.items()))
Thanks a lot for your help. I think what you have written is much
better than what I could have come up with on my own. I guess I just
need more experience.
Thanks,
Jeremy

Feb 6 '07 #7
jeremito a écrit :
On Feb 6, 2:36 pm, Bruno Desthuilliers
<bdesth.quelquech...@free.quelquepart.frwrote:
(snip)
>>Here's an alternative implementation, so you get the idea.

class Xs(dict):
oops ! I meant:
class Xs(object):

of course...

(snip)
I guess I just
need more experience.
Possibly - but not only. You may want to have a look at the
FineManual(tm) for all this kind of "magic", starting with :
http://docs.python.org/ref/specialnames.html
http://docs.python.org/ref/sequence-types.html

HTH
Feb 6 '07 #8
On Feb 6, 5:10 pm, Bruno Desthuilliers
<bdesth.quelquech...@free.quelquepart.frwrote:
jeremito a écrit :
On Feb 6, 2:36 pm, Bruno Desthuilliers <bdesth.quelquech...@free.quelquepart.frwrote:
>
(snip)
>>Here's an alternative implementation, so you get the idea.
>>
>>class Xs(dict):

oops ! I meant:
class Xs(object):

of course...

(snip)
I guess I just
need more experience.

Possibly - but not only. You may want to have a look at the
FineManual(tm) for all this kind of "magic", starting with :http://docs.python.org/ref/specialna...nce-types.html

HTH
Thanks again! Sometimes the problem is simply not knowing where to
find the documentation, or finding the right portion of the
documentation. Your help has been invaluable.

Jeremy

Feb 7 '07 #9
On Feb 7, 8:28 am, "jeremito" <jerem...@gmail.comwrote:
On Feb 6, 5:10 pm, Bruno Desthuilliers

<bdesth.quelquech...@free.quelquepart.frwrote:
jeremito a écrit :
On Feb 6, 2:36 pm, Bruno Desthuilliers <bdesth.quelquech...@free.quelquepart.frwrote:
(snip)
>>Here's an alternative implementation, so you get the idea.
>>class Xs(dict):
oops ! I meant:
class Xs(object):
of course...
(snip)
I guess I just
need more experience.
Possibly - but not only. You may want to have a look at the
FineManual(tm) for all this kind of "magic", starting with :http://docs..python.org/ref/specialn....python.org/re...
HTH

Thanks again! Sometimes the problem is simply not knowing where to
find the documentation, or finding the right portion of the
documentation. Your help has been invaluable.

Jeremy
One more question. I will be asking for the value of cs.xT *many*
(~millions) times. Therefore I don't want it's value to be calculated
on the fly. How can I set the value of xT whenever xS, xF, or xG are
changed, but not allow it to be set directly? From the example given
previously, it seems like it can't be done this way.

Thans,
Jeremy

Feb 7 '07 #10
jeremito kirjoitti:
On Feb 7, 8:28 am, "jeremito" <jerem...@gmail.comwrote:
>On Feb 6, 5:10 pm, Bruno Desthuilliers

<bdesth.quelquech...@free.quelquepart.frwrote:
>>jeremito a écrit :
On Feb 6, 2:36 pm, Bruno Desthuilliers <bdesth.quelquech...@free.quelquepart.frwrote:
(snip)
>>Here's an alternative implementation, so you get the idea.
>>class Xs(dict):
oops ! I meant:
class Xs(object):
of course...
(snip)
I guess I just
need more experience.
Possibly - but not only. You may want to have a look at the
FineManual(tm) for all this kind of "magic", starting with :http://docs.python.org/ref/specialna....python.org/re...
HTH
Thanks again! Sometimes the problem is simply not knowing where to
find the documentation, or finding the right portion of the
documentation. Your help has been invaluable.

Jeremy

One more question. I will be asking for the value of cs.xT *many*
(~millions) times. Therefore I don't want it's value to be calculated
on the fly. How can I set the value of xT whenever xS, xF, or xG are
changed, but not allow it to be set directly? From the example given
previously, it seems like it can't be done this way.

Thans,
Jeremy
I'm certainly no wizard in timing, but here goes:

Using the class definition given to you by Bruno, adding the following
to the end (and 'import timeit' at the start):

#============
lst = timeit.Timer('for i in xrange(10): xx=xs.xT', \
'from __main__ import Xs;xs = Xs()').repeat(100,1000)
lst.sort()
print lst
print 'Average:', sum(lst)/100
#============

I get the output:
[0.017246605364648282, 0.01727426251101738, 0.017275659336591698,
0.017290745052793044, 0.01733264982001903, 0.017347735536220377,

and so on ...

0.029063749722380933, 0.029163762433493667, 0.029422733894950315,
0.029790378386079785]
Average: 0.0182474979362

Thus: A 1000 assignments take a little over 18 milliseconds. The largest
values in the list are probably caused bu GC occurring. But even 30 ms /
1000 iterations i.e. 30 microseconds per fetch seems to be fast enough.

All this depends of course on the computer used. Mine is on the fast
side, you might test it on your PC.

The correct way of programming is to find a good simple algorithm and
the data structures needed, to program a clean solution with them and
then when you've got a correctly operating application and THEN IF you
need speed try to do something about it.

"Premature optimization is the worst evil" or something like that is how
the saying goes.

Hopefully I'm not leading you astray by being a novice in using the
timeit module.
HTH,
Jussi
Feb 7 '07 #11
On Feb 7, 12:48 pm, Jussi Salmela <tiedon_j...@hotmail.comwrote:
jeremito kirjoitti:
On Feb 7, 8:28 am, "jeremito" <jerem...@gmail.comwrote:
On Feb 6, 5:10 pm, Bruno Desthuilliers
<bdesth.quelquech...@free.quelquepart.frwrote:
jeremito a écrit :
On Feb 6, 2:36 pm, Bruno Desthuilliers <bdesth.quelquech...@free..quelquepart.frwrote:
(snip)
Here's an alternative implementation, so you get the idea.
class Xs(dict):
oops ! I meant:
class Xs(object):
of course...
(snip)
I guess I just
need more experience.
Possibly - but not only. You may want to have a look at the
FineManual(tm) for all this kind of "magic", starting with :http://docs.python.org/ref/specialna....python.org/re...
HTH
Thanks again! Sometimes the problem is simply not knowing where to
find the documentation, or finding the right portion of the
documentation. Your help has been invaluable.
Jeremy
One more question. I will be asking for the value of cs.xT *many*
(~millions) times. Therefore I don't want it's value to be calculated
on the fly. How can I set the value of xT whenever xS, xF, or xG are
changed, but not allow it to be set directly? From the example given
previously, it seems like it can't be done this way.
Thans,
Jeremy

I'm certainly no wizard in timing, but here goes:

Using the class definition given to you by Bruno, adding the following
to the end (and 'import timeit' at the start):

#============
lst = timeit.Timer('for i in xrange(10): xx=xs.xT', \
'from __main__ import Xs;xs = Xs()').repeat(100,1000)
lst.sort()
print lst
print 'Average:', sum(lst)/100
#============

I get the output:
[0.017246605364648282, 0.01727426251101738, 0.017275659336591698,
0.017290745052793044, 0.01733264982001903, 0.017347735536220377,

and so on ...

0.029063749722380933, 0.029163762433493667, 0.029422733894950315,
0.029790378386079785]
Average: 0.0182474979362

Thus: A 1000 assignments take a little over 18 milliseconds. The largest
values in the list are probably caused bu GC occurring. But even 30 ms /
1000 iterations i.e. 30 microseconds per fetch seems to be fast enough.

All this depends of course on the computer used. Mine is on the fast
side, you might test it on your PC.

The correct way of programming is to find a good simple algorithm and
the data structures needed, to program a clean solution with them and
then when you've got a correctly operating application and THEN IF you
need speed try to do something about it.

"Premature optimization is the worst evil" or something like that is how
the saying goes.

Hopefully I'm not leading you astray by being a novice in using the
timeit module.

HTH,
Jussi
Thank you. Once again this mailing list has proven most helpful. I
realize it probably isn't worth my time (or stress) to figure out how
to avoid calculating xT on the fly-at least not yet.

Jeremy

Feb 7 '07 #12
jeremito a écrit :
(snip)
>>
Thanks again! Sometimes the problem is simply not knowing where to
find the documentation,
http://python.org/doc is usually a good start !-)
>>or finding the right portion of the
documentation.
Yes, this is not always obvious. FWIW, browsing docs is probably the
most time-consuming task of programmers.
One more question. I will be asking for the value of cs.xT *many*
(~millions) times. Therefore I don't want it's value to be calculated
on the fly.
As Jussi said, first make it right. And then, *if* you have a *real*
performance problem, profile your code to find where bottlenecks really are.
How can I set the value of xT whenever xS, xF, or xG are
changed, but not allow it to be set directly? From the example given
previously, it seems like it can't be done this way.
A naive solution might be to turn all these attributes into properties,
so you could recompute and store the values of xA and xT each time xS,
xF or xG are modified. The example I gave you should be enough to get
you started here. But beware: in Python, function calls are somewhat
expansive, so you may end up slowing down your code.

Once again, first focus on cleanly solving the problem, and if *and only
if* you have objective performance problems, then *profile* your code
before trying to solve the wrong problem.
Feb 7 '07 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Paul | last post by:
Hi I'm implementing a few COM classes using Python. I've come across a problem when I'm trying the return an instance of a class from a method on one of my classes. The code is as follows,...
7
by: Edward Diener | last post by:
This simple code example gives me the message, "TypeError: 'staticmethod' object is not callable". class X(object): def Y(x): print x Y = staticmethod(Y) ad = { 1 : Y } def Z(self):...
12
by: Donnal Walter | last post by:
The following method is defined in one of my classes: def setup(self, items={}): """perform setup based on a dictionary of items""" if 'something' in items: value = items # now do something...
9
by: Ron Garret | last post by:
Is this a bug or a feature? class mydict(dict): def __setitem__(self, key, val): print 'foo' dict.__setitem__(self, key, val) >>> d=mydict() >>> d=2 foo
3
by: Bengt Richter | last post by:
Has anyone found a way besides not deriving from dict? Shouldn't there be a way? TIA (need this for what I hope is an improvement on the Larosa/Foord OrderedDict ;-) I guess I can just document...
8
by: Almad | last post by:
Hello, I discovered this behaviour in dictionary which I find confusing. In SneakyLang, I've tried to extend dictionary so it visits another class after something is added: class...
3
by: Tor Erik Soenvisen | last post by:
Hi, What do I need to do to make the code below work as expected: class str2(str): def __setitem__(self, i, y): assert type(y) is str assert type(i) is int assert i < len(self)
0
by: sapsi | last post by:
Hi, I am writing a SIMBL plugin for Mail.app, so far it loads and the correct method has been swizzled. However, i would like to call the original method and that is where the problem lies. If...
0
by: Jean-Paul Calderone | last post by:
On Wed, 22 Oct 2008 08:29:08 -0400, Neal Becker <ndbecker2@gmail.comwrote: I don't really know anything about the kind of classes that boost makes, however the behavior you describe is like the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.