Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Create child class of Python dict with modified values

Reply
Thread Tools

Create child class of Python dict with modified values

 
 
dmitrey
Guest
Posts: n/a
 
      08-09-2010
hi all,
suppose I have defined a child class of Python dict, currently it
constructor looks like that:
def __init__(self, *args, **kwargs):
dict.__init__(self, *args, **kwargs)
#(+some more insufficient code)

Constructor should be capable of calling with either any way Python
dict is constructed or with a Python dict instance to be derived from;
calculations speed is important.

So it works well for now, but I want __init__ to set modified values,
like this:
values_of_the_dict = [some_func(elem) for elem in self.values()]

How this could be done?

Thank you in advance,
Dmitrey.

 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      08-09-2010
dmitrey wrote:

> hi all,
> suppose I have defined a child class of Python dict, currently it
> constructor looks like that:
> def __init__(self, *args, **kwargs):
> dict.__init__(self, *args, **kwargs)
> #(+some more insufficient code)
>
> Constructor should be capable of calling with either any way Python
> dict is constructed or with a Python dict instance to be derived from;
> calculations speed is important.
>
> So it works well for now, but I want __init__ to set modified values,
> like this:
> values_of_the_dict = [some_func(elem) for elem in self.values()]
>
> How this could be done?


>>> class D(dict):

.... def __init__(self, *args, **kw):
.... if args:
.... args = ((k, v.upper()) for k, v in args[0]),
.... if kw:
.... for k in kw: kw[k] = 10*kw[k]
.... dict.__init__(self, *args, **kw)
....
>>> D(["ab", "cd"], e="f")

{'a': 'B', 'c': 'D', 'e': 'ffffffffff'}

Replace v.upper() and 10*kw[k] with the appropriate some_func() calls.
Personally I would apply the function before passing the data to the dict
subclass.

Peter
 
Reply With Quote
 
 
 
 
dmitrey
Guest
Posts: n/a
 
      08-09-2010
On Aug 9, 1:38*pm, Peter Otten <(E-Mail Removed)> wrote:
> dmitrey wrote:
> > hi all,
> > suppose I have defined a child class of Python dict, currently it
> > constructor looks like that:
> > * * def __init__(self, *args, **kwargs):
> > * * * * dict.__init__(self, *args, **kwargs)
> > * * * * #(+some more insufficient code)

>
> > Constructor should be capable of calling with either any way Python
> > dict is constructed or with a Python dict instance to be derived from;
> > calculations speed is important.

>
> > So it works well for now, but I want __init__ to set modified values,
> > like this:
> > values_of_the_dict = [some_func(elem) for elem in self.values()]

>
> > How this could be done?
> >>> class D(dict):

>
> ... * * def __init__(self, *args, **kw):
> ... * * * * * * if args:
> ... * * * * * * * * * * args = ((k, v.upper()) for k, v in args[0]),
> ... * * * * * * if kw:
> ... * * * * * * * * * * for k in kw: kw[k] = 10*kw[k]
> ... * * * * * * dict.__init__(self, *args, **kw)
> ...>>> D(["ab", "cd"], e="f")
>
> {'a': 'B', 'c': 'D', 'e': 'ffffffffff'}
>
> Replace v.upper() and 10*kw[k] with the appropriate some_func() calls.


OK, thank you.

> Personally I would apply the function before passing the data to the dict
> subclass.


It's impossible for the situation in hand.

D.
 
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
Learning Pyhton - Functional Programming - How intersect/differencetwo dict with dict/values? fast! macm Python 11 11-11-2010 10:04 AM
Re: dict.keys() and dict.values() are always the same order, is it? Cameron Simpson Python 6 04-21-2010 04:37 AM
dict.keys() and dict.values() are always the same order, is it? Menghan Zheng Python 1 04-20-2010 03:51 AM
How can I create a dict that sets a flag if it's been modified sandravandale@yahoo.com Python 11 01-12-2006 05:18 PM
How do I: Main thread spawn child threads, which child processes...control those child processes? Jeff Rodriguez C Programming 23 12-09-2003 11:06 PM



Advertisments