Created for
>>> import this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
>>>
def long_function_name(
var_one, var_two, var_three,
var_four):
print(var_one)
#Arguments on first line forbidden when not using vertical alignment.
def long_function_name(var_one, var_two, var_three,
var_four):
print(var_one)
my_list = [
1, 2, 3,
4, 5, 6,
]
my_list = [
1, 2, 3,
4, 5, 6,
]
result = (op1
+ (op2 * op3)
- op4)
result = (op1 +
op2 *
op3 -
op4)
pip3 install pycodestyle
pycodestyle my_file.py
def print_list(my_list):
for i in my_list:
print(i)
my_list = [
1, 2, 3,
4, 5, 6,]
print_list(my_list)
$ pycodestyle nonPEP8_styled.py
nonPEP8_styled.py:2:3: E111 indentation is not a multiple of four
nonPEP8_styled.py:4:1: E305 expected 2 blank lines after class or function definition, found 0
nonPEP8_styled.py:6:18: E231 missing whitespace after ','
nonPEP8_styled.py:8:20: W292 no newline at end of file
# TODO: put more semantics on this example
def foo(x,y):
"""foo() Summary
Args:
x (TYPE): int
y (TYPE): int
Returns:
TYPE: int
"""
return x+Y
The docstring describes the operation of the function or class and will be shown in an interactive Python session when the user types help
test*.py
by default, or the --pattern keyword argument, if given, on the command line
import unittest
def floor_div(x,y):
return x/y
class PositiveTest(unittest.TestCase):
def test_floor_div_with_positive_integers(self):
self.assertEqual(floor_div(4,3), 1)
$ python3 -m unittest floor_div_test.py
F
======================================================================
FAIL: test_floor_div_with_positive_integers (floor_div_test.PositiveTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/data/projects/www/wwwcourses.github.io/ProgressBG-VC2-Python/pages/themes/beginners/practicalities/examples/floor_div_test.py", line 9, in test_floor_div_with_positive_integers
self.assertEqual(floor_div(4,3), 1)
AssertionError: 1.3333333333333333 != 1
----------------------------------------------------------------------
Ran 1 test in 0.000s
FAILED (failures=1)
# install for a virtual env:
pipenv install pytest
# install for the system python3:
pip3 install --user pytest
def floor_div(x,y):
return x/y
def test_floor_div_with_positive_integers():
assert floor_div(4,3) == 1
>pytest floor_div_pytest.py
========================================================================= test session starts =========================================================================
platform linux -- Python 3.6.4, pytest-3.5.0, py-1.5.3, pluggy-0.6.0
rootdir: /data/projects/www/wwwcourses.github.io/ProgressBG-VC2-Python/pages/themes/beginners/practicalities/examples, inifile: pytest.ini
collected 1 item
floor_div_pytest.py F [100%]
============================================================================== FAILURES ===============================================================================
________________________________________________________________ test_floor_div_with_positive_integers ________________________________________________________________
def test_floor_div_with_positive_integers():
> assert floor_div(4,3) == 1
E assert 1.3333333333333333 == 1
E + where 1.3333333333333333 = floor_div(4, 3)
floor_div_pytest.py:6: AssertionError
====================================================================== 1 failed in 0.03 seconds =======================================================================
# discover tests from every Python file
[pytest]
python_files = *.py
$pytest --collect-only
========================================================================= test session starts =========================================================================
platform linux -- Python 3.6.4, pytest-3.5.0, py-1.5.3, pluggy-0.6.0
rootdir: /data/projects/www/wwwcourses.github.io/ProgressBG-VC2-Python/pages/themes/beginners/practicalities/examples, inifile: pytest.ini
collected 3 items
<Module 'floor_div_pytest.py'>
<Function 'test_floor_div_with_positive_integers'>
<Module 'floor_div_test.py'>
<UnitTestCase 'PositiveTest'>
<TestCaseFunction 'test_floor_div_with_positive_integers'>
<Module 'pytest_sample.py'>
<Function 'test_answer'>
==================================================================== no tests ran in 0.01 seconds =====================================================================
(ProgressBG-VC2-Python)nemsys@X230~gh-pages->
Raymond Hettinger - Beyond PEP 8 -- Best practices for beautiful intelligible code - PyCon 2015
These slides are based on
customised version of
framework