Taco Steemers

A personal blog.

Code Notes and Snippets

Notes and code snippets for quick reference.

Things I had to find out at some point. Hopefully I can save people some time by making them accessible here. This is not really an article or project, but I hope that people can find the solution they are looking for here when they are searching online.

PlantUML notes have a separate page.

On this page:

Shell commands, environment specific

find

Recursively finding and removing all files based on name

find . -type f -name ".DS_Store" -delete

Finding only in the current directory, or any number of directories deep

find . -maxdepth 1 -type f -name ".DS_Store"

Specifying a list of filenames

find . \( -name "*.jpg" -o -name "*.JPG" -o -name "*.jpeg" -o -name "*.JPEG" \)

LFTP

Let LFTP remove files from the target that are not present in the source

--delete

Let LFTP remove the source files after the command is finished

--Remove-source-dirs --Remove-source-files

Example LFTP command to mirror from local source to remote target

lftp sftp://$(FTP_USER)@$(FTP_HOST) -e "mirror --verbose=3 --delete -R $(OUTPUTDIR) $(FTP_TARGET_DIR) ; quit" This call will pause to ask for the password.

Bash

Script name, directory, present working directory

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#!/bin/bash

echo "Script name:"
echo "$BASH_SOURCE"
echo "$0"

echo "Directory that contains the currently running script, relative to PWD:"
echo "$(dirname $BASH_SOURCE)"
echo "$(dirname "$0")"
echo "${0%/*}"

echo "Present working directory: $PWD"

Includes the ./ when we call the script like ./my_script , but not when calling as sh my_script .

See this stackoverflow post . See also this stackoverflow post .

Make

Get the present working directory from inside a make script

echo "Current working directory of the make process: $(shell pwd)" . if pwd is present. shell calls out to the shell.

Call another target from inside a target

Sometimes we can't call another target from a prerequisite because that would result in the wrong order of operations.

One way to call another target from inside make is make other_target . This will spawn another make process.

Perhaps in your case spawning another make process adds too much overhead. In that case you might want to create a higher-level target that contains the original target as a prerequisite. This will not work if make runs in parallel mode because then the order of operations is not guaranteed. This StackOverflow answer shows how it can be done without creating a new make instance and still guaranteeing the order of operations. If we turn the other target into a variable instead of a target, we can use the call method to execute it:

OTHER_TARGET = echo "Executing other_target"

initial_target:
    echo "Executing initial_target"
    $(call OTHER_TARGET)

AWK

awk - pattern-directed scanning and processing language

Example script to make a <ul> of files found by find

1
2
3
4
5
6
7
#!/bin/bash

find . \( -name "*.md" -o -name "*.markdown" \) | awk -v pwd=$PWD '
    BEGIN{print "<ul>"}
    {printf("<li><a href=\"file://%s%s\">%s</a>",pwd,substr($1,2,length($1)),$1)}
    END{print "</ul>"}
' >> out.html;

Languages and formats

Python3

Style guide

The official style guide can be found here .

How to combine incoming byte chunks

I asked about this on StackOverflow , and then I had to look up the answer again on another occasion. So I'll list it here :). To safe you some clicks, there are two options for the byte storage:

The code might look as follows:

def process_incoming_connection(conn, addr):
    byte_array = bytearray()
    while 1:
        chunk = conn.recv(1024)
        if not chunk:
            break
        byte_array.extend(chunk)
    message = byte_array.decode(encoding='utf-8');
    conn.close()
    # Now we can do something with the message

How to manually update or patch an official Python package

Sometimes we might need to do a bugfix on a Python package, or we need to use the latest version from the repository that is not yet packaged for our platform. How can we install this new version? This is very simple, we only need to copy the source files.

For example, if we want to update our system version of a package 'anyplugin' that lives in the 'pelican plugins' namespace, we simply overwrite the system version with our source files.

cp -r anyplugin /usr/local/lib/python3.8/site-packages/pelican/plugins/

The directory we copy is the actual python source file directory, this is not necessarily the directory root.

Operating system specific

MacOS

Show hidden files in Finder

  1. Execute defaults write com.apple.finder AppleShowAllFiles YES
  2. Open the 'Force Quit' menu from the 'Apple' menu, or with the 'meta+option+escape' key combination (also known as 'command+option+escape').
  3. Locate 'Finder' in the list, click on it to select it, and then click the 'Relaunch' button.

Now hidden files will be shown. I assume the same procedure with NO instead of YES will undo it.