Code Monkey home page Code Monkey logo

sisort's Introduction

sisort

sandbox isort

pre-commit

Create virtualenvironment

$ virtualenv .env

Then we need to install pre-commit

(.env) $ pip install pre-commit

Initiate git:

$ git init

Add a pre-commit configuration

$ vim .pre-commit-config.yaml

in this example I copied from Django project.

Run:

$ pre-commit install

Reference: https://pre-commit.com/

Create a simple python script importing datetime module:

(.env) $ vim src/abrakadabra/views.py

from datetime import timedelta
from datetime import datetime


def index():
    pass

isort will complaining because we should import datetime first then timedelta (importing module should be in alphabetical order).

When we're trying to commit, we will see this:

(.env) $ gc

(.env) ➜  sisort git:(master) ✗ gc
isort....................................................................Failed
- hook id: isort
- files were modified by this hook

Fixing /home/za/git/sisort/src/abrakadabra/views.py

isort will automatically make the change:

(.env) ➜  sisort git:(master) ✗ gapa    
diff --git a/src/abrakadabra/views.py b/src/abrakadabra/views.py
index 08b8a38..b9a73c9 100644
--- a/src/abrakadabra/views.py
+++ b/src/abrakadabra/views.py
@@ -1,5 +1,4 @@
-from datetime import timedelta
-from datetime import datetime
+from datetime import datetime, timedelta

Added flake8 in pre-commit-config.yaml

Now we can prevent the new commit from having (example) syntax error. I believe we can configure this flake8.

➜  sisort git:(main) ✗ gc
isort....................................................................Passed
flake8...................................................................Failed
- hook id: flake8
- exit code: 1

src/abrakadabra/report.py:5:18: E999 SyntaxError: invalid syntax

So we can't commit until we fix this.

We can also have cleaner code if we have imported module but not being used:

➜  sisort git:(main) ✗ gc
isort....................................................................Passed
flake8...................................................................Failed
- hook id: flake8
- exit code: 1

src/abrakadabra/report.py:1:1: F401 'datetime' imported but unused

I believe there will be pros/cons of using pre-commit hook. It will make commit harder. Applying this at the beginning of the project seems great, but we need to discuss with the team if we're planning to apply this in ongoing project.

sisort's People

Contributors

za avatar

Watchers

 avatar  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.