Go: Image-Processing Microservice

Imaginary is a fun little package that can be Dockerized and deployed next to the rest of your services to offload image-processing:

Fast HTTP microservice written in Go for high-level image processing backed by bimg and libvips. imaginary can be used as private or public HTTP service for massive image processing with first-class support for Docker & Heroku. It’s almost dependency-free and only uses net/http native package without additional abstractions for better performance.

Advertisements

Go: Functions that satisfy interfaces

I ran across this while sifting through the http package:

// The HandlerFunc type is an adapter to allow the use of
// ordinary functions as HTTP handlers. If f is a function
// with the appropriate signature, HandlerFunc(f) is a
// Handler that calls f.
type HandlerFunc func(ResponseWriter, *Request)

// ServeHTTP calls f(w, r).
func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request) {
    f(w, r)
}

A function that satisfies http.HandlerFunc will also automatically get a function-on-a-function (who knew) that makes it look like a struct that satisfies the http.Handler interface as well.

I had looked for a footnote about this in the section on function declarations in the Go language specification but was not successful. The receiver is little more than just a repositioned first-argument. There’s nothing that says “MUST be a struct or a func”.

Add Timestamps to Your Bash History

Put this in your profile script (.bashrc):

export HISTTIMEFORMAT="%d/%m/%y %T "

I also find it helpful to increase the size of my command-line history:

export HISTFILESIZE=10000
export HISTSIZE=10000

The first controls how many are stored. The second controls how many are kept in memory. You can also tweak it to hold an actual unlimited number if you so wish.

Git: Get the Latest Commits in a Repository

Sometimes you have a large repository (many commits, many branches) and it either might be an old clone or you might be operating out of a secondary branch and not aware of which branch has the majority of activity. You can do a git-log that displays all commits without respect to the current branch and order them by parents before children, in descending commit-timestamp (as opposed to author-timestamp) order.

This prints the top five:

$ git log -5 --all --date-order

Browse Your Image Library With a Webpage

Easily install via PyPI and start a documentation server with RemoteImageBrowser. It also supports more robust/scalable installs (via uWSGI).

You can also reuse and augment the local Gnome thumbnail cache. This means that if you have a want to serve a large image library, you can precache all of your thumbnails from a scheduled process and benefit from them both when browsing/manipulating them directly as well as when you browse them from the website. A basic PIL-based thumbnailer is used by default.

Installing:

$ git clone https://github.com/dsoprea/RemoteImageBrowser.git
$ cd RemoteImageBrowser
$ sudo pip install -r requirements.txt
$ rib/resources/scripts/development \
    --env IMAGE_ROOT_PATH=~/Downloads \
    --env THUMBNAIL_ROOT_PATH=/tmp/thumbnails

Screenshots:

Browsing

Lightbox

Cog: Evaluate Arbitrary Python Fragments in a Template Document

You provide any text document with embedded fragments of Python, and Cog renders it:

using namespace std;

#include <iostream>

int main() {
    cout << "Hello Earth." << endl;


/*[[[cog

print("""\
cout << "Hello Mars." << endl;
printf("Goodbye Pluto.\\n");
""")

]]]*/

//[[[end]]]


  return 0;
}

Notice that newlines have to be escaped.

Translate:

$ cog.py hello_world.cpp.cog > hello_world.cpp

Output:

using namespace std;

#include <iostream>

int main() {
    cout << "Hello Earth." << endl;


/*[[[cog

print("""\
cout << "Hello Mars." << endl;
printf("Goodbye Pluto.\\n");
""")

]]]*/
cout << "Hello Mars." << endl;
printf("Goodbye Pluto.\n");

//[[[end]]]


  return 0;
}

General build and run:

$ g++ -o hello_world hello_world.cpp
$ ./hello_world
Hello Earth.
Hello Mars.
Goodbye Pluto.