|
| 1 | +# Productivity tools |
| 2 | + |
| 3 | +:::{objectives} |
| 4 | +- Know about tools that can help you **spot code problems** and help you following |
| 5 | + a **consistent code style** without you having to do it manually. |
| 6 | +- Get an overview of **AI-based tools** and how they can help you |
| 7 | + writing code. |
| 8 | +::: |
| 9 | + |
| 10 | +:::{instructor-note} |
| 11 | +- Demo/discussion: 20 min |
| 12 | +::: |
| 13 | + |
| 14 | + |
| 15 | +## Linters and formatters |
| 16 | + |
| 17 | +**Linter**: Tool that analyzes source code to detect potential errors, unused |
| 18 | +imports, unused variables, code style violations, and to improve readability. |
| 19 | +- Popular linters: |
| 20 | + - [Autoflake](https://pypi.org/project/autoflake/) |
| 21 | + - [Flake8](https://flake8.pycqa.org/) |
| 22 | + - [Pyflakes](https://pypi.org/project/pyflakes/) |
| 23 | + - [Pycodestyle](https://pycodestyle.pycqa.org/) |
| 24 | + - [Pylint](https://pylint.readthedocs.io/) |
| 25 | + - [Ruff](https://docs.astral.sh/ruff/) |
| 26 | + |
| 27 | +**Formatter**: Tool that automatically formats your code to a consistent style, |
| 28 | +for instance following [PEP 8](https://peps.python.org/pep-0008/). |
| 29 | + |
| 30 | +- Popular formatters: |
| 31 | + - [Black](https://black.readthedocs.io/) |
| 32 | + - [YAPF](https://github.com/google/yapf) |
| 33 | + - [Ruff](https://docs.astral.sh/ruff/) |
| 34 | + |
| 35 | +In this course we will focus on [Ruff](https://docs.astral.sh/ruff/) since it |
| 36 | +can do **both checking and formatting** and you don't have to switch between |
| 37 | +multiple tools. |
| 38 | + |
| 39 | +:::{discussion} Linters and formatters can be configured to your liking |
| 40 | +These tools typically have good defaults. But if you don't like the defaults, |
| 41 | +you can configure what they should ignore or how they should format or not format. |
| 42 | +::: |
| 43 | + |
| 44 | + |
| 45 | +## Examples |
| 46 | + |
| 47 | +This code example (which we possibly recognize from the previous section about |
| 48 | +{ref}`profiling`) |
| 49 | +has few problems (highlighted): |
| 50 | +```{code-block} python |
| 51 | +--- |
| 52 | +emphasize-lines: 2, 7, 10 |
| 53 | +--- |
| 54 | +import re |
| 55 | +import requests |
| 56 | +
|
| 57 | +
|
| 58 | +def count_unique_words(file_path: str) -> int: |
| 59 | + unique_words = set() |
| 60 | + forgotten_variable = 13 |
| 61 | + with open(file_path, "r", encoding="utf-8") as file: |
| 62 | + for line in file: |
| 63 | + words = re.findall(r"\b\w+\b", line.lower())) |
| 64 | + for word in words: |
| 65 | + unique_words.add(word) |
| 66 | + return len(unique_words) |
| 67 | +``` |
| 68 | + |
| 69 | +Please try whether you can locate these problems using Ruff: |
| 70 | +```console |
| 71 | +$ ruff check |
| 72 | +``` |
| 73 | + |
| 74 | +Next, let us try to auto-format a code example which is badly formatted and also difficult |
| 75 | +to read: |
| 76 | +:::::{tabs} |
| 77 | + ::::{tab} Badly formatted |
| 78 | + ```python |
| 79 | + import re |
| 80 | + def count_unique_words (file_path : str)->int: |
| 81 | + unique_words=set() |
| 82 | + with open(file_path,"r",encoding="utf-8") as file: |
| 83 | + for line in file: |
| 84 | + words=re.findall(r"\b\w+\b",line.lower()) |
| 85 | + for word in words: |
| 86 | + unique_words.add(word) |
| 87 | + return len( unique_words ) |
| 88 | + ``` |
| 89 | + :::: |
| 90 | + |
| 91 | + ::::{tab} Auto-formatted |
| 92 | + ```python |
| 93 | + import re |
| 94 | + |
| 95 | + |
| 96 | + def count_unique_words(file_path: str) -> int: |
| 97 | + unique_words = set() |
| 98 | + with open(file_path, "r", encoding="utf-8") as file: |
| 99 | + for line in file: |
| 100 | + words = re.findall(r"\b\w+\b", line.lower()) |
| 101 | + for word in words: |
| 102 | + unique_words.add(word) |
| 103 | + return len(unique_words) |
| 104 | + ``` |
| 105 | + |
| 106 | + This was done using: |
| 107 | + ```console |
| 108 | + $ ruff format |
| 109 | + ``` |
| 110 | + :::: |
| 111 | +::::: |
| 112 | + |
| 113 | + |
| 114 | +## Type checking |
| 115 | + |
| 116 | +A (static) type checker is a tool that checks whether the types of variables in your |
| 117 | +code match the types that you have specified. |
| 118 | +- Tools: |
| 119 | + - [Mypy](https://mypy.readthedocs.io/) |
| 120 | + - [Pyright](https://github.com/microsoft/pyright) (Microsoft) |
| 121 | + - [Pyre](https://pyre-check.org/) (Meta) |
| 122 | + |
| 123 | + |
| 124 | +## Integration with editors |
| 125 | + |
| 126 | +Many/most of the above tools can be integrated with your editor. For instance, |
| 127 | +you can configure your editor to automatically format your code when you save |
| 128 | +the file. However, this only makes sense when all team members agree to follow |
| 129 | +the same style, otherwise saving and possibly committing changes to version |
| 130 | +control will show up changes to code written by others which you possibly |
| 131 | +didn't intend to make. |
| 132 | + |
| 133 | + |
| 134 | +## Integration with Jupyter notebooks |
| 135 | + |
| 136 | +It is possible to automatically format your code in Jupyter notebooks! |
| 137 | +For this to work you need |
| 138 | +the following three dependencies installed: |
| 139 | +- `jupyterlab-code-formatter` |
| 140 | +- `black` |
| 141 | +- `isort` |
| 142 | + |
| 143 | +More information and a screen-cast of how this works can be found at |
| 144 | +<https://jupyterlab-code-formatter.readthedocs.io/>. |
| 145 | + |
| 146 | + |
| 147 | +## Integration with version control |
| 148 | + |
| 149 | +If you use version control and like to have your code checked or formatted |
| 150 | +**before you commit the change**, you can use tools like [pre-commit](https://pre-commit.com/). |
| 151 | + |
| 152 | + |
| 153 | +## AI-assisted coding |
| 154 | + |
| 155 | +We can use AI as an assistant/apprentice: |
| 156 | +- Code completion |
| 157 | +- Write a test based on an implementation |
| 158 | +- Write an implementation based on a test |
| 159 | + |
| 160 | +Or we can use AI as a mentor: |
| 161 | +- Explain a concept |
| 162 | +- Improve code |
| 163 | +- Show a different (possibly better) way of implementing the same thing |
| 164 | + |
| 165 | + |
| 166 | +:::{figure} productivity/chatgpt.png |
| 167 | +:alt: Screenshot of ChatGPT |
| 168 | +:width: 100% |
| 169 | + |
| 170 | +Example for using a chat-based AI tool. |
| 171 | +::: |
| 172 | + |
| 173 | +:::{figure} productivity/code-completion.gif |
| 174 | +:alt: Screen-cast of working with GitHub Copilot |
| 175 | +:width: 100% |
| 176 | + |
| 177 | +Example for using AI to complete code in an editor. |
| 178 | +::: |
| 179 | + |
| 180 | +:::{admonition} AI tools open up a box of questions |
| 181 | +- Legal |
| 182 | +- Ethical |
| 183 | +- Privacy |
| 184 | +- Lock-in/ monopolies |
| 185 | +- Lack of diversity |
| 186 | +- Will we still need to learn programming? |
| 187 | +- How will it affect learning and teaching programming? |
| 188 | +::: |
0 commit comments