Re: [Python-Dev] total ordering.

2006-05-18 Thread Jason Orendorff
Vladimir,

Your examples seem to indicate that you've misunderstood the change
that's proposed for Python 3000.  Especially this:

On 5/17/06, Vladimir 'Yu' Stepanov [EMAIL PROTECTED] wrote:
 # BEGIN: Emulation python3000
 if type(a) is not type(b) and (
 not operator.isNumberType(a) or
 not operator.isNumberType(b)
 ):
 raise TypeError(python3000: not-comparable types, 
 (a,b))
 # END: Emulation python3000

Python 3000 will not do anything like this.  It'll try a.__cmp__(b),
and failing that b.__cmp__(a) (but imagine this using tp_ slots
instead of actual Python method calls), and if both return
NotImplemented, it'll throw a TypeError (rather than guess, which is
what it does now).

There's a lot of legacy oddness in PyObject_RichCompare() and its many
helper functions; presumably they'll delete some of that, but it won't
be anything you care about.

Comparison with None should also continue to work as it does now,
unless I missed something.

-j
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] total ordering.

2006-05-16 Thread Vladimir 'Yu' Stepanov
Guido van Rossum wrote:
 On 5/11/06, Vladimir 'Yu' Stepanov [EMAIL PROTECTED] 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.

 Not really. We'll just document that sort() should only be used on a
 list of objects that implement a total ordering. The behavior
 otherwise will simply be undefined; it will raise whatever exception
 is first raised by an unsupported comparison (most likely TypeError).
 In practice this won't be a problem.


In my opinion for functions of comparisons there is no additional
kind of exceptions. If made action is not operation of reduction of
type more often exception TypeError means a mistake of programming.
At creation of exception, characteristic only for comparison, it
is possible to involve methods `.__r(eq|ne|le|lt|ge|gt|cmp)__()'
not being afraid to hide a mistake of programming TypeError (sorry
for a tautology).

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()'.
In case of unsuccessful comparison the user has an opportunity in
function `cmp' a method .sort() simple image to define the
behaviour necessary to it, including an opportunity of sorting of
diverse elements.

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.
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] total ordering.

2006-05-16 Thread Jason Orendorff
On 5/11/06, Vladimir 'Yu' Stepanov [EMAIL PROTECTED] 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?


On 5/16/06, Vladimir 'Yu' Stepanov [EMAIL PROTECTED] 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...

 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.)
NotImplemented has exactly one purpose in Python, as far as I can
tell.  What mathematical operations do you mean?

-j
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] total ordering.

2006-05-11 Thread Vladimir 'Yu' Stepanov
Guido van Rossum wrote:
 On 5/6/06, Vladimir Yu. Stepanov [EMAIL PROTECTED] wrote:
 [proposing a total ordering between types]

 It Ain't Gonna Happen. (From now on, I'll write this as IAGH.)

 In Python 3000, we'll actually *remove* ordering between arbitrary
 types as a feature; only types that explicitly care to be ordered with
 respect to one another will be ordered. Equality tests are unaffected,
 x==y will simply return False if x and y are of incomparable types;
 but xy (etc.) will raise an exception.

 --
 --Guido van Rossum (home page: http://www.python.org/~guido/)
 ___
 Python-Dev mailing list
 Python-Dev@python.org
 http://mail.python.org/mailman/listinfo/python-dev
 Unsubscribe: http://mail.python.org/mailman/options/python-dev/vys%40renet.ru


   
When comparison is made like (something  123), generating of
exception is necessary. At sorting or use of binary trees it is not
so obvious. The behavior function of comparison in this case
depends on needs of the user. At present time use of redefined
function is complicated.

I shall give an example. By a call of a method sort for the list
can give absolutely different exceptions, depending on the order
of sorted values or data (thanks for David Mertz and Josiah
Carlson):
-
  [chr(255),1j,1,u't'].sort()
Traceback (most recent call last):
  File stdin, line 1, in module
TypeError: no ordering relation is defined for complex numbers
  [chr(255),1j,u't',1].sort()
Traceback (most recent call last):
  File stdin, line 1, in module
UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 0: 
ordinal not in range(128)
-

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.

It would be quite good to create one more class of exceptions
which would unify generation of mistakes at comparison of
non-comparable types or data.

___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] total ordering.

2006-05-11 Thread Guido van Rossum
On 5/11/06, Vladimir 'Yu' Stepanov [EMAIL PROTECTED] 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.

Not really. We'll just document that sort() should only be used on a
list of objects that implement a total ordering. The behavior
otherwise will simply be undefined; it will raise whatever exception
is first raised by an unsupported comparison (most likely TypeError).
In practice this won't be a problem.

-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] total ordering.

2006-05-11 Thread Edward Loper
Guido van Rossum wrote:
 On 5/11/06, Vladimir 'Yu' Stepanov [EMAIL PROTECTED] 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.
 
 Not really. We'll just document that sort() should only be used on a
 list of objects that implement a total ordering. [...]

It might be useful in some cases to have a keyword argument to 
sort/sorted that says to ignore exceptions arising from comparing 
elements, and leaves the ordering of non-comparable values undefined.

-Edward
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] total ordering.

2006-05-11 Thread Delaney, Timothy (Tim)
Edward Loper wrote:

 It might be useful in some cases to have a keyword argument to
 sort/sorted that says to ignore exceptions arising from comparing
 elements, and leaves the ordering of non-comparable values undefined.

Why? Far better to use a key (or cmp if you really want) that imposes a
total (or at least consistent) ordering. Otherwise sorting is random.

Tim Delaney
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] total ordering.

2006-05-07 Thread Vladimir Yu. Stepanov
On Sat, May 06, 2006 at 03:12:11AM -0700, Josiah Carlson wrote:
 
 Vladimir 'Yu' Stepanov [EMAIL PROTECTED] wrote:
  Josiah Carlson wrote:
   This problem has nothing to do with dictionaries and hashing, it has to
   do with the fact that there may not be a total ordering on the elements
   of a sequence.
  
  It is sad. I did not know it. Therefore and have not understood.
  
  I have looked in Google on python dev total ordering. On intentions to
  correct this problem I have not found anything. It already earlier was
  discussed ?
 
 Not really.  It has to do with how non-comparable instances compare to
 each other.  Generally speaking, when instances aren't comparable,
 Python compares their type, which is actually comparing the names of
 types. This wouldn't be a big deal, except that:
 
  str  tuple  unicode
 True
 
 And you can actually compare str and unicode, so, if you have a str that
 is greater than the unicode, you run into this issue.  With unicode
 becoming str in Py3k, we may not run into this issue much then, unless
 bytes are comparable to str, in which case we end up witht the same
 problems.
 
 Actually, according to my google of python dev total ordering, it
 gives me...
 
 http://mail.python.org/pipermail/python-dev/2003-March/034169.html
 http://mail.python.org/pipermail/python-list/2003-March/154142.html
 
 Which were earlier discussions on this topic, which are quite topical. 
 The ultimate solution is to choose a total ordering on types and
 consider the problem solved.  Then list.sort() and binary trees will
 work properly.
 

Thanks for so detailed answer.

Under these references discussion is conducted is presented in the form
of so is, instead of as it to correct. Therefore I would like to
mention a question as it to correct.

In case of incomparability of types can be it is meaningful
compare the identifiers compared to each concrete type. More truly
on them to calculate weight which will play a direct role in case
of impossibility of comparison of types.

Below one of variants of the decision of this problem is resulted.

To each type of data in Python to add three fields:
..
int tp_id;
int tp_qualifier;
int tp_weight;
..

Rigidly to appoint some built in and external to types
identifiers (tp_id). The others receive free identifiers on their
initialization. Except for that types should be classified on
their basic properties - tp_qualifier. The type can be
classified as:
0 = None
1 = integer (bool, int, long, etc..)
2 = float (decimal, float, etc..)
3 = math (complex, matrix may be ?? )
4 = string (str, unicode, splice, etc..)
5 = sequence (iterators, deque, xrange, enumerate, etc..)
6 = array (tuple, list, bytes, array, etc..)
7 = dictionary (dict, defdict, etc..)
8 = set (set, etc..)
9 = file (file, socket.socket, cStringIO.StringIO)

..
127 = non qualifier (sre.*, datetime.*, ClassType, IntsenceType, etc..)

It is the approximate list on which it will be convenient
to classify types (in my opinion).

The weight can be precisely set in structure or if be equal -1
should is calculated under the formula:

ob_type-tp_weight = (ob_type-tp_qualifier24) + (ob_type-tp_id8)

Thus objects with similar properties will follow one after
another. A problem can make external types. But if to develop
further classification, problems to arise should not.


-- 
SY. Vladimir Stepanov

___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] total ordering.

2006-05-07 Thread Guido van Rossum
On 5/6/06, Vladimir Yu. Stepanov [EMAIL PROTECTED] wrote:
[proposing a total ordering between types]

It Ain't Gonna Happen. (From now on, I'll write this as IAGH.)

In Python 3000, we'll actually *remove* ordering between arbitrary
types as a feature; only types that explicitly care to be ordered with
respect to one another will be ordered. Equality tests are unaffected,
x==y will simply return False if x and y are of incomparable types;
but xy (etc.) will raise an exception.

--
--Guido van Rossum (home page: http://www.python.org/~guido/)
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com