clj-di: dependency injection for clojure and clojurescript



I was surprised when i found that no one wrote library for dependency injection which works on clojure and clojurescript. I found component, but it’s too complex and not working with clojurescript.

So i developed little library – clj-di. I wrote it using cljx, so library can work with clojure and clojurescript. Library uses atom with hash map for storing dependencies. Dependency can be registered and received using keyword.

Library has two ways for registering dependencies. Permanently with register! (i use it in lein-ring :init for creating db connections):

(register! :db (Database.)
           :logger (get-logger))

And for code block with with-registered (i use it in tests for registering mocks):

(with-registered [:db (Database.)
                  :logger (get-logger)]
  ...)

And two ways for receiving dependency. With get-dep function (useful for receiving single dependency):

(get-dep :db)

And with let-deps macro (useful for receiving more than one dependency):

(let-deps [conn :db
           log :logger]
  ..)

You can see more on github and in documentation.

CSP on pyboard



Lately i worked a lot with clojure and core.async, and i was very impressed with CSP. Mostly because it saves me from js callback hell. For example, js code with sequential http requests (heh, with promises it’s less ugly):

$http.get('/users/').then(function(data){
    return $http.post('/user-data/', data.items);
}).then(function(){
    return $http.get('/posts/');
}).then(function(posts){
    console.log(posts);
});

With clojurescript and core.async it will be:

(go (let [users (<! (http/get "/users/"))]
       (<! (http/post "/user-data/" (:items users)))
       (js/console.log (<! (http/get "/posts/")))))

Clojurescript code looks more readable and simple.

And i developed CSP for micropython in my microasync library.

Example application — servo should rotate to angle which equal to X angle of accelerometer, and user can start/stop app with button:

from microasync.device import get_servo, get_accel, get_switch
from microasync.async import coroutine, loop, Delay, select


@coroutine
def servo_coroutine():
    servo, _ = get_servo(1)  # get_servo returns set and get channels
    accel = get_accel()
    switch = get_switch()
    on = False
    x = 0
    accel_or_switch = select(switch, accel)  # like select from go and like clojure core.async alts!
    while True:
        chan, val = yield accel_or_switch.get()  # like clojure (<! (accel_or_switch))
        if chan == accel:
            x, *_ = val  # we don't need y and z
        elif chan == switch:
            on = not on

        if on:
            yield servo.put(x)  # like clojure (>! servo x)
        yield Delay(0)  # like clojure (<! (timeout 0))


servo_coroutine()
loop()

And recorded on google glass (yep, i bought it few days ago) video of result:

Fixing StackOverflowError on travis-ci when running clojurescript tests



Recently i received strange error when running clojurescript tests on travis-ci:

$ lein2 cljsbuild test
...
Compiling "target/cljs-test.js" failed.
Exception in thread "main" java.lang.StackOverflowError, compiling:(/tmp/form-init311799534829133165.clj:1:89)
	at clojure.lang.Compiler.load(Compiler.java:7142)
	at clojure.lang.Compiler.loadFile(Compiler.java:7086)
	at clojure.main$load_script.invoke(main.clj:274)
	at clojure.main$init_opt.invoke(main.clj:279)
	at clojure.main$initialize.invoke(main.clj:307)
	at clojure.main$null_opt.invoke(main.clj:342)
	at clojure.main$main.doInvoke(main.clj:420)
	at clojure.lang.RestFn.invoke(RestFn.java:421)
	at clojure.lang.Var.invoke(Var.java:383)
	at clojure.lang.AFn.applyToHelper(AFn.java:156)
	at clojure.lang.Var.applyTo(Var.java:700)
	at clojure.main.main(main.java:37)
Caused by: java.lang.StackOverflowError
...

But locally all worked ok. And i found simple solution – increase thread stack size up to 16mb by adding this line to my project.clj:

:jvm-opts ["-Xss16m"]

As a result travis-ci build succeeded.

Switching windows with Leap Motion



After i abandoned my project on Clojure Cup i received little free time on this weekend. And i decided to play with Leap Motion and create little app for switching windows using it.

Result — leaptab. By default app uses win + w for switching windows, but with --use-alt-tab it can use alt + tab.

For opening window switcher (win + w or alt + tab) and switching to window you need to use circle gesture. For selecting window – swipe gesture.

Video with usage of leaptab:

Leaptab tested with ubuntu, but should work on all major platforms, but probably only with --use-alt-tab argument.

let statement in python



Today, when i wrote another context manager, i came up with the idea – write let statement in python using context manager.

First i invented simple realisation, where we manually pass locals:

from contextlib import contextmanager


@contextmanager
def let(locals_, **bindings):
    original = {var: locals_.get(var) for var in bindings.keys()}
    locals_.update(bindings)
    yield
    locals_.update(original)

And usage:

>>> a = 1
>>> b = 2
>>> with let(locals(), a=10, b=20):
...     print(a, b)  # inside `let` scope
... 
(10, 20)
>>> print(a, b)  # outside of `let` scope
(1, 2)

Looks ugly. But we can use little piece of magic with inspect. We can get outer frame and get his locals

from contextlib import contextmanager
from inspect import currentframe, getouterframes


@contextmanager
def let(**bindings):
    frame = getouterframes(currentframe(), 2)[-1][0] # 2 because first frame in `contextmanager` decorator  
    locals_ = frame.f_locals
    original = {var: locals_.get(var) for var in bindings.keys()}
    locals_.update(bindings)
    yield
    locals_.update(original)

Now we don’t need to pass locals explicitly:

>>> a = 3
>>> b = 4
>>> with let(a=33, b=44):
...     print(a, b)
... 
(33, 44)
>>> print(a, b)
(3, 4)

Green threads on pyboard



Some weeks ago i received new fancy device — pyboard. It’s like arduino, but using python instead of wiring. I was little disappointed, because micropython wasn’t have multithreading module.

And i developed microasync — library with green threads for micropython.

Little example, code for toggling leds concurrently:

from microasync.async import loop, coroutine, Delay
import pyb


@coroutine  # decorator for making green thread from function
def toggle_led_on_interval(led, interval):
    while True:
        pyb.LED(led).toggle()
        yield Delay(interval)  # like time.sleep, but non-blocking


toggle_led_on_interval(1, 1)
toggle_led_on_interval(2, 2)
toggle_led_on_interval(3, 1)
toggle_led_on_interval(4, 2)

# start main loop:
loop()

And result:

doto from clojure in python



When i writing code in clojure i can use good macro – doto.

With whom python code like:

window = QMainWindow()
window.setTitle(TITLE)
window.setWindowFlags(Qt.FramelessWindowHint)
window.setAttribute(Qt.WA_TransparentForMouseEvents, True)
window.show()

can be translated in clojure code like:

(doto (QMainWindow.)
      (.setTitle title)
      (.setWindowFlags Qt/FramelessWindowHint)
      (.setAttribute Qt/WA_TransparentForMouseEvents True)
      (.show))

And i wrote hackish class for doing something similar in python.

from functools import partial


class DoTo(object):
    def __init__(self, obj):
        self._obj = obj

    def _do(self, name, *args, **kwargs):
        getattr(self._obj, name)(*args, **kwargs)
        return self

    def __getattr__(self, item):
        return partial(self._do, item)

With method chaining we can emulate behavior of clojure doto:

window = QMainWindow()
DoTo(window)\
    .setTitle(TITLE)\
    .setWindowFlags(Qt.FramelessWindowHint)\
    .setAttribute(Qt.WA_TransparentForMouseEvents, True)\
    .show()