Skip to content

Commit 10ea24e

Browse files
committed
paragraph style improved
1 parent 03edb78 commit 10ea24e

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

text/main/basics/collections/ruff/ruff.tex

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@
99
\listingToolOutput{lists:lists_error:mypy}{%
1010
The results of static type checking with \mypy\ of the program given in \cref{lst:lists:lists_error}.}%
1111
%
12+
\begin{sloppypar}%
1213
Recently, we learned that static code analysis tools can help us to discover subtle problems in our programs.
1314
Obviously, when dealing with more complex datastructures like lists, there are also more potential problems, more mistakes that one could make.
1415
Let us look at the very short example program \programUrl{lists:lists_error} in \cref{lst:lists:lists_error}.
1516
The program consists of only two lines, \pythonil{my_list: list[str] = list([1, 2, 3])} and \pythonil{print(my_list)}.
1617
It does not have any \emph{error} in the strict sense.
17-
We can execute it just fine and it will produce the output \textil{[1, 2, 3]} as shown in \cref{exec:lists:lists_error}.
18-
18+
We can execute it just fine and it will produce the output \textil{[1, 2, 3]} as shown in \cref{exec:lists:lists_error}.%
19+
\end{sloppypar}%
20+
%
1921
However, upon closer inspection, we discover some issues.
2022
In a first step, we would apply \mypy~(as~\cref{ut:mypy}) to check for problems with the types of variables.
2123
And indeed, \cref{exec:lists:lists_error:mypy} shows us \emph{three} errors!
@@ -83,14 +85,16 @@
8385
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}.}{}%
8486
%
8587
%
88+
\begin{sloppypar}%
8689
In \cref{lst:lists:lists_fixed} we implement the three recommendations from the two tools as program \programUrl{lists:lists_fixed}.
8790
We change the \pgls{typeHint} of the list to \pythonil{list[int]}, which solves the type confusion that \mypy\ discovered.
8891
We remove the useless copying of the list as \ruff\ recommended.
8992
Finally, we add a proper \pgls{docstring} at the top of the file, in which we even document the changes we applied.
9093
The output \cref{exec:lists:lists_fixed} of the new program remains the same.
9194
But now, both tools are satisfied, as shown in \cref{exec:lists:lists_fixed:mypy,exec:lists:lists_fixed:ruff}.
92-
And our program is much clearer and faster.
93-
95+
And our program is much clearer and faster.%
96+
\end{sloppypar}%
97+
%
9498
\gitExec{exec:lists:lists_fixed:mypy}{\programmingWithPythonCodeRepo}{.}{_scripts_/mypy.sh collections lists_fixed.py}%
9599
\listingToolOutput{lists:lists_fixed:mypy}{%
96100
The results of static type checking with \mypy\ of the program given in \cref{lst:lists:lists_fixed}.}%

text/main/controlFlow/exceptions/exceptions.tex

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -802,14 +802,16 @@
802802
We must make sure to definitely close this so-called text stream again, regardless what happens from now on.
803803
We know that this can be done with a \pythonilIdx{try}-\pythonilIdx{finally} statement.
804804
We simply put \pythonil{stream_out.close()}\pythonIdx{close}\pythonIdx{IO!close} into the \pythonilIdx{finally}~block.
805-
It will thus definitely be called and we thus ensure that \pythonil{stream_out} will be closed.
806-
805+
It will thus definitely be called and we thus ensure that \pythonil{stream_out} will be closed.%
806+
%
807+
\begin{sloppypar}%
807808
Into the \pythonilIdx{try}~block, we put \pythonil{stream_out.write("Hello world!")}.
808809
This line will write the string~\pythonil{"Hello world!"} to the file.
809810
This could, of course, fail.
810811
Maybe our hard disk does not have enough space left to store this string.
811-
But even if it fails, the \pythonilIdx{finally} block will make sure to close the file.
812-
812+
But even if it fails, the \pythonilIdx{finally} block will make sure to close the file.%
813+
\end{sloppypar}%
814+
%
813815
So after the block, the file is closed.
814816
We could now open it in a text editor and would find in there the text that we had written.
815817
Instead, we want to use code to read the text again right away.

text/main/controlFlow/functions/functions.tex

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -985,6 +985,7 @@
985985
%
986986
\pythonSyntax{syntax/callable.py}%
987987
%
988+
\begin{sloppypar}%
988989
Inside the \pythonil{Callable[...]}, we first provide the list of types of the parameters of function, again in square brackets.
989990
Then follows a comma~\pythonil{,} and then follows the return type.
990991
The \pythonil{f: Callable[[float], float | int]} in our listing thus states that our function \pythonil{integrate} expects, as first parameter, \emph{another function}~\pythonil{f}.
@@ -993,8 +994,9 @@
993994
Notice that the type \pythonilIdx{Callable} is provided by the module~\pythonilIdx{typing}\footnote{%
994995
\cite{PSF:P3D:TPSL:ACO}~states that this import is now deprecated and we should use \pythonil{collections.abc.Callable}\pythonIdx{collections.abc} instead. %
995996
In the past, this created some errors for me, so for now we stick with using~\pythonilIdx{typing}.}, %
996-
so we need to import it first if we want to use it.
997-
997+
so we need to import it first if we want to use it.%
998+
\end{sloppypar}%
999+
%
9981000
The actual implementation of \pythonil{integrate} is straightforward.
9991001
Inside the function, we add up the different trapezes as shown in the simplified equation in \cref{fig:normalDistPdfInteg}.
10001002
Only the values of \pythonil{f} at the interval ends \pythonil{a} and \pythonil{b} need to be halved in the sum.

0 commit comments

Comments
 (0)