News for hackers. Stuff that matters.
Re:Show HN: Change Python code while it's running using a re... by uryga on Friday October 18, 2019 @12:03PM (#21292507)
i wrote something similar a while ago, and it gave rise to a pretty head-scratching bug... my code was doing something like

  if type(x) is Foo:
and mysteriously failed for some data around the reload. turns out that Foo objects from before the reload were using a different class object than Foo objects after the reload (which makes sense in hindsight, as Foo was redefined). fun times debugging that!
Re:Show HN: Change Python code while it's running using a re... by julvo on Friday October 18, 2019 @03:17PM (#21294262)
That's a pain to debug. A similar thing happened to me with reload from Python's importlib. In the case of the reloading loop I think this will only happen when redefining the type within the loop, either through a class definition or an importlib reload.
Re:Show HN: Change Python code while it's running using a re... by uryga on Friday October 18, 2019 @03:27PM (#21294355)
yeah, importlib.reload caused mine as well! i'm wondering if there's a way to avoid that... maybe a @reloadable class decorator that saves a reference to each object you instantiate and switches out their .__class__ upon reload? obv not great for performance, but could be useful during development (which is where i'd mostly use hot reloading anyway)
Re:Show HN: Change Python code while it's running using a re... by orf on Friday October 18, 2019 @03:33PM (#21294420)
I did a talk on this recently at EuroPython. Hot reloaders are really, really really hard to implement in Python in the general case (I.e not an IPython shell). You run into corner cases like this, and it basically comes down to reloading the whole app anyway.
Re:Show HN: Change Python code while it's running using a re... by znpy on Friday October 18, 2019 @07:18PM (#21295829)
link plz?
Re:Show HN: Change Python code while it's running using a re... by orf on Saturday October 19, 2019 @06:27AM (#21297777)
https://youtu.be/IghyoR6ld60

I didn't go into as much detail as I would have liked about hot reloaders, but the tl;dr is that if you have a _single_ reference to something, then hot reloading it is somewhat simple. The problem comes when you want to hot reload a single module inside a large program.

Hot reloading a loop like this is super cool, but it only works if you're just changing constants and not defining anything. The moment you want to change the definitions of an object in the loop and hot reload it then you're in a lot of pain.

Here's the source code for IPythons autoreload magic command: https://github.com/ipython/ipython/blob/cd54f15544eee69449cc.... Take note of the Caveats section.

Re:Show HN: Change Python code while it's running using a re... by nerdponx on Friday October 18, 2019 @06:48PM (#21295675)
Would this still fail if you used the preferred idiom of

    isinstance(x, Foo)
Re:Show HN: Change Python code while it's running using a re... by uryga on Friday October 18, 2019 @11:28PM (#21296599)
had to check to make sure isinstance wasn't doing some magic, but yes. the whole issue is that as far as python is concerned, the pre- and post-reload Foo classes aren't related in any way except by name and module, which aren't checked by either of these methods

(aside: i know that `isinstance` is the official way, but i just don't like the way it reads – "x is an instance of y" is an infix construct and looks weird in a prefix position. i don't really use inheritance so `type(x) is y` is good enough, and it reads very naturally)

Re:Show HN: Change Python code while it's running using a re... by throwaway66920 on Friday October 18, 2019 @10:16AM (#21291544)
When I first read this I couldn’t think of a good use case. Then the example shown seemed like something I deal with all the time and wish I could do. But then I realized I would need to remember to add reloading to the range before I commit to the long loop I wish to change. Pretty damn cool though.

If it doesn’t add much overhead this would be amusing for something like game dev, swapping out how the game works while running it.

Re:Show HN: Change Python code while it's running using a re... by ken on Friday October 18, 2019 @06:45PM (#21295665)
This is how Lisp developers have worked for decades. It's been used for games before, too, e.g., https://en.wikipedia.org/wiki/Game_Oriented_Assembly_Lisp
Re:Show HN: Change Python code while it's running using a re... by Torwald on Friday October 18, 2019 @08:33AM (#21290809)
Like ESR said: "these languages [perl, python, ruby] subsequently become more like lisp" (paraphrasing)
Re:Show HN: Change Python code while it's running using a re... by marcpaq on Friday October 18, 2019 @10:28AM (#21291649)
Are you paraphrasing ESR paraphrasing Greenspun's Tenth Rule of Programming?

"Any sufficiently complicated C or Fortran program contains an ad-hoc, informally-specified bug-ridden slow implementation of half of Common Lisp."

Ref: http://philip.greenspun.com/research/

P.S. This is not a comment on TFA. Congratulations to the author on making Python even more interactive.

Re:Show HN: Change Python code while it's running using a re... by Torwald on Saturday October 19, 2019 @12:48PM (#21299367)
I am not so sure anymore where I read it. This is interesting:

https://mail.python.org/pipermail/python-dev/2003-January/03...

It refers to this, I guess. http://www.paulgraham.com/lispfaq1.html?viewfullsite=1

"If you look at these languages in order, Java, Perl, Python, you notice an interesting pattern. At least, you notice this pattern if you are a Lisp hacker. Each one is progressively more like Lisp."

This is the basic thought. Turns out PG wrote it more explicitly than ESR.

Re:Show HN: Change Python code while it's running using a re... by kamyarg on Friday October 18, 2019 @09:04AM (#21291012)
Who is ESR?

Edit: Ah, it is https://en.wikipedia.org/wiki/Eric_S._Raymond , found after some googling

Re:Show HN: Change Python code while it's running using a re... by ovi256 on Friday October 18, 2019 @07:34AM (#21290410)
This is great work, I could not believe the fact that it keeps state (like the current epoch number, and of course all the current model state). Makes Python even more dynamic.

Nice that you could condense it to less than 100 lines of Python!

Re:Show HN: Change Python code while it's running using a re... by deehouie on Friday October 18, 2019 @01:37PM (#21293401)
This is a great debugging tool for deep learning. Often your code might have been running for 20 hrs on 16 GPUs and you just don't want to restart from scratch again.
Re:Show HN: Change Python code while it's running using a re... by mrmuagi on Friday October 18, 2019 @03:36PM (#21294444)
You don't have to without this tool, you can save and load models from/to disk already from provided APIs for frameworks/libraries (e.g. [1]). Handy for when you need to hard reset, move, and/or share data.

[1] https://pytorch.org/tutorials/beginner/saving_loading_models...

Re:Show HN: Change Python code while it's running using a re... by liuliu on Friday October 18, 2019 @05:09PM (#21295109)
Yeah. But wouldn't it be nice / cool if the training process is interactive, reproducible, recordable and replayable?
Re:Show HN: Change Python code while it's running using a re... by mrmuagi on Friday October 18, 2019 @07:03PM (#21295748)
Not sure I totally agree that this achieves those goals, but I can't argue they would be ideal goals.

>interactive

AFAIK, as you type python code it will execute with the changes incorporated in the next loop execution. That's a heavily constraint "interactive" (forward propagating changes against an active execution). I mean, wouldn't a python shell with shell history be more interactive and hit all your goals? Sure the advantage of this is you can change code based on output feedback, but if you are too slow, you have to backtrack, and if you are too fast, you just wait anyways in both cases. And if you want to add code to earlier loop executions, you are kaput for earlier loop executions since those are set in stone against a different version of the code?

> reproducible

How can you get this, i.e. reproduce any loop changes outside of IDE/editor code undo/redo stack. It's hiding all the magic of what code outputted what with the "reloading" function.

> recordable, replayable

This moves the "history" to the IDE/editor code undo/redo stack. I think this is worse, cause, what happens when you close the IDE/editor without recording which specific console output was for which version of the code?

My main gripe is this sentence in the README:

> This lets you e.g. add logging, print statistics or save the model without restarting the training and, therefore, without losing the training progress.

If the italicised is the prime concern, and the purpose of this is to solve that, I'm just saying PyTorch already lets you save/persist training data, maybe the author didn't know about it + python shell combo?

Re:Show HN: Change Python code while it's running using a re... by noobiemcfoob on Friday October 18, 2019 @09:57AM (#21291366)
Really nice tool! I often develop in a similar pattern (though not quite as functional) using IPython and it's %autoreload magic. This looks a lot cleaner.
Re:Show HN: Change Python code while it's running using a re... by kaffeemitsahne on Friday October 18, 2019 @10:39AM (#21291739)
Could be very nice for live coding music/visuals etc.
Re:Show HN: Change Python code while it's running using a re... by technicalbard on Friday October 18, 2019 @01:15PM (#21293172)
Ah, something Common Lisp has had for over 30 years.
Re:Show HN: Change Python code while it's running using a re... by civilian on Friday October 18, 2019 @02:24PM (#21293834)
It'd be cool to be able to use this as a method decorator too!
Re:Show HN: Change Python code while it's running using a re... by julvo on Friday October 18, 2019 @02:52PM (#21294071)
Brilliant idea. That would make it useful for Keras, too, using decorated callbacks during training
Re:Show HN: Change Python code while it's running using a re... by PerryCox on Friday October 18, 2019 @07:37AM (#21290424)
Anyone else getting this error? I tried on Debian 9 and MacOS 10.14.6 after installing via pip.

  import reloading
  Traceback (most recent call last):
    File "<input>", line 1, in <module>
      import reloading
    File "/home/lucas.mccoy/.local/lib/python2.7/site-packages/reloading/__init__.py", line 1, in <module>
    from .reloading import reloading
    File "/home/lucas.mccoy/.local/lib/python2.7/site-packages/reloading/reloading.py", line 87
    exc = exc.replace('File "<string>"', f'File "{fpath}"')
                                                         ^
  SyntaxError: invalid syntax
Re:Show HN: Change Python code while it's running using a re... by julvo on Friday October 18, 2019 @07:42AM (#21290451)
It only works with Python 3. Thanks for reminding me that I should add a note to the Readme.
Re:Show HN: Change Python code while it's running using a re... by compressedgas on Friday October 18, 2019 @08:32AM (#21290801)
It does work in Python 2 with the following patch:

    diff --git a/reloading/reloading.py b/reloading/reloading.py
    index 1d28e2f..1a5f981 100644
    --- a/reloading/reloading.py
    +++ b/reloading/reloading.py
    @@ -84,9 +84,10 @@ def reloading(seq):
                 exec(body)
             except Exception:
                 exc = traceback.format_exc()
    -            exc = exc.replace('File "<string>"', f'File "{fpath}"')
    +            exc = exc.replace('File "<string>"', 'File "{}"'.format(fpath))
                 sys.stderr.write(exc + '\n')
    -            input('Edit the file and press return to continue with the next iteration')
    +            print('Edit the file and press return to continue with the next iteration')
    +            sys.stdin.readline()
     
         # copy locals back into the caller's locals
         for k, v in locals().items():
Re:Show HN: Change Python code while it's running using a re... by julvo on Friday October 18, 2019 @08:33AM (#21290810)
Brilliant, thank you. I'll patch that.
Re:Show HN: Change Python code while it's running using a re... by progval on Friday October 18, 2019 @05:12PM (#21295131)
That's an f-string, so it only works with Python 3.7 and later
Re:Show HN: Change Python code while it's running using a re... by kbd on Friday October 18, 2019 @05:30PM (#21295251)
f-strings were introduced in Python 3.6, fyi:

https://docs.python.org/3/whatsnew/3.6.html#whatsnew36-pep49...

Re:Show HN: Change Python code while it's running using a re... by yomansat on Friday October 18, 2019 @05:19PM (#21295177)
FYI there's also reload(module) which is a built-in function. One great use-case I've come across is in developing a network proxy for game packet interception, see:

https://youtu.be/iApNzWZG-10?t=641

Re:Show HN: Change Python code while it's running using a re... by Waterluvian on Friday October 18, 2019 @12:53PM (#21292946)
Django's development mode aka "runserver" behaves a lot like this (as does a ton of different Node/JavaScript systems, many of which are much more precise on what sub-components to actually re-load). It's a big win for development pacing.

Being able to loop a specific segment of code looks like a great tool for my toolbox when writing data processing code, particularly for spatial data. I will have an input dataset and be iterating on the algorithm(s) as I watch the output (often rendered in QGIS) update dynamically but requiring me to re-run the test script.

Re:Show HN: Change Python code while it's running using a re... by civilian on Friday October 18, 2019 @02:13PM (#21293714)
Agreed, but to clarify-- django's runserver is a simple restart process upon file change. Webservers don't have heavy startup costs, so that's fine.

Being able to modify code during a run, where it has a heavy startup cost, is a different beast. I also wonder if Django's runserver could incorporate something like `reloading`, for even speedier reloads?

Re:Show HN: Change Python code while it's running using a re... by _carl_jung on Friday October 18, 2019 @07:59AM (#21290560)
This is awesome. A nice improvement would be to have the loop continue with the old code when there are errors (rather than pausing iteration).
Re:Show HN: Change Python code while it's running using a re... by rcfox on Friday October 18, 2019 @01:15PM (#21293175)
That seems like it would be confusing. Imagine trying to track down the source of behaviour that no longer exists in your code...
Re:Show HN: Change Python code while it's running using a re... by ianai on Friday October 18, 2019 @08:09AM (#21290631)
How would that be possible without having to essentially keep track of two files? Nice idea though.
Re:Show HN: Change Python code while it's running using a re... by earenndil on Friday October 18, 2019 @10:51AM (#21291864)
Basically, you just need a double buffer system:

- Always run from the back buffer

- Always scan the updated file into the front buffer

- If the front buffer is fresh AND the front buffer contains no bugs, bufferswap

Re:Show HN: Change Python code while it's running using a re... by kazinator on Saturday October 19, 2019 @10:37AM (#21298719)
Change Bourne shell code while it's running:

  $ vi ~/.functions   # fix
  $ . ~/.functions    # load latest
Re:Show HN: Change Python code while it's running using a re... by potta_coffee on Friday October 18, 2019 @02:54PM (#21294087)
Python is almost as good as Lisp now.