Additional tips#
Code formatting#
Why does formatting matter?
Code is read more often than written
Setting up a formatter in your editor takes 5 minutes
Those 5 minutes are redeemed across the lifetime of the project
Ugly formatting example#
Put the following into a file myscript.py
and save it.
x = { 'a':37,'b':42,
'c':927}
y = 'hello '+ 'world'
class foo ( object ):
def f (self ):
return y **2
def g(self, x :int,
y : int=42
) -> int:
return x--y
def f ( a ) :
return 37+-a[42-a : y*3]
Black Code Formatter#
We chose black because it has very few options with which to fiddle.
First you need to install Black
in your environment:
conda install black
Then, the simplest way to use is
black myscript.py
The code should be automatically formatted to:
x = {"a": 37, "b": 42, "c": 927}
y = "hello " + "world"
class foo(object):
def f(self):
return y ** 2
def g(self, x: int, y: int = 42) -> int:
return x - -y
Warning
Note that black
just fix the code layout - this code still has syntax error!
Integrated development environment (IDE)#
Using an Integrated development environment (IDE) will certainly save you time, but the advantages of using an IDE go beyond that. Below are some IDE advantages
Syntax highlighting
Text autocompletion
Refactoring options
Easily Importing libraries
Build, compile, or run
Visual Studio Code#
You may already have a preferred IDE that you use regularly, however we strongly suggest that you use VS Code for this course and afterwards replicate the setup as you choose. If you already have VS Code installed please make sure it is updated to the latest version.
To install VS Code follow the instructions here.
Below are some VSC advantages
IntelliSense: Go beyond syntax highlighting and autocomplete
Run & Debug: Debug code right from the editor
Built-in Git: Review diffs, stage files, and make commits right from the editor.
Extensible and customizable: Install extensions to add new languages, themes, debuggers, and to connect to additional services.
VSC Example: automatically using black#
Configure VSC to use Black
Open Settings: Code (or File) > Preferences > Settings
Search for
python formatting provider
and chooseblack
Search for
format on save
and check the box to enable
Select interpreter
Open Command Palette
: View > Command Palette..
(or Ctrl+Shift+P
)
Search for
Python: Select Interpreter
Choose the correct environment
Now the Black package is going to fix your codes layout every time you save a code file.