|
7 | 7 | \hsection{Basic Functionality and Examples}%
|
8 | 8 | \label{sec:lists:basicFunctions}%
|
9 | 9 | %
|
10 |
| -\gitPythonAndOutput{\programmingWithPythonCodeRepo}{collections}{lists_1.py}{--args format}{lists:lists_1}{% |
11 |
| -A first example for using lists in \python: creating, indexing, printing of and appending elements and other lists to lists.}% |
| 10 | +\gitLoadAndExecPython{lists:lists_1}{}{collections}{lists_1.py}{}% |
| 11 | +\listingPythonAndOutput{lists:lists_1}{lists_1.py}{% |
| 12 | +A first example for using lists in \python:~creating, indexing, printing of and appending elements and other lists to lists.}{}% |
12 | 13 | %
|
13 |
| -\gitPythonAndOutput{\programmingWithPythonCodeRepo}{collections}{lists_2.py}{--args format}{lists:lists_2}{% |
14 |
| -A second example of using lists in \python: inserting and deleting elements, sorting and reversing lists.}% |
| 14 | +\gitLoadAndExecPython{lists:lists_2}{}{collections}{lists_2.py}{}% |
| 15 | +\listingPythonAndOutput{lists:lists_2}{lists_2.py}{% |
| 16 | +A second example of using lists in \python: inserting and deleting elements, sorting and reversing lists.}{}% |
15 | 17 | %
|
16 |
| -\gitPythonAndOutput{\programmingWithPythonCodeRepo}{collections}{lists_3.py}{--args format}{lists:lists_3}{% |
17 |
| -A third example of using lists in \python: slicing, adding, and multiplying lists.}% |
| 18 | +\gitLoadAndExecPython{lists:lists_3}{}{collections}{lists_3.py}{}% |
| 19 | +\listingPythonAndOutput{lists:lists_3}{lists_3.py}{% |
| 20 | +A third example of using lists in \python: slicing, adding, and multiplying lists.}{}% |
18 | 21 | %
|
19 | 22 | In \cref{lst:lists:lists_1}, we provide some first examples for using lists.
|
20 | 23 | A list can be defined by simply writing its contents, separated by~\pythonilIdx{,} inside square brackets~\pythonil{[...]}\pythonIdx{[\idxdots]}.
|
|
143 | 146 | \hsection{An Example of Errors and a new Tool}%
|
144 | 147 | \label{sec:listExampleForErrorsAndRuff}%
|
145 | 148 | %
|
146 |
| -\gitPythonAndOutput{\programmingWithPythonCodeRepo}{collections}{lists_error.py}{--args format}{lists:lists_error}{% |
147 |
| -A program processing lists which exhibits some subtle errors and inefficiencies.} |
148 |
| - |
| 149 | +\gitLoadAndExecPython{lists:lists_error}{}{collections}{lists_error.py}{}% |
| 150 | +\listingPythonAndOutput{lists:lists_error}{lists_error.py}{% |
| 151 | +A program processing lists which exhibits some subtle errors and inefficiencies.}{}% |
| 152 | +% |
149 | 153 | Now, in the previous chapter, we learned that static code analysis tools can help us to discover subtle problems in our programs.
|
150 | 154 | Obviously, when dealing with more complex datastructures like lists, there are also more potential problems, more mistakes that one could make.
|
151 | 155 | Let us look at the very short example \cref{lst:lists:lists_error}.
|
152 | 156 | The program consists of only two lines, \pythonil{my_list: list[str] = list([1, 2, 3])} and \pythonil{print(my_list)}.
|
153 | 157 | It does not have any \emph{error} in the strict sense.
|
154 | 158 | We can execute it just fine and it will produce the output \textil{[1, 2, 3]} as shown in \cref{exec:lists:lists_error}.
|
155 | 159 |
|
156 |
| -\gitOutputTool{\programmingWithPythonCodeRepo}{.}{_scripts_/mypy.sh collections lists_error.py}{lists:lists_error:mypy}{% |
| 160 | +\gitExec{exec:lists:lists_error:mypy}{\programmingWithPythonCodeRepo}{.}{_scripts_/mypy.sh collections lists_error.py}% |
| 161 | +\listingToolOutput{lists:lists_error:mypy}{% |
157 | 162 | The results of static type checking with \mypy\ of the program given in \cref{lst:lists:lists_error}.}
|
158 | 163 |
|
159 | 164 | However, upon closer inspection, we discover some issues.
|
|
179 | 184 | We provide a script for using \ruff\ with a reasonable default configuration in \cref{lst:bash:ruff} on \cpageref{lst:bash:ruff}.%
|
180 | 185 | }%
|
181 | 186 | %
|
182 |
| -\gitOutputTool{\programmingWithPythonCodeRepo}{.}{_scripts_/ruff.sh collections lists_error.py}{lists:lists_error:ruff}{% |
| 187 | +\gitExec{exec:lists:lists_error:ruff}{\programmingWithPythonCodeRepo}{.}{_scripts_/ruff.sh collections lists_error.py}% |
| 188 | +\listingToolOutput{lists:lists_error:ruff}{% |
183 | 189 | The results of linting with \ruff\ of the program given in \cref{lst:lists:lists_error}. (We used the script given in \cref{lst:bash:ruff} on \cpageref{lst:bash:ruff} to apply \ruff.)}%
|
184 | 190 | %
|
185 | 191 | Let us apply \ruff\ to the program \textil{lists_error.py} given in \cref{lst:lists:lists_error}, which produces the output \cref{exec:lists:lists_error:ruff}.
|
|
199 | 205 | It basically creates a list via \pythonil{[1, 2, 3]} and then immediately makes a copy of it via the \pythonilIdx{list} function wrapped around the list specification.
|
200 | 206 | We can leave this outer call to \pythonil{list} away.
|
201 | 207 |
|
202 |
| -\gitPythonAndOutput{\programmingWithPythonCodeRepo}{collections}{lists_fixed.py}{--args format}{lists:lists_fixed}{% |
203 |
| -The corrected version of~\cref{lst:lists:lists_error}, taking into account the information given by \mypy\ in \cref{exec:lists:lists_error:mypy} and \ruff\ in \cref{exec:lists:lists_error:ruff}.}% |
| 208 | +\gitLoadAndExecPython{lists:lists_fixed}{}{collections}{lists_fixed.py}{}% |
| 209 | +\listingPythonAndOutput{lists:lists_fixed}{lists_fixed.py}{% |
| 210 | +The corrected version of~\cref{lst:lists:lists_error}, taking into account the information given by \mypy\ in \cref{exec:lists:lists_error:mypy} and \ruff\ in \cref{exec:lists:lists_error:ruff}.}{}% |
204 | 211 | %
|
205 | 212 | %
|
206 | 213 | In \cref{lst:lists:lists_fixed} we implement the three recommendations from the two tools.
|
|
220 | 227 | %
|
221 | 228 | \FloatBarrier%
|
222 | 229 | %
|
223 |
| -\gitOutputTool{\programmingWithPythonCodeRepo}{.}{_scripts_/mypy.sh collections lists_fixed.py}{lists:lists_fixed:mypy}{% |
| 230 | +\gitExec{exec:lists:lists_fixed:mypy}{\programmingWithPythonCodeRepo}{.}{_scripts_/mypy.sh collections lists_fixed.py}% |
| 231 | +\listingToolOutput{lists:lists_fixed:mypy}{% |
224 | 232 | The results of static type checking with \mypy\ of the program given in \cref{lst:lists:lists_fixed}.}%
|
225 | 233 | %
|
226 |
| -\gitOutputTool{\programmingWithPythonCodeRepo}{.}{_scripts_/ruff.sh collections lists_fixed.py}{lists:lists_fixed:ruff}{% |
| 234 | +\gitExec{exec:lists:lists_fixed:ruff}{\programmingWithPythonCodeRepo}{.}{_scripts_/ruff.sh collections lists_fixed.py}% |
| 235 | +\listingToolOutput{lists:lists_fixed:ruff}{% |
227 | 236 | The results of static type checking with \ruff\ of the program given in \cref{lst:lists:lists_fixed}.}
|
228 | 237 |
|
229 | 238 | Well, this was only a two-line program.
|
|
0 commit comments