Skip to content

stefmolin/pre-commit-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Example of Using Pre-Commit Hooks

Example repository for article on setting up pre-commit hooks.

Requirements

Python >= 3.8

Setup

NOTE: If you choose to use GitHub Codespaces with this repository, you can skip ahead to step 4.

  1. Clone this repository and change to the directory for this repository:

    git clone git@github.com:stefmolin/pre-commit-example.git && \
        cd pre-commit-example;
  2. Create a virtual environment:

    python -m venv venv
  3. Activate the virtual environment:

    # bash/zsh
    source venv/bin/activate
    
    # Windows cmd.exe
    .\venv\Scripts\activate.bat
  4. Install the package (update pip if you encounter errors):

    python -m pip install -e .
  5. Install the pre-commit hooks in this repository:

    pre-commit install
  6. Create the file src/example/utils.py as follows and save it:

    import re
    
    def my_function(a):
        """My function."""
        pass
  7. Try to commit this file:

    git add src/example/utils.py
    git commit -m "Add utils.py"

    The pre-commit hooks will run, and your commit will be stopped since there are some violations:

    pre-commit hooks violations

  8. Fix those issues (example below) and then try to commit again.

    """Utility functions."""
    
    
    def my_function(a):
        """
        My function.
    
        Parameters
        ----------
        a : int
            The value to use.
        """
        pass

    If you fixed everything, your commit will be successful:

    pre-commit hooks passed

For an explanation of the setup, please consult the accompanying article.


About the Author

Stefanie Molin (@stefmolin) is a software engineer and data scientist at Bloomberg in New York City, where she tackles tough problems in information security, particularly those revolving around data wrangling/visualization, building tools for gathering data, and knowledge sharing. She is also the author of Hands-On Data Analysis with Pandas, which is currently in its second edition and has been translated into Korean. She holds a bachelor’s of science degree in operations research from Columbia University's Fu Foundation School of Engineering and Applied Science, as well as a master’s degree in computer science, with a specialization in machine learning, from Georgia Tech. In her free time, she enjoys traveling the world, inventing new recipes, and learning new languages spoken among both people and computers.

About

Example repository for article on setting up pre-commit hooks.

Resources

License

Stars

Watchers

Forks

Languages