PyWart: Language missing maximum constant of numeric types!

W

Wolfgang Meiners

Am 24.02.12 14:37, schrieb Rick Johnson:
I get sick and tired of doing this!!!

if maxlength == UNLIMITED:
allow_passage()
elif len(string) > maxlength:
deny_passage()

What Python needs is some constant that can be compared to ANY numeric
type and that constant will ALWAYS be larger!
If there is no limit for len(string), why not simply use

# get_limit() returns None if there is no limit
maxlength = get_limit()
if maxlength and (len(string) <= maxlength):
allow_passage()
else:
deny_passage()

Wolfgang
 
S

Serhiy Storchaka

25.02.12 02:37, MRAB напиÑав(ла):
We already have arbitrarily long ints, so there could be a special
infinite int singleton (actually, 2 of them, one positive, the other
negative).

float('inf') and float('-inf').
 
M

MRAB

Am 24.02.12 14:37, schrieb Rick Johnson:
If there is no limit for len(string), why not simply use

# get_limit() returns None if there is no limit
maxlength = get_limit()
if maxlength and (len(string)<= maxlength):
allow_passage()
else:
deny_passage()
That should be:

if maxlength is not None and len(string) <= maxlength:
 
R

Rick Johnson

I understand that a Python integer can run to infinity.  Quite how the
illustrious rr manages to test for the length of a string that's already
used all of the memory on his system has baffled me,

When did i ever say that i would need a string who's length is
INFINITY? In fact, i don't. My example was just that, as SIMPLIFIED
example of the problem that was the genesis of my question. In my
"real world" problem, i don't expect the string to EVER be more than
double digits in length. But as any good programmer knows, you never
want to solve problems as globally as possible. I need to do
comparisons on strings now, but maybe integers later, or who knows.
INFINITY comparisons are useful in many places.
but I'm sure that
all the people who frequent this list with their Phds, MScs or whatever
will soon correct me.

I don't believe you'd need a Phd to understand my problem.
 
R

Rick Johnson

But it would also be rejected, and rightly so, as unnecessary complexity
for the int type. There are already Decimal and float infinities, just
use one of them.

Sure there are float INFINITIES that work fine for ints and floats,
but where is the consistency? INFINITY need not be a int or a float or
a str, or whatever. All it need be is a an object who always returns
itself as being larger in any comparison.
Or make your own, it's not difficult.

INFINITY should be at the very least a constant of the math module.
 
R

Rick Johnson

[...]
That should be:
if maxlength is not None and len(string) <= maxlength:

Using "imaginary" infinity values defiles the intuitive nature of your
code. What is more intuitive?

def confine_length(string, maxlength=INFINITY):
if string.length < maxlength:
do_something()

def confine_length(string, maxlength=None):
if maxlength is not None and len(string) <= maxlength:
do_something()
 
W

Wolfgang Meiners

Am 25.02.12 18:54, schrieb MRAB:
That should be:

if maxlength is not None and len(string) <= maxlength:

Take a look at

http://docs.python.org/library/stdtypes.html

where you can read:
=========================================================================
Any object can be tested for truth value, for use in an if or while
condition or as operand of the Boolean operations below. The following
values are considered false:


None

False

zero of any numeric type, for example, 0, 0L, 0.0, 0j.

any empty sequence, for example, '', (), [].

any empty mapping, for example, {}.

instances of user-defined classes, if the class defines
__nonzero__() or __len__() method, when that method returns the
integer zero or bool value False. [1]

All other values are considered true — so objects of many types are
always true.
==========================================================================

That means:
if maxlength and (len(string) <= maxlength):

is equivalent to
if (maxlength is not None) and (len(string) <= maxlength):

which is more complicated to type and -in my opinion- not so intuitive.
But because it is equivalent, it is a matter of taste, what to use.

Wolfgang
 
C

Chris Angelico

That means:
if maxlength and (len(string) <= maxlength):

is equivalent to
if (maxlength is not None) and (len(string) <= maxlength):

On the contrary, it means they are distinctly NOT equivalent. The
shorter form would treat a maximum length of 0 as meaning "unlimited".
Now, that's an understandable notation, but it's not what's given
here; if None means unlimited, then 0 should enforce that string ==
"".

ChrisA
 
W

Wolfgang Meiners

Am 25.02.12 21:35, schrieb Rick Johnson:
[...]
That should be:
if maxlength is not None and len(string) <= maxlength:

Using "imaginary" infinity values defiles the intuitive nature of your
code. What is more intuitive?

def confine_length(string, maxlength=INFINITY):
if string.length < maxlength:
do_something()

def confine_length(string, maxlength=None):
if maxlength is not None and len(string) <= maxlength:
do_something()

I just had a closer look at it. It seems to be more complicated than i
thougth: You will have to write

def confine_length(string, maxlength=None):
if maxlength: # maxlength exists, comparison possible
if len(string) <= maxlength:
do_something()
else: # maxlength does not exist, so always do something
do_something()

you migth also write

def confine_length(str, maxlength=None):
do_it = (len(str) <= maxlength) if maxlength else True
if do_it:
do_something()

but it really does not look intuitive. Hmm. My idea was that None is a
perfect Value for infinity since there is no infinitely large number.
But as i see it, you must have two comparisons then. Maybe someone has a
better idea?

Wolfgang
 
S

Steven D'Aprano

That means:
if maxlength and (len(string) <= maxlength):

is equivalent to
if (maxlength is not None) and (len(string) <= maxlength):

which is more complicated to type and -in my opinion- not so intuitive.
But because it is equivalent, it is a matter of taste, what to use.

Incorrect. The two are *not* equivalent.


def test(maxlength, string):
flag1 = maxlength and (len(string) <= maxlength)
flag2 = (maxlength is not None) and (len(string) <= maxlength)
return bool(flag1), bool(flag2) # normalise to booleans

(False, True)


So the two forms will take opposite branches of the if statement when
maxlength is 0 and string is the empty string.
 
W

Wolfgang Meiners

Am 26.02.12 13:52, schrieb Chris Angelico:
On the contrary, it means they are distinctly NOT equivalent. The
shorter form would treat a maximum length of 0 as meaning "unlimited".
Now, that's an understandable notation, but it's not what's given
here; if None means unlimited, then 0 should enforce that string ==
"".

ChrisA

You are right. It seems I did not get the line
zero of any numeric type, for example, 0, 0L, 0.0, 0j.
right.

Wolfgang
 
W

Wolfgang Meiners

Am 26.02.12 14:16, schrieb Wolfgang Meiners:
I just had a closer look at it. It seems to be more complicated than i
thougth: You will have to write

Obviously not close enough, as i just learned.
def confine_length(string, maxlength=None):
if maxlength: # maxlength exists, comparison possible
if maxlength is not None: # maxlength exists, comparison possible
if len(string) <= maxlength:
do_something()
else: # maxlength does not exist, so always do something
do_something()

you migth also write

def confine_length(str, maxlength=None):
do_it = (len(str) <= maxlength) if maxlength else True
do_it = (len(str) <= maxlength) if maxlength is not None else True
if do_it:
do_something()

I hope, it's correct now.
Wolfgang
 
A

Arnaud Delobelle

     do_it = (len(str) <= maxlength) if maxlength is not None else True

That's a funny way to spell:

do_it = maxlength is None or len(str) <= maxlength
 
S

Steven D'Aprano

I just had a closer look at it. It seems to be more complicated than i
thougth: You will have to write

def confine_length(string, maxlength=None):
if maxlength: # maxlength exists, comparison possible
if len(string) <= maxlength:
do_something()
else: # maxlength does not exist, so always do something
do_something()

No, that still takes the wrong branch for maxlength = 0.

Be explicit in your code. If you want maxlength=None to be a sentinel for
"avoid the length test", then explicitly test for maxlength is None,
don't be tempted to take short-cuts that can fail.

def confine_length(string, maxlength=None):
if maxlength is None: # no length comparison needed
do_something()
elif len(string) <= maxlength:
do_something()


This can be simplified to:

def confine_length(string, maxlength=None):
if maxlength is None or len(string) <= maxlength:
do_something()


Or even simpler:

def confine_length(string, maxlength=float('inf')):
if len(string) <= maxlength:
do_something()
 
A

alex23

Sure there are float INFINITIES that work fine for ints and floats,
but where is the consistency?

Sure, there are all of the COMPLEXITIES of floating point arithmetic
but I want to ignore all of that and demand ridiculous consistencies.
Why should I have to do float(some_int) < float('inf') when it's a far
better use of my time to spend days if not weeks bemoaning yet another
language wart? Why should I be expected to know what float('inf')
actually represents before making stupid demands like:
INFINITY need not be a int or a float or
a str, or whatever.

Please provide a non-contrived use case of an "infinite string".
INFINITY should be at the very least a constant of the math module.

Why? This isn't a mathematical concept of 'infinite' when you're
talking about comparing against "str, or whatever". We need a more
appropriate location; please initiate a PEP to add the namespace
shit.rick.wants into the stdlib.
 
S

Steven D'Aprano

Sure, there are all of the COMPLEXITIES of floating point arithmetic but
I want to ignore all of that and demand ridiculous consistencies. Why
should I have to do float(some_int) < float('inf')

Ints and floats can be compared directly, no need to convert the int to a
float first:
True


Likewise Fractions and Decimals, at least in Python 3.2 (possibly not in
older versions):
True



when it's a far
better use of my time to spend days if not weeks bemoaning yet another
language wart? Why should I be expected to know what float('inf')
actually represents before making stupid demands like:


Please provide a non-contrived use case of an "infinite string".

Any lazy stream of characters that potentially goes on forever could be
considered an infinite string. But that's not what Rick is talking about.

He's talking about having a pair of special values, say, BIGGEST and
SMALLEST, which compare larger and smaller to any other value, regardless
of type and including strings, not literally a string with an infinite
number of characters.

I can see some value for this as a convenience, but not enough to make it
a built-in language feature. Every developer should have at least one
utility module with all the trivial code snippets they frequently use.
This belongs in there.
 
C

Chris Angelico

I can see some value for this as a convenience, but not enough to make it
a built-in language feature. Every developer should have at least one
utility module with all the trivial code snippets they frequently use.

+1. I used to call mine "oddsends" - it nicely fitted into eight
characters (yeah, was important then - I was on DOS), and I had quite
a few odds and ends in there.

ChrisA
 
A

alex23

Ints and floats can be compared directly, no need to convert the int to a
float first

Ah, cheers. You can see how often I use the two interchangeably :)
Any lazy stream of characters that potentially goes on forever could be
considered an infinite string. But that's not what Rick is talking about.

He's talking about having a pair of special values, say, BIGGEST and
SMALLEST, which compare larger and smaller to any other value, regardless
of type and including strings, not literally a string with an infinite
number of characters.

Yeah, my point was more to highlight Rick's laziness in co-opting a
defined term - INFINITE - and trying to use it to mean something else
that he couldn't express clearly. His original post stressed numeric
comparison, the feature creep to include all other types happened
later. Not the sort of thing we've come to expect from the resident
linguist extraordinaire :)
I can see some value for this as a convenience, but not enough to make it
a built-in language feature.

For me, it feels like a step backwards to comparing different types:
Traceback (most recent call last):
Every developer should have at least one
utility module with all the trivial code snippets they frequently use.
This belongs in there.

Agreed. Especially when it's so trivial:

class Bound(object):
def __init__(self, value=None, always_greater=False):
self.value = value
self.always_greater = always_greater

def __cmp__(self, other):
return True if self.always_greater else self.value.__cmp__(other)
True
 
J

Jean-Michel Pichavant

Rick said:
[...]
That should be:
if maxlength is not None and len(string) <= maxlength:

Using "imaginary" infinity values defiles the intuitive nature of your
code. What is more intuitive?

def confine_length(string, maxlength=INFINITY):
if string.length < maxlength:
do_something()

def confine_length(string, maxlength=None):
if maxlength is not None and len(string) <= maxlength:
do_something()
This one:

def confine_length(string, maxlength=None):
"""Confine the length.

@param maxlength: the maximum length allowed, set it to None to allow any length.
"""
if maxlength is not None and len(string) <= maxlength:
do_something()


I'm just feeding the troll, I know ... :-/

JM
 
N

Neil Cerutti

but it really does not look intuitive. Hmm. My idea was that
None is a perfect Value for infinity since there is no
infinitely large number. But as i see it, you must have two
comparisons then. Maybe someone has a better idea?

I do. A truncated string with a maxlength of INFINITY is just a
string.
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top