Python, oh how I’ve missed you (the power of unpack)

Recently I started playing around more with my Neo Smartpen. The company makes their data storage format public and I wondered how long it would take me to write a program to show my handwritten pages within a simple app. Given my background, I knew I could do this quickly in Java with a UI in Swing so I started there.

Parsing the data from the files was quick. You have a Page that consists of Strokes that is made up of Dots. Dots are the fundamental units the pen records as you write on a page. As you move the pen around it groups these dots up into strokes. Since each bit is well defined in the spec you get a lot of code doing things like

ByteBuffer.wrap(is.readNBytes(4)).order(order).getFloat();

to read x, y, and pressure date from the pen. While it’s a bit wordy, it works well enough. Running the app however, was pretty slow. Pages didn’t just snap onto the screen as I would have expected. I can’t recall what exactly was the root cause of the slowness anymore, and I decided to try creating a python version with a Gtk interface instead.

The first thing that impressed me with Python was how easily you can read data from a file.

        with open(self.filename, "rb") as file:
            file_id, file_version, note_id, page_number, notebook_width, notebook_height, created_time, \
                modified_time, dirty_bit, number_of_strokes = unpack('<3siiiffqqci', file.read(44))

Mind, seriously, blown…

The simple and immensely powerful ability of Python to read data from a file blew me away. It just shines a light on the large amounts of thought and time that went into this API. To be able to define and read blocks of data from a file is powerful. To top it all off, the Gtk version of the app ran significantly faster.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.