Getting Emacs for Mac OS X to honour the shell environment

If your are using the excellent Emacs for Mac OS X distribution, you may have noticed that it does not use the environment you may have defined in your ~/.bashrc. This can be a problem if you are using MacPorts, for example to install clojure and Leiningen. You will get an error message like this, when trying to run inferior-lisp in clojure-mode:

apply: Searching for program: No such file or directory, lein

This can be avoided by using the excellent exec-path-from-shell package for Emacs. It is available via (M)ELPA, just install it using list-packages, or download it from github. You can enable it, especially for OS X, by putting this in your init.el:
(when (memq window-system '(mac ns))
(exec-path-from-shell-initialize))

Working around connection problems with Emacs Tramp

From time to time I have to edit files on a SunOS 5.10 server. I use Emacs with tramp for this. However, after some time I get this error message from tramp:

File error: Couldn’t find exit status of `test -e …

It seems that the ssh connection goes bad for some reason. After this you won’t be able to save the file anymore. You can work around this by running M-x tramp-cleanup-all-connections and then saving again.

There are approximately N/ln(N) primes between N and 2N

Just saw this very nice video by @numberphile, and thought I whip up a small Python program to demonstrate the prime number theorem:


#!/usr/bin/env python
#
# "Chebyshev said it, and I say it again: There's always a prime between n and 2n."
#

import sys
import math

class PrimeFinder:

def __init__( self, n ):
self.n = n

def isNPrime( self, N ):
for x in range( 2, int( math.sqrt( N ) ) + 1 ):
if N % x == 0:
return False
return True

def computeAllPrimesBetweenNAndTwoN( self ):
result = []
for N in range( self.n, 2 * self.n + 1 ):
if self.isNPrime( N ):
result = result + [ N ]
return result

def main():
if len( sys.argv ) != 2:
print "Prints all prime numbers between N and 2N"
print "Usage: %s N" % sys.argv[ 0 ]
print "Where N is some positive, natural number."
sys.exit( 0 )

N = int( sys.argv[ 1 ] )
primeFinder = PrimeFinder( N )
allPrimes = primeFinder.computeAllPrimesBetweenNAndTwoN()
print "There are %u primes between %u and %u: %s" % (
len( allPrimes ), N, 2 * N, str( allPrimes )[ 1 : -1 ]
)

if __name__ == "__main__":
main()

And it seems to work, but check WolframAlpha if you don’t trust me 🙂


$ ./myprimes.py 100000
There are 8392 primes between 100000 and 200000: 100003, 100019, 100043 ...

How to use SciPy Least Squares to minimize multiple functions at once

SciPy comes with a least squares Levenberg-Marquardt implementation. This allows you to minimize functions. By defining your function as the difference between some measurements and your model function, you can fit a model to those measurements.

Sometimes your model contains multiple functions. You can also minimize for all functions using this approach:

  • Define your functions that you like to minimize A(p0), B(P1), …
    their cumulative paramaters will be a tuple (p0, p1, …).
  • Define your function to be minimized as f(x0), where x0 is expanded to the parameter tuple.
  • The function f returns a vector of differences between discrete measured sample and the individual functions A, B etc.
  • Let SciPy minimize this function, starting with a reasonably selected initial parameter vector.

This is an example implementation:


import math
import scipy.optimize

measured = {
1: [ 0, 0.02735, 0.47265 ],
6: [ 0.0041, 0.09335, 0.40255 ],
10: [ 0.0133, 0.14555, 0.34115 ],
20: [ 0.0361, 0.205, 0.2589 ],
30: [ 0.06345, 0.23425, 0.20225 ],
60: [ 0.132, 0.25395, 0.114 ],
90: [ 0.2046, 0.23445, 0.06095 ],
120: [ 0.2429, 0.20815, 0.04895 ],
180: [ 0.31755, 0.1618, 0.02065 ],
240: [ 0.3648, 0.121, 0.0142 ],
315: [ 0.3992, 0.0989, 0.00195 ]
}

def A( x, a, k ):
return a * math.exp( -x * k )

def B( x, a, k, l ):
return k * a / ( l - k ) * ( math.exp( -k * x ) - math.exp( -l * x ) )

def C( x, a, k, l ):
return a * ( 1 - l / ( l - k ) * math.exp( -x * k ) + k / ( l - k ) * math.exp( -x * l ) )

def f( x0 ):
a, k, l = x0
error = []
for x in measured:
error += [ C( x, a, k, l ) - measured[ x ][ 0 ],
B( x, a, k, l ) - measured[ x ][ 1 ],
A( x, a, k ) - measured[ x ][ 2 ]
]
return error

def main():
x0 = ( 0.46, 0.01, 0.001 ) # initial parameters for a, k and l
x, cov, infodict, mesg, ier = scipy.optimize.leastsq( f, x0, full_output = True, epsfcn = 1.0e-2 )
print x

if __name__ == "__main__":
main()

SciPy returns a lot more information, not only the final parameters. See their documentation for details. You also may want to tweak epsfcn for a better fit. This depends on your functions shape and properties.

Playing with REST: SilverShield USB power web frontend

Yesterday I started to code a small tool that lets me turn on and off power outlets on my USB controlled  power strip. You can find the humble beginnings over at github. It uses the sispmctl tool to turn on and off the sockets. It is implemented in Python using the Tornado web server. The rendering is simple, not styled and not yet HTML compliant. But it already scans for attached devices, and lets you toggle single outlets. However, today the power strip sort of died on me, and now I have to look for another project to practice REST with…

How to convert a Python list into a string in a strange way

Given a list in Python, suppose you wanted a string representation of that list. Easy enough:


str( [ 1, 2, 3 ] )

However, suppose you did not want the standard notation, but rather apply some function to each string element or simply do away with the brackets and commas, you can use list comprehension, the join function and an empty string literal:


''.join( str( i ) for i in [ 1, 2, 3 ] )

I already knew list comprehension, but using it in this scenario and with a string literal as an object was new to me. Anyway, using such code is probably a bad idea, since it might be hard to read! At the very least, one should stow it away in a function with a fitting name, such as jointStringRepresentationOfListItems( list ). But really, I am not even sure what I would use that for…

Update: even better is this:


','.join( map( str, [ 1, 2, 3 ] ) )

Improved handling of background GNU Global update

Earlier this year I detailed how to run gtags of GNU Global when Emacs is idle. However, this collides with some special Emacs modes, like ediff. That mode makes its own Emacs frame layout, which can be destroyed by the xgtags incremental update buffer. So I conceived a way to disable gtags temporarily as long as ediff is active:


;; This stuff is only needed, if you haven't got xgtags loaded yet
(autoload 'gtags-mode "gtags" "" t)
(require 'xgtags)
(add-hook 'c-mode-common-hook (lambda () (xgtags-mode 1)))
(defun global-update-incrementally () (shell-command "global -u -q" "*Messages*" "*Messages*") )

;; Call gtags update when idle for some time
(defcustom my-gtags-update-idle-time 60
"Number of idle seconds before an incremental gtags update is launched"
:group 'my-group
:type 'integer
)

;; initially allow gtags updates
(setq my-gtags-update-active t)

(run-with-idle-timer my-gtags-update-idle-time t
(lambda ()
(if (and my-gtags-update-active
(not (minibufferp) )
)
(progn
(message "Running gtags...")
(global-update-incrementally)
)
)
)
)

(add-hook 'ediff-quit-hook
(lambda ()
(message "Activating gtags update.")
(setq my-gtags-update-active t)
)
)

(add-hook 'ediff-before-setup-hook
(lambda ()
(message "Deactivating gtags update.")
(setq my-gtags-update-active nil)
)
)

Java Autocompletion for Emacs

The past few weeks I have been using the great eclim, and the corresponding emacseclim package. It allows you to use many of Eclipse’s features in your favourite editor. The installation of emacs-eclim is rather easy, and the one for eclim is even easier, since it comes with a simple clickety-click-installer.

I use emacs-eclim for Java development, and it works rather well. I use the two functions ac-complete-emacs-eclim and ac-complete-eclim-c-dot for auto-completion, which is the most important aspect when using such heavily object oriented languages and when developing large projects. Also useful are the functions eclim-java-import-missing, and eclim-java-remove-unused-imports. The current project can be compiled by calling eclim-project-build.

In theory, you can also use eclim with CDT for C++ development and auto-completion, but I rather stick with my earlier clang based solution, which is very fast and accurate.