Skip to content

Commit

Permalink
fix format
Browse files Browse the repository at this point in the history
  • Loading branch information
krother committed Oct 15, 2023
1 parent a425d9e commit 89e67da
Show file tree
Hide file tree
Showing 18 changed files with 13 additions and 106 deletions.
4 changes: 0 additions & 4 deletions class_diagram.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ The core logic of how the snake moves should not change because of that.
A great thing about class diagrams is that you can create them to code easily.
The Python `dataclasses` module saves you a lot of typing:

:::python3
from dataclasses import dataclass

@dataclass
Expand All @@ -74,7 +73,6 @@ But we leave the method bodies empty for now.
The `@dataclass` automatically creates the `__init__()` and `__repr__()` methods for you, so that you can set and inspect the attribute values.
The code is already executable:

:::python3
pf = PlayingField(size=(10, 10))
print(pf)
print(pf.size)
Expand Down Expand Up @@ -106,7 +104,6 @@ In Python, one could even state that the data structures are practically *identi
Using the `@property` decorator, you can translate attributes into each other.
The following code translates the `size` attribute into two new attributes `size_x` and `size_y`:

:::python3
@property
def size_x(self):
return self.size[0]
Expand All @@ -117,7 +114,6 @@ The following code translates the `size` attribute into two new attributes `size

Now you can use all three attributes without storing redundant data:

:::python3
pf = PlayingField(size=(5, 5))
print(pf.size)
print(pf.size_x)
Expand Down
11 changes: 0 additions & 11 deletions coding_style.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,14 @@ The **pylint** tool checks whether your code conforms to the PEP8 coding guideli

Install it with

:::bash
pip install pylint

Then you can analyze any Python file:

:::bash
pylint my_program.py

Or all the files in a folder:

:::bash
pyling *.py

----
Expand All @@ -36,7 +33,6 @@ In the output of `pylint`, there are two sections to pay attention to:

At the top of the output from **pylint**, you find a section with warning messages. Each warning contains the line number the warning refers to:

:::text
W:117,12:Template.prepare_identifiers: Unused variable 'x'
C: 32,0: Line too long (88/80)
C:134,16:Renumerator.get_identifiers_list: Operator not preceded by a space
Expand All @@ -47,29 +43,25 @@ These warnings point you to the following issues:

#### Bugs and dead code

:::text
W:117,12:Template.prepare_identifiers: Unused variable 'x'

This message indicates that line 117 either won't work or that the code has not been used at all.

#### Coding style

:::text
C: 32,0: Line too long (88/80)
C:134,16:Renumerator.get_identifiers_list: Operator not preceded by a space

Style issues regarding spaces, indentation and line lengths raised by pylint affect readability and are generally easy to fix.

#### Docstrings

:::text
C: 1,0: Missing docstring

Functions and classes without docstrings are more difficult to understand. If you get a lot of docstring warnings your code may be hard to understand for someone else.

#### Variable names

:::text
C:114,8:Renumerator.prepare_identifiers: Invalid name "fn" (should match [a-z_][a-z0-9_]{2,30}$)

Descriptive variable names are a big plus for code readability. Of course, it does not help much to replace **l** by **data_list** in order to satisfy pylint. But the name **fragment** tells you a lot more than **fn**.
Expand All @@ -78,7 +70,6 @@ Descriptive variable names are a big plus for code readability. Of course, it do

Pylint helps to analyze modularization by printing warning messages:

:::text
R: 19,0:Renumerator: Too many public methods (30/20)
R: 32,4:Renumerator.letter_generator: Method could be a function
R: 45,0:RNAResidue: Too many instance attributes (11/7)
Expand All @@ -94,7 +85,6 @@ To assess modularization of a program as a whole, pylint is not the right tool.

At the end of the pylint output you find a score of up to 10 points:

:::text
Your code has been rated at 8.18/10

When you have fixed some of the issues, re-run pylint and see your score improve. The score directly measures your success and makes working with pylint very rewarding.
Expand All @@ -109,7 +99,6 @@ A good practice is to disable some types of warnings (those you and your team ag

To ignore PEP8 warnings, create a file `.pylintrc` in your project directory. `pylint` finds it automatically. There you can list the types of warnings you would like to disable:

:::text
[pylint]
disable=C0103,C0111,line-too-long,too-few-public-methods

Expand Down
4 changes: 1 addition & 3 deletions continuous_integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ GitHub Actions needs instructions how to install the program.
Create a folder `.github/workflows/`.
Place a text file `check.yml` into that folder containing the following:

:::text
name: run_tests

on:
Expand Down Expand Up @@ -78,7 +77,6 @@ Commit and push the changes.

Copy the following code into your `README.md` file:

:::text
![Python application](https://github.com/USER/REPO/workflows/run_tests/badge.svg)

Replace **USER** and **REPO** by the data of your project.
Expand All @@ -89,4 +87,4 @@ You should see a red or green badge in the README that updates itself.

### Authors

**Malte Bonart participated in the writing of this chapter.**
**Malte Bonart participated in the writing of this chapter.**
2 changes: 0 additions & 2 deletions documenting.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ That said, there are a number of good Python tools to build and maintain documen

Running Sphinx could look like this:

:::bash
sphinx-build html

Sphinx has its strengths in:
Expand All @@ -37,7 +36,6 @@ If you like to know more, check out this **[Talk by Eric Holscher](https://www.y

A very cool feature is that you can run a local documentation server with

:::bash
mkdocs serve

and the local website is automatically updated as you edit the Markdown documents.
Expand Down
8 changes: 0 additions & 8 deletions environment_variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ In this article, you can learn how to set and read environment variables on a Un

Type into the terminal:

:::bash
export MY_TEXT=hello

Note the following:
Expand All @@ -35,15 +34,13 @@ Note the following:

Type into a terminal:

:::bash
echo $MY_TEXT

The Unix `echo` command is the equivalent of `print()` in Python.
The `$` symbol dereferences the variable.

If you want to see *all* environment variables that are defined, try the command:

:::bash
env

The output is usually quite a mess.
Expand All @@ -54,7 +51,6 @@ The output is usually quite a mess.

No. Each environment has a local *scope*. Each program has its own variables. That means that typing

:::bash
echo $MY_TEXT

in two terminals may yield different results.
Expand All @@ -69,13 +65,11 @@ E.g. when you start a Python program from a Unix command line, it receives the c
If you want **all** programs to have a certain environment variable, add the `EXPORT` statement to a configuration file in your home directory.
Open the file `.bashrc` (Linux) or `.bash_profile` (MacOS) and add the same line as above:

:::bash
export MY_TEXT=hello

The changes are applied as soon as you start a new terminal.
You can update your environment with:

:::bash
source ~/.bashrc

**Note: Restart your Python editor, if you want it to see the new environment variables.**
Expand All @@ -86,7 +80,6 @@ You can update your environment with:

You can read an environment variable in two lines:

:::python3
import os

text = os.getenv('MY_TEXT')
Expand All @@ -109,5 +102,4 @@ Here are a few common ones:

If you want to append a directory to an existing `PATH` or `PYTHONPATH`, this expression is useful:

:::bash
export PATH=$PATH:/my/new/dir/
6 changes: 0 additions & 6 deletions folders.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ A **Python package** is simply a folder that contains `.py` files.
Create a folder `snake` inside your repository.
On the bash terminal, you would use

:::bash
mkdir snake

If your git repository is also called `snake`, you may want to rename your project folder to something else like `snake_project`, `snake_repo` or similar.
Expand All @@ -26,7 +25,6 @@ You will also want to have a place where you add test code later.
Name that folder `tests/`.
We will leave it empty for now.

:::bash
mkdir tests

----
Expand All @@ -37,7 +35,6 @@ You may want to create a Python module (a `.py` file) to make sure everything is
Create a file `game.py` inside the `snake/` folder.
Add a placeholder function to it:

:::python3
def play_snake():
print('this is a snake game')

Expand All @@ -59,14 +56,12 @@ Importing the `play_snake()` function to play the game is a bit inconvenient.
Let's create a shortcut.
Create a file named `__main__.py` (with double underscores on both ends) in the package folder that contains the following code:

:::python3
from game import play_snake

play_snake()

Now it should be possible to start the game by typing:

:::text
python snake

----
Expand All @@ -75,7 +70,6 @@ Now it should be possible to start the game by typing:

At this point, your project folder should contain:

:::text
LICENSE
prototype.py
README.md
Expand Down
2 changes: 0 additions & 2 deletions github_issues.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@ Go to the **Issues** tab on your repository on GitHub.
Press the big **New Issue** button on the right side.
Enter a title for the Issue, e.g.

:::text
Features for the Snake Game

In the large text field below, you can add what is to be done.
There are plenty of controls to format text and attach files (e.g. screenshots).
One of the buttons lets you create a **Checklist**:

:::text
- [ ] there is a wall around the playing field
- [ ] there is food on the playing field
- [ ] the snake gets longer when it eats food
Expand Down
2 changes: 0 additions & 2 deletions good_software.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ When you use a program, you need to be 100% sure that it does exactly what you t

Each scientific program should include at least one set of sample data. There should be an instruction how to use the sample data and exactly what output it produces. Sometimes, this approach is broken down into small steps: a cookbook explaining small actions and their effect. Eventually, you will find an automatic test suite. This is a script that automatically checks whether different parts of the program work correctly. When you see a message like


:::text
110 of 110 tests OK.

you know that at least everything the developers felt important to check works.
Expand Down
1 change: 0 additions & 1 deletion interface.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ While both parts may change, the interface should remain stable.
Let's separate the **User Interface** of the snake game from the **Game Logic**.
For that, we will define a `SnakeGame` class that will be used as the only point of communication by the user interface:

:::python3
class SnakeGame:

running: bool
Expand Down
2 changes: 0 additions & 2 deletions loc.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@ More code means more work. The amount of code gives you a ballpark figure of how

You can count the total number of files on Unix:

:::bash
find . -name "*.py" | wc -l

A common metric is the number of **lines of code (LOC)**. The following command gives you the total number of LOC for all Python files in a Python directory tree:

:::bash
find . -name "*.py" | xargs wc -l

Empty lines, docstrings and comments are counted, too, as they are part of the source code.
Expand Down
10 changes: 0 additions & 10 deletions pip.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@

To install a Python package, call `pip` with the package name:

:::bash
pip install pandas

You can specify the exact version of a package:

:::bash
pip install pandas==0.25.0

----
Expand All @@ -21,15 +19,13 @@ You can specify the exact version of a package:

First, create a file `requirements.txt` in your project directory. The file should look similar to this:

:::text
pandas==0.25
numpy>=1.17
scikit-learn
requests

Second, ask `pip` to install everything:

:::bash
pip -r requirements.txt

----
Expand All @@ -38,7 +34,6 @@ Second, ask `pip` to install everything:

If a repository has a `setup.py` file, you could install directly from git. This is useful to install branches, forks and other work in progress:

:::bash
pip install git+https://github.com/pandas-dev/pandas.git

----
Expand All @@ -49,7 +44,6 @@ When developing, you might want to pip-install a working copy. This allows you t

For the following to work, your project folder needs to have a `setup.py`:

:::bash
pip install --editable .

----
Expand All @@ -58,20 +52,17 @@ For the following to work, your project folder needs to have a `setup.py`:

This one prints all packages you have installed and their versions:

:::bash
pip freeze

To search for a pacakge, use `grep`:

:::bash
pip freeze | grep pandas

----
### Uninstall a package

`pip` also removes packages:

:::bash
pip uninstall pandas

----
Expand All @@ -82,7 +73,6 @@ Usually, packages are stored in the `site_packages/` folder. Where this one is d

You might want to check your `PYTHONPATH` environment variable. To do so from Python, use:

:::python3
import sys
print(sys.path)

Expand Down
Loading

0 comments on commit 89e67da

Please sign in to comment.