46th Week of 2021
Coding⚑
Python⚑
asyncio⚑
-
New: Limit concurrency.
Use
asyncio.Semaphore
.sem = asyncio.Semaphore(10) async with sem: # work with shared resource
Type Hints⚑
-
New: Define a TypeVar with restrictions.
from typing import TypeVar AnyStr = TypeVar('AnyStr', str, bytes)
-
New: Use a constrained TypeVar in the definition of a class attributes.
If you try to use a
TypeVar
in the definition of a class attribute:class File: """Model a computer file.""" path: str content: Optional[AnyStr] = None # mypy error!
mypy will complain with
Type variable AnyStr is unbound [valid-type]
, to solve it, you need to make the class inherit from theGeneric[AnyStr]
.class File(Generic[AnyStr]): """Model a computer file.""" path: str content: Optional[AnyStr] = None
Properties⚑
-
New: Automatically generate a factory from a pydantic model.
Sadly it's not yet supported, it will at some point though. If you're interested in following this path, you can start with mgaitan snippet for dataclasses.
-
New: Give an overview on Python's @property decorator.
Python Snippets⚑
-
New: Make a flat list of lists with a list comprehension.
There is no nice way to do it :(. The best I've found is:
t = [[1, 2, 3], [4, 5, 6], [7], [8, 9]] flat_list = [item for sublist in t for item in sublist]
-
New: Remove a substring from the end of a string.
On Python 3.9 and newer you can use the
removeprefix
andremovesuffix
methods to remove an entire substring from either side of the string:url = 'abcdc.com' url.removesuffix('.com') # Returns 'abcdc' url.removeprefix('abcdc.') # Returns 'com'
On Python 3.8 and older you can use
endswith
and slicing:url = 'abcdc.com' if url.endswith('.com'): url = url[:-4]
Pydantic⚑
-
New: Define fields to exclude from exporting at config level.
Eagerly waiting for the release of the version 1.9 because you can define the fields to exclude in the
Config
of the model using something like:class User(BaseModel): id: int username: str password: str class Transaction(BaseModel): id: str user: User value: int class Config: fields = { 'value': { 'alias': 'Amount', 'exclude': ..., }, 'user': { 'exclude': {'username', 'password'} }, 'id': { 'dump_alias': 'external_id' } }
The release it's taking its time because the developer's gremlin and salaried work are sucking his time off.
-
New: Field customization.
Optionally, the
Field
function can be used to provide extra information about the field and validations. Such as thetitle
,default
,description
and many others
Tenacity⚑
-
New: Introduce the Tenacity python library.
Tenacity is an Apache 2.0 licensed general-purpose retrying library, written in Python, to simplify the task of adding retry behavior to just about anything.
DevOps⚑
Infrastructure Solutions⚑
Jobs⚑
-
New: Manually creating a job from a cronjob.
kubectl create job {{ job_name }} -n {{ namespace }} \ --from=cronjobs/{{ cronjob_name}}
Continuous Integration⚑
Pyment⚑
-
New: Introduce Pyment.
Pyment is a python3 program to automatically create, update or convert docstrings in existing Python files, managing several styles.
As of 2021-11-17, the program is not production ready yet for me, I've tested it in one of my projects and found some bugs that needed to be fixed before it's usable. Despite the number of stars, it looks like the development pace has dropped dramatically, so it needs our help to get better :).
Operative Systems⚑
Linux⚑
Github cli⚑
-
New: Basic usage of gh.
gh
is GitHub’s official command line tool.It can be used to speed up common operations done with github, such as opening PRs, merging them or checking the checks of the PRs