Skip to content

33rd Week of 2021

Computer Science

GNULinux

Bash

  • New: Remove an environment variable.

    To remove an exported environment variable use:

    unset VARIABLE
    

Programming

Basics

  • New: Abstract base classes.

    abstract base classes (ABCs) in Python, as outlined in PEP 3119.

  • New: Enum.

    An enumeration is a set of symbolic names (members) bound to unique, constant values. Within an enumeration, the members can be compared by identity, and the enumeration itself can be iterated over.

  • New: Custom exceptions.

    To provide more insightful error messages related to our code, we can create custom exceptions. For example:

    class ColorError(ValueError):
        def __init__(self, color):
            message = f"Invalid color {color}."
            super().__init__(message)
    
  • 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
    

Docstrings

  • New: Python docstrings.

    A docstring is a string literal that occurs as the first statement in a module, function, class, or method definition. Such a docstring becomes the __doc__ special attribute of that object. See PEP 257

mypy

  • New: Mypy.

    mypy is a static type checker for Python.

  • 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: Pydantic.

    pydantic: data validation and settings management using python type annotations.

    Enforces type hints at runtime, and provides user friendly errors when data is invalid.

  • New: Validators.

    Custom validation and complex relationships between objects can be achieved using the validator decorator.

  • New: Validators advanced use.

  • New: Root_validator.

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

pytest

  • New: Coverage.py.

    To see which parts of your code are executed when running the tests, you can use Coverage.py.

requests

  • New: Python requests library.

    Requests is an elegant and simple HTTP library for Python, built for human beings.

Snippets

  • New: Find in dictionary.

    Find first element in dictionary that satisfies a condition:

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

Other

  • New: Breakpoint().

    Since Python 3.7, we can just call breakpoint() and this will import pdb (or the debugger defined in the environment variable PYTHONBREAKPOINT) and set the breakpoint in that part of the code. This will stop the program execution once the breakpoint is reached. Then, an interactive console while appear.

  • New: Order of abstractmethod classmethod.

    Use @abstractmethod before classmethod. See Issue 16267.