473,498 Members | 1,911 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

question on dict subclassing and overriding __setitems__

I apologize if this is an FAQ but googling has not turned up anything,
at least to my keywords.

I need to parse a configuration file from an existing application and
I'm treating it as a dictionary. I created my class with a parent class
of dict. Everything works okay except I discover I need to force keys
to uppercase when setting a value.

I override __setitems__ and, as you'd expect, I get a recursive loop.
I'm obviously missing a clue on how to break the recursion. My
admittedly simpleminded method overloading looks like:

def __setitem__ (self, index, value):
"""force keys to uppercase"""
self[index.upper()] = value

thanks for any pointers.
Jun 13 '06 #1
3 1432
Eric S. Johansson schrieb:
I apologize if this is an FAQ but googling has not turned up anything,
at least to my keywords.

I need to parse a configuration file from an existing application and
I'm treating it as a dictionary. I created my class with a parent class
of dict. Everything works okay except I discover I need to force keys
to uppercase when setting a value.

I override __setitems__ and, as you'd expect, I get a recursive loop.
I'm obviously missing a clue on how to break the recursion. My
admittedly simpleminded method overloading looks like:

def __setitem__ (self, index, value):
"""force keys to uppercase"""
self[index.upper()] = value

dict.__setitem__(self, index.upper()) = value

Or better even

super(subclass, self).__setitem__(key.upper(), value)
DIez
Jun 13 '06 #2
Diez B. Roggisch wrote:
dict.__setitem__(self, index.upper()) = value
oh duh.
Or better even

super(subclass, self).__setitem__(key.upper(), value)


hmm. http://fuhm.net/super-harmful/

I think I need to do some more reading.

---eric

Jun 14 '06 #3
Eric S. Johansson wrote:
Diez B. Roggisch wrote:
Or better even

super(subclass, self).__setitem__(key.upper(), value)


hmm. http://fuhm.net/super-harmful/


Interesting link, didn't know about it. I've always found super() ugly
and doubted that it was in practice better suited for multiple
inheritance than explicit method call, but I kept using it nonetheless
since it was "blessed" for new style classes. Unless of course the code
broke for one of the reasons mentioned in the article, in which case I
reverted back to the "old way". Good to know that I can safely get rid
of practically all super() calls. Any python-dev-er's take on super()'s
utility and future in Py3K ?

George

Jun 14 '06 #4

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

Similar topics

3
1681
by: Peter Olsen | last post by:
I want to define a class "point" as a subclass of complex. When I create an instance sample = point(<arglist>) I want "sample" to "be" a complex number, but with its real and imaginary...
8
6712
by: Massimiliano Alberti | last post by:
Can I specialize a template function in a subclass without overriding it? (the main template function is defined in a base class). Now I'm doing something like that: (in base class)...
1
1295
by: jslowery | last post by:
Hmm, I know this is something fundamental about how Python implements predicate dispatch, but for some reason I believed that this would work: class delegate_dict(dict): def __init__(self,...
15
2422
by: George Sakkis | last post by:
Although I consider dict(**kwds) as one of the few unfortunate design choices in python since it prevents the future addition of useful keyword arguments (e.g a default value or an orderby...
16
4057
by: chosechu | last post by:
Hello Pythoneers: I need to pass a list of named arguments to a function in a given order, and make sure these named arguments are retrieved using keys() in the same order they were given....
10
1181
by: Frank Millman | last post by:
Hi all I recently posted a question about subclassing. I did not explain my full requirement very clearly, and my proposed solution was not pretty. I will attempt to explain what I am trying to...
11
1031
by: fejkadress | last post by:
I want to make an addressbook and I'm new to OO programming, so I wonder if this sounds reasonable. I think of making a class Address which contains all data about one person, that class can...
12
9710
by: jeremito | last post by:
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...
1
1598
by: | last post by:
Hello all, I have a question which might be simple or need some work around. I want to do something like this. My class/instance has a dict as a property. I want the instance to catch the...
0
7125
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7002
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...
1
6885
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
7379
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...
0
5462
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4908
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4588
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
1
656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
290
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.