Skip to content

Commit ec95331

Browse files
committed
refactor: update contents for coding conventions
1 parent 7fc11f8 commit ec95331

File tree

3 files changed

+42
-116
lines changed

3 files changed

+42
-116
lines changed

ch05construction/02conventions.ipynb.py

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,7 @@
1313

1414
# %% [markdown]
1515
# ## Coding Conventions
16-
17-
# %% [markdown]
18-
# Let's import first the context for this chapter.
19-
20-
# %%
21-
from context import *
22-
23-
# %% [markdown]
2416
# ### One code, many layouts:
25-
26-
# %% [markdown]
2717
#
2818
# Consider the following fragment of python:
2919
#
@@ -145,6 +135,8 @@ def method_name(a_variable):
145135
#
146136

147137
# %%
138+
sEntry = "10.0"
139+
iOffset = 1
148140
fNumber = float(sEntry) + iOffset
149141

150142
# %% [markdown]
@@ -153,6 +145,8 @@ def method_name(a_variable):
153145
# People may find this useful in languages like Python where the type is intrisic in the variable.
154146

155147
# %%
148+
entry = "10.0"
149+
offset = 1
156150
number = float(entry) + offset
157151

158152
# %% [markdown]
@@ -173,6 +167,13 @@ def method_name(a_variable):
173167
# The following two snippets do the same, but the second is separated into more steps, making it more readable.
174168

175169
# %%
170+
anothervariable=1
171+
flag1 = True
172+
flag2 = False
173+
variable = 1
174+
anothervariable = 1
175+
def do_something(): pass
176+
176177
anothervariable += 1
177178
if ((variable == anothervariable) and flag1 or flag2): do_something()
178179

@@ -219,43 +220,48 @@ def method_name(a_variable):
219220
#
220221
# There are automated tools which enforce coding conventions and check for common mistakes.
221222
#
222-
# These are called **linters**. A popular one is [pycodestyle](https://pypi.org/project/pycodestyle/):
223-
#
224-
# E.g. `pip install pycodestyle`
225-
#
226-
#
227-
#
223+
# These are called ** formatters** and **linters**. Some widely used linters and formatters in the Python ecosystem ar -
224+
# - [pycodestyle](https://pypi.org/project/pycodestyle/): check your code against PEP8
225+
# - [pylint](https://www.pylint.org/): useful information about the quality of your code
226+
# - [black](https://github.com/psf/black): code formatter written in Python
227+
# - [ruff](https://github.com/astral-sh/ruff): blazing fast code formatter and linter written in Rust with ideas borrowed from Pythonic linters and formatters
228228

229-
# %% magic_args="--no-raise-error" language="bash"
230-
# pycodestyle species.py
231229

232230
# %% [markdown]
233-
#
234-
#
235-
#
236-
# It is a good idea to run a linter before every commit, or include it in your CI tests.
237-
#
238-
#
231+
# Most of such tools can be directly used on Python files / repositories using a CLI utility. For instance -
239232

240-
# %% [markdown]
241-
# There are other tools that help with linting that are worth mentioning.
242-
# With [pylint](https://www.pylint.org/) you can also get other useful information about the quality of your code:
243-
#
244-
# `pip install pylint`
245-
#
233+
234+
# %% magic_args="--no-raise-error" language="bash"
235+
# pycodestyle species.py
246236

247237
# %% magic_args="--no-raise-error" language="bash"
248238
# pylint species.py
249239

240+
# %% magic_args="--no-raise-error" language="bash"
241+
# ruff check species.py
242+
250243
# %% [markdown]
251-
# and with [black](https://black.readthedocs.io/) you can fix all the errors at once.
252-
# ```bash
253-
# black species.py
254-
# ```
255244
# These linters can be configured to choose which points to flag and which to ignore.
256245
#
257246
# Do not blindly believe all these automated tools! Style guides are **guides** not **rules**.
258247

259248
# %% [markdown]
260-
# Finally, there are tools like [editorconfig](https://editorconfig.org/) to help sharing the conventions used within a project, where each contributor uses different IDEs and tools. There are also bots like [pep8speaks](https://pep8speaks.com/) that comments on contributors' pull requests suggesting what to change to follow the conventions for the project.
249+
#
250+
# It is a good idea to run a linter before every commit, or include it in your CI tests.
251+
#
252+
# [`pre-commit`](https://pre-commit.com) allows developers to add tools like linters and formatters
253+
# as git hooks, such that they run before every commit. The hooks can be installed locally using -
254+
#
255+
# ```bash
256+
# pip install pre-commit
257+
# pre-commit install # provided a .pre-commit-config.yaml is present in your repository
258+
# ```
259+
#
260+
# This would run the checks every time a commit is created locally. The checks will only run on the files
261+
# modified by that commit.
262+
263+
264+
# %% [markdown]
265+
# Finally, there are tools like [editorconfig](https://editorconfig.org/) to help sharing the conventions used within a project, where each contributor uses different IDEs and tools.
266+
# There are also bots like [pep8speaks](https://pep8speaks.com/) and [pre-commit.ci](https://pre-commit.ci) that comments/run checks on contributors' pull requests suggesting what to change to follow the conventions for the project.
261267
#

ch05construction/conventions.py

Lines changed: 0 additions & 81 deletions
This file was deleted.

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ imageio
2525
pycodestyle
2626
pylint
2727
webcolors
28+
ruff

0 commit comments

Comments
 (0)