Produce small executables with Common Lisp

:: lisp

You need roswell, ECL and UPX installed.

$ brew install --HEAD roswell
$ brew install ecl upx

The reason to install the head roswell is that, as of this writing, some of the Common Lisp installations did not work on the latest release on Homebrew.

The reason to install ecl on the system, and not using ros install, is that I had problems installing the latest version of ECL that way. Also, the ros list versions command did not find any available versions of ECL. So, I resorted to using the system installed ECL.

$ ros use ecl/system

Logging start and finish in Python

:: python

Just a quick post on how to log, in simple Python scripts, the start and finish times of a function.

import logging
import time
import sys


def main():
    start = time.time()
    logging.basicConfig(
        level=logging.DEBUG,
        format='%(asctime)s %(levelname)s - %(message)s',
        datefmt='%H:%M:%S',
    )
    logging.info("Start processing after %s", format_time_since(start))
    # Do some processing ...
    logging.info("Finished processing after %s", format_time_since(start))
    sys.exit(0)


def format_time_since(start):
    now = time.time()
    elapsed = now - start
    return time.strftime("%H:%M:%S", time.gmtime(elapsed))

Debug Scala with Spacemacs

:: scala, spacemacs

The documentation about debugging in ENSIME was a bit confusing to me. Here are my notes on how to start debugging an (sbt-based) Scala project from Emacs (Spacemacs):

  1. Launch the debugger from the sbt console first. Use the sbt-mode shell. I don’t really know if this works launching SBT on a terminal not running under Emacs. Use ensimeTestOnlyDebug if you are running a test, otherwise use ensimeRunDebug for an implementation class.

    > ensimeTestOnlyDebug com.company.division.squad.project.package.TestClass
    ...
    Listening for transport dt_socket at address: 5005
  2. Set a breakpoint in a source code line by moving your cursor to that line and using , d b

  3. Attach to the debugger by using , d A. The first time, you will need to specify Host (localhost) and Port (the number in the Listening for transport ... line above)

  4. When (if) the debugger hits the line where your breakpoint is set, the Emacs cursor will jump to that position, and you can start using other debugging commands (, d n, , d i, etc …)

Git setup with fork and upstream

:: git

Here is how to setup a fresh clone of a git repository so that it pulls changes from an upstream, but only pushes by default to another repository (usually, your own fork of the project).

I use the fish shell. If you use Bash or Zsh it should be straightforward to define these variables in your environment.

set gituser logc
set gitproject chat
set gitorg NacionLumpen

And this is how to setup the clone:

git clone git@github.com:$gituser/$gitproject.git
cd $gitproject
git remote add upstream git@github.com:$gitorg/$gitproject.git
git remote rename origin fork
git fetch upstream
git branch --set-upstream-to=upstream/master
git config remote.pushDefault fork

As always with git, it is trivial, if your name is ‘Linus’ :)

Minimal Racket web app

:: racket, web

This is the simplest module that responds to HTTP requests.

#lang racket/base

(require
 (only-in web-server/http
          response/full)
 (only-in web-server/http/request-structs
          make-header)
 (only-in web-server/servlet-env
          serve/servlet))

;; hello: request? -> response?
(define (hello req)
  ;; struct response/full
  (response/full
   200                          ;; code
   (string->bytes/utf-8 "OK")   ;; message
   (current-seconds)            ;; timestamp in s
   #f                           ;; mime or #f
   (list (make-header           ;; list of headers
          (string->bytes/utf-8 "Server")
          (string->bytes/utf-8 "Racket")))
   (list                        ;; body: list of bytes
     (string->bytes/utf-8 "Hello world!\n"))))

(module+ main
  (serve/servlet
   hello
   #:port 8080
   #:servlet-regexp #rx""))

Save it as hello.rkt and run it with $ racket hello.rkt

Check that it works:

$ curl -v http://localhost:8080/
*   Trying ::1...
* TCP_NODELAY set
* Connection failed
* connect to ::1 port 8080 failed: Connection refused
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET / HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Tue, 19 Dec 2017 18:13:14 GMT
< Last-Modified: Tue, 19 Dec 2017 18:13:14 GMT
< Content-Length: 13
< Server: Racket
<
Hello world!
* Connection #0 to host localhost left intact