Day 996
Python typing cheatsheet & other stuff
Nice cheatsheet, not mypy-specific: Type hints cheat sheet (Python 3) — Mypy 0.910 documentation
Otherwise:
- Functions that may return None:
x = 1 if True else None
,x
would beOptional[int]
- Iterables / Sequences:
- Iterable is anything usable inside a
for
- Sequence is anything that supports
len()
- For example:
def f(ints: Iterable[int]) -> List[str]: return [str(x) for x in ints]
- Iterable is anything usable inside a
python unittests through CLI
From docu 1:
python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method
When I’m in the directory with the test_xy.py
files, running python3 -m unittest
runs all of them. I can also do python3 -m unittest test_xy
for that file, and python3 -m unittest test_xy.TestXY.test_specific_thing
.
Debugging python from CLI through breakpoints
Found this, and it’s freaking awesome: Debugging by starting a REPL at a breakpoint is fun
Sample from there:
def make_request():
result = requests.get("https://google.com")
import ipdb; ipdb.set_trace()
There’s the default pdb, there’s ipdb that has to be installed.
Adding
import ipdb; ipdb.set_trace()
anywhere in the code launches a typical debug window that can be used to look into the vars etc.
Just used this for the first time to debug a python program that was running on a remote server and failing, but not locally.
SO much better than print(f"XXX {i}")
and friends!
Nice tutorial about its usage: Better Python Debugging With IPDB
n
- next line in current method (=“step over”)s
- next line of executable code anywhere (=“step into”)c
- continue till next breakpointr
- continue till function returns (would be nice to learn how to do this in pycharm btw!)a
- args - print arguments current function receivedb
- adds breakpoint to locationsb filename.py:234
b <function>
b 123
- line in current file
Full documentation here: 26.2. pdb — The Python Debugger — Python 2.7.18 documentation
Python serializing Enums by declaring them as subclass of str
My main issue with Enum classes was that serialization is weird, especially if you’re dumping parameters. Tried again, found this: python - Serialising an Enum member to JSON - Stack Overflow
TL;DR class EnumDerivedClass(str, Enum)
import json
from enum import Enum
class LogLevel(str, Enum):
DEBUG = 'DEBUG'
INFO = 'INFO'
print(LogLevel.DEBUG)
print(json.dumps(LogLevel.DEBUG))
print(json.loads('"DEBUG"'))
print(LogLevel('DEBUG'))
will output
LogLevel.DEBUG
"DEBUG"
DEBUG
LogLevel.DEBUG
Google Presentations work in progress slides
“Folie überspringen” is a much better way to do what I did with setting a yellow background color - easy to see and worst case scenario it’ll just get skipped
Tensorboard and no data because wrong input folder
If you run tensorboard on a non-existing folder, you’ll get no feedback about it anywhere?.. No data on Tensorboard itself, nothing useful in CLI.