Skip to content

Commit d0e9ba2

Browse files
committed
ruff; code cleanliness; typo fixes
1 parent a4b97aa commit d0e9ba2

Some content is hidden

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

54 files changed

+1210
-893
lines changed

.pre-commit-config.yaml

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,21 @@ repos:
1515
entry: nbstripout
1616
language: python
1717
types: [jupyter]
18-
- repo: https://github.com/psf/black
19-
rev: 22.8.0
18+
- repo: https://github.com/astral-sh/ruff-pre-commit
19+
# Ruff version.
20+
rev: v0.4.1
2021
hooks:
21-
- id: black
22-
name: black
23-
description: 'applies black formatter to .py files'
24-
entry: black
25-
language: python
26-
types: [python]
22+
# Run the linter.
23+
- id: ruff
24+
types_or: [python, pyi, jupyter]
25+
args: [ --fix ]
26+
- id: ruff
27+
types_or: [python, pyi, jupyter]
28+
name: sort imports with ruff
29+
args: [--select, I, --fix]
30+
# Run the formatter.
31+
- id: ruff-format
32+
types_or: [python, pyi, jupyter]
2733
- repo: https://github.com/pycqa/flake8
2834
rev: 3.9.2 # pick a git hash / tag to point to
2935
hooks:

auto-office.ipynb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
"metadata": {},
1919
"outputs": [],
2020
"source": [
21+
"import warnings\n",
22+
"\n",
23+
"import matplotlib.pyplot as plt\n",
2124
"import numpy as np\n",
2225
"import pandas as pd\n",
23-
"from rich import inspect\n",
24-
"import matplotlib.pyplot as plt\n",
25-
"import warnings\n",
2626
"\n",
2727
"# Plot settings\n",
2828
"plt.style.use(\n",
@@ -34,9 +34,11 @@
3434
"\n",
3535
"# Set seed for random numbers\n",
3636
"seed_for_prng = 78557\n",
37-
"prng = np.random.default_rng(seed_for_prng) # prng=probabilistic random number generator\n",
37+
"prng = np.random.default_rng(\n",
38+
" seed_for_prng\n",
39+
") # prng=probabilistic random number generator\n",
3840
"# Turn off warnings\n",
39-
"warnings.filterwarnings('ignore')"
41+
"warnings.filterwarnings(\"ignore\")"
4042
]
4143
},
4244
{

code-advanced.ipynb

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@
447447
"outputs": [],
448448
"source": [
449449
"number = 1\n",
450-
"for func in [lambda x: x + 1, lambda x: x * 2, lambda x: x ** 2]:\n",
450+
"for func in [lambda x: x + 1, lambda x: x * 2, lambda x: x**2]:\n",
451451
" number = func(number)\n",
452452
" print(number)"
453453
]
@@ -662,6 +662,7 @@
662662
"outputs": [],
663663
"source": [
664664
"from datetime import date\n",
665+
"\n",
665666
"year_selection = 2030\n",
666667
"new_year = date(year=year_selection, month=1, day=1)\n",
667668
"time_till_ny = new_year - date.today()\n",
@@ -844,7 +845,7 @@
844845
"metadata": {
845846
"celltoolbar": "Tags",
846847
"kernelspec": {
847-
"display_name": "Python 3.10.12 ('codeforecon')",
848+
"display_name": "codeforecon",
848849
"language": "python",
849850
"name": "python3"
850851
},
@@ -859,11 +860,6 @@
859860
"nbconvert_exporter": "python",
860861
"pygments_lexer": "ipython3",
861862
"version": "3.10.13"
862-
},
863-
"vscode": {
864-
"interpreter": {
865-
"hash": "c4570b151692b3082981c89d172815ada9960dee4eb0bedb37dc10c95601d3bd"
866-
}
867863
}
868864
},
869865
"nbformat": 4,

code-advcd-best-practice.ipynb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -994,7 +994,6 @@
994994
"metadata": {},
995995
"outputs": [],
996996
"source": [
997-
"import time\n",
998997
"import cProfile\n",
999998
"\n",
1000999
"\n",
@@ -1025,7 +1024,6 @@
10251024
"source": [
10261025
"from pyinstrument import Profiler\n",
10271026
"\n",
1028-
"\n",
10291027
"profiler = Profiler()\n",
10301028
"profiler.start()\n",
10311029
"\n",

code-basics.ipynb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,7 @@
767767
"outputs": [],
768768
"source": [
769769
"value = 20\n",
770-
"sqrt_val = 20 ** 0.5\n",
770+
"sqrt_val = 20**0.5\n",
771771
"print(f\"The square root of {value:d} is {sqrt_val:.2f}\")"
772772
]
773773
},
@@ -1655,6 +1655,7 @@
16551655
"source": [
16561656
"y = \"I'm a global variable\"\n",
16571657
"\n",
1658+
"\n",
16581659
"def print_y():\n",
16591660
" print(\"y is inside a function:\", y)\n",
16601661
"\n",

code-further-advanced.ipynb

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@
6464
"outputs": [],
6565
"source": [
6666
"def square(x):\n",
67-
" return x ** 2\n",
67+
" return x**2\n",
6868
"\n",
6969
"\n",
7070
"def cube(x):\n",
71-
" return x ** 3\n",
71+
" return x**3\n",
7272
"\n",
7373
"\n",
7474
"def higher_order_function(type): # a higher order function returning a function\n",
@@ -311,7 +311,7 @@
311311
"metadata": {},
312312
"outputs": [],
313313
"source": [
314-
"sq_nums_lc = [num ** 2 for num in range(2, 6)]\n",
314+
"sq_nums_lc = [num**2 for num in range(2, 6)]\n",
315315
"sq_nums_lc"
316316
]
317317
},
@@ -321,7 +321,7 @@
321321
"metadata": {},
322322
"outputs": [],
323323
"source": [
324-
"sq_nums_gc = (num ** 2 for num in range(2, 6))\n",
324+
"sq_nums_gc = (num**2 for num in range(2, 6))\n",
325325
"sq_nums_gc"
326326
]
327327
},
@@ -718,7 +718,7 @@
718718
" self.radius = radius\n",
719719
"\n",
720720
" def area(self) -> float:\n",
721-
" return np.pi * self.radius ** 2\n",
721+
" return np.pi * self.radius**2\n",
722722
"\n",
723723
"\n",
724724
"circle1 = Circle1(\"red\", 2)\n",
@@ -763,7 +763,7 @@
763763
" radius: float\n",
764764
"\n",
765765
" def area(self) -> float:\n",
766-
" return np.pi * self.radius ** 2\n",
766+
" return np.pi * self.radius**2\n",
767767
"\n",
768768
"\n",
769769
"circle2 = Circle2(\"blue\", 2)\n",
@@ -1050,12 +1050,15 @@
10501050
"outputs": [],
10511051
"source": [
10521052
"def outer_function():\n",
1053-
" x = \"hello\"\n",
1054-
" def nested_function():\n",
1055-
" nonlocal x\n",
1056-
" x = \"world\"\n",
1057-
" nested_function()\n",
1058-
" return x\n",
1053+
" x = \"hello\"\n",
1054+
"\n",
1055+
" def nested_function():\n",
1056+
" nonlocal x\n",
1057+
" x = \"world\"\n",
1058+
"\n",
1059+
" nested_function()\n",
1060+
" return x\n",
1061+
"\n",
10591062
"\n",
10601063
"print(outer_function())"
10611064
]

coding-for-economists-quickstart.ipynb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,10 @@
176176
"outputs": [],
177177
"source": [
178178
"# !pip install tqdm\n",
179-
"from tqdm import tqdm\n",
180179
"from time import sleep\n",
181180
"\n",
181+
"from tqdm import tqdm\n",
182+
"\n",
182183
"text = \"\\n Text: \"\n",
183184
"for bit_of_text in tqdm([\"This\", \"is\", \"a\", \"string\"]):\n",
184185
" sleep(0.25)\n",
@@ -296,9 +297,10 @@
296297
"metadata": {},
297298
"outputs": [],
298299
"source": [
299-
"import pandas as pd\n",
300300
"import os\n",
301301
"\n",
302+
"import pandas as pd\n",
303+
"\n",
302304
"# (Change to link)\n",
303305
"df = pd.read_csv(os.path.join(\"data\", \"ames_iowa_house_prices.csv\"))\n",
304306
"df.head()"
@@ -374,9 +376,9 @@
374376
"outputs": [],
375377
"source": [
376378
"import matplotlib.pyplot as plt\n",
377-
"import seaborn as sns\n",
378-
"import scipy.stats as st\n",
379379
"import numpy as np\n",
380+
"import scipy.stats as st\n",
381+
"import seaborn as sns\n",
380382
"\n",
381383
"plt.style.use(\"seaborn-notebook\")\n",
382384
"sns.distplot(df[\"SalePrice\"], bins=100, kde=False, fit=st.lognorm);"

craft_diss_pyramid.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import numpy as np
2-
import matplotlib.pyplot as plt
3-
import matplotlib.patches as mpatches
41
import textwrap as txtwrp
52

3+
import matplotlib.patches as mpatches
4+
import matplotlib.pyplot as plt
5+
import numpy as np
66

77
# Plot settings
88
plt.style.use(

data-advanced.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,6 @@
304304
"\n",
305305
"\n",
306306
"class Schema(pa.SchemaModel):\n",
307-
"\n",
308307
" column1: Series[int] = pa.Field(le=10)\n",
309308
" column2: Series[float] = pa.Field(lt=-1.2)\n",
310309
" column3: Series[str] = pa.Field(str_startswith=\"value_\")\n",
@@ -512,6 +511,7 @@
512511
"source": [
513512
"from datetime import datetime\n",
514513
"from typing import List, Optional\n",
514+
"\n",
515515
"from pydantic import BaseModel\n",
516516
"\n",
517517
"\n",

data-analysis-quickstart.ipynb

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@
3535
"metadata": {},
3636
"outputs": [],
3737
"source": [
38-
"import pandas as pd\n",
38+
"import matplotlib.pyplot as plt\n",
3939
"import numpy as np\n",
40-
"import matplotlib.pyplot as plt"
40+
"import pandas as pd"
4141
]
4242
},
4343
{
@@ -48,7 +48,9 @@
4848
"source": [
4949
"# Set seed for random numbers\n",
5050
"seed_for_prng = 78557\n",
51-
"prng = np.random.default_rng(seed_for_prng) # prng=probabilistic random number generator\n"
51+
"prng = np.random.default_rng(\n",
52+
" seed_for_prng\n",
53+
") # prng=probabilistic random number generator"
5254
]
5355
},
5456
{
@@ -87,12 +89,10 @@
8789
"metadata": {},
8890
"outputs": [],
8991
"source": [
90-
"df = (pd.read_csv(\n",
92+
"df = pd.read_csv(\n",
9193
" \"https://github.com/aeturrell/coding-for-economists/raw/main/data/starwars.csv\",\n",
9294
" index_col=0,\n",
93-
" )\n",
94-
" .dropna(subset=[\"species\"])\n",
95-
" )\n",
95+
").dropna(subset=[\"species\"])\n",
9696
"# Check info about dataframe\n",
9797
"df.info()"
9898
]
@@ -307,7 +307,7 @@
307307
"metadata": {},
308308
"outputs": [],
309309
"source": [
310-
"df['height_m'] = df['height']/100\n",
310+
"df[\"height_m\"] = df[\"height\"] / 100\n",
311311
"df.head()"
312312
]
313313
},
@@ -420,7 +420,9 @@
420420
"metadata": {},
421421
"outputs": [],
422422
"source": [
423-
"df[\"mass_demean_species\"] = df.groupby(\"species\")[\"mass\"].transform(lambda x: x - x.mean())\n",
423+
"df[\"mass_demean_species\"] = df.groupby(\"species\")[\"mass\"].transform(\n",
424+
" lambda x: x - x.mean()\n",
425+
")\n",
424426
"df.head()"
425427
]
426428
},

data-categorical.ipynb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@
2525
"outputs": [],
2626
"source": [
2727
"# remove cell\n",
28-
"import matplotlib_inline.backend_inline\n",
2928
"import matplotlib.pyplot as plt\n",
29+
"import matplotlib_inline.backend_inline\n",
3030
"\n",
3131
"# Plot settings\n",
32-
"plt.style.use(\"https://github.com/aeturrell/coding-for-economists/raw/main/plot_style.txt\")\n",
32+
"plt.style.use(\n",
33+
" \"https://github.com/aeturrell/coding-for-economists/raw/main/plot_style.txt\"\n",
34+
")\n",
3335
"matplotlib_inline.backend_inline.set_matplotlib_formats(\"svg\")"
3436
]
3537
},

data-databases.ipynb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,9 @@
491491
"metadata": {},
492492
"outputs": [],
493493
"source": [
494-
"track.group_by(\"AlbumId\").mutate(mean_mins_track=track.Milliseconds.mean()/1e3/60).order_by(\"mean_mins_track\").limit(5)"
494+
"track.group_by(\"AlbumId\").mutate(\n",
495+
" mean_mins_track=track.Milliseconds.mean() / 1e3 / 60\n",
496+
").order_by(\"mean_mins_track\").limit(5)"
495497
]
496498
},
497499
{

data-exploratory-analysis.ipynb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@
2020
"metadata": {},
2121
"outputs": [],
2222
"source": [
23-
"import pandas as pd\n",
23+
"import warnings\n",
24+
"\n",
2425
"import matplotlib.pyplot as plt\n",
2526
"import numpy as np\n",
26-
"import warnings\n",
27+
"import pandas as pd\n",
2728
"from rich import print"
2829
]
2930
},
@@ -49,7 +50,7 @@
4950
"# Set max rows displayed for readability\n",
5051
"pd.set_option(\"display.max_rows\", 6)\n",
5152
"\n",
52-
"warnings.filterwarnings('ignore')"
53+
"warnings.filterwarnings(\"ignore\")"
5354
]
5455
},
5556
{
@@ -333,8 +334,7 @@
333334
"outputs": [],
334335
"source": [
335336
"(\n",
336-
" df.groupby([\"year_sold\", \"bedrooms\"])\n",
337-
" [\"sale_price\"]\n",
337+
" df.groupby([\"year_sold\", \"bedrooms\"])[\"sale_price\"]\n",
338338
" .mean()\n",
339339
" .unstack()\n",
340340
" .apply(lambda x: x / 1e3)\n",

0 commit comments

Comments
 (0)