dicts + amb

B

bearophileHUGS

I have implemented yet another Odict class:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/498195
It's very slow, but it is O(1) for get, set and del too.

I have added ritervalues(), rkeys(), etc methods because I presume I
can't use the reversed(). So can an __riter__ hook be generally useful?
(I have seen few comments about it in the discussion regarding PEP
322).

What's the meaning of .updated() called without arguments? Can it
become an error in Python 3.0?

In practice I don't have problems using dict get() and pop() methods,
but their calling behaviour seem a bit incoherent. Both accept a second
value, that can be None too, that they return if the key is absent, but
the get() returns the None even if you don't specify it, while pop()
return a TypeError if you don't give explicitely something (like None)
as second argument. (Maybe in practice they are more useful if kept
with such different calling behaviour.)

------------

Something unrelated that I don't know where to put.
Recently I have seen the amb operator, it's not a simple operator. I
have seen a (very simple) Ruby implementation:
http://www.randomhacks.net/articles/2005/10/11/amb-operator
I think a good amb implementation is a very complex thing. Can such
operator be useful for Python too?

Bye,
bearophile
 
D

Diez B. Roggisch

Something unrelated that I don't know where to put.
Recently I have seen the amb operator, it's not a simple operator. I
have seen a (very simple) Ruby implementation:
http://www.randomhacks.net/articles/2005/10/11/amb-operator
I think a good amb implementation is a very complex thing. Can such
operator be useful for Python too?

You might consider researching ALMA, a constraint-imperative programming
language that allows for program execution to be rolled-back to
so-called choicepoints. It heavily reminds me of that amb-operator of
yours.

Diez
 
M

Marc 'BlackJack' Rintsch

bearophileHUGS said:
I have implemented yet another Odict class:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/498195
It's very slow, but it is O(1) for get, set and del too.

I have added ritervalues(), rkeys(), etc methods because I presume I
can't use the reversed(). So can an __riter__ hook be generally useful?
(I have seen few comments about it in the discussion regarding PEP
322).

I don't know if this is documented somewhere but the `reversed()` function
looks for a `__reversed__()` method that returns an iterator.

Ciao,
Marc 'BlackJack' Rintsch
 
B

bearophileHUGS

Marc 'BlackJack' Rintsch:
I don't know if this is documented somewhere but the `reversed()` function
looks for a `__reversed__()` method that returns an iterator.

You are right, thank you, I have done some tests already, and I'll soon
add that method too.

-------------------


Partially related.
I have used the new Google code search:
http://www.google.com/codesearch/advanced_code_search
To find the usage frequency of Python dict methods:

456 ".itervalues()" lang:python
415 ".iteritems()" lang:python
403 ".iterkeys()" lang:python
387 ".values()" lang:python
385 ".clear()" lang:python
256 ".update(" lang:python
254 ".fromkeys(" lang:python
224 ".has_key(" lang:python
201 ".get(" lang:python
200 ".items()" lang:python
159 ".popitem()" lang:python
113 "a.setdefault(" lang:python

The results are quite approximated, but they seem meaningful.

Bye,
bearophile
 
B

bearophileHUGS

(e-mail address removed):
456 ".itervalues()" lang:python
415 ".iteritems()" lang:python
403 ".iterkeys()" lang:python
387 ".values()" lang:python
385 ".clear()" lang:python
256 ".update(" lang:python
254 ".fromkeys(" lang:python
224 ".has_key(" lang:python
201 ".get(" lang:python
200 ".items()" lang:python
159 ".popitem()" lang:python
113 "a.setdefault(" lang:python

Sorry:

398 lang:python ".setdefault("

Results are probably too much approximated to be of any use...

Bye,
bearophile
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top