Skip to content

32nd Week of 2022

Activism

Antifascism

Antifascist Actions

Coding

Languages

Dash

  • Correction: Deprecate in favour of Streamlit.

    Streamlit is a much more easy, beautiful and clean library for the same purpose.

  • New: Running process in background.

    By default, each running command blocks until completion. If you have a long-running command, you can put it in the background with the _bg=True special kwarg:

    sleep(3)
    print("...3 seconds later")
    
    p = sleep(3, _bg=True)
    print("prints immediately!")
    p.wait()
    print("...and 3 seconds later")
    

    You’ll notice that you need to call RunningCommand.wait() in order to exit after your command exits.

    Commands launched in the background ignore SIGHUP, meaning that when their controlling process (the session leader, if there is a controlling terminal) exits, they will not be signalled by the kernel. But because sh commands launch their processes in their own sessions by default, meaning they are their own session leaders, ignoring SIGHUP will normally have no impact. So the only time ignoring SIGHUP will do anything is if you use _new_session=False, in which case the controlling process will probably be the shell from which you launched python, and exiting that shell would normally send a SIGHUP to all child processes.

    If you want to terminate the process use p.kill().

  • New: Output callbacks.

    In combination with _bg=True, sh can use callbacks to process output incrementally by passing a callable function to _out and/or _err. This callable will be called for each line (or chunk) of data that your command outputs:

    from sh import tail
    
    def process_output(line):
        print(line)
    
    p = tail("-f", "/var/log/some_log_file.log", _out=process_output, _bg=True)
    p.wait()
    

    To “quit” your callback, simply return True. This tells the command not to call your callback anymore. This does not kill the process though see Interactive callbacks for how to kill a process from a callback.

    The line or chunk received by the callback can either be of type str or bytes. If the output could be decoded using the provided encoding, a str will be passed to the callback, otherwise it would be raw bytes.

Qwik

  • New: Introduce Qwik.

    Qwik is a new kind of web framework that can deliver instantly load web applications at any size or complexity. Your sites and apps can boot with about 1kb of JS (regardless of application complexity), and achieve consistent performance at scale.

    You can see a good overview in the Qwik presentation.

Pytest

  • New: Change log level of a dependency.

    caplog.set_level(logging.WARNING, logger="urllib3")
    
  • New: Show logging messages on the test run.

    Add to your pyproject.toml:

    [tool.pytest.ini_options]
       log_cli = true
       log_cli_level = 10
    

    Or run it in the command itself pytest -o log_cli=true --log-cli-level=10 func.py.

    Remember you can change the log level of the different components in case it's too verbose.

Python Snippets

  • New: Create random number.

    import random
    a=random.randint(1,10)
    
  • New: Check if local port is available or in use.

    Create a temporary socket and then try to bind to the port to see if it's available. Close the socket after validating that the port is available.

    def port_in_use(port):
        """Test if a local port is used."""
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        with suppress(OSError):
            sock.bind(("0.0.0.0", port))
            return True
        sock.close()
        return False
    

Git

DevOps

Infrastructure Solutions

Kubectl

  • New: Port forward / Tunnel to an internal service.

    If you have a service running in kubernetes and you want to directly access it instead of going through the usual path, you can use kubectl port-forward.

    kubectl port-forward allows using resource name, such as a pod name, service replica set or deployment, to select the matching resource to port forward to. For example, the next commands are equivalent:

    kubectl port-forward mongo-75f59d57f4-4nd6q 28015:27017
    kubectl port-forward deployment/mongo 28015:27017
    kubectl port-forward replicaset/mongo-75f59d57f4 28015:27017
    kubectl port-forward service/mongo 28015:27017
    

    The output is similar to this:

    Forwarding from 127.0.0.1:28015 -> 27017
    Forwarding from [::1]:28015 -> 27017
    

    If you don't need a specific local port, you can let kubectl choose and allocate the local port and thus relieve you from having to manage local port conflicts, with the slightly simpler syntax:

    $: kubectl port-forward deployment/mongo :27017
    
    Forwarding from 127.0.0.1:63753 -> 27017
    Forwarding from [::1]:63753 -> 27017
    
  • New: Run a command against a specific context.

    If you have multiple contexts and you want to be able to run commands against a context that you have access to but is not your active context you can use the --context global option for all kubectl commands:

    kubectl get pods --context <context_B>
    

    To get a list of available contexts use kubectl config get-contexts

Monitoring

Prometheus