Today I Learned

Exclude Directories using the Find command

January 18, 2024

TIL you can exclude directories when using the find command.

## Exclude the node modules directory when searching with find
find ./ -name "*.css" ! -path "*/node_modules/*"

Source: Stack Overflow


Python Http Server

January 18, 2024

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

Bonus: To use a different port

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

Double Bonus: On older systems, or systems without python3 installed, python2 uses a different command

$ python2 -m SimpleHTTPServer
Serving HTTP on 0.0.0.0 port 8000 ...
$ python2 -m SimpleHTTPServer 9000
Serving HTTP on 0.0.0.0 port 9000 ...


Python Context Manager

September 21, 2023

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

May 20, 2023

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:

podman run -d --pod my-new-pod --name my-nginx docker.io/library/nginx

Load 127.0.0.1:8080 in a browser and see the default nginx page from your pod!


Special tld '.home.arpa'

January 03, 2023

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.)!


Slash or Not to Slash with rsync

November 26, 2022

TIL an easy way to remember if you want a slash or not when you run rsync. A trailing slash will copy the contents of the directory while just the directory name will copy the directory itself. For example

# copies everything in dir into new-dir
rsync -r dir/ new-dir

# copies dir into new-dir
rsync -r dir new-dir


Python List Comprehensions

November 25, 2022

TIL about Python List Comprehensions. I came across code that looked like this in a codebase.

 baz = [foo.bar for foo in foos] 

I could see what was happening (baz is now a list of bar for each object in foos). I didn’t know this was called list comprehension but now I understand the data structure.

List comprehensions are useful for lots of things when you want to create lists from other objects (docs source).


Hugo Debug Jsonify

November 13, 2022

TIL that you can output all the data in “the dot” from a Hugo template using jsonify

<pre>{{ . | jsonify (dict "indent" "  ") }}</pre>

Here is the output of the .Params value for this post

<pre>{{ .Params | jsonify (dict "indent" "  ") }}</pre>
{
  "date": "2022-11-13T16:54:29-05:00",
  "draft": false,
  "iscjklanguage": false,
  "lastmod": "2022-11-13T16:54:29-05:00",
  "publishdate": "2022-11-13T16:54:29-05:00",
  "tags": [
    "hugo",
    "ssg",
    "debug"
  ],
  "title": "Hugo Debug Jsonify"
}


cal and ncal

November 08, 2022

TIL about ncal (most Linux distros) or CAL (on MacOS). I have used cal for quite a while which is a convenient calendar tool in the terminal. However, I hit CAPS LOCK on accident and ran CAL instead of cal, to my surprise it output

    November 2022
Mo     7 14 21 28
Tu  1  8 15 22 29
We  2  9 16 23 30
Th  3 10 17 24
Fr  4 11 18 25
Sa  5 12 19 26
Su  6 13 20 27

(the normal cal output)

   November 2022
Su Mo Tu We Th Fr Sa
       1  2  3  4  5
 6  7  8  9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30