Skip to content

22nd August 2021

Computer Science

Programming

Basics

  • New: String remove prefix suffix methods.

    • str.removeprefix(prefix): If the string starts with the prefix string, return str[len(prefix):]. Otherwise, return a copy of the original string.
    • str.removesuffix(suffix): If the string ends with the suffix string and that suffix is not empty, return str[:-len(suffix)]. Otherwise, return a copy of the original string.
  • New: Current local date and time.

  • New: TypedDict.

    Special construct to add type hints to a dictionary. At runtime it is a plain dict.

    Example:

    class Point2D(TypedDict):
        x: int
        y: int
        label: str
    
    a: Point2D = {'x': 1, 'y': 2, 'label': 'good'}  # OK
    b: Point2D = {'z': 3, 'label': 'bad'}           # Fails type check
    

mypy

  • New: Ignore line for type checking.

    To ignore type checking in one particular line of the code, add the comment # type: ignore at the end of that line

pydantic

  • New: Root_validator.

    Validation can also be performed on the entire model's data.

Snippets

  • New: Find in dictionary.

    Find first element in dictionary that satisfies a condition:

    next(item for item in {dict} if {{ condition }})
    

Other

  • New: Order of abstractmethod classmethod.

    Use @abstractmethod before classmethod. See Issue 16267.