Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
no-clobber dicts?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  13 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
kj  
View profile  
 More options Aug 3 2009, 5:07 pm
Newsgroups: comp.lang.python
From: kj <no.em...@please.post>
Date: Mon, 3 Aug 2009 21:07:32 +0000 (UTC)
Local: Mon, Aug 3 2009 5:07 pm
Subject: no-clobber dicts?

I use the term "no-clobber dict" to refer to a dictionary D with
the especial property that if K is in D, then

  D[K] = V

will raise an exception unless V == D[K].  In other words, D[K]
can be set if K doesn't exist already among D's keys, or if the
assigned value is equal to the current value of D[K].  All other
assignments to D[K] trigger an exception.

The idea here is to detect inconsistencies in the data.

This is a data structure I often need.  Before I re-invent the
wheel, I thought I'd ask: is it already available?

TIA!

kynn


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
r  
View profile  
 More options Aug 3 2009, 5:47 pm
Newsgroups: comp.lang.python
From: r <rt8...@gmail.com>
Date: Mon, 3 Aug 2009 14:47:54 -0700 (PDT)
Local: Mon, Aug 3 2009 5:47 pm
Subject: Re: no-clobber dicts?
On Aug 3, 4:07 pm, kj <no.em...@please.post> wrote:

Not sure if something like this already exists, but it would be
trivial to implement by overriding dict.__setitem__()

badda-bing baby!


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Rebert  
View profile  
 More options Aug 3 2009, 6:00 pm
Newsgroups: comp.lang.python
From: Chris Rebert <c...@rebertia.com>
Date: Mon, 3 Aug 2009 15:00:14 -0700
Local: Mon, Aug 3 2009 6:00 pm
Subject: Re: no-clobber dicts?

That is, if you don't care about .update() not preserving the
invariant. Otherwise, one will need to look at the UserDict module.

Cheers,
Chris
--
http://blog.rebertia.com


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
r  
View profile  
 More options Aug 3 2009, 7:39 pm
Newsgroups: comp.lang.python
From: r <rt8...@gmail.com>
Date: Mon, 3 Aug 2009 16:39:25 -0700 (PDT)
Local: Mon, Aug 3 2009 7:39 pm
Subject: Re: no-clobber dicts?
On Aug 3, 5:00 pm, Chris Rebert <c...@rebertia.com> wrote:

> On Mon, Aug 3, 2009 at 2:47 PM, r<rt8...@gmail.com> wrote:
[snip]
> > Not sure if something like this already exists, but it would be
> > trivial to implement by overriding dict.__setitem__()

> That is, if you don't care about .update() not preserving the
> invariant. Otherwise, one will need to look at the UserDict module.

> Cheers,
> Chris
> --http://blog.rebertia.com

Good catch Chris. However since the OP said this was for testing
purposes,  i just *assumed* he would be smart enough *not* to call
update() on the dict at hand ;-). But sometimes i need to be protected
from myself too -- at least thats what my therapist keeps telling
me :-)

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Steven D'Aprano  
View profile  
 More options Aug 3 2009, 11:23 pm
Newsgroups: comp.lang.python
From: Steven D'Aprano <ste...@REMOVE.THIS.cybersource.com.au>
Date: 04 Aug 2009 03:23:20 GMT
Local: Mon, Aug 3 2009 11:23 pm
Subject: Re: no-clobber dicts?

On Mon, 03 Aug 2009 21:07:32 +0000, kj wrote:
> I use the term "no-clobber dict" to refer to a dictionary D with the
> especial property that if K is in D, then

>   D[K] = V

> will raise an exception unless V == D[K].  In other words, D[K] can be
> set if K doesn't exist already among D's keys, or if the assigned value
> is equal to the current value of D[K].  All other assignments to D[K]
> trigger an exception.

Coincidentally, I just built something very close to what you ask. Here
it is:

class ConstantNamespace(dict):
    """Dictionary with write-once keys."""
    def __delitem__(self, key):
        raise TypeError("can't unbind constants")
    def __setitem__(self, key, value):
        if key in self:
            raise TypeError("can't rebind constants")
        super(ConstantNamespace, self).__setitem__(key, value)
    def clear(self):
        raise TypeError('cannot unbind constants')
    def pop(self, key, *args):
        raise TypeError("cannot pop constants")
    def popitem(self):
        raise TypeError("cannot pop constants")
    def update(self, other):
        for key in other:
            if key in self:
                raise TypeError('cannot update constants')
        # If we get here, then we're not modifying anything,
        # so okay to proceed.
        super(ConstantNamespace, self).update(other)
    def copy(self):
        c = self.__class__(**self)
        return c

I also have a series of unit tests for it if you're interested in them.

--
Steven


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
alex23  
View profile  
 More options Aug 4 2009, 1:33 am
Newsgroups: comp.lang.python
From: alex23 <wuwe...@gmail.com>
Date: Mon, 3 Aug 2009 22:33:33 -0700 (PDT)
Local: Tues, Aug 4 2009 1:33 am
Subject: Re: no-clobber dicts?

Steven D'Aprano <ste...@REMOVE.THIS.cybersource.com.au> wrote:
> I also have a series of unit tests for it if you're interested in them.

That's several times today that kj has asked a question and you've
responded with ready-to-go code. If this was Stackoverflow, I'd accuse
you of reputation-whoring...

You _can_ just post your cool code without the double act, y'know! :)


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
kj  
View profile  
 More options Aug 4 2009, 3:29 pm
Newsgroups: comp.lang.python
From: kj <no.em...@please.post>
Date: Tue, 4 Aug 2009 19:29:00 +0000 (UTC)
Subject: Re: no-clobber dicts?
In <pan.2009.08.04.03.23...@REMOVE.THIS.cybersource.com.au> Steven D'Aprano <ste...@REMOVE.THIS.cybersource.com.au> writes:

Thanks.  As you note this not quite what I'm looking for, but it's
a good template for it.

kynn


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
kj  
View profile  
 More options Aug 4 2009, 3:30 pm
Newsgroups: comp.lang.python
From: kj <no.em...@please.post>
Date: Tue, 4 Aug 2009 19:30:51 +0000 (UTC)
Local: Tues, Aug 4 2009 3:30 pm
Subject: Re: no-clobber dicts?
In <mailman.4183.1249336823.8015.python-l...@python.org> Chris Rebert <c...@rebertia.com> writes:

The implication here is that .update() does not in turn use
.__setitem__(), which I find a bit surprising.

kynn


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
kj  
View profile  
 More options Aug 4 2009, 4:01 pm
Newsgroups: comp.lang.python
From: kj <no.em...@please.post>
Date: Tue, 4 Aug 2009 20:01:12 +0000 (UTC)
Local: Tues, Aug 4 2009 4:01 pm
Subject: Re: no-clobber dicts?
In <pan.2009.08.04.03.23...@REMOVE.THIS.cybersource.com.au> Steven D'Aprano <ste...@REMOVE.THIS.cybersource.com.au> writes:

>class ConstantNamespace(dict):

<snip>

>I also have a series of unit tests for it if you're interested in them.

Actually, come to think of it, I think I'll take you up on this.
I'd love to see those tests.  Unit testing in Python is in area I
need to work on.

TIA!

kynn


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Rebert  
View profile  
 More options Aug 4 2009, 4:03 pm
Newsgroups: comp.lang.python
From: Chris Rebert <c...@rebertia.com>
Date: Tue, 4 Aug 2009 13:03:08 -0700
Local: Tues, Aug 4 2009 4:03 pm
Subject: Re: no-clobber dicts?

The builtin types are allowed to take such shortcuts for performance reasons.

Cheers,
Chris
--
http://blog.rebertia.com


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Raymond Hettinger  
View profile  
 More options Aug 4 2009, 4:14 pm
Newsgroups: comp.lang.python
From: Raymond Hettinger <pyt...@rcn.com>
Date: Tue, 4 Aug 2009 13:14:29 -0700 (PDT)
Local: Tues, Aug 4 2009 4:14 pm
Subject: Re: no-clobber dicts?
[kj]

> The implication here is that .update() does not in turn use
> .__setitem__(), which I find a bit surprising.

It's never wise to make assumptions about this sort of thing.
Every method in a class or type is allowed to directly access
or modify its private, internal data.  The implementation is
free to choose whether to limit that access to a few accessors
(i.e. getitem, setitem, delitem, len) or to let all of the
methods have direct access to the underlying data structure.
Even if accessors were used, the choice would be arbitrary
(perhaps delitem() gets implemented in terms of pop() or somesuch).

This is a basic for OO programming.  Unless the class documentation
promises to use particular hooks, the implementation is free to
change in any way that doesn't violate the published API (this is
one of the benefits of encapsulation and abstract data types).

In general, if a subclass is overriding, rather than extending,
then it should override *every* method that could be affected.

Raymond


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Steven D'Aprano  
View profile  
 More options Aug 4 2009, 11:07 pm
Newsgroups: comp.lang.python
From: Steven D'Aprano <st...@REMOVE-THIS-cybersource.com.au>
Date: Wed, 05 Aug 2009 13:07:26 +1000
Local: Tues, Aug 4 2009 11:07 pm
Subject: Re: no-clobber dicts?
On Wed, 5 Aug 2009 06:01 am kj wrote:

> In <pan.2009.08.04.03.23...@REMOVE.THIS.cybersource.com.au> Steven
> D'Aprano <ste...@REMOVE.THIS.cybersource.com.au> writes:

>>class ConstantNamespace(dict):
> <snip>

>>I also have a series of unit tests for it if you're interested in them.

> Actually, come to think of it, I think I'll take you up on this.
> I'd love to see those tests.  Unit testing in Python is in area I
> need to work on.

No problem. Here you go:

http://www.cybersource.com.au/users/steve/python/constants.py

Comments welcome.

--
Steven


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
kj  
View profile  
 More options Aug 5 2009, 3:58 pm
Newsgroups: comp.lang.python
From: kj <no.em...@please.post>
Date: Wed, 5 Aug 2009 19:58:39 +0000 (UTC)
Local: Wed, Aug 5 2009 3:58 pm
Subject: Re: no-clobber dicts?
In <00027aa9$0$2969$c3e8...@news.astraweb.com> Steven D'Aprano <st...@REMOVE-THIS-cybersource.com.au> writes:

Extremely helpful.  Thanks!

kynn


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »