Skip to content

Commit

Permalink
Python_Dos: fix syntax highlighting
Browse files Browse the repository at this point in the history
  • Loading branch information
sarah-hlsn committed Dec 8, 2023
1 parent 387ab35 commit 1e7e9b4
Showing 1 changed file with 110 additions and 48 deletions.
158 changes: 110 additions & 48 deletions doc/guide/Guide_PythonDos-n-Donts.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -131,25 +131,30 @@
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* _Indentation_: 4 spaces per level. For continuation lines, decide between vertical alignment & hanging indentation as shown here:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"# Vertically aligned with opening delimiter.\n",
"foo = long_function_name(var_one, var_two,\n",
" var_three, var_four)\n",
"\n",
"* _Indentation_: 4 spaces per level. For continuation lines, decide between vertical alignment & hanging indentation as shown here:\n",
" <pre><code>\n",
" # Vertically aligned with opening delimiter.\n",
" foo = long_function_name(var_one, var_two,\n",
" var_three, var_four)\n",
"\n",
" # Hanging indentation (4 additonal spaces)\n",
" def very_very_long_function_name(\n",
" var_one, var_two, var_three,\n",
" var_four):\n",
" print(var_one)\n",
" </code></pre>\n"
"# Hanging indentation (4 additonal spaces)\n",
"def very_very_long_function_name(\n",
" var_one, var_two, var_three,\n",
" var_four):\n",
" print(var_one)\n"
]
},
{
Expand Down Expand Up @@ -179,12 +184,24 @@
"* _Whitespaces_: \n",
" * **None** immediately inside parentheses, brackets or braces; after trailing commas; for keyword assignments in functions. \n",
" * **Do** for assignments (`i = i + 1`), around comparisons (`>=`, `==`, etc.), around booleans (`and`, `or`, `not`) \n",
" <pre><code>\n",
" # the following 3 examples are correct:\n",
" spam(ham[1], {eggs: 2})\n",
" if x == 4: print x, y; x, y = y, x\n",
" def complex(real, imag=0.0):\n",
" </code></pre>\n",
" * the following 3 examples are correct:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"spam(ham[1], {eggs: 2})\n",
"if x == 4: print x, y; x, y = y, x\n",
"def complex(real, imag=0.0):"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* There's more in the PEP 8 guide!"
]
},
Expand Down Expand Up @@ -271,7 +288,15 @@
},
"source": [
"* Be consistent in return statements. Either all return statements in a function should return an expression, or none of them should. Any return statements where no value is returned should explicitly state this as `return None`.\n",
" <pre><code>\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Correct\n",
"def foo(x):\n",
" if x >= 0:\n",
Expand All @@ -281,9 +306,7 @@
"# Wrong\n",
"def foo(x):\n",
" if x >= 0:\n",
" return math.sqrt(x)\n",
" </code></pre>\n",
" "
" return math.sqrt(x)"
]
},
{
Expand All @@ -294,13 +317,19 @@
}
},
"source": [
"* Object type comparisons should always use isinstance() instead of comparing types directly: \n",
" <pre><code>\n",
"* Object type comparisons should always use isinstance() instead of comparing types directly: \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Correct: \n",
"if isinstance(obj, int):\n",
"# wrong:\n",
"if type(obj) is type(1)\n",
"</code></pre>\n"
"# Wrong:\n",
"if type(obj) is type(1)"
]
},
{
Expand All @@ -311,15 +340,21 @@
}
},
"source": [
"* Remember: sequences (strings, lists, tuples) are false if empty; this can be used: \n",
" <pre><code>\n",
"* Remember: sequences (strings, lists, tuples) are false if empty; this can be used: "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Correct:\n",
"if not seq:\n",
"if seq:\n",
"# Wrong:\n",
"if len(seq):\n",
"if not len(seq)\n",
" </code></pre>"
"if not len(seq)"
]
},
{
Expand All @@ -330,13 +365,19 @@
}
},
"source": [
"* Don't compare boolean values to True or False using `==`:\n",
" <pre><code>\n",
"* Don't compare boolean values to True or False using `==`:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Correct:\n",
"if greeting: \n",
"# Wrong:\n",
"if greeting == True: \n",
" </code></pre>\n"
"if greeting == True: "
]
},
{
Expand All @@ -347,14 +388,25 @@
}
},
"source": [
"* Use ''.startswith() and ''.endswith() instead of string slicing to check for prefixes or suffixes.\n",
" <pre><code>\n",
"* Use ''.startswith() and ''.endswith() instead of string slicing to check for prefixes or suffixes."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Correct:\n",
"if foo.startswith('bar'):\n",
"# Wrong:\n",
"if foo[:3] == 'bar':\n",
" </code></pre>\n",
"\n",
"if foo[:3] == 'bar':"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* Context managers exist and can be useful (mainly for opening and closing files"
]
},
Expand Down Expand Up @@ -537,14 +589,25 @@
}
},
"source": [
"* decorators (a design pattern in Python that allows a user to add new functionality to an existing object without modifying its structure) <br>\n",
"something like \n",
" <pre><code>\n",
"* decorators (a design pattern in Python that allows a user to add new functionality to an existing object without modifying its structure). Something like:\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"@uppercase_decorator\n",
"def say_hi():\n",
" return 'hello there'\n",
" </code></pre>\n",
" \n",
" return 'hello there'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* type checking (Python is a dynamically typed language; also: cf. \"Duck typing\". Yet, as a best practice, variables should not change type once assigned)\n",
"* Do not use mutable default arguments in your functions (e.g. lists). For example, if you define a function as such:\n",
"\n",
Expand All @@ -557,8 +620,7 @@
" ```\n",
" def func(x, list=None):\n",
" list = [] if list is None\n",
" ```\n",
" "
" ```"
]
},
{
Expand Down

0 comments on commit 1e7e9b4

Please sign in to comment.