Skip to content

Commit 5c9d28a

Browse files
committed
Add illustration of using pylint
1 parent 0449fe8 commit 5c9d28a

File tree

16 files changed

+544
-0
lines changed

16 files changed

+544
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Static analysis
2+
3+
Static analyzers such as pylint or flack8 help to improve the
4+
quality of your code, both by detecting some potential bugs
5+
as well as ensuring PEP 8 compliance.
6+
7+
## What is it?
8+
9+
* `pylint`: illustration of steps to improve some old code and
10+
make it PEP 8 compliant.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# pylint
2+
3+
pylint is a static analyzer that helps you improve your code by
4+
detecting potential bugs and style issues. It rates the source code
5+
on a scale from 0 to 10.
6+
7+
# What is it?
8+
9+
* `context_00.py`/`report_00.txt`: original code and corresponding
10+
report.
11+
* `context_01.py`/`report_01.txt`: the string formatting using the
12+
`format` method has been replaced by f-strings.
13+
* `context_02.py`/`report_02.txt`: Python 2-style `object` base class
14+
eliminated, and superfluous `else` removed.
15+
* `context_03.py`/`report_03.txt`: Python 2-style `object` base class
16+
eliminated, and superfluous `else` removed.
17+
* `context_04.py`/`report_04.txt`: fix variable names.
18+
* `context_05.py`/`report_05.txt`: remove unreachalbe statement.
19+
* `context_06.py`/`report_06.txt`: add docstrings.
20+
21+
## Note
22+
23+
The exception is raised on purpose to illustrate exception handling by
24+
a context manager.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/env python
2+
3+
from contextlib import contextmanager
4+
import sys
5+
6+
7+
class ContextTest(object):
8+
9+
def __init__(self, context_nr):
10+
self._context_nr = context_nr
11+
print('created with {0}'.format(self._context_nr))
12+
13+
def __enter__(self):
14+
print('entering {0}'.format(self._context_nr))
15+
return self
16+
17+
def __exit__(self, exception_type, exception_value, backtrace):
18+
print('exiting {0}'.format(self._context_nr))
19+
if exception_type:
20+
print('exception in context {0}:'.format(self._context_nr))
21+
print('\t', exception_type, exception_value, backtrace)
22+
return
23+
else:
24+
print('no exception in context {0}'.format(self._context_nr))
25+
26+
27+
@contextmanager
28+
def label(name):
29+
print('entering label({0})'.format(name))
30+
yield name
31+
print('exiting label({0})'. format(name))
32+
33+
34+
def main():
35+
with ContextTest(1) as c1, ContextTest(2) as c2:
36+
print('in context {0}'.format(c1._context_nr))
37+
print('in context {0}'.format(c2._context_nr))
38+
with label('foo') as foo, label('bar') as bar:
39+
print(foo, bar)
40+
with ContextTest(1) as c1, ContextTest(2) as c2:
41+
print('in context {0}'.format(c1._context_nr))
42+
raise Exception()
43+
print('in context {0}'.format(c2._context_nr))
44+
return 0
45+
46+
if __name__ == '__main__':
47+
status = main()
48+
sys.exit(status)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/env python
2+
3+
from contextlib import contextmanager
4+
import sys
5+
6+
7+
class ContextTest(object):
8+
9+
def __init__(self, context_nr):
10+
self._context_nr = context_nr
11+
print(f'created with {self._context_nr}')
12+
13+
def __enter__(self):
14+
print(f'entering {self._context_nr}')
15+
return self
16+
17+
def __exit__(self, exception_type, exception_value, backtrace):
18+
print(f'exiting {self._context_nr}')
19+
if exception_type:
20+
print(f'exception in context {self._context_nr}:')
21+
print('\t', exception_type, exception_value, backtrace)
22+
return
23+
else:
24+
print(f'no exception in context {self._context_nr}')
25+
26+
27+
@contextmanager
28+
def label(name):
29+
print(f'entering label({name})')
30+
yield name
31+
print(f'exiting label({name})')
32+
33+
34+
def main():
35+
with ContextTest(1) as c1, ContextTest(2) as c2:
36+
print(f'in context {c1._context_nr}')
37+
print(f'in context {c2._context_nr}')
38+
with label('foo') as foo, label('bar') as bar:
39+
print(foo, bar)
40+
with ContextTest(1) as c1, ContextTest(2) as c2:
41+
print(f'in context {c1._context_nr}')
42+
raise Exception()
43+
print(f'in context {c2._context_nr}')
44+
return 0
45+
46+
if __name__ == '__main__':
47+
status = main()
48+
sys.exit(status)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env python
2+
3+
from contextlib import contextmanager
4+
import sys
5+
6+
7+
class ContextTest:
8+
9+
def __init__(self, context_nr):
10+
self._context_nr = context_nr
11+
print(f'created with {self._context_nr}')
12+
13+
def __enter__(self):
14+
print(f'entering {self._context_nr}')
15+
return self
16+
17+
def __exit__(self, exception_type, exception_value, backtrace):
18+
print(f'exiting {self._context_nr}')
19+
if exception_type:
20+
print(f'exception in context {self._context_nr}:')
21+
print('\t', exception_type, exception_value, backtrace)
22+
return
23+
print(f'no exception in context {self._context_nr}')
24+
25+
26+
@contextmanager
27+
def label(name):
28+
print(f'entering label({name})')
29+
yield name
30+
print(f'exiting label({name})')
31+
32+
33+
def main():
34+
with ContextTest(1) as c1, ContextTest(2) as c2:
35+
print(f'in context {c1._context_nr}')
36+
print(f'in context {c2._context_nr}')
37+
with label('foo') as foo, label('bar') as bar:
38+
print(foo, bar)
39+
with ContextTest(1) as c1, ContextTest(2) as c2:
40+
print(f'in context {c1._context_nr}')
41+
raise Exception()
42+
print(f'in context {c2._context_nr}')
43+
return 0
44+
45+
if __name__ == '__main__':
46+
status = main()
47+
sys.exit(status)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/env python
2+
3+
from contextlib import contextmanager
4+
import sys
5+
6+
7+
class ContextTest:
8+
9+
_context_nr: int
10+
11+
@property
12+
def context_nr(self):
13+
return self._context_nr
14+
15+
@context_nr.setter
16+
def context_nr(self, value):
17+
self._context_nr = value
18+
19+
def __init__(self, context_nr):
20+
self._context_nr = context_nr
21+
print(f'created with {self._context_nr}')
22+
23+
def __enter__(self):
24+
print(f'entering {self._context_nr}')
25+
return self
26+
27+
def __exit__(self, exception_type, exception_value, backtrace):
28+
print(f'exiting {self._context_nr}')
29+
if exception_type:
30+
print(f'exception in context {self._context_nr}:')
31+
print('\t', exception_type, exception_value, backtrace)
32+
return
33+
print(f'no exception in context {self._context_nr}')
34+
35+
36+
@contextmanager
37+
def label(name):
38+
print(f'entering label({name})')
39+
yield name
40+
print(f'exiting label({name})')
41+
42+
43+
def main():
44+
with ContextTest(1) as c1, ContextTest(2) as c2:
45+
print(f'in context {c1.context_nr}')
46+
print(f'in context {c2.context_nr}')
47+
with label('foo') as foo, label('bar') as bar:
48+
print(foo, bar)
49+
with ContextTest(1) as c1, ContextTest(2) as c2:
50+
print(f'in context {c1.context_nr}')
51+
raise Exception()
52+
print(f'in context {c2.context_nr}')
53+
return 0
54+
55+
if __name__ == '__main__':
56+
status = main()
57+
sys.exit(status)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/env python
2+
3+
from contextlib import contextmanager
4+
import sys
5+
6+
7+
class ContextTest:
8+
9+
_context_nr: int
10+
11+
@property
12+
def context_nr(self):
13+
return self._context_nr
14+
15+
@context_nr.setter
16+
def context_nr(self, value):
17+
self._context_nr = value
18+
19+
def __init__(self, context_nr):
20+
self._context_nr = context_nr
21+
print(f'created with {self._context_nr}')
22+
23+
def __enter__(self):
24+
print(f'entering {self._context_nr}')
25+
return self
26+
27+
def __exit__(self, exception_type, exception_value, backtrace):
28+
print(f'exiting {self._context_nr}')
29+
if exception_type:
30+
print(f'exception in context {self._context_nr}:')
31+
print('\t', exception_type, exception_value, backtrace)
32+
return
33+
print(f'no exception in context {self._context_nr}')
34+
35+
36+
@contextmanager
37+
def label(name):
38+
print(f'entering label({name})')
39+
yield name
40+
print(f'exiting label({name})')
41+
42+
43+
def main():
44+
with ContextTest(1) as context_1, ContextTest(2) as context_2:
45+
print(f'in context {context_1.context_nr}')
46+
print(f'in context {context_2.context_nr}')
47+
with label('foo') as foo_label, label('bar') as bar_label:
48+
print(foo_label, bar_label)
49+
with ContextTest(1) as context_1, ContextTest(2) as context_2:
50+
print(f'in context {context_1.context_nr}')
51+
raise Exception()
52+
print(f'in context {context_2.context_nr}')
53+
return 0
54+
55+
if __name__ == '__main__':
56+
STATUS = main()
57+
sys.exit(STATUS)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/env python
2+
3+
from contextlib import contextmanager
4+
import sys
5+
6+
7+
class ContextTest:
8+
9+
_context_nr: int
10+
11+
@property
12+
def context_nr(self):
13+
return self._context_nr
14+
15+
@context_nr.setter
16+
def context_nr(self, value):
17+
self._context_nr = value
18+
19+
def __init__(self, context_nr):
20+
self._context_nr = context_nr
21+
print(f'created with {self._context_nr}')
22+
23+
def __enter__(self):
24+
print(f'entering {self._context_nr}')
25+
return self
26+
27+
def __exit__(self, exception_type, exception_value, backtrace):
28+
print(f'exiting {self._context_nr}')
29+
if exception_type:
30+
print(f'exception in context {self._context_nr}:')
31+
print('\t', exception_type, exception_value, backtrace)
32+
return
33+
print(f'no exception in context {self._context_nr}')
34+
35+
36+
@contextmanager
37+
def label(name):
38+
print(f'entering label({name})')
39+
yield name
40+
print(f'exiting label({name})')
41+
42+
43+
def main():
44+
with ContextTest(1) as context_1, ContextTest(2) as context_2:
45+
print(f'in context {context_1.context_nr}')
46+
print(f'in context {context_2.context_nr}')
47+
with label('foo') as foo_label, label('bar') as bar_label:
48+
print(foo_label, bar_label)
49+
with ContextTest(1) as context_1, ContextTest(2) as context_2:
50+
print(f'in context {context_1.context_nr}')
51+
raise Exception()
52+
# print(f'in context {context_2.context_nr}')
53+
return 0
54+
55+
56+
if __name__ == '__main__':
57+
STATUS = main()
58+
sys.exit(STATUS)

0 commit comments

Comments
 (0)