• Automating vimdiff's HTML diff (TOhtml)

    Proof of concepts are often done pretty differently than other kind of work. The rules described in (in)famous “Effective engineer” are even more important to succeed with PoCs. At the end of a day it all gravitates around “value added divided by effort spent”.

    I was doing some quick PoC project that lets user paste log fragment and see what are other log fragments in the database that are most similar to the pasted one. But apart from showing the list user wants to inspect the differences too.

    Simplest solution is to generate diff in e.g. HTML and present it to the user. But what is the best way to achieve that quickly? By using existing tool!

    This post is about automating vimdiff’s TOhtml using headlessvim library. Apart from presenting actual solution I cover some of the details on how Vim loads configuration files, how can we pass commands to it, etc.

    vimdiff in action

    Read on →

  • Accessing globals after wrong code.interact() call

    def foo():
        global database_index
        bar()
        import code; code.interact(local=locals())

    Have you ever called code.interact() and forgot to pass local=locals() or local={**globals(), **locals()}. Most of the time you may just exit interactive console, add missing parameters and run program again. But what if the program was executing for couple of hours before interactive console was started? You might want to access e.g. global variables without running it again. Fortunately Python is a language for adults, so it’s totally doable.

    Read on →

  • tee equivalent as a Python class

    Do you know tee program? Its man page reads:

    tee - read from standard input and write to standard output and files

    It makes it easy to split output of one program into both stdout and files. It’s a nice UNIX tool. Recently I was doing code review and it turned out that equivalent of such thing may be pretty useful in Python programs too:

    with open("file1.txt") as f1, tee(open("file2.txt")) as f2:
      shutil.copyfileobj(f1, f2)
      if f2.tail not in ('\r', '\n'):
        f2.fileobj.write('\n')

    It allows to do extra work, so we can employ it to e.g. simultaneous hash calculation or other job.

    Read on →

  • Roccat Suora driver for Linux > 4.11.0

    Recently I decided it’s time to install dedicated driver for my keyboard to programatically control its LED behavior. I have Roccat Suora keyboard. Fortunately all of the code is already available here. However the kernel module failed to compile because of signal_pending being undeclared. I had to add following code in hid-roccat.c and it worked like a charm.

    #include <linux/version.h>
    #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0))
      #include <linux/sched/signal.h>
    #endif

    signal_pending was moved to linux/sched/signal.h

    Read on →

  • Explanation of C++ expression on Code::Dive T-Shirts

    Code::Dive 2019 T-Shirt

    Read on →