2 Tools to Automatically Reload when Python Files Change | by Khuyen Tran | Towards Data Science

Have you ran a for-loop and wished to add more details to the code inside the for loop? You might decide not to because adding more details means that you need to stop your progress and rerun everything again.
Stopping your progress is especially not ideal if the code has been running for hours. Wouldn’t it be great if you can reload a loop body on each iteration without losing state like below?
In this article, I will show you 2 tools to reload the code or run arbitrary commands when your Python files change.
entr allows you to run arbitrary commands whenever the files change. To understand how entr can be helpful, let’s imagine we want to create a function that takes a list of numbers and returns even numbers.
We name the file above as entr_examples.py .Then use pytest to check if theget_evens function returns even numbers.
$ pytest entr_examples.py
The test fails because we haven’t added any code to the get_evens function yet. To try different ways to get even numbers, let’s use entr to rerun the command pytest entr_examples.py whenever the code changes.
$ ls entr_examples.py | entr python entr_examples.py
Cool! By using entr, we don’t need to rerun the same command all over again while developing our function.
Since the first law of Test Driven Development is “You may not write production code until you have written a failing unit test”, entr encourages developers to test different ways to write code until it works.
Below is the general syntax of entr:
Note that if you want to track multiple Python files at once, use ls *.py | entr command_to_runinstead. This means if any of the files being tracked are changed, command_to_run will be executed.
To install entr on MacOS, type:
brew install entr
To install entr on Ubuntu, type:
sudo apt-get updatesudo apt-get install entr
Another option is to clone entr repo and follow the installation instructions.
Reloading is a Python library that allows you to reload a loop body on each iteration without losing its state.
To understand how reloading can be useful, imagine you write a Python loop that prints even numbers. However, while the loop is running, you also want it to print odd numbers without stopping the loop.
Normally, that cannot be done in Python. In the code below, even though we add print(f'{num} is odd') to the loop body during the execution, there are no odd numbers in the output.
However, that could easily be done with reloading. The GIF below shows how adding reloading to the loop can make a difference.
As we can see, the odd numbers are printed after adding an else statement to the loop body during the execution!
Reloading doesn’t seem really helpful here since it doesn’t take a long time to execute a for loop. But this will become really handy if you have trained a deep learning model for hours and you want to print more statistics in each epoch without stopping the loop.
Find how to use reloading with your favorite libraries here.
To use reloading , simply add it to a Python loop like below:
pip install reloading
Congratulations! You have just learned how to run arbitrary commands when your Python files change and reload a Python loop body without losing its state. Now you can prevent wasting time on running the same command or rerunning a loop to show more outputs.
Feel free to play and fork the source code of this article here:
github.com
I like to write about basic data science concepts and play with different algorithms and data science tools. You could connect with me on LinkedIn and Twitter.
Star this repo if you want to check out the codes for all of the articles I have written. Follow me on Medium to stay informed with my latest data science articles like these: