years later DeprecationWarning

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dan Jacobson

    #1

    years later DeprecationWarning

    Here's the deal: I have to maintain this long gone guy's programs and
    lately they've been saying
    ../north_pass.py:1 4: DeprecationWarn ing: integer argument expected, got float
    fl=range(1000*( math.floor(2500 0*f2m/1000)),46000*f2 m,1000)

    As I don't know python, I tried sticking an int( ) in various places
    in that line but couldn't get whatever is bothering python to shut up. Help.

    (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.)

  • Terry Reedy

    #2
    Re: years later DeprecationWarn ing


    "Dan Jacobson" <jidanni@jidann i.org> wrote in message
    news:874q1qp51b .fsf@jidanni.or g...[color=blue]
    > Here's the deal: I have to maintain this long gone guy's programs and
    > lately they've been saying
    > ./north_pass.py:1 4: DeprecationWarn ing: integer argument expected, got
    > float
    > fl=range(1000*( math.floor(2500 0*f2m/1000)),46000*f2 m,1000)[/color]

    The warning is from the range function. If f2m is float, so too the first
    2 args. Either convert f2m to int first or both args to int. The second
    is what happens now. A long too large to convert to int also raises an
    error.

    Terry Jan Reedy



    Comment

    • Chris Lasher

      #3
      Re: years later DeprecationWarn ing

      Two things:
      1) math.floor returns a float, not an int. Doing an int() conversion on
      a float already floors the value, anyways. Try replacing
      math.floor(...) with int(...)
      e.g.[color=blue][color=green][color=darkred]
      >>> math.floor(5.9)[/color][/color][/color]
      5.0[color=blue][color=green][color=darkred]
      >>> int(5.9)[/color][/color][/color]
      5

      2) What kind of data is in f2m? If f2m is a float, you will get float
      values in the expressions that f2m is a multiplicand.

      Comment

      • Ben Finney

        #4
        Re: years later DeprecationWarn ing

        Dan Jacobson <jidanni@jidann i.org> writes:
        [color=blue]
        > Here's the deal: I have to maintain this long gone guy's programs and
        > lately they've been saying
        > ./north_pass.py:1 4: DeprecationWarn ing: integer argument expected, got float
        > fl=range(1000*( math.floor(2500 0*f2m/1000)),46000*f2 m,1000)[/color]

        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_c ount = 25
        some_ending_cou nt = 46
        some_starting_s cale = some_starting_c ount * some_increment_ thing
        some_ending_sca le = some_ending_cou nt * some_increment_ thing
        fl = range(some_incr ement_thing*(ma th.floor(some_s tarting_scale*f 2m/some_increment_ thing)), some_ending_sca le*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_sca le * f2m
        fl = range(range_sta rt, 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.
        [color=blue]
        > (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.)[/color]

        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

        Comment

        • Enigma Curry

          #5
          Re: years later DeprecationWarn ing

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

          I had a great long laugh with this bit. I guess it's because I can
          relate so well :)

          Comment

          • Steven D'Aprano

            #6
            Re: years later DeprecationWarn ing

            On Wed, 22 Mar 2006 13:59:00 -0800, Chris Lasher wrote:
            [color=blue]
            > Two things:
            > 1) math.floor returns a float, not an int. Doing an int() conversion on
            > a float already floors the value, anyways.[/color]

            No it doesn't, or rather, int() is only equivalent to floor() if you limit
            the input to non-negative numbers:

            int(-2.2) => -2, but floor(-2.2) should give -3.

            The standard definition of floor() and ceil() are:

            floor(x) = maximum integer n such that n <= x
            ceil(x) = minimum integer n such that n >= x

            or as Python functions:

            def floor(x):
            "Returns the maximum integer less than or equal to x"
            if x >= 0:
            return int(x)
            else:
            if x % 1: return int(x)-1
            else: return int(x)

            def ceil(x):
            "Returns the minimum integer greater than or equal to x"
            return -floor(-x)

            or even simpler:

            from math import floor, ceil

            (Caution: the functions defined in the math module return the floor and
            ceiling as floats, not int, so you may want to wrap them in a call to int.)



            --
            Steven.

            Comment

            Working...