Python temporary file

August 2, 2020 | python, programming, libs

tags Python Programming Python’s tempfile standard library is pretty neat when we need to create a temporary file and/or directories. Instead of having a much of code like this: import pathlib def create_file(filename): if pathlib.Path(filename).exists(): # handle path removal/rewrite/backup pass with open(filename, "w") as f: # write to file pass We can ask Python to take care of handling this type of tasks for us. tempfile handles most of the OS API, so we can focus on writting the logic instead. ...

Python Async

July 30, 2020 | python, programming

tags Python Programming Important keywords # Asynchronous IO (async IO) Language-agnostic paradigm (model) coroutine A Python (sort of generator function) async/await Python keywords used to defined a coroutine asyncio Python package that provides an API for running/managing coroutines Coroutine # A coroutine allows a function to pause before returning or indirectly call another coroutine for some time, for example: import asyncio import time async def count(n): print(f"n is {n}") await asyncio. ...

Patching requests HTTP hooks with custom arguments

May 7, 2020 | python

I am working on a project where we have lots of functions integrated with external APIs. Functions where HTTP requests get dispatched, we log the current caller’s name, headers, and data (if any) in case we need to debug anything. For example: import logging import requests LOGGER = logging.getLogger("external") def create_external_services(): ..."[func] | Request {data}") response ="..")"[func] | Response {response.status_code} {response.text}") ... def delete_external_services():"[func] | Request {data}") response = requests. ...