years later DeprecationWarning

Ben Finney bignose+hates-spam at benfinney.id.au
Wed Mar 22 18:02:17 EST 2006


Dan Jacobson <jidanni at jidanni.org> writes:

> Here's the deal: I have to maintain this long gone guy's programs and
> lately they've been saying
> ./north_pass.py:14: DeprecationWarning: integer argument expected, got float
>   fl=range(1000*(math.floor(25000*f2m/1000)),46000*f2m,1000)

You haven't shown us the value of all the terms there; specifically,
we don't know what value has been bound to 'f2m'.

Ideally, this code would have been written to be more easily readable
and explicit. This is an illustration that it's never too late to do
so, and that it can help you understand what the heck is going wrong.

Replace those literals with named constants, that indicate what the
heck they are.

    import math
    f2m = 1.0   # assuming this is where the float value gets introduced
    some_increment_thing = 1000
    some_starting_count = 25
    some_ending_count = 46
    some_starting_scale = some_starting_count * some_increment_thing
    some_ending_scale = some_ending_count * some_increment_thing
    fl = range(some_increment_thing*(math.floor(some_starting_scale*f2m/some_increment_thing)), some_ending_scale*f2m, some_increment_thing)

Use names that make sense in the problem domain, of course. The idea
is to not keep the reader (that's you, months or years from now)
guessing why '1000' is used three times, or whether it's mere
coincidence that all the other values seem to be multiples of 1000, or
whether each of those 1000s is meant to be the same thing, or whether
one of them can change, etc.

Split out this mess into separate operations, so you can see what's
failing.

    range_start = some_increment_thing * math.floor(some_starting_scale*f2m/some_increment_thing)
    range_limit = some_ending_scale * f2m
    fl = range(range_start, range_limit, some_increment_thing)

That will get you closer to the point of knowing what's causing the
error. It will also (if you've chosen meaningful names) make the code
much more understandable; and perhaps even give you ways to re-think
the algorithm used.

> (That long-gone guy is actually me, according to the notes in the
> program.  However those brain cells are long gone now, so it might
> as well not be me.)

One should always write code for some unknown future person, months or
years after the original context of the problem is forgotten, to
understand, without the benefit of your explanation. As you've found,
that person is most frequently oneself.

-- 
 \          "Friendship is born at that moment when one person says to |
  `\    another, 'What!  You too?  I thought I was the only one!'"  -- |
_o__)                                                       C.S. Lewis |
Ben Finney




More information about the Python-list mailing list