From ec953310494d817015cafc2ebf7790471058d961 Mon Sep 17 00:00:00 2001 From: Saransh Chopra Date: Mon, 12 Aug 2024 15:04:54 +0100 Subject: [PATCH 1/2] refactor: update contents for coding conventions --- ch05construction/02conventions.ipynb.py | 76 ++++++++++++----------- ch05construction/conventions.py | 81 ------------------------- requirements.txt | 1 + 3 files changed, 42 insertions(+), 116 deletions(-) delete mode 100644 ch05construction/conventions.py diff --git a/ch05construction/02conventions.ipynb.py b/ch05construction/02conventions.ipynb.py index 41d78adf4..6b3aaabe4 100644 --- a/ch05construction/02conventions.ipynb.py +++ b/ch05construction/02conventions.ipynb.py @@ -13,17 +13,7 @@ # %% [markdown] # ## Coding Conventions - -# %% [markdown] -# Let's import first the context for this chapter. - -# %% -from context import * - -# %% [markdown] # ### One code, many layouts: - -# %% [markdown] # # Consider the following fragment of python: # @@ -145,6 +135,8 @@ def method_name(a_variable): # # %% +sEntry = "10.0" +iOffset = 1 fNumber = float(sEntry) + iOffset # %% [markdown] @@ -153,6 +145,8 @@ def method_name(a_variable): # People may find this useful in languages like Python where the type is intrisic in the variable. # %% +entry = "10.0" +offset = 1 number = float(entry) + offset # %% [markdown] @@ -173,6 +167,13 @@ def method_name(a_variable): # The following two snippets do the same, but the second is separated into more steps, making it more readable. # %% +anothervariable=1 +flag1 = True +flag2 = False +variable = 1 +anothervariable = 1 +def do_something(): pass + anothervariable += 1 if ((variable == anothervariable) and flag1 or flag2): do_something() @@ -219,43 +220,48 @@ def method_name(a_variable): # # There are automated tools which enforce coding conventions and check for common mistakes. # -# These are called **linters**. A popular one is [pycodestyle](https://pypi.org/project/pycodestyle/): -# -# E.g. `pip install pycodestyle` -# -# -# +# These are called ** formatters** and **linters**. Some widely used linters and formatters in the Python ecosystem ar - +# - [pycodestyle](https://pypi.org/project/pycodestyle/): check your code against PEP8 +# - [pylint](https://www.pylint.org/): useful information about the quality of your code +# - [black](https://github.com/psf/black): code formatter written in Python +# - [ruff](https://github.com/astral-sh/ruff): blazing fast code formatter and linter written in Rust with ideas borrowed from Pythonic linters and formatters -# %% magic_args="--no-raise-error" language="bash" -# pycodestyle species.py # %% [markdown] -# -# -# -# It is a good idea to run a linter before every commit, or include it in your CI tests. -# -# +# Most of such tools can be directly used on Python files / repositories using a CLI utility. For instance - -# %% [markdown] -# There are other tools that help with linting that are worth mentioning. -# With [pylint](https://www.pylint.org/) you can also get other useful information about the quality of your code: -# -# `pip install pylint` -# + +# %% magic_args="--no-raise-error" language="bash" +# pycodestyle species.py # %% magic_args="--no-raise-error" language="bash" # pylint species.py +# %% magic_args="--no-raise-error" language="bash" +# ruff check species.py + # %% [markdown] -# and with [black](https://black.readthedocs.io/) you can fix all the errors at once. -# ```bash -# black species.py -# ``` # These linters can be configured to choose which points to flag and which to ignore. # # Do not blindly believe all these automated tools! Style guides are **guides** not **rules**. # %% [markdown] -# 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. +# +# It is a good idea to run a linter before every commit, or include it in your CI tests. +# +# [`pre-commit`](https://pre-commit.com) allows developers to add tools like linters and formatters +# as git hooks, such that they run before every commit. The hooks can be installed locally using - +# +# ```bash +# pip install pre-commit +# pre-commit install # provided a .pre-commit-config.yaml is present in your repository +# ``` +# +# This would run the checks every time a commit is created locally. The checks will only run on the files +# modified by that commit. + + +# %% [markdown] +# 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/) 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. # diff --git a/ch05construction/conventions.py b/ch05construction/conventions.py deleted file mode 100644 index e45c57a19..000000000 --- a/ch05construction/conventions.py +++ /dev/null @@ -1,81 +0,0 @@ -### "dense" - -import species -def AddToReaction(name, reaction): - reaction.append(species.Species(name)) - -### "sparse" - -from species import Species - -def add_to_reaction(a_name, - a_reaction): - l_species = Species(a_name) - a_reaction.append( l_species ) - -### "layout1" - -reaction= { - "reactants": ["H","H","O"], - "products": ["H2O"] -} - -### "layout2" - -reaction2=( -{ - "reactants": - [ - "H", - "H", - "O" - ], - "products": - [ - "H2O" - ] -} -) - -### "naming1" - -class ClassName(object): - def methodName(variable_name): - instance_variable=variable_name - -### "naming2" - -class class_name(object): - def method_name(a_variable): - m_instance_variable=a_variable - -### "setup" - -# Define some variables to make following fragments work -sInput="2.0" -input ="2.0" -iOffset=1 -offset =1 -anothervariable=1 -flag1=True -variable=1 -flag2=False -def do_something(): pass - -### "naming3" - -fNumber= float(sInput) + iOffset -number = float(input) + offset - -### "syntax1" - -anothervariable+=1 -if ((variable==anothervariable) and flag1 or flag2): do_something() - -### "syntax2" - -anothervariable = anothervariable + 1 -variable_equality = (variable == anothervariable); -if ((variable_equality and flag1) or flag2): - do_something() - diff --git a/requirements.txt b/requirements.txt index 7e6ce547d..6cc06824c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -25,3 +25,4 @@ imageio pycodestyle pylint webcolors +ruff From 689ed4216faeb2db362f3295940fa4d6425afb2c Mon Sep 17 00:00:00 2001 From: Saransh Chopra Date: Wed, 14 Aug 2024 14:47:02 +0100 Subject: [PATCH 2/2] better import for context --- ch05construction/02conventions.ipynb.py | 28 +++++++++++++++---------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/ch05construction/02conventions.ipynb.py b/ch05construction/02conventions.ipynb.py index 6b3aaabe4..9816140ab 100644 --- a/ch05construction/02conventions.ipynb.py +++ b/ch05construction/02conventions.ipynb.py @@ -13,6 +13,23 @@ # %% [markdown] # ## Coding Conventions +# +# Let's import a few variables from context.py that will be used in the following lesson. + +# %% +from context import ( + sEntry, + iOffset, + entry, + offset, + anothervariable, + variable, + flag1, + flag2, + do_something, +) + +# %% [markdown] # ### One code, many layouts: # # Consider the following fragment of python: @@ -135,8 +152,6 @@ def method_name(a_variable): # # %% -sEntry = "10.0" -iOffset = 1 fNumber = float(sEntry) + iOffset # %% [markdown] @@ -145,8 +160,6 @@ def method_name(a_variable): # People may find this useful in languages like Python where the type is intrisic in the variable. # %% -entry = "10.0" -offset = 1 number = float(entry) + offset # %% [markdown] @@ -167,13 +180,6 @@ def method_name(a_variable): # The following two snippets do the same, but the second is separated into more steps, making it more readable. # %% -anothervariable=1 -flag1 = True -flag2 = False -variable = 1 -anothervariable = 1 -def do_something(): pass - anothervariable += 1 if ((variable == anothervariable) and flag1 or flag2): do_something()