George V. Reilly

Negative Circled Digits

I found something very useful in the dingbats range of Unicode characters: the negative circled san-serif digits, ➊ ➋ ➌ ➍ ➎ ➏ ➐ ➑ ➒ ➓ .

I've started using them to label points of interest in code. They play well with the code-block directive in re­Struc­tured­Text.

sudo docker images --format '{{.Repository}}:{{.Tag}}' \ | grep $IMAGE_NAME \ 
continue.

JSON data from Docker Images

I was trying to get some structured in­for­ma­tion from docker images, hoping to replace some ugly Sed and AWK trickery. I could have used the docker-py library. Instead I chose to use the poorly documented --format option to docker images (and some other Docker CLI commands). Adrian Mouat gives some useful starting points at Docker Inspect Template Magic and notes that formatting is built around Go templates.

I quickly figured out that this format would meet my immediate need.

sudo docker images --format '{{.Repository}}:{{.Tag}}' \
    | grep $IMAGE_NAME \
    | grep -v latest \
    | head -1

That's fine, but continue.

Bash: echo success of previous command

C-like languages have a ternary operator, cond ? true_re­sult : false_re­sult. Python has true_re­sult if cond else false_re­sult. Bash doesn't have a ternary operator, but there are various workarounds.

I wanted to print succeeded or failed based on the exit code of the previous command in a shell script. In Unix, all programs exit with an integer status code. Successful programs exit with 0; all other values, positive or negative, indicate failure. In Bash, the status code of the previous program is held in $?.

some/command or-other fer example

STATUS="$([ "$?" == 0 ] && echo 'succeeded' || echo 'failed')"
echo "Results: $STATUS"

There are other ways to handle this.

Git File Modes

Ever wonder what the six-digit file modes are in a Git commit? The mysterious 100644 and 100755 modes?

diff --git a/foo/bar.py b/foo/bar.py
old mode 100644
new mode 100755
index b829edea4..ee6bda024
--- a/foo/bar.py
+++ b/foo/bar.py
@@ -1,3 +1,4 @@
...

I had made foo/bar.py executable by using chmod +x and adding a #!/usr/bin/env python shebang. The last three digits are obviously the same octal digits that you can use with chmod. But what's that 100 prefix?

The ex­pla­na­tion can be found in a Stack­Over­flow answer:

100644₈  regular file (non-executable)  S_IFREG | S_IRUSR | S_IWUSR
                     
continue.

Computed Parallel Branches in Jenkins Pipeline

I've been using Jenkins lately, setting up Pipeline builds. I have mixed feelings about that, but I'm quite liking Groovy.

Here's an example of a Closure called ac­cep­tance_in­te­gra­tion_tests, where the re­lease_lev­el parameter is a String which must be either "dev" or "prod".

def acceptance_integration_tests = { String release_level ->
    assert release_level =~ /^(dev|prod)$/
    String arg = "--${release_level}"

    def branches = [
        "${release_level}_acceptance_tests": {
            run_tests("ci_acceptance_test", arg, '**/*nosetests.xml')
        },
       
continue.

Diff a Transformed File

I wanted to diff two files. One of them needed some seds on each line and sorting. I wanted to do that on the fly, without leaving a massaged in­ter­me­di­ate file lying around.

colordiff --unified <(cat orphaned_permalinks.txt
                        | sed 's@http://www.georgevreilly.com/@@'
                        | sed 's/.aspx$/.html/'
                
continue.

LKRhash: Scalable Hash Tables

LKRhash is a hashtable that scales to multiple processors and to millions of items. LKRhash was invented at Microsoft in 1997 by Per-Åke (Paul) Larson of Microsoft Research and Murali Krishnan and George Reilly of Internet In­for­ma­tion Services. LKRhash has been used in many Microsoft products. The techniques that give LKRhash its per­for­mance include linear hashing, cache-friendly data structures, and fine-grained locking.

If Microsoft had had 20% time, LKRhash would have been my main 20% project. I put a lot of continue.

Git: pruning unused branches

Ever had a Git repository where there's an over­whelm­ing number of branches, most of which are surely abandoned? You run git branch --remote and you see dozens of unfamiliar branches. Where to begin?

Here's an example for fly­ing­cloud:

$ git for-each-ref --sort=-committerdate \
    --format='%(committerdate:short) %(refname)' refs/heads refs/remotes
2016-12-29 refs/remotes/origin/master
2016-12-29 refs/remotes/origin/HEAD
2016-12-29 refs/heads/master
2016-12-11 
continue.

Markdown Live Preview

It's very useful when creating Markdown to be able to preview it live. For example, creating a complex pull request or a README.md. I usually use the built-in Atom Markdown Preview package in Atom. Just type ⌃⇧M (aka Ctrl+Shift+M) to see a live preview in an adjacent pane. I use vim-mode-plus to edit in Atom, which provides an acceptable emulation of Vim.

I recently discovered VS Code Markdown Preview in Visual Studio Code. Type ⌘K V (aka Ctrl+K V on Windows or Linux) to invoke the side-by-side live preview. I use VSCodeVim to meet my Vim needs.

Un­for­tu­nate­ly, neither previewer gives identical results to GitHub's Markdown renderer. GitHub itself seems to use different continue.

Alembic: Data Migrations

We use Alembic to perform schema migrations whenever we add (or drop) tables or columns from our databases. It's less well known that Alembic can also perform data migrations, updating existing data in tables.

Here's an example adapted from a migration I put together this afternoon. I added a non-NULL Boolean stooge column to the old_timers table, with a default value of FALSE. I wanted to update certain rows to have stooge=TRUE as part of the migration. The following works with PostgreSQL.

Note the server_de­fault=sa.false() in the de­c­la­ra­tion of the stooge column, which is needed to initially set all instances of stooge=FALSE. I then declare a table which has only the two continue.

Previous » « Next