python bijection

geremy condra debatem1 at gmail.com
Thu Dec 3 11:54:27 EST 2009


On Thu, Dec 3, 2009 at 7:04 AM, M.-A. Lemburg <mal at egenix.com> wrote:
> Raymond Hettinger wrote:
>> [Joshua Bronson]
>>> Raymond, do you think there might be any future in including a built-
>>> in bidict data structure in Python?
>>
>> I don't think so.  There are several forces working against it:
>>
>> * the recipe is new, so it hasn't had a chance to mature
>>   or to gain a fan club.
>>
>> * there are many approaches to the solving the problem and
>>   there is no reason to assume this one is the best.
>>
>> * it extends the language with arcane syntax tricks instead of
>>   using the language as designed by Guido.  That makes it harder
>>   to learn and remember.
>>
>> * we've already got one (actually two).  The two dictionary approach
>>   uses plain python, requires no new learning, and is more flexible.
>>   Also, sqlite3 provides another way to use multiple lookups to a
>>   single record.  The database approach is much more general
>>   (extending to trijections, allowing multiple sort orders,
>>   providing persistence, etc).
>>
>> * the semantics of a bijection aren't obvious:
>>
>>      b['x'] = 'ex'      # first record:  ('x', 'ex')
>>      b['y'] = 'why'     # second record: ('y', 'why')
>>      b[:'why'] = 'x'    # do two records collapse into one? is there
>> an error?
>>
>> * the proposed syntax doesn't address the issue covered in my previous
>> post.
>>   Since bijections are symmetrical, they do not have an obvious
>> direction
>>   (which is the primary key, the husband or the wife?).  The syntax
>> needs to
>>   allow user names to make it clear which is being accessed:
>>
>>      marriages.h2w['john'] = 'amy'
>>      marriages.w2h['amy'] = 'john'
>>
>>   Contrast this with:
>>
>>      marriages['jordan'] = 'taylor'    # are you sure you got the
>> order correct?
>>      marriages[:'taylor'] = 'jordan'   # this is easy to get backwards
>
> I think the only major CS data type missing from Python is some
> form of (fast) directed graph implementation à la kjGraph:
>
>    http://gadfly.sourceforge.net/kjbuckets.html
>
> With these, you can easily build all sorts of relations between
> objects and apply fast operations on them. In fact, it should then
> be possible to build a complete relational database in Python
> (along the lines of Gadfly).

If you're in the market for a Python graph library, you may want
to check out Graphine- I'm obviously biased (I wrote most of it)
but it has a few more bells and whistles than kjbuckets, and is
pretty darned easy to use. It also supports undirected and
bridge graphs.

Geremy Condra



More information about the Python-list mailing list