Skip to content

46th Week of 2021

Coding

Python

asyncio

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 the Generic[AnyStr].

    class File(Generic[AnyStr]):
        """Model a computer file."""
    
        path: str
        content: Optional[AnyStr] = None
    

Properties

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 and removesuffix 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

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

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

Vim

  • Correction: Correct vim snippet to remember the folds when saving a file.