Code Monkey home page Code Monkey logo

remove-print-statements's Introduction

remove-print-statements

test codecov pre-commit.ci status PyPi Status Python versions MIT License

A CLI 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 pip or equivalent.

python -m pip install remove-print-statements

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


for _ in range(5):
    print()

if __name__ == "__main__":
    print()

Running remove-print-statements will transform the above code as below:

def foo():
    pass


for _ in range(5):
    pass

if __name__ == "__main__":
    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

packaing: poetry code style: black pre-commit: enabled

Release

  1. Run poetry lock
  2. Bump version in pyproject.toml and __version__ variable
  3. Commit and push the changes with message release: <version>
  4. 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.

remove-print-statements's People

Contributors

dhruvmanila avatar pre-commit-ci[bot] avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.