Skip to content

Commit

Permalink
Add new subchapters
Browse files Browse the repository at this point in the history
  • Loading branch information
baniasbaabe committed Mar 2, 2024
1 parent d8eb423 commit 66cc51f
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 0 deletions.
2 changes: 2 additions & 0 deletions _toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ chapters:
- file: book/codequality/memory.ipynb
- file: book/codequality/security.ipynb
- file: book/codequality/typing.ipynb
- file: book/codequality/principles.ipynb
- file: book/cooltools/index.ipynb
sections:
- file: book/cooltools/Chapter.ipynb
Expand Down Expand Up @@ -48,6 +49,7 @@ chapters:
sections:
- file: book/pythontricks/Chapter.ipynb
- file: book/pythontricks/utility.ipynb
- file: book/pythontricks/tooling.ipynb
- file: book/selenium/index.ipynb
sections:
- file: book/selenium/Chapter.ipynb
Expand Down
79 changes: 79 additions & 0 deletions book/codequality/principles.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Principles for Code Quality"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## The Law of Demeter"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"It's also known as the Principle of Least Knowledge, saying that an object should only communicate with its immediate neighbors, avoiding to access deeper and deeper objects.\n",
"\n",
"See below for a small example how we would violate and obey the Law of Demeter."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Bad Example\n",
"class Department:\n",
" def __init__(self, manager):\n",
" self.manager = manager\n",
"\n",
" def get_manager_name(self):\n",
" # Bad: Accessing a method of an object deep within the hierarchy\n",
" return self.manager.employee.name\n",
"\n",
"class Employee:\n",
" def __init__(self, name):\n",
" self.name = name\n",
"\n",
"class Manager:\n",
" def __init__(self, employee):\n",
" self.employee = employee\n",
"\n",
"# Good Example\n",
"class Department:\n",
" def __init__(self, manager):\n",
" self.manager = manager\n",
" \n",
" def get_manager_name(self):\n",
" # Good: Not going deeper\n",
" return self.manager.get_name()\n",
"\n",
"class Employee:\n",
" def __init__(self, name):\n",
" self.name = name\n",
"\n",
"class Manager:\n",
" def __init__(self, employee):\n",
" self.employee = employee\n",
"\n",
" def get_name(self):\n",
" return self.employee.name"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
66 changes: 66 additions & 0 deletions book/pythontricks/tooling.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Tooling for Python Projects"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## `uv`: Super-fast pip Alternative "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Forget plain pip for installing packages.\n",
"\n",
"Use `uv` for Python package installing.\n",
"\n",
"`uv` is a blazingly fast package installer and resolver, written in Rust for high performance.\n",
"\n",
"It is a drop-in replacement for pip and pip-tools, being up to 115x faster.\n",
"\n",
"`uv` is still in an early phase, but it's interesting to see where it goes."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"outputs": [],
"source": [
"!curl -LsSf https://astral.sh/uv/install.sh | sh"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"outputs": [],
"source": [
"!uv pip install requests"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit 66cc51f

Please sign in to comment.