python(3/0)
Run CLI commands in python
Python is a great language. Sometimes, however, you need to interact directly with the operating system. Like in this case, when I needed to run a few Git commands, or if I’d written a Rustlang CLI. Turns out, a pretty simple problem to solve. def try_run_cmd(cmds: List[str], cwd: str) -> Tuple[str, str]: output = None try: output = subprocess.run(cmds, capture_output=True, check=True, cwd=cwd) except CalledProcessError as e: return None, f"{e} : {e.…
Shrink your python dockerfile
A discovery I made when moving services to Docker containers was the sheer size of a Python container! If you only pull down the latest Python base image and install your dependencies, your image can be upwards of 200Mb. Fortunately, there’s at least one multi-stage Dockerfile build tip that can save you lots of space. Local Package Install The command pip install -r requirements installs your package dependencies under the root python library.…
Use HTTP response objects
As Victoria mentions in her post, the type of code you’re writing affects how you’ll handle errors. Her example matches code that integrates with other systems on the same machine (and probably other cases). The code I’m writing these days, when it’s not JavaScript, is for back-end services. I’ve discovered that services delivered over HTTP/S are best implemented to capture exceptions in the service and return response objects. I’ll usually do something like this:…