| Instead of | Do this | 
| git checkout {file} | git restore {file} | 
| git checkout {branch} | git switch {branch} | 
| git checkout -b {newbranch} | git switch -c {newbranch} | 
| git push --force | git push --force-with-lease | 
| git rebase | git rebase --update-refs | 
How to make Firefox auto-suggest “shortcut URLs”
I just switched back to Firefox (ublock origin forever!) and it massively down-rates URLs that redirect in its “what to auto-suggest” algorithm. You can tweak this, though, by putting `about: config` into the URL bar and setting `places.frecency.permRedirectVisitBonus` and `places.frecency.redirectSourceVisitBonus` to 2000 over their initial low values.
firefox-source-docs.mozilla.org/browser/urlbar/ranking.html has all the gory details and more.
pipenv no longer chokes on `python_requires=”==3.*”`
As of version 2024.04 this bug that I investigated earlier in the year has been fixed. Hurray!
Nord Light Theme
I like my terminal to change theme when I ssh into another machine. I have been using Solarized Light and Solarized Dark to facilitate this for some time. But now I have gotten hooked on Nord which has no light version. So I made one, just for Terminal for now at least.

There are notes about a “bright ambiance” version of the theme in the Nord docs but no actual themes available. The official Terminal.app theme actually deviates from the core 16-color palette but I didn’t do that here. I just chose the most appropriate color from the 16 color palette for each option in a Terminal theme.
Avoid trailing comma when emitting lists in Mustache templates
In pure Mustache there’s no (simple) way to avoid a trailing comma on a list:
{{#items}}{{.}}, {{/items}}
Which produces something like: “First, Second, Third, “
There are various ways around this. If you’re working in HTML I think the nicest is to model your list as an actual HTML list and use CSS:
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8"/>
    <title>Lists</title>
    <style>
      .list {           
        margin: 0;      
        padding: 0;
      }
      .list li {           
        display: inline;             
      }
      .list li:not(:last-child):after {
        content: ", ";               
      }
    </style>
  </head>
  <body>
    <ol class="list">
      <li>First</li>
      <li>Second</li>
      <li>Third</li>
    </ol>
  </body>
</html>
Which produces: “First, Second, Third”.
Python types for a decorator that is agnostic about the wrapped func’s return type
import random
from typing import Callable, ParamSpec, TypeVar
T = TypeVar("T")
P = ParamSpec("P")
def some_global_condition() -> bool:
return random.choice([True, False])
def my_return_type_preserving_decorator(val: T) -> Callable[[Callable[P, T]], Callable[P, T]]:
def decorator(func: Callable[P, T]) -> Callable[P, T]:
def wrapper(*args: P.args, **kwargs: P.kwargs) -> T:
if some_global_condition():
return val
return func(*args, **kwargs)
return wrapper
return decorator
@my_return_type_preserving_decorator((True, True, True))
def my_func(n: int) -> tuple[bool, bool, bool]:
return n > 10, n > 100, n > 1000
x = my_func(5)
for r in x:
print("yes" if r else "no")
Format Percentage for Display in JavaScript
| Input | Output | 
|---|---|
| 0.12 | 12% | 
| 0.0012345 | 0.12% | 
| 0.0 | 0 | 
2 significant digits but locale-sensitive, strip meaningless trailing zeroes and decimal dividers.
const formatPercentage = function (value) {
    return new Intl.NumberFormat(
        undefined, // use browser locale
        // Choose the options that get us closest to our desired style – a percentage that tells you enough to be useful but no unnecessary decimal places or trailing zeroes.
        {
            style: "percent", // Treat decimals as percentages, multiplying by 100 and adding a % sign.
            minimumSignificantDigits: 2,
            maximumSignificantDigits: 2
        }
    )
        .format(value)
        // \D here is the locale-agnostic decimals separator.
        .replace(/(\D[0-9]*?)0+%$/, "$1%") // Get rid of trailing zeroes after the decimal separator.
        .replace(/\D%/, "%") // Clean up the scenario where we got rid of everything after the decimal separator and now have something like "4.%.
        .replace(/^0%$/, "0"); // Replace literal "0%" with "0" as zero is unitless.
};
	Force pipenv to use Charles Proxy
- Export charles-ssl-proxying-certificate.pem from Charles Proxy
- REQUESTS_CA_BUNDLE=/path/to/charles-ssl-proxying-certificate.pem pipenv {cmd} {args}
Python Types for Union of String Literal and Other Type
I wanted to add a single special value to another type for a union type, something like this:
from typing import Literal
                                                                            
UNMEASURED = "unmeasured"                                                    
Unmeasured = Literal[UNMEASURED]
Resource = int | Unmeasured
notes: dict[Resource, str] = {1: "foo", 2: "bar", 3: "baz", UNMEASURED: "quux"}
resources = [1, 2, 3] + [UNMEASURED]
for resource in resources:
    print(notes[resource])
This works, but mypy complains: ‘error: Parameter 1 of Literal[…] is invalid [valid-type]’. So to make this pass mypy typechecking you have to repeat “unmeasured” as a literal. Even if you do that:
from typing import Literal
UNMEASURED = "unmeasured"
Unmeasured = Literal["unmeasured"]
Resource = int | Unmeasured
notes: dict[Resource, str] = {1: "foo", 2: "bar", 3: "baz", UNMEASURED: "quux"}
resources = [1, 2, 3] + [UNMEASURED]
for resource in resources:
    print(notes[resource])
mypy will complain: ‘Dict entry 3 has incompatible type “str”: “str”; expected “int | Literal[‘unmeasured’]”: “str” [dict-item]’ and also ‘Invalid index type “str | int” for “dict[int | Literal[‘unmeasured’], str]”; expected type “int | Literal[‘unmeasured’]” [index]’
Eventually I came up with:
from enum import Enum
from typing import Literal
class Aspect(Enum):
    UNMEASURED = "unmeasured"
Resource = int | Aspect
notes: dict[Resource, str] = {1: "foo", 2: "bar", 3: "baz", Aspect.UNMEASURED: "quux"}
resources = [1, 2, 3] + [Aspect.UNMEASURED]
for resource in resources:
    print(notes[resource])
which both produces the same result, and passes the typechecker. I don’t love the weird Enum hanging out on its own but it does at least put the magic string in one place only and pass. I wonder what would be better.
Automatially Activate venv When Found in Directory
# Function to activate virtualenv if present                                    
auto_activate_virtualenv() {                                                    
  if [ -f "bin/activate" ]; then                                                
    source bin/activate                                                         
  fi                                                                            
}                                                                               
# Add the function to chpwd hooks                                               
add-zsh-hook chpwd auto_activate_virtualenv
	