Skip to content

Commit

Permalink
Code formatted with Ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
egges authored and github-actions[bot] committed Jan 8, 2025
1 parent 0e02517 commit 983f0f6
Show file tree
Hide file tree
Showing 19 changed files with 118 additions and 82 deletions.
21 changes: 12 additions & 9 deletions 2025/builtin/all.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
],
"source": [
"mixed_list: list[int | None] = [1, 2, None, 4, 5]\n",
"all(mixed_list) # None is falsy\n"
"all(mixed_list) # None is falsy"
]
},
{
Expand Down Expand Up @@ -89,7 +89,7 @@
"\n",
"\n",
"objects = [IsEven(1), IsEven(2), IsEven(0)]\n",
"all(objects) # Output: False (the last object is falsy)\n"
"all(objects) # Output: False (the last object is falsy)"
]
},
{
Expand Down Expand Up @@ -118,7 +118,7 @@
"\n",
"\n",
"objects = [Cart(items=[\"a\"]), Cart(items=[\"b\"]), Cart(items=[])]\n",
"all(objects) # the last object is falsy due to empty list\n"
"all(objects) # the last object is falsy due to empty list"
]
},
{
Expand All @@ -144,12 +144,13 @@
"\n",
" def __len__(self):\n",
" return len([item for item in self.items if item != \"b\"])\n",
" \n",
"\n",
" def __bool__(self):\n",
" return \"a\" not in self.items\n",
" \n",
"\n",
"\n",
"objects = [Cart(items=[\"b\"])]\n",
"all(objects) # __bool__ seems to have higher priority than __len__\n"
"all(objects) # __bool__ seems to have higher priority than __len__"
]
},
{
Expand All @@ -171,7 +172,7 @@
"empty_string = \"\"\n",
"\n",
"print(f\"non_empty_string is {all(non_empty_string)}\") # all characters are truthy\n",
"print(f\"empty_string is {all(empty_string)}\") # empty iterable\n"
"print(f\"empty_string is {all(empty_string)}\") # empty iterable"
]
},
{
Expand All @@ -192,7 +193,9 @@
],
"source": [
"nested_list: list[list[int]] = [[1, 2], [3, 4], []]\n",
"all(nested_list) # the empty list is falsy in this case, since it does not recursively check the nested lists\n"
"all(\n",
" nested_list\n",
") # the empty list is falsy in this case, since it does not recursively check the nested lists"
]
},
{
Expand All @@ -214,7 +217,7 @@
"print(all(numbers)) # all elements are > 0\n",
"\n",
"gen_with_falsy = (x > 0 for x in range(-5, 5))\n",
"print(all(gen_with_falsy)) # -5 to -1 are falsy\n"
"print(all(gen_with_falsy)) # -5 to -1 are falsy"
]
}
],
Expand Down
12 changes: 6 additions & 6 deletions 2025/builtin/any.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"outputs": [],
"source": [
"empty_list: list[int] = []\n",
"print(any(empty_list)) # Output: False\n"
"print(any(empty_list)) # Output: False"
]
},
{
Expand All @@ -17,7 +17,7 @@
"outputs": [],
"source": [
"mixed_values: list[str | int | list[int] | None] = [0, \"\", [], 5, None]\n",
"print(any(mixed_values)) # 5 is truthy\n"
"print(any(mixed_values)) # 5 is truthy"
]
},
{
Expand All @@ -30,7 +30,7 @@
"empty_string = \"\"\n",
"\n",
"print(f\"non_empty_string is {any(non_empty_string)}\") # all characters are truthy\n",
"print(f\"empty_string is {any(empty_string)}\") # empty iterable\n"
"print(f\"empty_string is {any(empty_string)}\") # empty iterable"
]
},
{
Expand All @@ -40,7 +40,7 @@
"outputs": [],
"source": [
"numbers = [-1, 0, -3, 4, -5]\n",
"print(any(x > 0 for x in numbers)) "
"print(any(x > 0 for x in numbers))"
]
},
{
Expand All @@ -60,7 +60,7 @@
"# This will run forever, because all values are positive\n",
"print(any(x < -1 for x in infinite_sequence()))\n",
"\n",
"print(any(x > 10 for x in infinite_sequence()))\n"
"print(any(x > 10 for x in infinite_sequence()))"
]
},
{
Expand All @@ -77,7 +77,7 @@
"\n",
"\n",
"# `any()` stops after finding the first truthy value\n",
"print(any(x > 10 for x in infinite_sequence()))\n"
"print(any(x > 10 for x in infinite_sequence()))"
]
}
],
Expand Down
37 changes: 22 additions & 15 deletions 2025/builtin/dir.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"a = 10\n",
"b = 20\n",
"\n",
"print(dir()) "
"print(dir())"
]
},
{
Expand All @@ -29,6 +29,7 @@
"source": [
"# dir with a custom class\n",
"\n",
"\n",
"class Invoice:\n",
" def __init__(self, client: str, total: float):\n",
" self.client = client\n",
Expand All @@ -37,7 +38,8 @@
" def __str__(self):\n",
" return f\"Invoice from {self.client} for {self.total}\"\n",
"\n",
"print(dir(Invoice))\n"
"\n",
"print(dir(Invoice))"
]
},
{
Expand All @@ -62,10 +64,11 @@
"\n",
" def __str__(self):\n",
" return f\"Invoice from {self.client} for {self.total}\"\n",
" \n",
"\n",
"\n",
"invoice_instance = Invoice(\"Alice\", 200.00)\n",
"\n",
"print(dir(invoice_instance))\n"
"print(dir(invoice_instance))"
]
},
{
Expand All @@ -79,7 +82,7 @@
"import math\n",
"\n",
"print(dir(math))\n",
"print(dir(\"math\"))\n"
"print(dir(\"math\"))"
]
},
{
Expand All @@ -100,7 +103,7 @@
"\n",
"sample_list = [1, 2, 3]\n",
"\n",
"print(dir(sample_list))\n"
"print(dir(sample_list))"
]
},
{
Expand Down Expand Up @@ -142,7 +145,7 @@
"\n",
"sample_string = \"Hello, World!\"\n",
"\n",
"print(dir(sample_string))\n"
"print(dir(sample_string))"
]
},
{
Expand All @@ -153,10 +156,12 @@
"source": [
"# dir with a function\n",
"\n",
"\n",
"def sample_function(x: int) -> int:\n",
" return x * 2\n",
"\n",
"print(dir(sample_function))\n"
"\n",
"print(dir(sample_function))"
]
},
{
Expand All @@ -167,7 +172,7 @@
"source": [
"# dir on a built-in type\n",
"\n",
"print(dir(int))\n"
"print(dir(int))"
]
},
{
Expand All @@ -178,7 +183,7 @@
"source": [
"# Check the directory of None\n",
"\n",
"print(dir(None))\n"
"print(dir(None))"
]
},
{
Expand All @@ -197,12 +202,13 @@
"source": [
"class Person:\n",
" age: int\n",
"\n",
" def __init__(self):\n",
" self.name = \"Python\"\n",
"\n",
"\n",
"obj = Person()\n",
"print(dir(obj)) # 'age' is not listed by dir() because it is not initialized yet\n"
"print(dir(obj)) # 'age' is not listed by dir() because it is not initialized yet"
]
},
{
Expand All @@ -218,11 +224,12 @@
" def __init__(self):\n",
" self._age = \"Hidden\"\n",
"\n",
" def __dir__(self) -> Iterable[str]: # Overriding __dir__ method\n",
" return [\"name\"] \n",
" def __dir__(self) -> Iterable[str]: # Overriding __dir__ method\n",
" return [\"name\"]\n",
"\n",
"\n",
"person = Person()\n",
"print(dir(person)) \n"
"print(dir(person))"
]
},
{
Expand All @@ -247,7 +254,7 @@
"\n",
"obj = Person()\n",
"obj.age = 25\n",
"print(dir(obj))\n"
"print(dir(obj))"
]
}
],
Expand Down
11 changes: 7 additions & 4 deletions 2025/builtin/help.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
" self.total = total\n",
"\n",
" def __str__(self):\n",
" return f'Invoice from {self.client} for {self.total}'\n",
" return f\"Invoice from {self.client} for {self.total}\"\n",
"\n",
"\n",
"help(Invoice)"
]
Expand All @@ -33,6 +34,7 @@
"from dataclasses import dataclass\n",
"from decimal import Decimal\n",
"\n",
"\n",
"@dataclass\n",
"class Invoice:\n",
" def __init__(self, client: str, total: Decimal):\n",
Expand All @@ -43,7 +45,7 @@
" return f\"Invoice from {self.client} for {self.total}\"\n",
"\n",
"\n",
"help(Invoice)\n"
"help(Invoice)"
]
},
{
Expand All @@ -52,7 +54,7 @@
"metadata": {},
"outputs": [],
"source": [
"NAME=\"John\"\n",
"NAME = \"John\"\n",
"\n",
"help(NAME)"
]
Expand All @@ -73,9 +75,10 @@
"outputs": [],
"source": [
"def pay_invoice(invoice: Invoice):\n",
" print(f'Paying invoice {invoice}')\n",
" print(f\"Paying invoice {invoice}\")\n",
" return invoice\n",
"\n",
"\n",
"help(pay_invoice(Invoice(\"John\", Decimal(\"100.00\"))))"
]
},
Expand Down
10 changes: 5 additions & 5 deletions 2025/builtin/iter.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"try:\n",
" print(next(iterator))\n",
"except StopIteration:\n",
" print(\"End of iterator\")\n"
" print(\"End of iterator\")"
]
},
{
Expand All @@ -31,7 +31,7 @@
"iterator = iter(string)\n",
"\n",
"print(next(iterator)) # Output: h\n",
"print(next(iterator)) # Output: e\n"
"print(next(iterator)) # Output: e"
]
},
{
Expand All @@ -56,7 +56,7 @@
"iterator = iter(read_input, \"STOP\")\n",
"\n",
"for value in iterator:\n",
" print(f\"You entered: {value}\")\n"
" print(f\"You entered: {value}\")"
]
},
{
Expand Down Expand Up @@ -84,7 +84,7 @@
"counter = Counter(1, 5)\n",
"for number in counter:\n",
" print(number)\n",
"# Output: 1 2 3 4 5\n"
"# Output: 1 2 3 4 5"
]
},
{
Expand All @@ -111,7 +111,7 @@
"iterator = iter(gen)\n",
"\n",
"print(next(iterator)) # Output: 0\n",
"print(next(iterator)) # Output: 1\n"
"print(next(iterator)) # Output: 1"
]
},
{
Expand Down
6 changes: 3 additions & 3 deletions 2025/builtin/map.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
],
"source": [
"celsius = [0, 10, 20, 30]\n",
"fahrenheit = list(map(lambda c: (c * 9/5) + 32, celsius))\n",
"fahrenheit = list(map(lambda c: (c * 9 / 5) + 32, celsius))\n",
"print(fahrenheit)"
]
},
Expand Down Expand Up @@ -99,7 +99,7 @@
"# Less readable:\n",
"list(map(lambda x: x**2, [1, 2, 3]))\n",
"# More readable:\n",
"[x**2 for x in [1, 2, 3]]\n"
"[x**2 for x in [1, 2, 3]]"
]
},
{
Expand Down Expand Up @@ -130,7 +130,7 @@
"result = map(power, bases, exponents)\n",
"\n",
"# Convert the result to a list and print\n",
"print(list(result))\n"
"print(list(result))"
]
}
],
Expand Down
Loading

0 comments on commit 983f0f6

Please sign in to comment.