Unpack Iterables in Python
You can unpack a Python iterable (such as list, tuple, string) by assigning the items of that iterable to different variables.
You can unpack a Python iterable (such as list, tuple, string) by assigning the items of that iterable to different variables.
Comparing two Python dictionaries ignores the order of items. If you want to consider the order of items, use OrderedDict instead.
OrderedDict: Create an Ordered Python Dictionary Read More »
map() runs a Python function for each item in an iterable synchronously. To speed up the execution, map a function asynchronously with Prefect.
For basic concepts of Prefect, check out the getting started tutorials.
If you are running a function that occasionally fails, such as calling an API, it is helpful to rerun the function on failure.
Prefect allows you to retry a task (a function with the task decorator) up to a specified number of times.
If you are not aware of Prefect, check out the getting started tutorials.
Sometimes, it is useful to cancel a function call when the execution time is longer than expected.
In Prefect, you can limit the execution time of a Python function call with the decorators task(timeout_seconds=n) or flow(timeout_seconds=n).
If you are not aware of Prefect, checkout the getting started tutorials.
Limit the Execution Time of a Function Call with Prefect Read More »
Are you using nested for-loops to experiment with different combinations of parameters? If so, use itertools.product instead.
itertools.product is more efficient than nested loop because product(A, B) is equivalent to ((x,y) for x in A for y in B).
itertools.product: Nested For-Loops in a Generator Expression Read More »
Usually, you might use nested parentheses to combine multiple functions. If you want to increase the readability of your code by using pipes, try Pipe.
If you want to get the path relative to another path, use pathlib.Path.relative_to.
If you want to use type hints to check that a variable or a function parameter is in a set of literal values, use typing.Literal.
In the example above, since grape is not in the set of literal values, mypy raises an error.
This method is available in Python 3.8 and above.
typing.Literal: Specify the Possible Values for a Function Parameter Read More »
If you want to extract substrings from a string, but find it challenging to do so with RegEx, try parse. parse makes it easy to extract strings that are inside brackets.
parse also allows you to:
add field names to make brackets more readable
get the string with a certain format