Skip to content

Commit d6a6b19

Browse files
authored
Merge branch 'master' into subprocess
2 parents 5639a77 + 143b339 commit d6a6b19

File tree

382 files changed

+44364
-4324
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

382 files changed

+44364
-4324
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ __pycache__/
88

99
# C extensions
1010
*.so
11+
*.o
1112

1213
# Distribution / packaging
1314
.Python

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Build Status:
77

88
## Got a Question?
99

10-
The best way to get support for Real Python courses, articles, and code in this repository is to join one of our [weekly Office Hours calls](https://realpython.com/office-hours/) or to ask your question in the [RP Community Slack](https://realpython.com/community/).
10+
The best way to get support for Real Python courses, articles, and code in this repository is to join one of our [weekly Office Hours calls](https://realpython.com/office-hours/) or to ask your question in the [RP Community Chat](https://realpython.com/community/).
1111

1212
Due to time constraints, we cannot provide 1:1 support via GitHub. See you on Slack or on the next Office Hours call 🙂
1313

add-to-path/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# How to Add Python to Path
2+
3+
Supporting materials for the Real Python tutorial [How to Add Python to PATH](https://realpython.com/add-python-to-path/).
4+
5+
Here you'll find a file for Windows and one for Linux and macOS. They have snippets and instructions distilled from the tutorials.
6+
7+
Additionally, you'll find some ways to set environment variables and modify `PATH` with PowerShell on Windows that aren't included in the tutorial.

add-to-path/linux-and-macos.sh

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# ------------------------------------
2+
# Add Path to PATH
3+
# ------------------------------------
4+
5+
# Make sure you use the right profile for your system
6+
$ export PATH="<PATH_TO_PYTHON>:$PATH"
7+
8+
# ------------------------------------
9+
# Add Path-Adding Operation to Profile
10+
# ------------------------------------
11+
12+
# Make sure you use the right profile for your system
13+
$ echo export PATH="<PATH_TO_PYTHON>:$PATH" >> ~/.profile
14+
# Reread profile to see effect in same session
15+
$ source ~/.profile
16+
17+
# ------------------------------------
18+
# Filtering Bad Paths from PATH
19+
# ------------------------------------
20+
21+
# View current PATH
22+
$ echo $PATH
23+
# Output:
24+
# /usr/local/sbin:/usr/local/bin:/usr/sbin:/home/realpython/badpython:/usr/bin:
25+
# /sbin:/bin:/usr/games:/usr/local/games
26+
27+
# View on separate lines by replacing colons with newlines
28+
$ echo $PATH | tr ":" "\n"
29+
# Output:
30+
# /usr/local/sbin
31+
# /usr/local/bin
32+
# /usr/sbin
33+
# /home/realpython/badpython
34+
# /usr/bin
35+
# /sbin
36+
# /bin
37+
# /usr/games
38+
# /usr/local/games
39+
40+
# Filter out certain lines with grep -v
41+
$ echo $PATH | tr ":" "\n" | grep -v 'badpython'
42+
# Output:
43+
# /usr/local/sbin
44+
# /usr/local/bin
45+
# /usr/sbin
46+
# /usr/bin
47+
# /sbin
48+
# /bin
49+
# /usr/games
50+
# /usr/local/games
51+
52+
# Replace newlines with colons
53+
$ echo $PATH | tr ":" "\n" | grep -v 'badpython' | tr "\n" ":"
54+
# Output:
55+
# /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:
56+
# /usr/local/games
57+
58+
# Assign to PATH
59+
$ export PATH=`echo $PATH | tr ":" "\n" | grep -v 'badpython' | tr "\n" ":"`

add-to-path/windows.ps1

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# --------------------------------------------------
2+
# How to Add to `PATH` via GUI
3+
# --------------------------------------------------
4+
5+
<#
6+
7+
Once you've located your Python executable, open the Start menu and search
8+
for the _Edit the system environment variables_ entry, which opens up a
9+
_System Properties_ window. In the _Advanced_ tab, click on the button
10+
_Environment Variables_. There you'll see _User_ and _System_ variables,
11+
which you'll be able to edit.
12+
13+
In the section entitled _User Variables_, double-click on the entry that
14+
says _Path_. Another window will pop up showing a list of paths. Click the
15+
_New_ button and paste the path to your Python executable there. Once that's
16+
inserted, select your newly added path and click the _Move Up_ button until
17+
it's at the top.
18+
19+
That's it! You may need to reboot your computer for the changes to take
20+
effect, but you should now be able to call `python` from the command line.
21+
22+
#>
23+
24+
# --------------------------------------------------
25+
# Working With Environment Variables With PowerShell
26+
# --------------------------------------------------
27+
28+
# Set temporary environment variable (only active for current shell session)
29+
PS> $ENV:TEST = "VALUE"
30+
31+
# Set permanent environment variable (need to restart shell to take effect)
32+
PS> [Environment]::SetEnvironmentVariable("TEST", "VALUE", "User")
33+
34+
PS> cd ENV:
35+
PS> ls
36+
37+
<# OUTPUT
38+
39+
Name Value
40+
---- -----
41+
ALLUSERSPROFILE C:\ProgramData
42+
ANSICON 166x32766 (166x66)
43+
ANSICON_DEF 7
44+
APPDATA C:\Users\RealPython\AppData\Roaming
45+
AZ_ENABLED False
46+
...
47+
TEST VALUE
48+
...
49+
50+
#>
51+
52+
# View PATH
53+
PS> (cat ENV:PATH) -Split ";"
54+
55+
<# OUTPUT
56+
57+
C:\Program Files\PowerShell\7
58+
C:\Windows\system32
59+
C:\Windows
60+
C:\Windows\System32\WindowsPowerShell\v1.0\
61+
C:\Windows\System32\OpenSSH\
62+
63+
#>
64+
65+
# Append path to PATH
66+
PS> $ENV:PATH = "$ENV:PATH;C:\new\path"
67+
68+
# Prepend path to PATH
69+
PS> $ENV:PATH = "C:\new\path;$ENV:PATH"

advent-of-code/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Advent of Code: Solving Your Puzzles With Python
2+
3+
This repository holds the code for the Real Python [Advent of Code: Solving Your Puzzles With Python](https://realpython.com/python-advent-of-code/) tutorial.
4+
5+
## Dependencies
6+
7+
[Pytest](https://realpython.com/pytest-python-testing/) is used for testing. You should first create a virtual environment:
8+
9+
```console
10+
$ python -m venv venv
11+
$ source venv/bin/activate
12+
```
13+
14+
You can then install `pytest` with `pip`:
15+
16+
```console
17+
(venv) $ python -m pip install pytest
18+
```
19+
20+
The [aoc_grid.py](aoc_grid.py) example uses [Colorama](https://pypi.org/project/colorama/) and [NumPy](https://realpython.com/numpy-tutorial/). To run that example, you should also install those packages into your environment:
21+
22+
```console
23+
(venv) $ python -m pip install colorama numpy
24+
```
25+
26+
The puzzle solutions only use Python's standard library. Note that the solution to [Day 5, 2021](solutions/2021/05_hydrothermal_venture/) uses [structural pattern matching](https://realpython.com/python310-new-features/#structural-pattern-matching) which is only available in [Python 3.10](https://realpython.com/python310-new-features/) and later.
27+
28+
## Author
29+
30+
- **Geir Arne Hjelle**, E-mail: [[email protected]]([email protected])
31+
32+
## License
33+
34+
Distributed under the MIT license. See [`LICENSE`](../LICENSE) for more information.

advent-of-code/aoc_grid.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import numpy as np
2+
from colorama import Cursor
3+
4+
grid = np.array(
5+
[
6+
[1, 1, 1, 1, 1],
7+
[1, 0, 0, 0, 1],
8+
[1, 1, 1, 0, 1],
9+
[1, 0, 0, 2, 1],
10+
[1, 1, 1, 1, 1],
11+
]
12+
)
13+
14+
num_rows, num_cols = grid.shape
15+
for row in range(num_rows):
16+
for col in range(num_cols):
17+
symbol = " *o"[grid[row, col]]
18+
print(f"{Cursor.POS(col + 1, row + 2)}{symbol}")
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from dataclasses import dataclass
2+
3+
4+
@dataclass
5+
class StateMachine:
6+
memory: dict[str, int]
7+
program: list[str]
8+
9+
def run(self):
10+
"""Run the program"""
11+
current_line = 0
12+
while current_line < len(self.program):
13+
instruction = self.program[current_line]
14+
15+
# Set a register to a value
16+
if instruction.startswith("set "):
17+
register, value = instruction[4], int(instruction[6:])
18+
self.memory[register] = value
19+
20+
# Increase the value in a register by 1
21+
elif instruction.startswith("inc "):
22+
register = instruction[4]
23+
self.memory[register] += 1
24+
25+
# Move the line pointer
26+
current_line += 1
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Day 1, 2019: The Tyranny of the Rocket Equation
2+
3+
Resources:
4+
5+
- **Puzzle:** [adventofcode.com](https://adventofcode.com/2019/day/1)
6+
- **Solution:** [realpython.com](https://realpython.com/python-advent-of-code/#practicing-advent-of-code-day-1-2019)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"""AoC 1, 2019: The Tyranny of the Rocket Equation"""
2+
3+
# Standard library imports
4+
import pathlib
5+
import sys
6+
7+
8+
def parse(puzzle_input):
9+
"""Parse input"""
10+
return [int(line) for line in puzzle_input.split("\n")]
11+
12+
13+
def part1(module_masses):
14+
"""Solve part 1"""
15+
return sum(mass // 3 - 2 for mass in module_masses)
16+
17+
18+
def part2(module_masses):
19+
"""Solve part 2"""
20+
return sum(all_fuel(mass) for mass in module_masses)
21+
22+
23+
def all_fuel(mass):
24+
"""Calculate fuel while taking mass of the fuel into account.
25+
26+
## Example:
27+
28+
>>> all_fuel(1969)
29+
966
30+
"""
31+
fuel = mass // 3 - 2
32+
if fuel <= 0:
33+
return 0
34+
else:
35+
return fuel + all_fuel(mass=fuel)
36+
37+
38+
def solve(puzzle_input):
39+
"""Solve the puzzle for the given input"""
40+
data = parse(puzzle_input)
41+
solution1 = part1(data)
42+
solution2 = part2(data)
43+
44+
return solution1, solution2
45+
46+
47+
if __name__ == "__main__":
48+
for path in sys.argv[1:]:
49+
print(f"\n{path}:")
50+
solutions = solve(puzzle_input=pathlib.Path(path).read_text().strip())
51+
print("\n".join(str(solution) for solution in solutions))

0 commit comments

Comments
 (0)