python: from programmer to tamer
Post on 14-Sep-2014
936 views
DESCRIPTION
Knuth’s sentence "It was nice to learn Python; a nice afternoon" describes very well how easy it is to get engaged and fall in love with our favourite snake. But if you are a professional programmer and use and abuse Python for paying your bills, there is a chance that your beloved pet will go out of control and bite you! To save you some scars, I want to share with you 10 topical steps of my evolution from a Python programmer into a Python tamer.TRANSCRIPT
![Page 1: PYTHON: From programmer to tamer](https://reader034.vdocuments.site/reader034/viewer/2022050804/5415981c8d7f722f6c8b47a8/html5/thumbnails/1.jpg)
PYTHON: From programmer to tamerAlessandro Pisa - 2013/07/03 - EuroPython 2013
![Page 3: PYTHON: From programmer to tamer](https://reader034.vdocuments.site/reader034/viewer/2022050804/5415981c8d7f722f6c8b47a8/html5/thumbnails/3.jpg)
HOW I MET PYTHON
![Page 4: PYTHON: From programmer to tamer](https://reader034.vdocuments.site/reader034/viewer/2022050804/5415981c8d7f722f6c8b47a8/html5/thumbnails/4.jpg)
It was nice to play football
![Page 5: PYTHON: From programmer to tamer](https://reader034.vdocuments.site/reader034/viewer/2022050804/5415981c8d7f722f6c8b47a8/html5/thumbnails/5.jpg)
But rain happens!
![Page 6: PYTHON: From programmer to tamer](https://reader034.vdocuments.site/reader034/viewer/2022050804/5415981c8d7f722f6c8b47a8/html5/thumbnails/6.jpg)
Gimme more football!
![Page 7: PYTHON: From programmer to tamer](https://reader034.vdocuments.site/reader034/viewer/2022050804/5415981c8d7f722f6c8b47a8/html5/thumbnails/7.jpg)
c64
![Page 8: PYTHON: From programmer to tamer](https://reader034.vdocuments.site/reader034/viewer/2022050804/5415981c8d7f722f6c8b47a8/html5/thumbnails/8.jpg)
Something went wrong with Santa...
![Page 9: PYTHON: From programmer to tamer](https://reader034.vdocuments.site/reader034/viewer/2022050804/5415981c8d7f722f6c8b47a8/html5/thumbnails/9.jpg)
MSX2
![Page 10: PYTHON: From programmer to tamer](https://reader034.vdocuments.site/reader034/viewer/2022050804/5415981c8d7f722f6c8b47a8/html5/thumbnails/10.jpg)
Let the children play(otherwise bad things happen!)
![Page 11: PYTHON: From programmer to tamer](https://reader034.vdocuments.site/reader034/viewer/2022050804/5415981c8d7f722f6c8b47a8/html5/thumbnails/11.jpg)
Blue screen of Death...
...all the time!
![Page 12: PYTHON: From programmer to tamer](https://reader034.vdocuments.site/reader034/viewer/2022050804/5415981c8d7f722f6c8b47a8/html5/thumbnails/12.jpg)
Ready to become a Nerd
![Page 13: PYTHON: From programmer to tamer](https://reader034.vdocuments.site/reader034/viewer/2022050804/5415981c8d7f722f6c8b47a8/html5/thumbnails/13.jpg)
HARDWARE FAILURES
![Page 14: PYTHON: From programmer to tamer](https://reader034.vdocuments.site/reader034/viewer/2022050804/5415981c8d7f722f6c8b47a8/html5/thumbnails/14.jpg)
Let's GIVE Software another chance
True story :(
![Page 15: PYTHON: From programmer to tamer](https://reader034.vdocuments.site/reader034/viewer/2022050804/5415981c8d7f722f6c8b47a8/html5/thumbnails/15.jpg)
A lucky afternoon!
![Page 16: PYTHON: From programmer to tamer](https://reader034.vdocuments.site/reader034/viewer/2022050804/5415981c8d7f722f6c8b47a8/html5/thumbnails/16.jpg)
Ready to gooooo!
![Page 17: PYTHON: From programmer to tamer](https://reader034.vdocuments.site/reader034/viewer/2022050804/5415981c8d7f722f6c8b47a8/html5/thumbnails/17.jpg)
OR NOT?
![Page 18: PYTHON: From programmer to tamer](https://reader034.vdocuments.site/reader034/viewer/2022050804/5415981c8d7f722f6c8b47a8/html5/thumbnails/18.jpg)
Question time
![Page 19: PYTHON: From programmer to tamer](https://reader034.vdocuments.site/reader034/viewer/2022050804/5415981c8d7f722f6c8b47a8/html5/thumbnails/19.jpg)
It's A HARD LIFE
![Page 20: PYTHON: From programmer to tamer](https://reader034.vdocuments.site/reader034/viewer/2022050804/5415981c8d7f722f6c8b47a8/html5/thumbnails/20.jpg)
Only the strongest will survive
![Page 21: PYTHON: From programmer to tamer](https://reader034.vdocuments.site/reader034/viewer/2022050804/5415981c8d7f722f6c8b47a8/html5/thumbnails/21.jpg)
RTFM!But...
![Page 22: PYTHON: From programmer to tamer](https://reader034.vdocuments.site/reader034/viewer/2022050804/5415981c8d7f722f6c8b47a8/html5/thumbnails/22.jpg)
Documentation is searchable and browsablehttp://www.python.org/doc/
![Page 23: PYTHON: From programmer to tamer](https://reader034.vdocuments.site/reader034/viewer/2022050804/5415981c8d7f722f6c8b47a8/html5/thumbnails/23.jpg)
Kaizen
Daily homeworkhttp://planet.python.org/http://twitter.comhttp://plus.google.com...
Don't be afraid of asking helpAny pythonista aroundMailing listIRChttp://stackoverflow.comhttp://www.python.org/community
You are here, and this is
nice!
![Page 24: PYTHON: From programmer to tamer](https://reader034.vdocuments.site/reader034/viewer/2022050804/5415981c8d7f722f6c8b47a8/html5/thumbnails/24.jpg)
YOur friend PDB
Get in touch with your gears
![Page 25: PYTHON: From programmer to tamer](https://reader034.vdocuments.site/reader034/viewer/2022050804/5415981c8d7f722f6c8b47a8/html5/thumbnails/25.jpg)
Debugging
A nice writeup to start playing with PDB:http://pymotw.com/2/pdb/
Search for PDB in PyPI: https://pypi.python.org/pypi?%3Aaction=search&term=pdb+debugger&submit=search
Search debugging tools for your app on PyPI!
![Page 26: PYTHON: From programmer to tamer](https://reader034.vdocuments.site/reader034/viewer/2022050804/5415981c8d7f722f6c8b47a8/html5/thumbnails/26.jpg)
No more print
![Page 27: PYTHON: From programmer to tamer](https://reader034.vdocuments.site/reader034/viewer/2022050804/5415981c8d7f722f6c8b47a8/html5/thumbnails/27.jpg)
Logging
Logging is an important part of your code!
![Page 28: PYTHON: From programmer to tamer](https://reader034.vdocuments.site/reader034/viewer/2022050804/5415981c8d7f722f6c8b47a8/html5/thumbnails/28.jpg)
Exceptions
EXCEPTIONS ARE an important part of your code!
![Page 29: PYTHON: From programmer to tamer](https://reader034.vdocuments.site/reader034/viewer/2022050804/5415981c8d7f722f6c8b47a8/html5/thumbnails/29.jpg)
How good is your code?
https://pypi.python.org/pypi/pep8https://pypi.python.org/pypi/pyflakeshttps://pypi.python.org/pypi/PyCheckerhttps://pypi.python.org/pypi/pylint
![Page 30: PYTHON: From programmer to tamer](https://reader034.vdocuments.site/reader034/viewer/2022050804/5415981c8d7f722f6c8b47a8/html5/thumbnails/30.jpg)
Use PYPI - The Cheeseshophttps://pypi.python.org/pypi
https://testpypi.python.org/pypi
![Page 31: PYTHON: From programmer to tamer](https://reader034.vdocuments.site/reader034/viewer/2022050804/5415981c8d7f722f6c8b47a8/html5/thumbnails/31.jpg)
Virtualenv
https://pypi.python.org/pypi/virtualenvhttp://docs.python-guide.org/en/latest/dev/virtualenvs.html
http://www.buildout.org/
![Page 32: PYTHON: From programmer to tamer](https://reader034.vdocuments.site/reader034/viewer/2022050804/5415981c8d7f722f6c8b47a8/html5/thumbnails/32.jpg)
Forget backup copies
● bazaar (bzr)● git (git)● mercurial (hg)● subversion (svn)
![Page 33: PYTHON: From programmer to tamer](https://reader034.vdocuments.site/reader034/viewer/2022050804/5415981c8d7f722f6c8b47a8/html5/thumbnails/33.jpg)
You can choose your place
No warranty about qualitY of life...
● - http://bitbucket.org (git, hg)● - http://code.google.com (git, hg, svn)● - http://github.org (git, svn)● - http://launchpad.net (bzr)
![Page 34: PYTHON: From programmer to tamer](https://reader034.vdocuments.site/reader034/viewer/2022050804/5415981c8d7f722f6c8b47a8/html5/thumbnails/34.jpg)
Tickets!
![Page 35: PYTHON: From programmer to tamer](https://reader034.vdocuments.site/reader034/viewer/2022050804/5415981c8d7f722f6c8b47a8/html5/thumbnails/35.jpg)
A quite comprehensive listhttp://en.wikipedia.org/wiki/Comparison_of_issue-tracking_systems
A ticket aggregatorhttps://pypi.python.org/pypi/bugwarrior
![Page 36: PYTHON: From programmer to tamer](https://reader034.vdocuments.site/reader034/viewer/2022050804/5415981c8d7f722f6c8b47a8/html5/thumbnails/36.jpg)
Tests
![Page 37: PYTHON: From programmer to tamer](https://reader034.vdocuments.site/reader034/viewer/2022050804/5415981c8d7f722f6c8b47a8/html5/thumbnails/37.jpg)
Be agile!
![Page 38: PYTHON: From programmer to tamer](https://reader034.vdocuments.site/reader034/viewer/2022050804/5415981c8d7f722f6c8b47a8/html5/thumbnails/38.jpg)
Measure twice, cut once!
![Page 39: PYTHON: From programmer to tamer](https://reader034.vdocuments.site/reader034/viewer/2022050804/5415981c8d7f722f6c8b47a8/html5/thumbnails/39.jpg)
SCALABILITY
Will your application handle...
... more data?
... more users?
... more REQUEST?
![Page 40: PYTHON: From programmer to tamer](https://reader034.vdocuments.site/reader034/viewer/2022050804/5415981c8d7f722f6c8b47a8/html5/thumbnails/40.jpg)
Fail fast!
![Page 41: PYTHON: From programmer to tamer](https://reader034.vdocuments.site/reader034/viewer/2022050804/5415981c8d7f722f6c8b47a8/html5/thumbnails/41.jpg)
The times I was bitten by Python!
![Page 43: PYTHON: From programmer to tamer](https://reader034.vdocuments.site/reader034/viewer/2022050804/5415981c8d7f722f6c8b47a8/html5/thumbnails/43.jpg)
Nerd wars
● Use the Source, Luke● The Source will be with you, always● The Source is strong with this one● May the Source be with you
https://pypi.python.org/pypi/jedi
![Page 44: PYTHON: From programmer to tamer](https://reader034.vdocuments.site/reader034/viewer/2022050804/5415981c8d7f722f6c8b47a8/html5/thumbnails/44.jpg)
Some random tips
Use builtins!all, any, filter, map, sum, zip, ...http://docs.python.org/3/library/functions.html
Code like a Pythonista!http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html
Context managers!http://www.python.org/dev/peps/pep-0343/
![Page 45: PYTHON: From programmer to tamer](https://reader034.vdocuments.site/reader034/viewer/2022050804/5415981c8d7f722f6c8b47a8/html5/thumbnails/45.jpg)
Use comprehensions
[ale@padme presentations]$python3>>> [x for x in range(10)][0, 1, 2, 3, 4, 5, 6, 7, 8, 9]>>> (x for x in range(10))<generator object <genexpr> at 0xb7187234>>>> {x for x in range(10)}{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}>>> {x:x for x in range(10)}{0: 0, 1: 1, 2: 2, 3: 3, 4: 4, ..., 9: 9}
![Page 46: PYTHON: From programmer to tamer](https://reader034.vdocuments.site/reader034/viewer/2022050804/5415981c8d7f722f6c8b47a8/html5/thumbnails/46.jpg)
Logging
from logging import getLogger
logger = getLogger('Test logger')
def abs_ratio(x, y):
try:
return abs(x / y)
except:
logger.exception('Error abs_ratio: %r, %r' % (x, y))
return -1
abs_ratio(1, None)
abs_ratio(1, 0)
![Page 47: PYTHON: From programmer to tamer](https://reader034.vdocuments.site/reader034/viewer/2022050804/5415981c8d7f722f6c8b47a8/html5/thumbnails/47.jpg)
Logging
Error abs_ratio: 1, None
Traceback (most recent call last):
File "logexample.py", line 6, in abs_ratio
return abs(x / y)
TypeError: unsupported operand type(s) for /: 'int'
and 'NoneType'
Error abs_ratio: 1, 0
Traceback (most recent call last):
File "logexample.py", line 6, in abs_ratio
return abs(x / y)
ZeroDivisionError: division by zero
http://pymotw.com/2/logging/http://pymotw.com/2/exceptions/