paint-brush
Automate Python Dev in VS Codeby@andreas.007
4,640 reads
4,640 reads

Automate Python Dev in VS Code

by AndreasAugust 21st, 2018
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

We are going to talk about how to setup python development environment with Visual Studio Code. We gonna put here up on how to automate several tiny bitty important stuff to make the coding in Python standardise in reference with PEP :

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - Automate Python Dev in VS Code
Andreas HackerNoon profile picture

We are going to talk about how to setup python development environment with Visual Studio Code. We gonna put here up on how to automate several tiny bitty important stuff to make the coding in Python standardise in reference with PEP :

  1. Automate Linter Check using PyLint to inspect your code
  2. Auto formatting the code
  3. Automate running unittest on save
  4. Automate coverage run on save, with graphical indicator (green / red / blank) on the code itself

Table of Content








MotivationAssumptionPrerequisiteAction00 — Create Our ProjectAction01 — PyLint Standardise our CodeAction02 — Autoformatting on SaveAction03 — Automate Unittest & Coverage ReportsConclusion

Motivation

I am totally just start writing in Python !

Personally I am interested in this because I am starting to embark on PySpark project via a relatively new Amazon Serverless Service of AWSGlue; Hence where python start to come into my life.

Previously I was coding in Golang, where my development environment has automate much of stuff to help me produce good code. There I want it too on my Python !

The challenge is its not that easy to find the way to do so in VS Code, hence this article shall show you how to use multiple existing extensions out there to achieve this.

Assumption

This article is written on the perspective of a Mac OS users, comparable stuff should be translatable to other machines like windows, though further own searching to adapt might required.

Prerequisite

Tools :

  1. Installing Python 2.7 in your host machine (cos’ in my case AWS Glue works only with 2.7 version at the moment — Aug 2018)
  2. PIP
  3. Virtualenv
  4. Visual Studio Code
  5. VSCode Python Plugin by Microsoft

Skills :

  1. Installing plugins extension in VSCode
  2. Basic Python Coding (Their syntaxes etc)

Action00 — Create Our Project

  • start project
  • virtualenv

let’s create a project, start with an empty project, here I named it python-dev-setup. Open up the directory on Visual Studio Code.

The .gitignore and LICENSE file there are stuff we got from github.

let’s create the virutalenv for this project, open up a terminal into this project folder (cmd + j)

$ virtualenv venv

Project with Virtualenv

Virtualenv is very crucial for a more tidy Python project as it tidy up the dependencies.

Set the workspace to use our venv for its python intrepreters. Open Code > Preferences > Settings, and go to the WORKSPACE SETTINGS tab. Set it up as the following snippet & save it.




{"python.pythonPath": "${workspaceFolder}/venv/bin/python","python.venvPath": "${workspaceFolder}/venv"}

Setting our Workspace Python Intrepreter to use venv

Action01 — PyLint Standardise our Code

Next we are going to setup linter in a more detailed analysis mode, so that it reminds us to type documentation etc.


Install the pylint for our venvFirst we activate the virutalenv, then we run a pip install command.


... $ . venv/bin/activate(venv)... $ pip install pylint

Open Code > Preferences > Settings, and go to the WORKSPACE SETTINGS tab. Set it up as the following snippet & save it.





{"python.pythonPath": "${workspaceFolder}/venv/bin/python","python.venvPath": "${workspaceFolder}/venv","python.linting.pylintUseMinimalCheckers": false}

For checking out the effect of our actions we shall create a simple codes.

Create a directory to holds our python sample code, python_dev_setup, and a file inside called multiply.py and __init__.py. (__init__.py is to indicates that it is a module directory)

Type the following code inside the multiply.py, and save it


def multiply_2(num):return num * 2

Noticed the swirling line and hover over it, you will be served with these 2 pylint messages


[pylint] C0111:Missing module docstring[pylint] C0111:Missing function docstring

Now you’ve got assistance to remind you to follow good standard of coding.

Let’s follow the pylint recommendation

"""Simplify Multiplication"""



def multiply_2(num):"""multiply with 2"""return num * 2

There you go ! now no more shouts from pylint, we got better coding standard!

Action02— Autoformatting on Save

By this step we gonna have an auto format based on PEP8 for every save we do, which helps us to produce standard code type layout, and focus more on our code logic. Standardisation of layout is very crucial as it shows your craft, easy on the eyes, what is easy on the eyes is good as it increase the code review accuracy.

Start with installing Autopep8 in our virtualenv


... $ . venv/bin/activate(venv)... $ pip install autopep8

Open Code > Preferences > Settings, and go to the WORKSPACE SETTINGS tab. Set it up as the following snippet & save it.







{"python.pythonPath": "${workspaceFolder}/venv/bin/python","python.venvPath": "${workspaceFolder}/venv","python.linting.pylintUseMinimalCheckers": false,"python.formatting.provider": "autopep8","editor.formatOnSave": false}

Setting up Workspace Settings Autoformat with PEP8

Following up the sample code from Action01— PyLint Standardize our Code, lets try the effect of the above setup.

Open up the multiply.py, remove the space between module docstring and the function definition (dont save yet)




"""Simplify Multiplication"""def multiply_2(num):"""multiply with 2"""return num * 2


Now save it !You’ll see that it automatically re-formatted into spreading 2 linebreaks

Autoformat with Autopep8

Action03 — Automate Unittest & Coverage Reports

  1. ensure that we have the coverage python packages in our venv.


... $ . venv/bin/activate(venv)... $ pip install coverage

2. Install the following plugins in VSCode : Coverage Gutter and RunOnSave (reload of VSCode might needed)

3. Prepare an empty-cov.xml under a tools directory








<?xml version="1.0" ?><coverage><sources><source>empty</source></sources><packages></packages></coverage>

empty-cov.xml

4. Update the VSCode workspace settings into the following















{"python.pythonPath": "${workspaceFolder}/venv/bin/python","python.venvPath": "${workspaceFolder}/venv","python.linting.pylintUseMinimalCheckers": false,"python.formatting.provider": "autopep8","editor.formatOnSave": false, "emeraldwalk.runonsave": {"autoClearConsole": true,"commands": [{"match": "\\.py$","cmd": "set -e; cd ${workspaceRoot}; . venv/bin/activate; cp tools/empty-cov.xml cov.xml; coverage run --source=${fileDirname} -m unittest discover -p \"*test*.py\" -f -s ${fileDirname}/; coverage xml -o cov.xml; coverage report -m; deactivate"}]}}

Explanation:

set -e // exit on any fail exitcode of the command

cd ${workspaceRoot} // change directory to our workspace dir

. venv/bin/activate // activating virtuanenv

cp tools/empty-cov.xml cov.xml // initialise with empty coverage xml




// running coverage with unittest// referencing toward the file directory// and discover file with regex of *test*.py as unittest filecoverage run --source=${fileDirname} -m unittest discover -p "*test*.py" -f -s ${fileDirname}/

coverage xml -o cov.xml // generate xml report file named cov.xml


// printout on the Output of Run On Save our Coverage reportcoverage report -m

5. Let’s create a sample unittest file, called multiply_test.py under the python_dev_setup directory.



"""Unit Test for Multiply"""import unittestimport python_dev_setup.multiply as mpi


class TestMultiply(unittest.TestCase):"""Testing Multiply Function"""



def test_successful_multiply_2(self):"""Check if returns the 2 times of input integer"""self.assertEqual(mpi.multiply_2(5), 10)


6. Turn on the Gutter Watch Mode !(On the blue bottom bar, there is a Watch clickable, on activated it will says Remove Watch)

Coverage Unittest on Save !

Noticed the Green color indicator of your code being covered

7. On failing unit test, there will be blank color, and failure indicator on the Run On Save output.

Break the code !

8. On adding new function and no unittest yet it shall show red color on those not covered yet

Not Covered Yet ! Go Cover it !

Conclusion

Now you got a development environment which automatically assist you to produce better code!

Here is a repository of the sample project: https://gitlab.com/suekto.andreas/python-dev-setup/tags/v1.0.0