Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 8977566

Browse filesBrowse files
committed
Squash-merge branch 'main'
1 parent 3103b4d commit 8977566
Copy full SHA for 8977566
Expand file treeCollapse file tree

16 files changed

+665
-337
lines changed

‎.flake8

Copy file name to clipboardExpand all lines: .flake8
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ ignore =
33
E501
44
W503
55
E203
6+
TRY301
67
per-file-ignores =
78
binder/ipython_config.py:E266
+30Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
name: Build Tutorial Container
3+
on:
4+
push:
5+
branches:
6+
- main
7+
paths-ignore:
8+
- '*.md'
9+
- slides/**
10+
- images/**
11+
- .gitignore
12+
workflow_dispatch:
13+
14+
jobs:
15+
repo2docker:
16+
runs-on: ubuntu-latest
17+
permissions:
18+
packages: write
19+
steps:
20+
- name: checkout files in repo
21+
uses: actions/checkout@main
22+
23+
- name: update jupyter dependencies with repo2docker
24+
uses: jupyterhub/repo2docker-action@master
25+
with:
26+
DOCKER_USERNAME: ${{ github.actor }}
27+
DOCKER_PASSWORD: ${{ secrets.GITHUB_TOKEN }}
28+
DOCKER_REGISTRY: ghcr.io
29+
IMAGE_NAME: ${{ github.repository }}
30+
FORCE_REPO2DOCKER_VERSION: jupyter-repo2docker==2023.06.0

‎.gitignore

Copy file name to clipboardExpand all lines: .gitignore
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,4 @@ dmypy.json
132132
.DS_Store
133133
*_files/
134134
*.html
135+
.idea/

‎.mypy.ini

Copy file name to clipboard
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[mypy]
2+
; Do not warn about missing return statements
3+
warn_no_return = False
4+
; Show error messages in context
5+
show_error_context = True
6+
; Allow untyped definitions and calls
7+
disallow_untyped_defs = False
8+
disallow_untyped_calls = False
9+
; Disable import errors
10+
disable_error_code = import

‎.pre-commit-config.yaml

Copy file name to clipboardExpand all lines: .pre-commit-config.yaml
+6-6Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,21 @@ repos:
1717
- id: isort
1818
args: [--profile, black, --filter-files]
1919
- repo: https://github.com/PyCQA/autoflake
20-
rev: v2.2.0
20+
rev: v2.2.1
2121
hooks:
2222
- id: autoflake
2323
- repo: https://github.com/asottile/pyupgrade
24-
rev: v3.9.0
24+
rev: v3.14.0
2525
hooks:
2626
- id: pyupgrade
2727
args: [--py38-plus]
2828
- repo: https://github.com/psf/black
29-
rev: 23.7.0
29+
rev: 23.9.1
3030
hooks:
3131
- id: black
3232
language_version: python3 # Should be a command that runs python3.6+
3333
- repo: https://github.com/PyCQA/flake8
34-
rev: 6.0.0
34+
rev: 6.1.0
3535
hooks:
3636
- id: flake8
3737
args: [--count, --show-source, --statistics]
@@ -45,7 +45,7 @@ repos:
4545
- pyflakes
4646
- tryceratops
4747
- repo: https://github.com/pre-commit/mirrors-mypy
48-
rev: v1.4.1
48+
rev: v1.5.1
4949
hooks:
5050
- id: mypy
5151
additional_dependencies:
@@ -58,7 +58,7 @@ repos:
5858
hooks:
5959
- id: yamlfmt
6060
- repo: https://github.com/asottile/setup-cfg-fmt
61-
rev: v2.4.0
61+
rev: v2.5.0
6262
hooks:
6363
- id: setup-cfg-fmt
6464
- repo: https://github.com/kynan/nbstripout

‎README.md

Copy file name to clipboardExpand all lines: README.md
+55-6Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
# python-tutorial
22

3-
[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/empa-scientific-it/python-tutorial.git/main?labpath=index.ipynb)
4-
3+
[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/empa-scientific-it/python-tutorial.git/main?labpath=index.ipynb) [![Build Docker container](https://github.com/empa-scientific-it/python-tutorial/actions/workflows/build-docker-image.yml/badge.svg)](https://github.com/empa-scientific-it/python-tutorial/actions/workflows/build-docker-image.yml)
54

65
## Run the tutorial on your computer
76

8-
### 0. Prerequisites
7+
You have two ways in which you can run the tutorial **locally**.
8+
9+
### 1. With a `conda` environment
10+
11+
#### 0. Prerequisites
912

1013
To run the tutorial locally, you should first install [conda](https://docs.conda.io/en/latest/miniconda.html) (or [mamba](https://mamba.readthedocs.io/en/latest/installation.html)).
1114

1215
It is also suggested that you have a recent version of `git`. Check out [how to install `git`](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) on your operating system.
1316

14-
### 1. Download the material
17+
#### 1. Download the material
1518

1619
Go to the directory on your machine where you want to download the material and clone the repository:
1720

@@ -25,7 +28,7 @@ Alternatively, you can manually download a ZIP archive with the latest version o
2528

2629
Extract the archive in a directory of your choice.
2730

28-
### 2. Create a dedicated environment
31+
#### 2. Create a dedicated environment
2932

3033
Enter the tutorial folder with
3134

@@ -45,6 +48,7 @@ conda env create -f binder/environment.yml
4548
> If you are on Windows and using Command Prompt or the PowerShell, please make sure to adjust the paths in the commands above accordingly.
4649
4750
Then activate the environment with
51+
4852
```console
4953
conda activate python-tutorial
5054
```
@@ -55,9 +59,54 @@ You can update the existing environment (that is, downloading the latest version
5559
conda env update -f binder/environment.yml
5660
```
5761

58-
### 3. Launch the tutorial via Jupyter
62+
#### 3. Launch the tutorial via Jupyter
5963

6064
Finally, launch JupyterLab with
65+
6166
```console
6267
jupyter lab
6368
```
69+
70+
To update the existing environment, run
71+
72+
```console
73+
conda env update -f environment.yml
74+
```
75+
76+
### 2. With Docker
77+
78+
> **Note**
79+
>
80+
> The following instructions are for Windows. With minor changes, the steps work on macOS or Linux as well.
81+
82+
1. Install Docker Desktop: First, you need to install Docker Desktop on your Windows machine. You can download it from the official Docker website: https://www.docker.com/products/docker-desktop.
83+
84+
2. Create a folder: Open File Explorer and create a new folder where you want to save the tutorial's materials. For example, you could create a folder called "python-tutorial" on your Desktop.
85+
86+
3. Open PowerShell: Once Docker Desktop is installed, open PowerShell on your Windows machine. You can do this by pressing the "Windows" key and typing "PowerShell" in the search bar.
87+
88+
4. Pull the Docker image: In PowerShell, run the following command to pull the "empascientificit/python-tutorial" Docker image:
89+
90+
```console
91+
docker pull ghcr.io/empa-scientific-it/python-tutorial:latest
92+
```
93+
94+
5. Run the Docker container: Once the image is downloaded, run the following command to start a Docker container from the image:
95+
96+
```console
97+
docker run -p 8888:8888 --name python_tutorial -v /path/to/python-tutorial:/home/jovyan/work ghcr.io/empa-scientific-it/python-tutorial:latest jupyter lab --ip 0.0.0.0 --no-browser
98+
```
99+
100+
Replace `/path/to/python-tutorial` with the path to the folder you created in step 2, for example `C:/Users/yourusername/Desktop/python-tutorial`.
101+
102+
> **Note**
103+
>
104+
> The above command will **mirror** the content of your local folder (e.g., `C:/Users/yourusername/Desktop/python-tutorial`) to the `work/` folder **inside the container**. In this way, every file or folder you copy or create into `work/` will be saved on your machine, and will remain there **even if you stop Docker**.
105+
106+
6. Access the Jupyter Notebook: Open a web browser and navigate to `http://localhost:8888/lab`. You should see the Jupyter Notebook interface. Enter the token provided in the PowerShell console to access the notebook. Alternatively, you can directly click on the link that appears in the PowerShell after the container has started.
107+
108+
You can now use the Jupyter in the Docker container to run the python-tutorial. When you're done, you can stop the container by pressing `Ctrl+C` in the PowerShell console.
109+
110+
> **Note**
111+
>
112+
> If you want to restart the container, you can simply run the command `docker container start python_tutorial`.

‎binder/environment.yml

Copy file name to clipboardExpand all lines: binder/environment.yml
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: python-tutorial
33
channels:
44
- conda-forge
55
dependencies:
6-
- python
6+
- python=3.10
77
- pip
88
- pip:
99
- numpy

‎binder/ipython_config.py

Copy file name to clipboardExpand all lines: binder/ipython_config.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Configuration file for ipython.
22

3-
c = get_config() # noqa
3+
c = get_config() # noqa # type: ignore
44

55
# ------------------------------------------------------------------------------
66
# InteractiveShellApp(Configurable) configuration

‎magic_example.ipynb

Copy file name to clipboardExpand all lines: magic_example.ipynb
+1-6Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,8 @@
2222
"%%ipytest\n",
2323
"# or %%ipytest test_module_name\n",
2424
"\n",
25-
"len('a')\n",
2625
"def solution_power2(x: int) -> int:\n",
27-
" print('hi')\n",
28-
" len('b')\n",
29-
" print(len('bb'))\n",
30-
" return x ** 2\n",
31-
"len('aaa')"
26+
" return x * 2"
3227
]
3328
},
3429
{

‎tutorial/tests/test_control_flow.py

Copy file name to clipboardExpand all lines: tutorial/tests/test_control_flow.py
+14-18Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def is_prime(num: int) -> bool:
140140
return True
141141

142142

143-
def reference_solution_find_factors(num: int) -> List[int]:
143+
def reference_find_factors(num: int) -> List[int]:
144144
"""Dumb way to find the factors of an integer"""
145145
if is_prime(num):
146146
return [1, num]
@@ -149,7 +149,7 @@ def reference_solution_find_factors(num: int) -> List[int]:
149149

150150
@pytest.mark.parametrize("num", [350, 487, 965, 816, 598, 443, 13, 17, 211])
151151
def test_find_factors(num: int, function_to_test) -> None:
152-
assert function_to_test(num) == reference_solution_find_factors(num)
152+
assert function_to_test(num) == reference_find_factors(num)
153153

154154

155155
#
@@ -162,7 +162,7 @@ def test_find_factors(num: int, function_to_test) -> None:
162162
)
163163

164164

165-
def reference_solution_find_pair(nums: List[int]) -> int:
165+
def reference_find_pair(nums: List[int]) -> int:
166166
"""Reference solution (part 1)"""
167167
complements = {}
168168
for num in nums:
@@ -173,10 +173,10 @@ def reference_solution_find_pair(nums: List[int]) -> int:
173173

174174
@pytest.mark.parametrize("nums", [nums_1, nums_2])
175175
def test_find_pair(nums: List[int], function_to_test) -> None:
176-
assert function_to_test(nums) == reference_solution_find_pair(nums)
176+
assert function_to_test(nums) == reference_find_pair(nums)
177177

178178

179-
def reference_solution_find_triplet_slow(nums: List[int]) -> int:
179+
def reference_find_triplet_slow(nums: List[int]) -> int:
180180
"""Reference solution (part 2), O(n^3)"""
181181
n = len(nums)
182182
for i in range(n - 2):
@@ -186,7 +186,7 @@ def reference_solution_find_triplet_slow(nums: List[int]) -> int:
186186
return nums[i] * nums_2[j] * nums[k]
187187

188188

189-
def reference_solution_find_triplet(nums: List[int]) -> int:
189+
def reference_find_triplet(nums: List[int]) -> int:
190190
"""Reference solution (part 2), O(n^2)"""
191191
n = len(nums)
192192
for i in range(n - 1):
@@ -201,15 +201,15 @@ def reference_solution_find_triplet(nums: List[int]) -> int:
201201

202202
@pytest.mark.parametrize("nums", [nums_1, nums_2])
203203
def test_find_triplet(nums: List[int], function_to_test) -> None:
204-
assert function_to_test(nums) == reference_solution_find_triplet(nums)
204+
assert function_to_test(nums) == reference_find_triplet(nums)
205205

206206

207207
#
208208
# Exercise 3: Cats with hats
209209
#
210210

211211

212-
def reference_solution_cats_with_hats() -> int:
212+
def reference_cats_with_hats() -> int:
213213
"""Solution with dictionaries"""
214214
cats = {i: False for i in range(1, 101)}
215215

@@ -222,7 +222,7 @@ def reference_solution_cats_with_hats() -> int:
222222

223223

224224
def test_cats_with_hats(function_to_test) -> None:
225-
assert function_to_test() == reference_solution_cats_with_hats()
225+
assert function_to_test() == reference_cats_with_hats()
226226

227227

228228
#
@@ -241,9 +241,7 @@ def parse_data(filename: str) -> List[List[int]]:
241241
trees_1, trees_2 = (parse_data(f"trees_{num}.txt") for num in (1, 2))
242242

243243

244-
def reference_solution_toboggan_p1(
245-
trees_map: List[List[int]], right: int, down: int
246-
) -> int:
244+
def reference_toboggan_p1(trees_map: List[List[int]], right: int, down: int) -> int:
247245
"""Reference solution (part 1)"""
248246
start, trees, depth, width = [0, 0], 0, len(trees_map), len(trees_map[0])
249247
while start[0] < depth:
@@ -262,18 +260,16 @@ def reference_solution_toboggan_p1(
262260
def test_toboggan_p1(
263261
trees_map: List[List[int]], right: int, down: int, function_to_test
264262
) -> None:
265-
assert function_to_test(trees_map, right, down) == reference_solution_toboggan_p1(
263+
assert function_to_test(trees_map, right, down) == reference_toboggan_p1(
266264
trees_map, right, down
267265
)
268266

269267

270-
def reference_solution_toboggan_p2(
271-
trees_map: List[List[int]], slopes: Tuple[Tuple[int]]
272-
) -> int:
268+
def reference_toboggan_p2(trees_map: List[List[int]], slopes: Tuple[Tuple[int]]) -> int:
273269
"""Reference solution (part 2)"""
274270
total = 1
275271
for right, down in slopes:
276-
total *= reference_solution_toboggan_p1(trees_map, right, down)
272+
total *= reference_toboggan_p1(trees_map, right, down)
277273
return total
278274

279275

@@ -293,6 +289,6 @@ def reference_solution_toboggan_p2(
293289
def test_toboggan_p2(
294290
trees_map: List[List[int]], slopes: Tuple[Tuple[int]], function_to_test
295291
) -> None:
296-
assert function_to_test(trees_map, slopes) == reference_solution_toboggan_p2(
292+
assert function_to_test(trees_map, slopes) == reference_toboggan_p2(
297293
trees_map, slopes
298294
)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.