Python
Links
- https://learnxinyminutes.com/docs/python/
- https://github.com/amontalenti/elements-of-python-style
- https://github.com/realpython/discover-flask
- https://pythonprogramming.net/basic-gui-pyqt-tutorial/
- Project Layout: http://docs.python-guide.org/en/latest/writing/structure/
- https://mitelman.engineering/blog/python-best-practice/automating-python-best-practices-for-a-new-project/
Local Docs
pydoc3 -b
Download https://docs.python.org/3/download.html and run webserver in the folder.
Oneliners
python -m http.server
IPython
# Autoreload imports
%load_ext autoreload
%autoreload 2
# Debugger
%pdb on
%pdb off
Debugger/PDB
python -m pdb myscript.py
import pdb; pdb.set_trace()
- Debugger Commands
- h(elp)
- step
- next
- continue
- list; ll
- args
- p or pp
- interact (Quit with
Ctrl + d) - up/down
- Setting a Breakpoint:
b myscript.py:10
b function
b requests/sessions.py:555
%debug function()
%pdb on
%pdb off
Argparse
- Documentation
- Example:
def parse_args(args=sys.kwargs[1:]):
parser = argparse.ArgumentParser()
parsed_args = parser.parse_args(args)
return args
Try Except
try:
# Use "raise" to raise an error
raise IndexError("This is an index error")
except IndexError as e:
pass # Pass is just a no-op. Usually you would do recovery here.
except (TypeError, NameError):
pass # Multiple exceptions can be handled together, if required.
else: # Optional clause to the try/except block. Must follow all except blocks
print "All good!" # Runs only if the code in try raises no exceptions
finally: # Execute under all circumstances
print "We can clean up resources here"
Logging
import logging
logger = logging.getLogger(__name__)
# in main():
logging.basicConfig(level=logging.DEBUG)