Proactively Lazy

Get more done by embracing laziness. Motivation—the fickle beast—doesn’t make you productive, your interaction with your environment does. Use your motivation to improve the environment by reducing—or increasing—the energy needed do things. Clean the dishes as soon as you finish your meal, display your guitar on a stand instead of in the closet, log out of all social media accounts when you finish scrolling. James Clear, in his book Atomic Habits, quoted Oswald Nuckols who said, “People think I work hard but I’m actually really lazy. I’m just proactively lazy.” Use your motivation energy to reduce the “activation energy” of productivity by improving your environment. Embrace your laziness—proactively.


Simple patterns to do useful things with the Ex Mode :global command in Vim

Delete all lines that match a pattern (:help pattern)

:g/pattern/d
:global/pattern/delete

Delete all lines that don’t match a pattern (:help :vglobal)

:g!/pattern/d
:v/pattern/d
:vglobal/pattern/delete

Remove all trailing whitespace with :substitute

:%s/\s\+$//e

Find more info by running :help :global and :help :substitute


SQL Simple Pivot Pattern

TIL a simple pattern for pivoting data with an SQL query using FILTER (or a CASE WHEN clause if the FILTER keyword isn’t supported by the database). The example input data is from the Chinook Database and is modeled like this:

|     InvoiceDate     | BillingCountry | Total |
|---------------------|----------------|-------|
| 2011-02-15 00:00:00 | Belgium        | 1.98  |
| 2009-10-17 00:00:00 | Brazil         | 13.86 |
| 2009-03-05 00:00:00 | USA            | 3.96  |
| 2012-09-05 00:00:00 | Czech Republic | 16.86 |
| 2012-10-27 00:00:00 | India          | 1.98  |
| 2012-08-27 00:00:00 | Hungary        | 3.96  |
| 2011-07-25 00:00:00 | Canada         | 8.91  |
| 2012-06-25 00:00:00 | USA            | 1.98  |
| 2012-02-22 00:00:00 | Poland         | 1.98  |
| 2013-03-05 00:00:00 | Italy          | 8.91  |

A pivot query using FILTER (with a SQLite db)

Read More ...

TIL you can use environment variables in your docker compose files. I use environment variables with my homelab setup as a simple way to keep secrets out of version control and reduce repeating common file paths for binded volumes. Docker compose even uses a .env file out of the box. Most shell interpolation features work too.

services:
  web:
    image: nginx
    ports:
      - "8080:${NGINX_PORT:-80}"
    environment:
      - NGINX_HOST=${NGINX_HOST:?error}
      - NGINX_PORT=${NGINX_PORT:-80}

Reference: Environment Variables in Compose


TIL you can save multiple nvim config setups with the $NVIM_APPNAME environment variable. Nvim will look for, or create, the config in the $XDG_CONFIG_HOME/$NVIM_APPNAME directory. Easily switch between the configs with aliases in the startup file for your shell (i.e. .bashrc, .zshrc, etc.).

# $NVIM_APPNAME defaults to nvim
alias avim='NVIM_APPNAME="nvim-astro" nvim'
alias zvim='NVIM_APPNAME="nvim-lazy" nvim'
alias kvim='NVIM_APPNAME="nvim-kickstart" nvim'

Source: $NVIM_APPNAME docs



Python Http Server

Python comes with a simple http server you can use to serve a directory of files as a web server. I use it all the time to serve code coverage reports and various other static web projects that don’t come with a server. Start the server using this command:

$ python -m http.server 
Serving HTTP on :: port 8000 (http://[::]:8000/) ... 

Once this is running you can now access the files in the directory with your web browser (at http://localhost:8000).

Read More ...

Python Context Manager

TIL you can write your own functions to use in a python with statement. These functions are called context managers. A context manager is a simple way to wrap a try/except/finally block in a reusable function.

Writing your own context manager is simple.

from contextlib import contextmanager

@contextmanager
def random_number():
    """ https://xkcd.com/221/ """
    try:
        # do any setup
        yield 4 # Yielded objects can be accessed from the `var` after the `as` keyword
    except:
        # handle any errors
        pass
    finally:
        # Clean up here (e.g. close db connections or open files)
        pass

## Call the context manager like this
with random_number() as n:
    # do something with `n`
    print(n)

PEP-343 has some other context manager examples.


Podman Pods

TIL the POD in Podman is the same idea as Pods in Kubernetes. Pods are collections of containers that share resources (i.e. volumes, networks, etc.). Create a pod with the podman pod create --name [pod-name] command. You will need to expose ports and setup other resources with the pod create command.

podman pod create --name my-new-pod --infra --publish 8080:80 --network bridge 

Add a container to a pod with the --pod [pod-name] flag in your run command:

Read More ...

Special tld '.home.arpa'

TIL about the special-use *.home.arpa tld. The .home.arpa tld is reserved for “non-unique residential home networks”. If you use Adguard Home, Pi-Hole, or any other DNS server you can freely use .home.arpa for all your local assets (printers, self-hosted server, etc.)!