Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #54627

Re: reload and work flow suggestions

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <oscar.j.benjamin@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.008
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'float': 0.07; '101': 0.09; '[1]:': 0.09; '[2]:': 0.09; '[3]:': 0.09; 'integers': 0.09; 'try:': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'changes': 0.15; '100x': 0.16; '2):': 0.16; '381': 0.16; '[4]:': 0.16; 'cc:name:python list': 0.16; 'docstring': 0.16; 'file;': 0.16; 'internally': 0.16; 'last)': 0.16; 'nameerror:': 0.16; 'opens,': 0.16; 'scratch': 0.16; 'subject:flow': 0.16; 'wrote:': 0.18; 'basically': 0.19; 'meant': 0.20; 'import': 0.22; 'tests': 0.22; 'cc:addr:python.org': 0.22; 'load': 0.23; 'typical': 0.24; 'initial': 0.24; 'math': 0.24; 'looks': 0.24; 'cc:2**0': 0.24; 'script': 0.25; 'defined': 0.27; 'gets': 0.27; 'header:In-Reply- To:1': 0.27; 'point': 0.28; 'function': 0.29; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; '1-3': 0.31; '100000': 0.31; '4.0': 0.31; 'assert': 0.31; 'file': 0.32; 'probably': 0.32; "we're": 0.32; 'quite': 0.32; '(most': 0.33; 'except': 0.35; 'convert': 0.35; 'done.': 0.35; 'test': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'executing': 0.36; 'false': 0.36; "i'll": 0.36; 'positive': 0.37; 'too': 0.37; 'step': 0.37; 'handle': 0.38; 'whatever': 0.38; 'little': 0.38; 'recent': 0.39; 'launch': 0.39; 'sure': 0.39; 'how': 0.40; 'negative': 0.60; 'then,': 0.60; 'took': 0.61; 'name': 0.63; 'to:addr:gmail.com': 0.65; 'here': 0.66; 'close': 0.67; '10000': 0.68; 'paper': 0.75; '100': 0.79; 'forward,': 0.84; 'hardly': 0.84; 'optimisation': 0.84; 'oscar': 0.84; 'repeat.': 0.84; 'rusi': 0.91; 'baby': 0.95; '2013': 0.98
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; bh=tEAj15aGWwYkDR5+2VzC+qzXCpy5V8BpEV6nnarAHjI=; b=EVipjPHp27V9XRQ/0H4iWIW96RDv164zspAH3HSPMTh50Ew7C/LBgl3QcUiR065UTI Yxiazs3AJeWMeC84kKlwOXe9O0B4RVprVGJA1wDirYbT6vG5yr6u+ZbpFJJI5XL62VX0 GHsxoASBGVcjIIlYzao4cUwi9fgrPqWUfhfikh2fCMeiaf6Dq4VwOmZ+mFGViW5DmuZh EK7rm50W8GRnVu520PUAscdK01T7iBcnpRwoqECOtsIL/s/4zF67G0QL5B8plYYQvM61 YxBqSxIomgTy6BfpK+fsSUUzNDzXG3flsP2yCKB316IU3Ox6nHV0eINAaRUa5F54LMmA uZIQ==
X-Received by 10.58.187.47 with SMTP id fp15mr1060251vec.22.1379932872726; Mon, 23 Sep 2013 03:41:12 -0700 (PDT)
MIME-Version 1.0
In-Reply-To <bdbf27ac-65bc-43ed-88e1-9c0baca825f2@googlegroups.com>
References <61d45045-187c-4be7-9da4-21a3124b753f@googlegroups.com> <d15afc73-a4f3-4c10-b66e-41ceceae0fa5@googlegroups.com> <bdbf27ac-65bc-43ed-88e1-9c0baca825f2@googlegroups.com>
From Oscar Benjamin <oscar.j.benjamin@gmail.com>
Date Mon, 23 Sep 2013 11:40:52 +0100
Subject Re: reload and work flow suggestions
To rusi <rustompmody@gmail.com>
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
Cc Python List <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.258.1379932881.18130.python-list@python.org> (permalink)
Lines 194
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1379932881 news.xs4all.nl 15905 [2001:888:2000:d::a6]:47356
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:54627

Show key headers only | View raw


On 23 September 2013 10:35, rusi <rustompmody@gmail.com> wrote:
>> Then, I launch iPython, which can intellisense launch 3 easily. Then I make
>> whatever changes I need to 1-3 to make a baby step forward, close iPython,
>> and repeat.
>
> Hardly looks very ergonomic to me

I'm not quite sure what's meant by intellisense here but using ipython
with a scratch script works very well for me.

Here's how a typical session works for writing a sqrt_floor() function
that works for all positive integers:

In [1]: import math

In [2]: math.sqrt(16)
Out[2]: 4.0

In [3]: math.sqrt(15)
Out[3]: 3.872983346207417

In [4]: int(math.sqrt(15))
Out[4]: 3

In [5]: x = 10 ** 20 + 1

In [6]: int(math.sqrt(x ** 2)) == x
Out[6]: False

In [7]: edit tmp.py

[At this point vim opens, I do some working on paper and write the
following in vim:

import math

def sqrt_floor(y):
    x = int(y)
    while x ** 2 <= y < (x+1) ** 2:
        x = (x + y // x) // 2
    return x
]

done. Executing edited code...

In [8]: sqrt_floor(100)
Out[8]: 100

In [9]: sqrt_floor(101)
Out[9]: 101

[Okay whoops! that's not what I wanted... Need to invert the condition
in the while loop!]

In [10]: edit tmp.py
 done. Executing edited code...

In [11]: edit tmp.py
 done. Executing edited code...

In [12]: sqrt_floor(101)
Out[12]: 10

In [13]: sqrt_floor(100)
Out[13]: 10

In [14]: x = 10 ** 20 + 1

In [15]: sqrt_floor(x ** 2) == x
Out[15]: True

[Okay that's better]

In [16]: edit tmp.py

[Now I'll test against a load of integers so the file looks like

import math

def sqrt_floor(y):
    x = int(y)
    while not (x ** 2 <= y < (x+1) ** 2):
        x = (x + y // x) // 2
    return x


for y in range(10 ** 6):
    x = sqrt_floor(y)
    assert x ** 2 <= y < (x+1) ** 2
]
done. Executing edited code...

[All tests have passed but it took a little while so let's time this function]

In [17]: timeit sqrt(10 ** 10)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<snip traceback>
NameError: global name 'sqrt' is not defined

In [18]: from math import sqrt

In [19]: timeit sqrt(10 ** 10)
1000000 loops, best of 3: 393 ns per loop

In [20]: timeit sqrt_floor(10 ** 10)
10000 loops, best of 3: 52.4 us per loop

In [21]: edit tmp.py

[Since sqrt is 100x faster we can use it internally to get an initial guess.
Now the file looks like:

import math

def sqrt_floor(y):
    x = int(math.sqrt(y))
    while not (x ** 2 <= y < (x+1) ** 2):
        x = (x + y // x) // 2
    return x


for y in range(10 ** 6):
    x = sqrt_floor(y)
    assert x ** 2 <= y < (x+1) ** 2
]
done. Executing edited code...

In [22]: timeit sqrt_floor(10 ** 10)
100000 loops, best of 3: 4.58 us per loop

[Great. That's 10x faster than before and probably as good as it gets
for speed but this optimisation has problems...]

In [23]: sqrt_floor(10 ** 1000)
---------------------------------------------------------------------------
OverflowError                             Traceback (most recent call last)
<ipython-input-23-378bf805089d> in <module>()
----> 1 sqrt_floor(10 ** 1000)

Q:\tmp.py in sqrt_floor(y)
      2
      3 def sqrt_floor(y):
----> 4     x = int(math.sqrt(y))
      5     while not (x ** 2 <= y < (x+1) ** 2):
      6         x = (x + y // x) // 2

OverflowError: long int too large to convert to float

In [24]: edit tmp.py

[Now the file looks like

import math

def sqrt_floor(y):
    try:
        x = int(math.sqrt(y))
    except OverflowError:
        x = y
    while not (x ** 2 <= y < (x+1) ** 2):
        x = (x + y // x) // 2
    return x


for y in range(10 ** 6):
    x = sqrt_floor(y)
    assert x ** 2 <= y < (x+1) ** 2
]

 done. Executing edited code...
[ This reruns the tests in the file; no AssertionError so all good ]

In [25]: sqrt_floor(10 ** 1000)
Out[25]: 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000L

In [26]: timeit sqrt(10 ** 10)
1000000 loops, best of 3: 381 ns per loop

And now we're basically done. Recheck the proof, think about how to
handle negative numbers, write a docstring and move on.


Oscar

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

reload and work flow suggestions Peter Cacioppi <peter.cacioppi@gmail.com> - 2013-09-21 14:43 -0700
  Re: reload and work flow suggestions Terry Reedy <tjreedy@udel.edu> - 2013-09-21 18:04 -0400
  Re: reload and work flow suggestions Westley Martínez <anikom15@gmail.com> - 2013-09-21 21:01 -0700
  Re: reload and work flow suggestions rusi <rustompmody@gmail.com> - 2013-09-22 06:44 -0700
  Re: reload and work flow suggestions Peter Cacioppi <peter.cacioppi@gmail.com> - 2013-09-23 01:31 -0700
    Re: reload and work flow suggestions rusi <rustompmody@gmail.com> - 2013-09-23 02:35 -0700
      Re: reload and work flow suggestions Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-09-23 11:40 +0100
  Re: reload and work flow suggestions Peter Cacioppi <peter.cacioppi@gmail.com> - 2013-09-23 14:35 -0700

csiph-web