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
-
Debugger/PDB
- <https://pymotw.com/2/pdb/>
- [Documentation](https://docs.python.org/3.10/library/pdb.html#module-pdb)
- [Debugger Commands](https://docs.python.org/3.10/library/pdb.html#debugger-commands)
- [Blog Post](https://djangostars.com/blog/debugging-python-applications-with-pdb/)
- [Interactive PDB](https://pypi.org/project/ipdb/)
- ```bash
python -m pdb myscript.py
```
```python
import pdb; pdb.set_trace()
```
- [Debugger Commands](https://docs.python.org/3/library/pdb.html#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
```
- Use debugger in IPython:
- [%debug](https://ipython.readthedocs.io/en/stable/interactive/magics.html#magic-debug)
- [%pdb](https://ipython.readthedocs.io/en/stable/interactive/magics.html#magic-pdb)
- ```python
%debug function()
%pdb on
%pdb off
```
Argparse
- [Documentation](https://docs.python.org/3.10/howto/argparse.html)
- Example:
```python
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"
-
-
Django
- [[Django]]
Logging
- [Howto](https://docs.python.org/3/howto/logging.html#)
- [Advanced](https://docs.python.org/3/howto/logging.html#advanced-logging-tutorial)
Base Template:
- ```python
import logging
logger = logging.getLogger(__name__)
# in main():
logging.basicConfig(level=logging.DEBUG)
```