Skip to content

August of 2021

Computer Science

GNULinux

Alacritty

  • New: Alacritty.

    alacritty/alacritty is a cross-platform, OpenGL terminal emulator.

  • New: Shortcuts.

    • Ctrl+Shift+Space: Enter vi mode.
  • New: Interactive functionality SSH.

Bash

  • New: Remove an environment variable.

    To remove an exported environment variable use:

    unset VARIABLE
    

Commitizen

  • New: Retry failed commit.

Git

  • New: Remove sensitive data from a repository.

Programming

Basics

  • New: Most common built-in exceptions.
  • New: Add datetime.timestamp() method.
  • New: Change function default argument.
  • New: Filter function.
  • New: Dict.setdefault.

    setdefault(key[, default]): If key is in the dictionary, return its value. If not, insert key with a value of default and return default. default defaults to None.

  • New: F-strings.

  • 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
    
  • New: Issubclass.

    issubclass(class, classinfo): Return True if class is a subclass (direct, indirect or virtual) of classinfo. A class is considered a subclass of itself. classinfo may be a tuple of class objects, in which case every entry in classinfo will be checked. In any other case, a TypeError exception is raised.

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

FastAPI

  • New: HTTP API from Python type hints.

    FastAPI is a modern, fast (high-performance), web framework for building HTTP APIs based on standard Python type hints.

  • New: Add route to other module functions.

    There are two main ways of using this module, with the decorator @app.get("{{route}}") or with FastAPI().add_route("{{route}}", {{function}}).

    The first alternative is by far the most common, but if you want to separate the functions from the HTTP API entrypoint, you have to use the second option.

    The advantage of this alternative is that we can import the function from another module.

  • Improvement: Wrapping imported functions.

  • New: Dependency injection.

    A FastAPI decorated function can import the necessary arguments for other functions or objects.

  • New: Default JSON encoder.

    The default the responses will be serialized to JSON. You can use a different JSON encoder than the default one, for example ijl/orjson which is more efficient and natively support serialization of dataclasses and more.

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

psycopg2

  • New: Dynamic SQL queries.
  • New: Dynamic queries with list of vars.

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: Mock environment variables.
  • New: Parametrization.
  • New: How to invoke pytest.

    Invoke with python -m pytest so that the current directory is included to the python path.

  • New: Execute just one test or test class.

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

statsmodels

  • New: Linear regression tutorial.

Snippets

  • New: Find in dictionary.

    Find first element in dictionary that satisfies a condition:

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

Typing

  • New: Type.

    • Type[Class]: Accepts instances of the Class and the Class itself.

unittest

  • New: Python unittest and unittest.mock.

    The unittest unit testing framework was originally inspired by JUnit and has a similar flavor as major unit testing frameworks in other languages. It supports test automation, sharing of setup and shutdown code for tests, aggregation of tests into collections, and independence of the tests from the reporting framework.

    unittest.mock is a library for testing in Python. It allows you to replace parts of your system under test with mock objects and make assertions about how they have been used.

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.

  • New: Getattr.

    getattr(object, name[, default])
    

    Return the value of the named attribute of object.name must be a string. If the string is the name of one of the object's attributes, the result is the value of that attribute. If the named attribute does not exist, default is returned if provided, otherwise AttributeError is raised.

  • New: Weekdays enum.

    Weekdays = IntEnum(
        "Weekdays",
        "Monday Tuesday Wednesday Thursday Friday Saturday Sunday",
        start=0,
    )
    
  • New: Str.strip().

    • str.strip([chars]): Return a copy of the string with the leading and trailing characters removed.