Skip to content

Commit

Permalink
Update slides and requirements.txt file
Browse files Browse the repository at this point in the history
  • Loading branch information
debakarr committed Mar 11, 2024
1 parent b136733 commit 97e4271
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 58 deletions.
2 changes: 1 addition & 1 deletion content/00_index.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.4"
"version": "3.8.18"
}
},
"nbformat": 4,
Expand Down
64 changes: 58 additions & 6 deletions content/01_python_basic_overview.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@
" PI = 3.14159\n",
" ```\n",
"<br />\n",
"\n",
"8. **Global Identifiers:**\n",
" ```python\n",
" global_variable = 10\n",
Expand Down Expand Up @@ -249,7 +250,7 @@
"id": "f9271fdf-38aa-410f-b258-fc325e23b8c7",
"metadata": {},
"source": [
"❗**Gotcha**: `isidentifier` is a bit misleading as it returns `True` for keywords which cannot be used as identifier. There is already [an issue for that](https://bugs.python.org/issue33014)."
"❗**Gotcha**: `isidentifier` is a bit misleading as it returns `True` for keywords which cannot be used as identifier."
]
},
{
Expand Down Expand Up @@ -297,6 +298,27 @@
"with = 1"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f07984dc-358c-4591-a2ef-0433ec3b91da",
"metadata": {},
"outputs": [],
"source": [
"from tabulate import tabulate\n",
"\n",
"\n",
"table_data = []\n",
"for possible_identifier in possible_identifiers:\n",
" is_identifier = possible_identifier.isidentifier()\n",
" is_keyword = possible_identifier in keyword.kwlist\n",
" table_data.append([possible_identifier, is_identifier, is_keyword])\n",
"\n",
"headers = [\"Identifier\", \"isidentifier\", \"iskeyword\"]\n",
"\n",
"print(tabulate(table_data, headers=headers, tablefmt=\"grid\"))"
]
},
{
"cell_type": "markdown",
"id": "479277ab-15f9-4261-a932-bb8e160e702c",
Expand Down Expand Up @@ -349,6 +371,10 @@
"execution_count": null,
"id": "13e74c8c-fc1a-4d3e-94c8-903359591e5c",
"metadata": {
"editable": true,
"slideshow": {
"slide_type": ""
},
"tags": [
"raises-exception"
]
Expand Down Expand Up @@ -1398,7 +1424,13 @@
"cell_type": "code",
"execution_count": null,
"id": "9843aace-0147-408a-ab0d-8643be6a0f0f",
"metadata": {},
"metadata": {
"editable": true,
"slideshow": {
"slide_type": ""
},
"tags": []
},
"outputs": [],
"source": [
"import sys\n",
Expand All @@ -1422,7 +1454,13 @@
"cell_type": "code",
"execution_count": null,
"id": "26f538bd-910d-442a-aece-54b3cd3c43b5",
"metadata": {},
"metadata": {
"editable": true,
"slideshow": {
"slide_type": ""
},
"tags": []
},
"outputs": [],
"source": [
"if \"pyodide\" in sys.modules:\n",
Expand Down Expand Up @@ -2302,7 +2340,15 @@
"cell_type": "code",
"execution_count": null,
"id": "ea01912c-c31d-4b67-89cf-b629b20aa8b0",
"metadata": {},
"metadata": {
"editable": true,
"slideshow": {
"slide_type": ""
},
"tags": [
"raises-exception"
]
},
"outputs": [],
"source": [
"dict3 = dict1 | dict2\n",
Expand Down Expand Up @@ -2894,7 +2940,13 @@
{
"cell_type": "markdown",
"id": "b46061df-1039-4abc-b9c2-e048621a482b",
"metadata": {},
"metadata": {
"editable": true,
"slideshow": {
"slide_type": ""
},
"tags": []
},
"source": [
"\\[<< []() | [Index](./00_index.ipynb) | [Memory Management in Python](./02_memory_management_in_python.ipynb) >>\\]"
]
Expand All @@ -2917,7 +2969,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.4"
"version": "3.8.18"
}
},
"nbformat": 4,
Expand Down
69 changes: 33 additions & 36 deletions content/02_memory_management_in_python.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
{
"cell_type": "markdown",
"id": "abce30df-1482-44e4-9124-47a1c6d17713",
"metadata": {},
"metadata": {
"editable": true,
"slideshow": {
"slide_type": ""
},
"tags": []
},
"source": [
"\\[<< [Basic Overview](./01_python_basic_overview.ipynb) | [Index](./00_index.ipynb) | [Function parameters and arguments](./03_function_parameters_and_arguments.ipynb) >>\\]"
]
Expand Down Expand Up @@ -636,7 +642,13 @@
"cell_type": "code",
"execution_count": null,
"id": "bb5ccdd4-9a9c-4618-983a-d04668367947",
"metadata": {},
"metadata": {
"editable": true,
"slideshow": {
"slide_type": ""
},
"tags": []
},
"outputs": [],
"source": [
"from pympler import asizeof\n",
Expand All @@ -648,7 +660,13 @@
"cell_type": "code",
"execution_count": null,
"id": "47d3f8ec-d600-4690-9586-96282fa7538e",
"metadata": {},
"metadata": {
"editable": true,
"slideshow": {
"slide_type": ""
},
"tags": []
},
"outputs": [],
"source": [
"%pip install scalene"
Expand Down Expand Up @@ -730,18 +748,10 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"id": "b35e7e4d-760d-4339-895c-209cff1fc317",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting example/memory_profile.py\n"
]
}
],
"outputs": [],
"source": [
"%%writefile example/memory_profile.py\n",
"@profile\n",
Expand All @@ -756,29 +766,10 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": null,
"id": "0e3bc5c2-eb97-47f4-9fa2-060f46d64719",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Filename: example/memory_profile.py\n",
"\n",
"Line # Mem usage Increment Occurrences Line Contents\n",
"=============================================================\n",
" 1 37.766 MiB 37.766 MiB 1 @profile\n",
" 2 def my_func():\n",
" 3 45.410 MiB 7.645 MiB 1 a = [1] * (10 ** 6)\n",
" 4 198.000 MiB 152.590 MiB 1 b = [2] * (2 * 10 ** 7)\n",
" 5 45.414 MiB -152.586 MiB 1 del b\n",
" 6 45.414 MiB 0.000 MiB 1 return a\n",
"\n",
"\n"
]
}
],
"outputs": [],
"source": [
"!python -m memory_profiler example/memory_profile.py"
]
Expand Down Expand Up @@ -1817,7 +1808,13 @@
{
"cell_type": "markdown",
"id": "b9b302c9-35a6-4646-9b5a-6bce8d5e9609",
"metadata": {},
"metadata": {
"editable": true,
"slideshow": {
"slide_type": ""
},
"tags": []
},
"source": [
"\\[<< [Basic Overview](./01_python_basic_overview.ipynb) | [Index](./00_index.ipynb) | [Function parameters and arguments](./03_function_parameters_and_arguments.ipynb) >>\\]"
]
Expand All @@ -1839,7 +1836,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
"version": "3.8.18"
}
},
"nbformat": 4,
Expand Down
2 changes: 1 addition & 1 deletion content/03_function_parameters_and_arguments.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1193,7 +1193,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.4"
"version": "3.8.18"
}
},
"nbformat": 4,
Expand Down
2 changes: 1 addition & 1 deletion content/04_namespaces_scopes_and_closures.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.4"
"version": "3.8.18"
}
},
"nbformat": 4,
Expand Down
8 changes: 3 additions & 5 deletions content/example/concate_with_join.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
@profile
def add_string_with_join(iters):
l = []
for i in range(iters):
l.append("abc")
s = "".join(l)
assert len(s) == 3 * iters


add_string_with_join(50000)
assert len(s) == 3*iters

add_string_with_join(500000)
8 changes: 3 additions & 5 deletions content/example/concate_with_plus.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
@profile
def add_string_with_plus(iters):
s = ""
for i in range(iters):
s += "abc"
assert len(s) == 3 * iters


add_string_with_plus(50000)
assert len(s) == 3*iters

add_string_with_plus(500000)
5 changes: 2 additions & 3 deletions content/example/memory_profile.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
@profile
def my_func():
a = [1] * (10**6)
b = [2] * (2 * 10**7)
a = [1] * (10 ** 6)
b = [2] * (2 * 10 ** 7)
del b
return a


my_func()
1 change: 1 addition & 0 deletions content/example/scope.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

val8 = "global scope"

breakpoint()
Expand Down
5 changes: 5 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,8 @@ tabulate

# Black
black[jupyter]

# Perf tools
pympler
scalene
memory-profiler

0 comments on commit 97e4271

Please sign in to comment.