Skip to content

Commit 5556d5d

Browse files
committed
Add imports and docstring sections on style page
1 parent 267eb02 commit 5556d5d

File tree

2 files changed

+65
-4
lines changed

2 files changed

+65
-4
lines changed

episodes/style-guide.md

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,77 @@ The [PEP 8 Style Guide for Python Code](https://peps.python.org/pep-0008/) gives
88

99
## Indentation
1010

11-
Use 4 spaces per indentation level and spaces are the preferred indentation method.
11+
Spaces (not tabs) are the preferred indentation method. Use 4 spaces per indentation level.
12+
13+
```python
14+
# Correct
15+
for x in range(10):
16+
print("x is", i)
17+
18+
def sayhello():
19+
x = "Hello there"
20+
return x
21+
22+
# Wrong
23+
for x in range(10):
24+
print("x is", x)
25+
26+
def sayhello():
27+
x = "Hello there"
28+
return x
29+
```
1230

1331
## Imports
1432

1533
Imports should be on separate lines except when using the from syntax. Imports are always put at the top of the file, just after any module comments and docstrings, and before module globals and constants.
1634

1735
```python
18-
import os
19-
import sys
36+
# Correct
37+
import numpy
38+
import polars
2039

2140
from subprocess import Popen, PIPE
41+
42+
# Wrong
43+
import numpy, polars
44+
```
45+
46+
Imports should be grouped in the order shown below. An empty line should be placed between each group of imports.
47+
48+
1. Standard library imports
49+
2. Third party imports
50+
3. Local application/library specific imports
51+
52+
```python
53+
# Standard library
54+
import os
55+
import math
56+
57+
# Third party
58+
import numpy as np
59+
import matplotlib.pyplot as plt
60+
61+
# Local package
62+
from mypackage.utils import sayhello
63+
```
64+
65+
Wildcard imports such as `from package import *` should be avoided because it is unclear what is actually imported from the package. This confuses both human readers of the code as well as automated tools.
66+
67+
## Documentation strings
68+
69+
Documentation strings, or docstrings, should be written for modules, functions, classes, and methods to describe the associated code. Several docstring styles exist such as the [NumPy style](https://numpydoc.readthedocs.io/en/latest/format.html) and the [Google style](https://google.github.io/styleguide/pyguide.html#s3.8-comments-and-docstrings). The example below uses the Google docstring style.
70+
71+
```python
72+
def sayhello(x: str) -> str:
73+
"""Create a greeting to say hello to someone.
74+
75+
Args:
76+
x: Name of a person for the greeting.
77+
78+
Returns:
79+
A greeting for the given name.
80+
"""
81+
greeting = f"Hello {x}, how are you today?"
82+
return greeting
2283
```
2384

episodes/writing.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ Specifically:
1313
- Poor grammar and spelling can undermine the perceived quality of the research, reduce the article’s impact, and may lead to rejection from journals or negative reviewer assessments.
1414
- Mastery of grammar and spelling demonstrates attention to detail and academic rigor, which are vital in scholarly publishing.
1515

16-
In summary, grammar and spelling are foundational to writing a clear, credible, and professional journal article, helping to communicate research findings effectively to the academic community. Just as grammar and spelling are essential for clear communication in journal articles, proper spelling, consistent naming, and correct syntax in code are vital for clarity, functionality, maintainability, and teamwork in software development. Developers can accomplish well written Python code by adhering to Python's style guide while using linter and formatter tools.
16+
Just as grammar and spelling are essential for clear communication in journal and technical articles, proper spelling, consistent naming, and correct syntax in code are vital for clarity, functionality, maintainability, and teamwork in software development. Developers can accomplish well written Python code by adhering to Python's style guide while using linter and formatter tools.
1717

0 commit comments

Comments
 (0)