Author Archives: cpiekarski

About cpiekarski

create; explore; share.

Expanding The Toolbox – Google Go

I’ve decided to experiment with 12 new programming languages or frameworks over the next year. Just like spoken languages, mastery of a coding language can take years even with daily use. With just one month per language I hope to develop a basic understanding of the languages strengths and weaknesses. Languages are tools that help us solve problems and having a broad perspective of what’s in the toolbox can simplify our solutions. Lots of problems can be solved with C, but why use it for the subset that Python can solve faster and more reliably?

All programming languages have a unique feel. Python fells concise and lucid whereas C++ feels bulky and ridged. For August’s language I picked Google’s Go. I selected Go because it supposedly has the feel of a dynamic interpreted language with the speed and type safety of a compiled language.

To get a feel for the syntax I wrote a simple closure that returns two functions, one for adding a list of integers to the sum and one for calculating the average. As you can see, Go certainly has the feel of a scripting language but requires variable type declaration. Like Python it also allows for returning multiple results from a method.

// Note: WordPress doesn't have a 'Go' syntax highlight yet so I'm using javascript
package main

import (
	"fmt"
)

//Closure that returns a sum and average method
func stats() (func(a []int) int, func() float64){
	sum := 0
	entries := 0
	log := func(){
		fmt.Printf("sum: %d, entries: %d\n",sum,entries)
	}
	return func(l []int) int {
		defer log()
		for i:=0;i<len(l);i++ {
			sum += l[i]
			entries += 1
		}
		return sum
	},
	func() float64 {
		return float64(sum)/float64(entries)
	}
}

func main() {
	sum,avg := stats()
	s := sum([]int{1,2,3})
	s = sum([]int{1,1,1,4,1})
	fmt.Printf("sum: %d avg: %f\n", s, avg())
}

As a long time C programmer I initially found the reversed type and declaration syntax to be awkward, but after writing a few hundred lines it actually flows nicely. Rob Pike put together a great three day tutorial that you can access online or in the /go/doc directory. For more information on the syntax check out the day one slides. By far the most compelling feature of Go is the built in support for concurrency. As someone who has done lots of multi-threaded and multi-process development I believe the Go development team did a fantastic job at making it a fundamentally ingrained principle. To illustrate this feature I wrote a concurrent Pell Number generator below.

package pell

// Pell Numbers
// 0, 1, 2, 5, 12, 29, 70, 169, 408, 985, 2378
// P0 = 0, P1 = 1, PN = 2PN-1 + PN-2

// Day 3: Do not communicate by sharing memory.
// Instead, share memory by communicating.

func Pell(ch chan<- uint64) {
	var fn_1, fn_2 uint64 = 1, 0
	ch <- 0
	ch <- 1
	for {
		fn_2, fn_1 = fn_1, ((2*fn_1) + fn_2)
		ch <- fn_1
	}
}

There is nothing special about the above Pell routine. Its parameter is a communication pipe (called a channel in Go) that guarantees synchronization because it is a blocking write. To start the thread you use the “go” keyword and read data from the same channel passed to the routine. Upon a read it unblocks the thread which then calculates the next number in the sequence.

package main

import ( "fmt"
	 "flag"
	 "pell"
)

func main() {
	c := make(chan uint64)
	var iterations *int = flag.Int("i", 10, "number of iterations to run")
	flag.Parse()

	go pell.Pell(c)
	for i:=0;i < *iterations; i++ {
		var num uint64 = <-c
		fmt.Println(num)
	}

}

From my perspective the one major red flag is the lack of support for object oriented design. Although the ability to add a method to a struct or any data type is very cool I don’t yet see how it can fully replace the concept of a class. I added a small miscellaneous Go repository on gitHub that anyone interested in some social coding can fork and modify at will.

Python Generators

Generators are an easy-to-use tool in Python for making any object method or class an iterator. I’ve found them to be a very useful development tool and wanted to briefly expand on my previous PyQt Charts post.

The simplest generator is an object method that uses the “yield” keyword. Yield is just like return only the instruction counter is persistent in the method. In the example below, a generator method is created to return characters of a string one at a time in reverse order. A more real world example would be making a read socket a generator that returns packet data.

def reversed(s):
    index = len(s)
    while index >= 0:
        yield s[index]
        index = index - 1

revGen = reversed("Android")
for c in revGen:
     print c

The second and more useful approach is to let classes be generators. This is my preferred method because OOD is always better :-). Every class definition can overload the __iter__ and next methods. When using the class approach the StopIteration exception is responsible for terminating the iteration.

class QCharts(object):
	def __init__(self, **kwargs):
		self.charts = []
		self.index = 0
		self.len = 0

	def __iter__(self):
		self.index = 0
		return self

	def next(self):
		if self.index == self.len:
			raise StopIteration
		self.index = self.index + 1
		return self.charts[self.index-1]

	def add(self, c):
		self.charts.append(c)
		self.len = len(self.charts)

Now, using this iterator class we can loop through a series of QWidget based charts. Below is a slightly modified MainWindow class from the PyQt Charts post.


from PyQt4 import QtCore, QtGui
from pygooglechart import PieChart3D

class QChart(parent, type, **kwargs):...
class QCharts(object):...	

class MainWindow(QtGui.QMainWindow):
	def __init__(self, **kwargs):
		super(QtGui.QMainWindow, self).__init__(**kwargs)
		self.charts = QCharts()

	def addPieChart(self):
		i = self.charts.len
		t = QChart(self, PieChart3D, pos=QtCore.QPoint(i*75,i*75), size=QtCore.QSize(250,100))
		t.add_data([(20*i)+10,(i*10)+20])
		t.set_pie_labels(['Hello', 'World'])
		t.download()
		self.charts.add(t)

	def debug(self):
		for x in self.charts:
			print x

if __name__ == '__main__':
	app = QtGui.QApplication(sys.argv)
	app.setApplicationName("Blank PyQt Demo")
	app.setQuitOnLastWindowClosed(True)

	scaledSize = QtCore.QSize(scale.width(500),scale.height(500))
	window = MainWindow(size=scaledSize)
	window.addPieChart()
	window.addPieChart()
	window.addPieChart()
	window.debug()
	window.setWindowTitle("Immersive Blank PyQt Demo")
	window.show()

	sys.exit(app.exec_())

For more information on generators check out this great write up here. Another great feature of generators is that they can be used in generator expressions. These expressions look like this:

#extract the even numbers from the series
ints = [0,1,2,3,4,5,6,7,8,9,10]
even = [x for x in ints if x % 2 == 0 ]

#get the directory names from glob
import glob
def dirs(p):
     return [x for x in glob.iglob(os.path.join( p, '*')) if os.path.isdir(x) ]
dirs("/")

Immersive – The Code Inception

I snapped this photo the moment before I wrote the first line of code for Immersive. I was on an extended month long vacation with friends in Kauai, Hawaii. The trip was a celebration of life for my good late friend Ed Bortolini. We were staying on Poipu Beach and enjoyed everything from helicopter rides, scuba diving, surfing, kayaking, great dinning, and even backpacked the Kalalau trail.

I started with a programming language I didn’t know, using a framework I had never heard of, solving a problem I had never thought about in an industry I knew nothing about. My only motivation was to create something new. Oh… and I did it for free with no binding contract. Immersive’s code base still uses much of this code today and demos of it working are now plastered all over the internet. A mere 9 months later Immersive has generated a lot of press and obtained several clients. See below for just a small fraction of the coverage. E ho’a’o no i pau kuhihewa!

CNN Money Article

Live CNN Video

Mashable Article
New York Times Article

Huffington Post Article and Video

ADWEEK Article

The Next Web Article

PSFK Article

BNET Article

BetaBeat Article

BrandChannel Article

CNN Forune Article

PC World Article

Sprouter Article

Business Insider Top 25 Startups

Also check out the YouTube Channel.

RMNP Spearhead

I spent today high up in the alpine of Rocky Mountain National Park. My friend Tim and I set out at 3am to summit Spearhead via the North Ridge (~5.6 seven pitch trad climb at 12,000 feet). The approach is about 6 miles through the gorgeous Glacier Gorge and starts about a mile past Black Lake. It was a heavy snow year for the alpine this year (~300% above average), crampons and a mountaineering ax where required for safe passage after Mills Lake.

The quality of the granite on Spearhead is amazing and the several hours of climbing spent on the rock was epic. It was my first day at altitude in over a year and I definitely felt the effects. I had forgotten how harsh the elements can be above 12,000 feet. The decent off the summit was a little rough to do in climbing shoes but doable. After getting back to the packs we slammed some much needed cold water that had been chilling in the snow all day and started the 6 mile trek back across the snow field to the trail head. After 14 hours up in the alpine we heading back to Boulder where we had some great bbq, cold beers, and watched the Folsom Field firework show. Happy Independence Day!

The Pace of Innovation – Is Singularity Possible?

Resistance is futile. Over the last century our society has innovated at an ever increasing rate  and machines have come to play a dominate role in our homes and every commercial industry. In just 12 decades mankind has seen the advancement of the first diesel engine, jet propulsion, nuclear power, manned space flight, mass long distance communication (TV, radio, Internet), the first personal computer, the first computer programming language, laser guided bombs, 6K rounds a minute machine guns, real-time video streams, robotic surgery and now machines that build new machines from your living room (MakerBot).

If you continue this list of machine aided advancements you’ll quickly realize that not only do they already outnumber us, but their intelligence is rapidly increasing and our dependence on their services has never been greater. Despite being in the during the worst recession since the “great depression”, there is so much money flowing into the Technology industry that experts are warning of a second bubble . This focus on technology has swamped the USPTO with applications (it can now take over 4 years to receive an acceptance/denial notice). The bottom line: machines optimized to maximize our comfort and minimize commodity costs run almost every aspect of our lives.

It was only 3 decades ago that the first portable personal computer hit the market, known as the Osborne 1. This revolutionary machine weighed ~24 pounds and came equip with 64 Kb of memory and an 8-bit 4 MHz processor. The company went bankrupt only two years after its release. None-the-less it is the origins of the 3 pound 8GB of memory and 64-bit 2.4 GHz dual core processor sitting in my lap at the moment.

As a recent co-founder of an HCI company I’ve starting hearing the term Technological Singularity for the first time. To grossly undervalue and summarize the concept for those who don’t know, the Singularity is a point in time in the near future (~50 years) where Technology is expected to be capable of innovating on its own without human intervention. The premise of the theory is based on the notion that it’s possible for engineers to write an AI algorithm that is capable of finding flaws in a design and optimize itself to no longer have those flaws. As a result, the pace on innovation would in essence occur at the speed of light.

To be honest, when I first heard of the Singularity I laughed and dismissed it as complete bullshit. I’m not so sure anymore… It might sound far-fetched but our society is already starting to use similar real-time optimization algorithms in online search engines, marketing campaigns, and even Walmart distribution channels. Having recently spent a lot of time around the machine learning experts in the software engineering industry, I’ve seen firsthand that they are very good at teaching machines to learn. Even though the field of AI has yet to see its own Einstine, its only a matter of time before one emerges and AI becomes the new split atom.

def Singularity():
     while True:
          do()
          result = measure()
          learn(result)

JavaScript & Python Closures

Closures are generally considered to be JavaScripts most advanced and useful feature, and hence. As a C++ coder I like to think of Closures as a method with a privately malloc’d stack that is persistent across multiple invocations. As a Python coder I like to think of Closures as a method object with a preserved variable environment. Regardless of how you want to think about them, Closures allow us to redefine how we approach coding up methods.

Take the following simple JS example. Here, we are creating a very simple incrementer. The method inc() takes a starting value and returns a method that simply adds one to the start value (but not until its actually executed in the loop below).

function inc(start){
	var i = start;
	return function() { i = i + 1; document.write(i); }
}

addOne = inc(0)
for (i = 0; i < 10; ++i){
	addOne();
}
// prints 12345678910

I find this feature of the language very cool. We can also do the same thing with Python.

#Works with Python 3.x only
def inc(start):
    y = start
    def adder():
        nonlocal y
        y += 1
        print y
        return y
    return adder
 
addOne = inc(0)
x = 0
for x in range(10):
    addOne()
#prints 12345678910

The ramifications of Closures to popular programming languages are still in its infancy even though the concept has been around for decades (adding them to Java has been a debate for years). It will be extremely interesting to see how the wide spread adoption of Closures will ultimately impact our coding habits in the near future.

Will Python ever be taken seriously?

I’ve been writing Python for almost a year now and have been pleasantly surprised by the language. Even though Python has been around for a few decades now it still hasn’t been widely accepted as a production grade programming language.

Its primary use is still in the online world where it has developed a large rivalry with Ruby. It’s been my observation that Ruby wins this match up in the eyes of developers about 80% of the time. In the embedded world the use of Python is almost unheard of and I’ve personally never seen it used exclusively for a full stand alone application.

The language is aesthetically pleasing, requires less typing, and is fantastic for rapid prototyping. It definitely has its quarks and drawbacks but what language doesn’t? When I mention to other developers that I’m currently writing in Python the most common reaction is disbelief. The language just isn’t taken seriously. Why?

First Flatiron

Today I accomplished a 20 year old goal of climbing
the First Flatiron above Boulder, Colorado. I initially set the goal when I was ten years old at summer camp out hiking with peers.  I had no idea it would take two decades to get around to doing it!

The First Flatiron is an iconic Boulder climb that offers spectacular views. Despite the inability to place good protection it’s a fantastic classic climb. The climbing is relatively easy. Depending on the route its rated about a 5.5 or 5.6 all the way to the top. The most challenging aspect of the climb is the high exposure level and lack of opportunities to place protection (the possibility of a 40 foot fall is common).

My friend Tim and I did it as a 5 pitch “trad” climb, but it is very common for people to climb it without gear. The climb ends with a well protected 100 foot free rap off the back side of the flatiron. I had a blast and cant wait to do it again!

PyQt Charts

The Qt framework doesn’t contain any useful charting widgets. There are a few python charting modules out there, most notably PyQwt. As a PyQt developer I didn’t find this module very useful. What I really wanted was a charting widget that used QWidget as its base class. At the time of this post I couldn’t find anything so I wrote my own based on the google charts module.

What I love about this snippet is that it mimics the C++ template functionality in Python. The QChart function below abstracts the PyQtChart class definition and any pygooglechart object can be instantiated as a QWidget.

__author__	  = "Christopher Piekarski"
__email__	  = "@c_piekarski"

import time, os, sys
from PyQt4 import QtCore, QtGui
from pygooglechart import *

def QChart(parent, type, **kwargs):
	class PyQtChart(type, QtGui.QWidget):
		def __init__(self, parent, **kwargs):
			QtGui.QWidget.__init__(self, parent, **kwargs)
			type.__init__(self, kwargs["size"].width(), kwargs["size"].height())
			self.pix = QtGui.QPixmap()

		def download(self):
			file = "./%f.png" % time.time()
			type.download(self, file)
			self.pix.load(file)

		def paintEvent(self, event):
			p = QtGui.QPainter(self)
			p.drawPixmap(0,0,self.pix)
			super(PyQtChart, self).paintEvent(event)

	return PyQtChart(parent, **kwargs)

class MainWindow(QtGui.QMainWindow):
	def __init__(self, **kwargs):
		super(QtGui.QMainWindow, self).__init__(**kwargs)

		t = QChart(self, PieChart3D, size=QtCore.QSize(250,100))
		t.add_data([10,20])
		t.set_pie_labels(['Hello', 'World'])
		t.download()

if __name__ == '__main__':
	app = QtGui.QApplication(sys.argv)
	app.setApplicationName("PyQt Charting Demo")
	app.setQuitOnLastWindowClosed(True)

	scaledSize = QtCore.QSize(500,500)
	window = MainWindow(size=scaledSize)
	window.setWindowTitle("PyQt Charting Demo")
	window.show()

	sys.exit(app.exec_())

Web Apps – The More Custom The Better

I’m a big fan of the WebKit Qt framework integration. It will without a doubt change the face of web based content delivery over the next few decades (previous post). By far the most powerful aspect of this integration is the ability to create custom JavaScript APIs for hybrid web based applications in a browser.

Put simply, browsers can customize the functionally they offer by implementing features into the browser that can be accessed by web content via JavaScript.  Web apps that exercise this functionally must be written to do so, making them “custom” web apps.  Meaning that an app written to utilize features defined in certain browsers will not work without modification in Explorer, Firefox, or Chrome. For example, when GE integrates a web browser into their next generation refrigerators, it would be beneficial to expose an API that allowed an app to check the inside temperature or level of the ice tray.

I’ve been in many debates with other engineers about this option.  Not one engineer has ever agreed with me right off the bat.  Most oppose doing it as it limits were an app can be run and defeats the purpose of web content.  The “purpose” being mass distribution and play-ability on many different standardize browsers.  Most don’t understand why you’d create a web app that can only be executed on a limited number of browsers.  Many also argue that it burdens app developers with having to learn yet another JavaScript API.  Regardless of the objection, this option is widely opposed and not fully understood by most engineers.

The engineering objections are easy to argue against.  First of all, there is no such thing as a standardized browser.  Despite standardization efforts from W3C,  the JavaScript language is a mess.  The big three, IE, Firefox, and Chrome all use a different JavaScript engine and have varying degrees of standardization completeness (some even have features not covered by W3C).  If you want your website to work across the big 3 you already have to add additional code.  I know its a stretch, but this is already a form of web app customization.

The real value add of customized web apps is from a business perspective.  I come from the embedded device world where the number of units sold is the driving factor (compared to the number of hits). By forcing apps to write to your API you are ensuring a certain level of stickiness.  It can be a competitive advantage to limit the devices an app can run on by exposing custom functionality through the browser.  GE won’t want its ice tray level app to run on LG with no modification for the same reason Apple does’t want its apps to run on Android without a rewrite.

Adding a custom JavaScript API to your Qt WebKit browser is easy.  Nokia has a great blog post on its forum here. To illustrate its simplicity I’ve included my own snippet below.

Any C++ object can be complied into the browser and exposed to the JS engine.

class FakeObject
{
public:
	FakeObject(QObject *parent);
public slots:
	posterInternalCommand(QString command, QString data);
signals:
	fakeSignal();
}

FakeObject::FakeObject(QObject *parent)
{

}

bool FakeObject::postInternalCommand(QString command, QString data)
{
	// Do something with data
	return True;
}

FakeObject::emitSignalTest()
{
	emit fakeSignal();
}

After instantiation the object is exposed to the JS engine using a QWebFrame method.

FakeObject* m_pfake = new FakeObject(webView);
webView->page()->mainFrame()->addToJavaScriptWindowObject(QString("fake"),m_pFake);

The exposed object can now be acted on via JavaScript. Public slots are methods that can be called directly from JS. Signals can also be connected to local JS methods making for more asynchronous functionality.

b = window.fake.postInternalCommand("fakeCommand", "fakeData")

function fakeCallBack(){
	x = x + 1
}

window.fake.fakeSignal.connect(fakeCallBack)