Skip to content

Commit 4a75a3d

Browse files
committed
Moving descriptions from README to docstrings
1 parent 68725aa commit 4a75a3d

17 files changed

+102
-80
lines changed

subprocess/README.md

Lines changed: 1 addition & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -4,80 +4,4 @@ Here are supporting materials for the Real Python tutorial [The `subprocess` Mod
44

55
Be aware that some examples are designed for particular operating systems. The `basics_unix.py` file won't work on Windows, for instance.
66

7-
Below, you'll find quick descriptions of what the different scripts do.
8-
9-
## [basics_unix.py](basics_unix.py)
10-
11-
**Only works on Linux or macOS**
12-
13-
Demonstrates basic usage of `subprocess.run()`.
14-
15-
## [basics_win.py](basics_win.py)
16-
17-
**Only works on Windows**
18-
19-
Demonstrates basic usage of `subprocess.run()`.
20-
21-
## [custom_exit.py](custom_exit.py)
22-
23-
Raises custom exit codes:
24-
25-
```shell
26-
$ python custom_exit.py 3
27-
```
28-
29-
This will exit with error code `3`. By default, will exit with code `0`.
30-
31-
## [exiter_run.py](exiter_run.py)
32-
33-
Calls [custom_exit.py](custom_exit.py) with `subprocess.run()`, demonstrating the `check` argument to `.run()`.
34-
35-
## [error_handling.py](error_handling.py)
36-
37-
Demonstrates error handling with `subprocess.run()`, catching common errors that `subprocess.run()` can encounter. Try changing the commands in `subprocess.run()` to raise different errors.
38-
39-
## [timer.py](timer.py)
40-
41-
Accepts an integer argument and will sleep for the given time. For example:
42-
43-
```
44-
$ python timer.py 5
45-
Starting timer of 5 seconds
46-
.....Done!
47-
```
48-
49-
## [popen_pipe.py](popen_pipe.py)
50-
51-
**Only works on Linux or macOS**
52-
53-
Demonstrates using `subprocess.Popen()` to pipe one command into the other.
54-
55-
## [popen_timer.py](popen_timer.py)
56-
57-
Demonstrates `subprocess.Popen()` calling [timer.py](timer.py).
58-
59-
## [random_num_gen.py](random_num_gen.py)
60-
61-
Generates and prints a random number.
62-
63-
## [random_num_gen_check_output.py](random_num_gen_check_output.py)
64-
65-
Uses `subprocess.run()` to call [random_num_gen.py](random_num_gen.py) to generate a random number, read the output, and then print it.
66-
67-
## [reaction_game.py](reaction_game.py)
68-
69-
Tests your reaction times.
70-
71-
Note that this game can be cheated by pressing enter before the game prompts you to press enter.
72-
73-
## [reaction_game_v2.py](reaction_game_v2.py)
74-
75-
Tests your reaction times, but guards against the cheating described above by making you input a random letter instead.
76-
77-
## [reaction_game_v2_hack.py](reaction_game_v2_hack.py)
78-
79-
Uses `subprocess.Popen()` to cheat the second version of the reaction game.
80-
81-
## [create_project.py](create_project.py)
82-
83-
Creates a Python project, complete with a virtual environment and initialized Git repository.
7+
You'll find quick descriptions of what the different scripts do in the docstring of each file.

subprocess/basics_unix.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
import subprocess
1+
"""
2+
**Only works on Linux or macOS**
3+
4+
Demonstrates basic usage of `subprocess.run()`.
5+
"""
26

3-
# LINUX examples
7+
import subprocess
48

59
subprocess.run(["sh", "-c", "ls"])
610

subprocess/basics_win.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
"""
2+
**Only works on Windows**
3+
4+
Demonstrates basic usage of `subprocess.run()`.
5+
"""
6+
17
import subprocess
28

39
# https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/cmd

subprocess/create_project.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
1+
"""
2+
Uses `subprocess` to creates a Python project, complete with a virtual
3+
environment and initialized Git repository.
4+
5+
Must have Git installed on the system with the `git` command available.
6+
7+
If your Python command is `python3`, change the `PYTHON_COMMAND` variable.
8+
"""
9+
110
from argparse import ArgumentParser
211
from pathlib import Path
312
import subprocess
413

14+
PYTHON_COMMAND = "python"
15+
516

617
def create_new_project(name):
718
project_folder = Path.cwd().absolute() / name
@@ -11,7 +22,7 @@ def create_new_project(name):
1122
f.write("\n".join(["venv", "__pycache__"]))
1223
commands = [
1324
[
14-
"python",
25+
PYTHON_COMMAND,
1526
"-m",
1627
"venv",
1728
f"{project_folder}/venv",

subprocess/custom_exit.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
"""
2+
Raises custom exit codes:
3+
4+
```shell
5+
$ python custom_exit.py 3
6+
```
7+
8+
This will exit with error code `3`. By default, will exit with code `0`.
9+
"""
10+
111
import sys
212

313
print(sys.argv)

subprocess/error_handling.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
"""
2+
Demonstrates error handling with `subprocess.run()`, catching common errors
3+
that `subprocess.run()` can encounter. Try changing the commands in
4+
`subprocess.run()` to raise different errors.
5+
"""
6+
17
import subprocess
28

39

subprocess/exiter.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
"""
2+
Immediately exits with exit code 1
3+
"""
4+
15
import sys
26

37
sys.exit(1)

subprocess/exiter_run.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
"""
2+
Calls [custom_exit.py](custom_exit.py) with `subprocess.run()`,
3+
demonstrating the `check` argument to `.run()`.
4+
"""
5+
16
import subprocess
27

38
subprocess.run(["python", "custom_exit.py", "5"], check=False)

subprocess/popen_pipe.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
"""
2+
**Only works on Linux or macOS**
3+
4+
Demonstrates using `subprocess.Popen()` to pipe one command into the other.
5+
"""
6+
17
import subprocess
28

39
ls_process = subprocess.Popen(["ls", "/usr/bin"], stdout=subprocess.PIPE)

subprocess/popen_timer.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
"""
2+
Demonstrates `subprocess.Popen()` calling [timer.py](timer.py).
3+
"""
4+
15
import subprocess
26
from time import sleep
37

0 commit comments

Comments
 (0)