Skip to content

2021

Computer Science

CICD

Github Actions

  • New: Concurrency.

    You can use concurrency to cancel any in-progress job or run. Example:

    concurrency:
      group: docs-${{ github.head_ref }}
      cancel-in-progress: true
    

    This is useful to cancel previous jobs if new commits are pushed, which saves minutes, energy and avoids conflicts when pushing changes during the action.

Programming

Basics

  • New: Add Flutter basics about maps.
  • New: Writing Flutter documentation.
  • New: Combine/merge/concat maps.

    You can use spread operator ...:

    final firstMap = {"1":"2"};
    final secondMap = {"2":"3"};
    
    final thirdMap = {
       ...firstMap,
       ...secondMap,
    };
    
  • New: Find element from list.

    Use .firstWhere():

    List<Currency> currencies = ...;
    Currency dollar = currencies.firstWhere((currency) => currency.code == "USD");
    
  • New: .? operator.

    Use ?. when you want to call a method/getter on an object if that object is not null (otherwise, return null).

    Example:

    currentState?.open();
    

Debugging

  • New: Debugging Flutter.

    Import dart:developer and add debugger(); wherever you want. Then, open your browsers developer tools and use the console.

FastAPI

  • New: Testing startup and shutdown events.

    When you need your event handlers (startup and shutdown) to run in your tests, you can use the TestClient with a with statement:

    import pytest
    from fastapi.testclient import TestClient
    from api.main import api
    
    @pytest.fixture
    def client():
        with TestClient(api) as client:
            yield client
    
    def test_endpoint(client):
        response = client.get("/endpoint")
    
        assert response.status_code == 200
    

pytest

  • New: Class fixture.

    To run a custom function for every test in a class do:

    class TestClass:
        @pytest.fixture(autouse=True)
        def setup(self):
            self.variable = 42
    
        def test_something(self):
            ...
    

Other

  • New: Catch only some kind of errors.

    Use @retry(retry=retry_if_exception_type(IOError)).

    Several types of exceptions can be combined as follows:

    @retry(retry=(retry_if_exception_type(IOError) | retry_if_exception_type(TimeoutError)))
    
  • New: Parse hexadecimal color string.

    /// Construct a color from a hex code string, of the format #RRGGBB.
    Color hexToColor(String code) {
      return new Color(int.parse(code.substring(1, 7), radix: 16) + 0xFF000000);
    }
    
  • New: Find duplicated lines.

    Also counts how many times they appear.

    sort <file> | uniq -c