Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
3 changes: 1 addition & 2 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ updates:
interval: monthly
open-pull-requests-limit: 10
allow:
- dependency-name: flake8
- dependency-name: black
- dependency-name: ruff
10 changes: 5 additions & 5 deletions .github/workflows/linters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# https://raw.githubusercontent.com/actions/python-versions/main/versions-manifest.json

name: linters
on:
on:
push:
branches:
- master
Expand All @@ -18,12 +18,12 @@ jobs:
fail-fast: false
matrix:
include:
- {name: Linux312, python: '3.12.3', os: ubuntu-latest}
- {name: Linux313, python: '3.13.7', os: ubuntu-latest}
steps:
- name: Check out repository
uses: actions/checkout@v2

- name: Set up Python
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python }}
Expand All @@ -48,8 +48,8 @@ jobs:
- name: Check code style
run: |
. venv/bin/activate
python -m ruff check .
python -m black --check .
python -m ruff format --check
python -m ruff check

- name: Check directory layout
run: |
Expand Down
17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,26 @@ Due to time constraints, we cannot provide 1:1 support via GitHub. See you on Sl

### Running Code Style Checks

We use [ruff](https://docs.astral.sh/ruff/) and [black](https://black.readthedocs.io/) to ensure a consistent code style for all of our sample code in this repository.
We use [Ruff](https://realpython.com/ruff-python/) to ensure a consistent code style and formatting for all of our sample code in this repository.

Run the following commands to validate your code against the linters:

```sh
$ ruff check .
$ black --check .
$ ruff format --check
$ ruff check
```

Make sure you're using the exact Ruff version specified in [`requirements.txt`](https://github.com/realpython/materials/blob/master/requirements.txt).

### Running Python Code Formatter

We're using a tool called [black](https://black.readthedocs.io/) on this repo to ensure consistent formatting. On CI it runs in "check" mode to ensure any new files added to the repo follow PEP 8. If you see linter warnings that say something like "would reformat some_file.py" it means that black disagrees with your formatting.
Ruff can automatically ensure a consistent code formatting in this repository. On CI, it runs in "check" mode to ensure any new files added to the repo follow [PEP 8](https://realpython.com/python-pep8/). If you see linter warnings that say something like "would reformat some_file.py", then it means that Ruff disagrees with your formatting.

**The easiest way to resolve these errors is to run Black locally on the code and then commit those changes, as explained below.**
The easiest way to resolve these errors is to run Ruff locally on the code and then commit those changes, as explained below.

To automatically re-format your code to be consistent with our code style guidelines, run [black](https://black.readthedocs.io/) in the repository root folder:
To automatically reformat your code to be consistent with our code style guidelines, run [Ruff](https://pypi.org/project/ruff/) in the repository root folder:

```sh
$ black .
$ ruff format
$ ruff check --fix
```
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ def points(line):
case (x1, y1, x2, y2) if y1 == y2:
return [(x, y1) for x in coords(x1, x2)]
case (x1, y1, x2, y2):
return [(x, y) for x, y in zip(coords(x1, x2), coords(y1, y2))]
return [
(x, y)
for x, y in zip(coords(x1, x2), coords(y1, y2), strict=False)
]


def coords(start, stop):
Expand Down
1 change: 0 additions & 1 deletion arcade-platformer/arcade_platformer/11_title_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ def on_update(self, delta_time: float) -> None:

# If the timer has run out, we toggle the instructions
if self.display_timer < 0:

# Toggle whether to show the instructions
self.show_instructions = not self.show_instructions

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ def on_update(self, delta_time: float) -> None:

# If the timer has run out, we toggle the instructions
if self.display_timer < 0:

# Toggle whether to show the instructions
self.show_instructions = not self.show_instructions

Expand Down
1 change: 0 additions & 1 deletion arcade-platformer/arcade_platformer/13_pause_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ def on_update(self, delta_time: float) -> None:

# If the timer has run out, we toggle the instructions
if self.display_timer < 0:

# Toggle whether to show the instructions
self.show_instructions = not self.show_instructions

Expand Down
1 change: 0 additions & 1 deletion arcade-platformer/arcade_platformer/14_enemies.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ def on_update(self, delta_time: float) -> None:

# If the timer has run out, we toggle the instructions
if self.display_timer < 0:

# Toggle whether to show the instructions
self.show_instructions = not self.show_instructions

Expand Down
1 change: 0 additions & 1 deletion arcade-platformer/arcade_platformer/15_moving_platforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ def on_update(self, delta_time: float) -> None:

# If the timer has run out, we toggle the instructions
if self.display_timer < 0:

# Toggle whether to show the instructions
self.show_instructions = not self.show_instructions

Expand Down
1 change: 0 additions & 1 deletion arcade-platformer/arcade_platformer/arcade_platformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ def on_update(self, delta_time: float) -> None:

# If the timer has run out, we toggle the instructions
if self.display_timer < 0:

# Toggle whether to show the instructions
self.show_instructions = not self.show_instructions

Expand Down
4 changes: 1 addition & 3 deletions asyncio-walkthrough/asyncq.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ async def consume(name: int, q: asyncio.Queue) -> None:
await randsleep(caller=f"Consumer {name}")
i, t = await q.get()
now = await seconds()
print(
f"Consumer {name} got element <{i}>" f" in {now-t:0.5f} seconds."
)
print(f"Consumer {name} got element <{i}> in {now - t:0.5f} seconds.")
q.task_done()


Expand Down
3 changes: 1 addition & 2 deletions brython/sha256/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ def compute(evt):
return
if value in hash_map:
alert(
f"The SHA-256 value of '{value}' "
f"already exists: '{hash_map[value]}'"
f"The SHA-256 value of '{value}' already exists: '{hash_map[value]}'"
)
return
hash = hashlib.sha256()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Generated by Django 4.2.4 on 2023-08-29 12:43

from django.db import migrations, models
import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):
Expand Down
1 change: 1 addition & 0 deletions build-a-blog-from-scratch-django/django-blog/manage.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""

import os
import sys

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""

import os
import sys

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""

import os
import sys

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""

import os
import sys

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""

import os
import sys

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""

import os
import sys

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""

import os
import sys

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""

import os
import sys

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""

import os
import sys

Expand Down
5 changes: 1 addition & 4 deletions build-a-web-scraper/04_pipeline.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import requests\n",
"from bs4 import BeautifulSoup"
]
"source": []
},
{
"cell_type": "markdown",
Expand Down
1 change: 1 addition & 0 deletions celery-async-tasks/source_code_final/manage.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""

import os
import sys

Expand Down
1 change: 1 addition & 0 deletions celery-async-tasks/source_code_initial/manage.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""

import os
import sys

Expand Down
6 changes: 5 additions & 1 deletion chatgpt-mentor/fizzbuzz_chatgpt_option_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ def fizzbuzz(n):
(
"fizz buzz"
if i % 15 == 0
else "fizz" if i % 3 == 0 else "buzz" if i % 5 == 0 else i
else "fizz"
if i % 3 == 0
else "buzz"
if i % 5 == 0
else i
)
for i in range(1, n + 1)
]
Expand Down
6 changes: 5 additions & 1 deletion chatgpt-mentor/fizzbuzz_chatgpt_option_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ def fizzbuzz(n):
(
"fizz buzz"
if i % 15 == 0
else "fizz" if i % 3 == 0 else "buzz" if i % 5 == 0 else i
else "fizz"
if i % 3 == 0
else "buzz"
if i % 5 == 0
else i
)
for i in range(1, n + 1)
)
6 changes: 3 additions & 3 deletions chatgpt-unit-tests-python/test_fizzbuzz_pytest.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@
],
)
def test_fizzbuzz(input, expected):
assert (
fizzbuzz(input) == expected
), f"Expected {expected} for input {input}"
assert fizzbuzz(input) == expected, (
f"Expected {expected} for input {input}"
)
2 changes: 1 addition & 1 deletion chatterbot/source_code_final/bot.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from chatterbot.trainers import ListTrainer
from cleaner import clean_corpus

from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer

CORPUS_FILE = "chat.txt"

Expand Down
3 changes: 2 additions & 1 deletion chatterbot/source_code_step_2/bot.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer

from chatterbot import ChatBot

chatbot = ChatBot("Chatpot")

trainer = ListTrainer(chatbot)
Expand Down
3 changes: 2 additions & 1 deletion chatterbot/source_code_step_3/bot.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer

from chatterbot import ChatBot

chatbot = ChatBot("Chatpot")

trainer = ListTrainer(chatbot)
Expand Down
3 changes: 2 additions & 1 deletion chatterbot/source_code_step_4/bot.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer

from chatterbot import ChatBot

chatbot = ChatBot("Chatpot")

trainer = ListTrainer(chatbot)
Expand Down
2 changes: 1 addition & 1 deletion chatterbot/source_code_step_5/bot.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from chatterbot.trainers import ListTrainer
from cleaner import clean_corpus

from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer

CORPUS_FILE = "chat.txt"

Expand Down
4 changes: 2 additions & 2 deletions complex-numbers/bermuda.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"metadata": {},
"outputs": [],
"source": [
"from ipywidgets import interact, IntSlider, FloatSlider"
"from ipywidgets import FloatSlider, IntSlider, interact"
]
},
{
Expand Down Expand Up @@ -66,7 +66,7 @@
" plt.plot(x, y, \"o\", color=stroke)\n",
" plt.gca().add_patch(\n",
" plt.Polygon(\n",
" list(zip(x, y)),\n",
" list(zip(x, y, strict=False)),\n",
" facecolor=fill,\n",
" edgecolor=stroke,\n",
" alpha=0.5,\n",
Expand Down
4 changes: 2 additions & 2 deletions complex-numbers/fourier.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@
}
],
"source": [
"from ipywidgets import interact, FloatSlider\n",
"from IPython.display import Audio, display\n",
"from ipywidgets import FloatSlider, interact\n",
"\n",
"tone = None\n",
"\n",
Expand Down Expand Up @@ -168,7 +168,7 @@
"metadata": {},
"outputs": [],
"source": [
"from cmath import pi, exp\n",
"from cmath import exp, pi\n",
"\n",
"\n",
"def discrete_fourier_transform(x, k):\n",
Expand Down
3 changes: 1 addition & 2 deletions consuming-apis-python/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ def print_user_info(access_token=None):
username = response["login"]
private_repos_count = response["total_private_repos"]
print(
f"{name} ({username}) | "
f"number of private repositories: {private_repos_count}"
f"{name} ({username}) | number of private repositories: {private_repos_count}"
)


Expand Down
2 changes: 0 additions & 2 deletions data-analysis/data_analysis_findings.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -958,9 +958,7 @@
"metadata": {},
"outputs": [],
"source": [
"from sklearn.linear_model import LinearRegression\n",
"import matplotlib.pyplot as plt\n",
"\n",
"from sklearn.linear_model import LinearRegression\n",
"\n",
"x = data.loc[:, [\"imdb\"]]\n",
Expand Down
Loading