[Python-Dev] total ordering.

Vladimir 'Yu' Stepanov vys at renet.ru
Wed May 17 14:06:50 CEST 2006


Jason Orendorff wrote:
> On 5/11/06, Vladimir 'Yu' Stepanov <vys at renet.ru> wrote:
>> If for Python-3000 similar it will be shown concerning types
>> str(), int(), complex() and so on, and the type of exceptions
>> will strongly vary, it will make problematic redefinition of
>> behavior of function of sorting.
>
> I don't see what you mean by "redefinition of behavior of function of
> sorting".  Is this something a Python programmer might want to do?
> Can you give an example?
>

It can be shown concerning external types. When everyone them
we shall compare to any internal type, but not with each other.
It concerns functions and the methods realized through CPython
API. For example types of the whole greater numbers of different
mathematical libraries. Or in case of use of several different
representation of character arrays.

One more example of work with a database which is more
realistic. For Python-3000 it will be possible to use
comparison only compatible types. There can be a problem
to sort types of data incompatible for comparison.
------------------------------------------------------------
conn = MySQLdb.connect(...)
cu = conn.cursor()
cu.execute("""
CREATE TABLE bigtable (
        id INT NOT NULL AUTO_INCREMENT,
        group_id INT NULL,
        value VARCHAR(255) NOT NULL,
        PRIMARY KEY (id)
)""")
for query in (
"INSERT INTO bigtable VALUES (NULL, 50, 'i am lazy')",
"INSERT INTO bigtable VALUES (NULL, 20, 'i am very lazy')",
"INSERT INTO bigtable VALUES (NULL, 19, 'i am very very lazy')",
"INSERT INTO bigtable VALUES (NULL, 40, 'i am very very very lazy :)')",
"INSERT INTO bigtable VALUES (NULL, NULL, 'Something different')"
):
        cu.execute(query)
cu.execute("SELECT * FROM bigtable ORDER BY id")
lst = [ r for r in cu ]
... do something in given order ...
lst.sort(cmp=lambda a, b: cmp(a[1], b[1]))
------------------------------------------------------------

If it is required to sort records on a column group_id it
hardly will be possible to us for python-3000 as numbers in
a column group_id will alternate with None values. Certainly
it is possible to filter the list preliminary. But it will not
be effective. Especially, if to assume, that sorted values can
have some various incompatible types (it already beyond the
framework of the given example).

The essence of my offer consists in at first to admit check of
types due to fast mechanisms CPython of methods, and only in
case of incompatibility of types of data to use spare model of
comparison, only if it is necessary. Creation of uniform
exception should facilitate creation of a design try-except.

Class of exceptions TypeError have wide using. For functions
and methods of comparison the specialized class is necessary.
In fact nobody uses class StopIteration for generation of
messages on a error :)

Other example when classes of different modules are used, and
it is necessary to sort them (see attached "example1.py").

> On 5/16/06, Vladimir 'Yu' Stepanov <vys at renet.ru> wrote:
>> It will be possible it conveniently to use as exception of
>> management by a stream, for indication of necessity to involve
>> `.__r(eq|ne|le|lt|ge|gt|cmp)__()' a method. This kind of a class
>> can carry out function, similarly to StopIteration for `.next()'.
>
> There are no .__r(eq|ne|le|lt|ge|gt|cmp)__() methods, for a logical
> reason which you might enjoy deducing yourself...

Oops :) I has simply hastened. About __rcmp__ has once read
through, and only here has emerged in memory when about it
already all to think have forgotten. As inopportunely.

>
>> At present time similar function is carried out with exception
>> NotImplemented. This exception is generated in a number of
>> mathematical operations. For this reason I ask to consider an
>> opportunity of creation of a new class.
>
> Can you explain this?  NotImplemented isn't an exception.
> (NotImplementedError is, but that's something quite different.)

I was mistaken. Sorry. It is valid not a class of exceptions.
> NotImplemented has exactly one purpose in Python, as far as I can
> tell.  What mathematical operations do you mean?

At multiplication/division/addition/subtraction/binary of operation/etc
- if types are non-comparable (binary_op1 in abstract.c). Very universal
element. CPython API will transform returned element NotImplemented to
generation of exception TypeError. Whether it is good?

In my opinion thus it is possible to hide a mistake of programming. As
an example the attached file example2.py.

Thanks.

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: example1.py
Url: http://mail.python.org/pipermail/python-dev/attachments/20060517/e78e4455/attachment.asc 
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: example2.py
Url: http://mail.python.org/pipermail/python-dev/attachments/20060517/e78e4455/attachment.pot 


More information about the Python-Dev mailing list