A command-line tool (and pre-commit hook) to remove print statements from your Python project.
Project description
remove-print-statements
A command-line tool (and pre-commit hook) to remove all the print
statements
from your Python project.
Do you use print
statements for debugging? We all do, and there's nothing wrong
with it. After the bug has been resolved, we need to manually open all the files
which we added the print statements in, only if we remember all of them after
hours of debugging, and remove them. A better way would be to use some sort of
find and replace from the editor or command-line, but that's still a lot of
manual work. Worst case, it gets pushed and deployed to production.
Who wants to do all the manual work in the age of automation? No one. So,
install this tool and forget about removing the print statements manually
forever. You could either run this tool manually or add it as a pre-commit
hook. You could even preview the print statements along with it's location
without removing it. How nice is that!
Installation
You can install remove-print-statements
from the Python Package Index (PyPI)
with pipx
or equivalent.
pipx install remove-print-statements
Or, try it out using pipx
:
pipx run remove-print-statements --help
Or, with pre-commit in the repos
section of your
.pre-commit-config.yaml
file (docs):
- repo: https://github.com/dhruvmanila/remove-print-statements
rev: '' # Replace with latest tag on GitHub
hooks:
- id: remove-print-statements
# args: ['--verbose'] # Show all the print statements to be removed
Usage
Run it on a given set of files:
remove-print-statements foo.py bar.py ...
# or use globbing
remove-print-statements *.py
You can ignore files as well. To specify multiple files to ignore, use the flag multiple times otherwise it's difficult to know the difference between the files to ignore and the ones to check.
remove-print-statements *.py --ignore foo.py --ignore bar.py
You can preview the print statements which would be removed without modifying
the source files using both --dry-run
and --verbose
flags like so:
$ remove-print-statements --dry-run --verbose test.py
test.py
7 print("module")
18 print("property")
27 print("method")
29 print("for loop")
1 file would be transformed, 4 print statements would be removed
remove-print-statements
is a command-line tool that rewrites the files in
place. It focuses on upgrading your code and not on making it look nice. Run
remove-print-statements before formatters like Black.
remove-print-statements
does not have any ability to recurse through
directories. Use the pre-commit integration, globbing, or another technique for
applying to many files such as with git ls-files | xargs
.
Single statement
If there's only a single statement in the body of a function, for/while loop, if
statement, etc., which is a print statement, then it will be replaced with the
pass
keyword.
def foo():
- print()
+ pass
for _ in range(5):
- print()
+ pass
if __name__ == "__main__":
- print()
+ pass
Exit status
remove-print-statements
command returns exit statuses as follows:
Status | Description |
---|---|
0 | No print statements / changes made successfully |
1 | Files would be updated (dry run) |
123 | Some error happened |
Development
Release
- Run
poetry lock
- Bump version in
pyproject.toml
and__version__
variable - Commit and push the changes with message
release: <version>
- Publish a new release on GitHub which will trigger an automated workflow to publish on PyPi
License
remove-print-statements is licensed under the MIT License.
See LICENSE for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file remove_print_statements-0.5.2.tar.gz
.
File metadata
- Download URL: remove_print_statements-0.5.2.tar.gz
- Upload date:
- Size: 8.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.9.16 Linux/5.15.0-1038-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 61c7bac7feab96c712760c11285bc73f3caa822705bc192c67235cb297f22148 |
|
MD5 | 1949982a11f5463d5cfc9bb06ad58811 |
|
BLAKE2b-256 | b7123910d960b598cf623033c0a0a569fe43764dba7c66a26b957ed64088797b |
File details
Details for the file remove_print_statements-0.5.2-py3-none-any.whl
.
File metadata
- Download URL: remove_print_statements-0.5.2-py3-none-any.whl
- Upload date:
- Size: 8.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.9.16 Linux/5.15.0-1038-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2d9e6f56b735cb32fe1d5a1bd07f158fedbc5a0cf2fc9b6bb3728fecefd99823 |
|
MD5 | 60d9e4aa3680284a93929ba9fcae9a27 |
|
BLAKE2b-256 | e5e742bf9acbaa01df7d9738f00df1037a1eeba95bcaf845485ce4e8e28e7119 |