Skip to content

Commit

Permalink
Wraps up python concept review 01
Browse files Browse the repository at this point in the history
  • Loading branch information
hlapp committed Oct 22, 2024
1 parent 8a9ab97 commit d2e3b8f
Showing 1 changed file with 111 additions and 40 deletions.
151 changes: 111 additions & 40 deletions Python-ML/Python-for-ML_01.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,13 @@
"metadata": {},
"outputs": [],
"source": [
"from functools import lru_cache"
"class VerboseFunc:\n",
" def __init__(self, func):\n",
" self.func = func\n",
"\n",
" def __call__(self, *args):\n",
" print(f'called with args = {args} for function {str(self.func)}')\n",
" return self.func(*args)\n"
]
},
{
Expand All @@ -840,12 +846,24 @@
"metadata": {},
"outputs": [],
"source": [
"def fib(n):\n",
" print(n, end=', ')\n",
" if n <= 1:\n",
" return n\n",
" else:\n",
" return fib(n-2) + fib(n-1)"
"import math\n",
"verbose_prod = VerboseFunc(lambda x, y: x * y)\n",
"verbose_prod(4,5)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Classes \n",
"\n",
"- Key idea is encapsulation into objects \n",
"- Everything in Python is an object \n",
"- Attributes and methods \n",
"- What is self? \n",
"- Special methods - double underscore methods \n",
"- Avoid complex inheritance schemes - prefer composition \n",
"- Learn “design patterns” if interested in OOP"
]
},
{
Expand All @@ -854,7 +872,7 @@
"metadata": {},
"outputs": [],
"source": [
"fib(10)"
"(3.0).is_integer()"
]
},
{
Expand All @@ -863,13 +881,7 @@
"metadata": {},
"outputs": [],
"source": [
"@lru_cache(maxsize=100)\n",
"def fib_cache(n):\n",
" print(n, end=', ')\n",
" if n <= 1:\n",
" return n\n",
" else:\n",
" return fib_cache(n-2) + fib_cache(n-1)"
"'hello world'.title()"
]
},
{
Expand All @@ -878,22 +890,34 @@
"metadata": {},
"outputs": [],
"source": [
"fib_cache(10)"
"class Student:\n",
" def __init__(self, first, last):\n",
" self.first = first\n",
" self.last = last\n",
"\n",
" @property\n",
" def name(self):\n",
" return f'{self.first} {self.last}'\n",
"\n",
" @staticmethod\n",
" def is_student(obj):\n",
" return isinstance(obj, Student)\n",
"\n",
" @classmethod\n",
" def fromlist(cls, l):\n",
" print(cls)\n",
" while len(l) < 2:\n",
" l = l + ['Nameless']\n",
" return cls(*l)"
]
},
{
"cell_type": "markdown",
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"### Classes \n",
"\n",
"- Key idea is encapsulation into objects \n",
"- Everything in Python is an object \n",
"- Attributes and methods \n",
"- What is self? \n",
"- Special methods - double underscore methods \n",
"- Avoid complex inheritance schemes - prefer composition \n",
"- Learn “design patterns” if interested in OOP"
"s = Student('Santa', 'Claus')"
]
},
{
Expand All @@ -902,7 +926,7 @@
"metadata": {},
"outputs": [],
"source": [
"(3.0).is_integer()"
"s.name"
]
},
{
Expand All @@ -911,7 +935,7 @@
"metadata": {},
"outputs": [],
"source": [
"'hello world'.title()"
"Student.is_student(s)"
]
},
{
Expand All @@ -920,14 +944,24 @@
"metadata": {},
"outputs": [],
"source": [
"class Student:\n",
" def __init__(self, first, last):\n",
" self.first = first\n",
" self.last = last\n",
" \n",
"s2 = Student.fromlist(['Santa','Claus'])\n",
"type(s2), s2.name"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class GraduateStudent(Student):\n",
" def __init__(self, first, last, program='Masters'):\n",
" super().__init__(first,last)\n",
" self.program = program\n",
"\n",
" @property\n",
" def name(self):\n",
" return f'{self.first} {self.last}' "
" return super().name + f' ({self.program} program)'\n"
]
},
{
Expand All @@ -936,7 +970,7 @@
"metadata": {},
"outputs": [],
"source": [
"s = Student('Santa', 'Claus')"
"s3 = GraduateStudent.fromlist(['Santa','Claus','PhD'])"
]
},
{
Expand All @@ -945,7 +979,25 @@
"metadata": {},
"outputs": [],
"source": [
"s.name"
"s3.name"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"Student.is_student(s3), GraduateStudent.is_student(s3)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"GraduateStudent.fromlist(['John']).name"
]
},
{
Expand Down Expand Up @@ -982,6 +1034,16 @@
" SUN = 7"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from collections.abc import Iterable\n",
"type(Day), isinstance(Day, Iterable)"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -992,6 +1054,15 @@
" print(day)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"[str(day) for day in Day]"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -1140,7 +1211,7 @@
"### Imports, modules and namespaces \n",
"\n",
"- A namespace is basically just a dictionary \n",
"- LEGB \n",
"- **L**(ocal)**E**(nclosing)**G**(lobal)**B**(uiltin) \n",
"- Avoid polluting the global namespace"
]
},
Expand Down Expand Up @@ -1179,9 +1250,9 @@
"x = 23\n",
"\n",
"def f2(x):\n",
" print(locals())\n",
" print('enclosing locals:', locals())\n",
" def g(x):\n",
" print(locals())\n",
" print('enclosed locals:', locals())\n",
" return x \n",
" return g"
]
Expand Down Expand Up @@ -1212,7 +1283,7 @@
"source": [
"### Loops \n",
"\n",
"- Prefer vectorization unless using numba \n",
"- Prefer vectorization unless using [Numba](https://numba.pydata.org) \n",
"- Difference between continue and break \n",
"- Avoid infinite loops \n",
"- Comprehensions and generator expressions"
Expand Down

0 comments on commit d2e3b8f

Please sign in to comment.