Read my book

I wrote books about Webpack and React. Check them out!

Thursday, May 30, 2013

How to Write a Simple Templating Engine in Python?

I have covered the concept of templating earlier on this blog. To recap the idea is simple. You have a template which contains "slots". It is possible to inject content into these "slots". You end up with a string that contains that new content injected to your template. Simple as that.

There are many applications for this basic technique ranging from web development to document generation.

Existing Templating Solutions

Python comes with a few templating mechanisms already. I've listed a few common ones below:
  • '%d bottles of beer on the wall' % 50 # the old way
  • '{amount} bottles of beer on the wall'.format(amount=50) # the new way
  • '{amount} bottles of beer on the wall'.format(**{'amount': 50}) # unpack dictionary (handy trick)
  • string.Template('$amount bottles of beer on the wall').format(amount=50) # the verbose way
  • string.Template('$amount bottles of beer on the wall').format({'amount': 50}) # no need to unpack. See Template Strings for more information
As you might guess, there are a lot of templating engines available for Python. In my case using one seemed a bit overkill and I ended up coming up with a simple pattern that goes beyond what Python provides by default.

Simple Templating Engine

In my case I wanted to use a recursive dictionary structure that contains the context. This is something I construct based on a collection of JSON files. This task is very simple thanks to Python's json module. Unfortunately the tools above break down when trying to access recursive data.

After thinking about it for a while, the solution wasn't that difficult. It did mean I had to use a regular expression but nothing too shady. You can find my rendering function below:

import re


def render(tpl, context):
    """Substitute text in <> with corresponding variable value."""
    regex = re.compile('\<([a-zA-Z.]+)\>', re.MULTILINE)

    def repl(m):
        group = m.group(1)
        parts = group.split('.')
        value = context

        for part in parts:
            try:
                value = value[part]
                value = str(value) if isinstance(value, int) else value
            except KeyError:
                value = ''

        return value

    return regex.sub(repl, tpl)


print(render('<amount> bottles of <bottle.label>', {
    'amount': 10,
    'bottle': {
        'label': 'Amarillo'
    }
}))

The solution above takes a template and fields (a dictionary) as its parameters and returns a rendered template. There is some error handling in place and it deals with a recursive context.

Conclusion

I think the example highlights the usefulness of regular expressions. The expression doesn't look too bad. If you investigate it further, you noticed I've used a group to extract the data out of a match. It's a handy trick. You can even extract multiple groups in the same time.

There are likely other ways to achieve the same result but this one seemed to fit my purposes just fine. This is a tiny bit of an invoice generator we've been working on at our co-op. We're trying to replace our Google Drive based solution with something nicer and programmatic. So far it looks pretty good and might make a cool web service later on even!

Tuesday, May 28, 2013

Recent Work and Media Attention

I've been working on a CDN related project lately. It will go public within the next few days and I'll make a separate post out of it. During its development I wrote a simple API client for Pingdom on Node.js. It's read-only for now but that's all you need for many purposes.

Pingdom is one of those services that makes it easy for you to monitor the status of your service. They provide features such as SMS alerts and visualizations of course. This can be especially valuable if you are running a commercial site and uptime is money.

They interviewed me and a friend of mine from jsDelivr, a CDN service meant for JavaScript libraries. Check out their interview to see what I have to say about JSter.net.

A couple of days ago I got in talks with the gun.io guys by a chance. They have a hacker specific job board available. As a result of the discussions a post was published on their blog. I share my thoughts about how to find JavaScript libraries in it. Another article perhaps worth checking out.

I hope this brief post gave you some idea on what I have been up to lately. There are still quite a few things in the oven. :)

Friday, May 24, 2013

Getting Most Out of Social Media Marketing

Today I attended a lecture by Jussi Kämäräinen of bGH Uusmedia. He is an expert on the topic of social media and shared some insight to it. It has made a huge impact on marketing and keeps on growing as a medium. It has already began to disrupt the traditional media. Companies are starting to realize the advantages of this new approach.

Traditional vs. Social Media

Newspaper by Mike Bailey-Gates (CC BY-NC-ND)
The primary difference between so-called traditional and social media has to do with the level of interaction. In traditional media it's unidirectional. You simply receive a message designed by someone else. Social media on the other hand works more like a dialogue.

You feed in something and then receive feedback onto which you may reply again. As Jussi stated it's like a game of table tennis whereas traditional media is like shooting with a cannon and hoping it hits something.

Both mediums are often used for marketing. Jussi defines marketing as something that is meant to increase sales. Based on this definition it includes quite a few things such as communication, r&d, strategy, planning, visibility (where and how). Even though the definition is somewhat broad it makes sense. Marketing is an umbrella term after all.

Even though you might not participate in social media willingly, it may still affect you. Good examples of this are recent cases in which some company has violated the environment in a way that has lead to a public outcry. These sort of things capture the attention of the masses and easily grow out of control. PR damage is done.

This is the reason why it is a good idea to have a social media strategy in place. You might go the way of Apple and avoid it. Or you might embrace it and build your brand recognition that way. You could even involve your customers in your product design. At the very least you should prepare for the continuity in which something goes wrong. How will you react and minimize the damage?

Purchased, Earned and Owned

Pwned by Mike Smail (CC BY-NC)
Jussi splits media into three distinct categories: purchased, earned and owned. A television ad is an example of a media which you can purchase. In case the television channel decided to interview you then that's something you have earned. If you own the television channel, you can use it quite freely.

Of course that applies to this blog as well. I get to publish whatever I like. My impact is pretty limited but I still own the media for whatever it is worth.

Of these three particularly earned and owned mediums are effective cost-wise. By definition purchasing visibility can be costly. Maintaining this blog costs me nothing but time. Earning media attention is a bit more difficult but still quite feasible.

The Nature of Social Media

Nature by A Guy Taking Pictures (CC BY)
How do people use social media? According to Jussi it's mostly about discussing and sharing. In addition it allows people to reach out to their favorite brands. This is quite contrary to traditional marketing.

Rather than trying to persuade people to use your product, you provide them a way to give feedback to you and get to engage with them in a dialogue.

This sort of social capital can be very important. It allows you to reach out to your target market with less effort and you will have a good idea of who your customers are. In fact taking something traditional, such as listening music, to the social level can be a business itself. Consider Spotify for instance.

Now that companies have begun to understand the value of social media, how to approach it? The strategies that worked in traditional media won't work anymore. Rather you should aim to build a community around your products? But how to achieve that? By providing concrete value for your customers. For instance you could provide an exclusive view behind the scenes or involve people in your design process.

It's All About Campaigns

Realize by Omar Chatriwala (CC  BY-NC-ND)
Marketing on social media is a bit like sniping. You have to know what you are after. For instance you might want to increase your brand recognition within some specific group of people.

Usually this sort of campaign can range from between a couple of months up to a year. You will evaluate its success based on metrics you have set up at the beginning. After a campaign has finished, you are ready to start a new one. It's an iterative process.

If you really want to succeed in your campaigns, you have to figure out how to stand out. How are you different than the others?

This sort of thinking also applies in traditional media. The campaign framework is generic. Thanks to modern technologies it is easy to keep track of how well you are doing. The services in use provide a wide range of metrics you may use to evaluate your success.

Conclusion

I think Jussi's brief presentation went through the most important things related to social media marketing and strengthened my view on the topic. It is definitely one of those industries that will keep on growing and transforming media.

The world will look quite different by the end of decade as the media permeates the society in a way that is hard to imagine. We will likely still have some vestiges of traditional media around but I expect that the way we consume and produce media will change somewhat. The question in my mind is what is the next big thing?

Wednesday, May 22, 2013

Why the Finnish Game Industry Rules the World?

The Finnish game industry is comparatively speaking quite small. Depending on the source it employs somewhere between 2,000 to 3,000 people. Nevertheless it has made a significant impact on the market. How can that be possible?

I managed to get some insight to the topic yesterday as I participated in a local event organized by Jyväskylä Entrepreneurship Society and Expa, a gaming collective of sorts. They managed to bring Samuli Syvähuoko, a serial entrepreneur on a crazy crusade, here in the middle of nowhere.

He is the man behind successes such as Remedy Entertainment and has his roots in the Finnish demoscene through Future Crew. This makes him an ideal observer and mentor. And that is the role he seems to have taken right now. In this post I will go through some thoughts inspired by his excellent presentation.

Angry Birds, Clashing Clans

Many of you might recognize names such as "Angry Birds" or "Clash of Clans". You could claim these games are just quirks, freaks of nature. But there's more to the Finnish game industry than that. There have been success stories before. Remedy's "Death Rally" and "Max Payne" come to mind for instance. If you dig a bit you can find many more titles that have at least made a dent on the market.

Success breeds success. You could say the foundation of the current success was laid by the demoscene culture beginning in the 80s. Many companies, Remedy and Futuremark included, were born out of the connections made back then. Then there was that company known as Nokia. It made a difference on the mobile sector and helped to develop the talent. Finland is an engineering country by definition.

When you combine dark winters, high technology and sisu ("persistence"), you get an explosive combination it seems. Sure, there has been a lot of failure but it looks like there is something inherent in the Finnish culture that allows at least some the get past that. In addition there is capital available up till certain point. The situation could be better, though.

Better than Swedes

The Swedish game industry provides an interesting contrast to the Finnish one. It is very surprising that even though Finland is much smaller as a country (5 vs. 9 million), there are way more game studios in Finland. And they are making a bigger impact. Can you name a Swedish game title? Took a bit of thinking, didn't it?

According to Samuli the major difference has to do with persistence (that "sisu" thing). Finns simply take failure better and don't give up as easily. Granted sometimes this sort of doggedness isn't entirely healthy but that's what it takes in the game industry. There are no instant wins.

The industry has been structured differently as well. There are a couple of leading stars like Remedy and Rovio but even they are quite small (hundreds of people). It seems in Sweden the winners take it all to quote Abba. In short the talent gets employed to the established companies rather than striking on their own.

Boom, boom, boom

There is definitely some kind of a game industry boom going on at Finland. At least based on the amount of hype. You know something has changed when even the government begins to take steps to increase the amount of education and resources available for the industry. It feels like there is a bubble forming. Some of the hype might be justified but I think some people will just disappoint themselves.

That is the nature of the industry, though. As Samuli said, it's not enough to make a great game. You have to make a game that allows you to pay the bills. Effectively you have to figure out how to monetize on it. Rovio and Supercell are excellent examples of companies that have made good enough games with excellent monetization models. And they have become great at optimizing these models. They know what makes the players tick and what does not.

Fun + Money = Profit

It seems this is one of the cornerstones of a modern game company, analytics. Business analytics have been around for a long while. It has been just during the past few years that the field has found its way into gaming, though. It is making a huge difference on the sector. Rather than developing something and then praying it works you can effectively measure how well it works. This leads to better games, and of course, monetization.

It can be dangerous to focus only on the monetization aspect, however. If you start designing your games in Excel, you will lose all the fun and lose the whole point of gaming. Just like other creative industries, it is one of those fields that allow us to experience something new. You could say games are all about stories given they might be a bit simple ones at times.

Industry Challenges

How can Finland capitalize on its recent success? As I stated earlier even the government has become aware of the situation. The industry is booming and barely can keep up with the demand for labour. New industry hubs are appearing all around the Finland. Tampere is a very good example of one. The biggest challenges remain in developing mentoring and the funding sector. You will need those leading stars to show the way. We have a couple but we could use some more.

Conclusion

The answer to the question "Why the Finnish Game Industry Rules the World?" isn't entirely simple but I think I have some clue now. It comes down to at least the following factors:

  • Demoscene - The legacy lead to the birth of certain leading stars of the industry
  • Climate - Cold, dark winters provide an opportunity
  • Culture - "sisu" (that persistence thing) is a cultural value that allows Finns to plow through failure
  • Technology - As an engineering country we have a strong understanding of technology
  • Education - Even though we don't have Harvard level education it seems to be good enough

I might be missing a few but these are the most important ones in my mind. As a capital poor country we have had to find our own ways to success. It seems finns and gaming are a very good fit. The recent mobile revolution certainly didn't hurt things even though we lost that one big company. The industry as a whole seems to be reforming itself and I expect we will see a lot of small yet profitable companies emerge within the remaining decade.

In case you find the topic interesting and wish to gain more insight to it, I recommend checking out a documentary known as Pelinrakentajat (Game Makers) over at Youtube. There are English subtitles available.

Wednesday, May 15, 2013

Extracting Object Property Values in JavaScript

There are times when you need to extract property values of object in JavaScript. This task is quite trivial to solve. It is also one of those cases that allows you to use functional composition effectively.

If I need to extract property values, I like to use a pattern looking like this: arr.map(prop('name')). In this case our array (arr, matey) contains a bunch of objects names of which we want to extract. I've defined a helper, prop, for this purpose. prop is just a factory that returns a function that extracts the value. Consider the example implementation below:
function prop(name) {
    return function(o) {
        return o[name];
    };
}
Who says factories are useful only in Java? :)

If you are using Underscore, you may find the pluck method useful. It does effectively the same thing.

Thursday, May 9, 2013

Web Architecture Styles

This is a topic I touched upon at my AgileJkl talk this year. I thought it might be nice to recap some core thoughts and expand on it. I will go through some basic patterns I have observed. These patterns are on pretty high level and I won't go into detail too much. The basic idea is just to provide some food for thought and perhaps to spark some conversation.

Static Site

Since the beginning of the web people have been developing static sites. After all that was what the web was designed for, as a document platform with hyperlinks.

The model is quite useable even these days. Better yet it is possible to implement certain dynamic functionality using JavaScript so it doesn't have to be entirely static. Rather in this case static can be considered to mean the way we host our site.

Hosting is very simple in this approach. You simply need to dump your static files on some server and it will just work. That's it.

The security model of this approach is simple. If someone can root your server you're done for. In addition possible external services you might be using could inject some content you might not like. Compared to others this is the simplest security-wise, though. Contrast this approach with something like WordPress and you might see how much smaller the attack vector and the need for server maintenance is.

If you examine the image, you can see I've used GitHub Pages as my server platform. This works surprisingly well for hosting. As a bonus you don't have to take care of the server maintenance yourself. Naturally this approach integrates well with GitHub. Basically all you need to do is to update your Git repository and the machinery over at GitHub will update your site. It also provides support for Jekyll, a popular static site generator, although pure HTML works as well.

The image also highlights the way you might integrate an external service, such as Disqus, to your site. In this case Disqus provides commenting. Incidentally I use that service on this blog and it has grown immensely popular in the past few years. You could also aggregate data from RSS and services such as Twitter. You can see this approach in action at the site of my co-op.

Client-Server Approach

There are times when having just a static site and some dynamic JavaScript based doodads isn't quite enough. This is true particularly if you need to store some of the data yourself and it is dynamic by nature.

This approach is used at JSter, a catalog of JavaScript libraries, which I help to maintain. Basically we have a database of our own for the libraries. The users of the site can modify that through a web interface.

The approach is a bit monolithic by nature and I think that we're moving away from it. The first step towards this was implementing a REST interface for the data. The idea is that external services could consume our data through it. I'll go through the details in the next section.

If you examine the image, you can see Disqus there. Not surprisingly we use it to provide commenting for various parts of our site. Overall the approach can be considered a superset of a static site. There is some extra complexity but on the other hand you gain some extra flexibility. The most important feature is the possibility to render HTML in a dynamic way on the backend side. So as the data changes so does your view. This isn't the only way to achieve the effect, however.

Security-wise the approach is more complicated than a pure static site. You will have to make sure you are using recent versions of the libraries in addition to making sure your server stays secure. There are additional attack vectors available that are related to the fact that you are allowing the users to modify the data. This is a source of a whole range of attacks including those famous injections.

REST Architecture

As noted above a traditional client-server approach is quite monolithic by definition. These days we're moving towards something more distributed. In effect we're moving the data behind an interface of its own and then access that data using another service. There is a whole set of principles on how to implement this kind of interface.

The nice thing about this sort of split is that it allows you to implement multiple frontends for the same data. It also allows other services to consume your data more effectively making it possible to implement mashups.

The image shows how the data might be consumed by another service in case of JSter. We would still probably like to have an intermediate storage at the server running the main site to introduce some redundancy to the system. It isn't particularly nice if REST server goes down. Alternatively it should be possible to add some redundancy over there to make sure this cannot happen quite easily.

In this case the approach can be taken further. Besides consuming we could make it possible for other services to modify the data. This comes with a risk of vandalism, though. You definitely don't want that someone messes up your valuable data. As a result it likely makes sense to restrict write rights and implement some sort of revisioning over at the REST server. Of course you should have backups of the data available in any case should anything go wrong.

This approach also allows us to move some computation to the client. Client-side MVC frameworks are a good example of this trend. There seems to be some sort of counter-trend going on, though, as aspects such as SEO can be difficult to deal in a pure JavaScript based approach. I think we'll see more frameworks that take these concerns in count. It would not surprise me if that was the next wave of web development.

Conclusion

The examples above were just a couple of individual examples of possible architectures. These cover my use cases quite well, though. During AgileJkl I saw an interesting, generalized variant. What if a whole web service was just a collection of very small web services talking to each other? There could be literally hundreds or even thousands of these.

Even though that might sound a bit counter-intuitive there's some sense to it. It's the ultimate anti-thesis of a monolithic approach. Given you have standard ways in which the services communicate with each other it is possible to use whatever technology you happen to find suitable for the purpose.

The approach incurs some extra latency compared to more monolithic approaches. Perhaps clever caching and other performance tweaks can counter this, though. Overall it seems like an interesting way to think about web architectures.

Wednesday, May 8, 2013

Book Review - From Zero to the Appstore

Disclaimer: I received a free copy of the ebook and associated material. Olaf also contributed a guest post for JSter earlier.

In the past few years we've seen increasing interest particularly in mobile development and HTML5 technologies. These days it is possible to implement mobile games using HTML5 technologies even. Olaf Horstmann of indiegamr covered various approaches at JSter earlier.

He has also authored a book on the topic. This book, known as From Zero to the Appstore, delves further in the topic and shows you how to create a game and get it out there. You can try out the game in your browser. It is one of those sidescrollers with parallax and shiz. Click to jump (double jump works too).

The question is can the book live to its name? Let's find out!

Overview of the Book

The book is available in three configurations named as "Book & Resources", "Additional Material" and "Team License". The basic package costs $29 where as the the beefier ones $59 and $99 respectively. The more expensive options allow commercial usage of the source, assets and such whereas you may use the material included in the basic package for non-commercial purposes. "Additional Material" contains a bonus demo as well. Team license in particular seems quite benevolent for larger development teams interested in the topic.

The book, containing 129 pages, is provided in ebook format (pdf, epub, mobi). In addition source code, assets and final project files are included. The book itself has been split in two parts. The first one focuses on producing an actual game whereas the latter one discusses on how to publish it on mobile platforms.

Book Content

The first part has been written in iterative manner. You start out from the very basics and slowly but surely build the whole game on top of that. The author has chosen to use EaselJS and SoundJS of CreateJS suite. A good pick in my opinion. Particularly those with Flash background will appreciate this.

In addition some theory is covered including the basics of collision detection. I think this is a good idea and adds some depth to the book. It could have been nice to have even more theory available or at least links per theory topic covered. There is a list of these at the end but I feel something more specific could have been more appropriate.

The second part focuses on the workflow related to getting this game into various app stores (Apple and Google ones in this case). The author covers both PhoneGap Build and Cocoon in detail. It is definitely nice to have these sort of workflows covered.

You can get a better idea of the approach and writing style by checking out the sample chapters.

Analysis

Overall the book reads quite well. The author goes through each line of code thoroughly in almost a literate programming kind of manner. Unfortunately there were some grammatical errors. The book could have benefitted from some editing. Given it's an ebook it isn't too late to do this and I hope the author continues to update the content.

There were various things I would've coded differently myself. To some extent this is more of a matter of style, though. For instance I prefer to use functional constructs when applicable and like to keep my code terse when possible. Instead of XAMPP I would have suggested Node.js and serve package instead. You will need Node.js anyway in case you wish to hook into tool infrastructure developed based on it.

It would have been nice if the author would have covered some module based approach. I have used Require.js with some success. Browserify could have been an interesting solution to cover as well.

I know it might a bit much to chew, especially for beginners, as you need to set up some infrastructure. Learning to use these kind of tools is very beneficial longer term as your project grows. The last thing you want to do is to have to fiddle with those script tags too much. And then there's the whole configuration management thing. What if you want different kind of builds for different platforms?

It would be very interesting to see the first part rewritten in a form which you can run through your browser. Something like ExplainJS would complement the approach taken really well. There is definitely room for introducing more interactive ways to go through the basic concepts. Games and interactive programming are a nice fit as demonstrated by Bret Victor.

It's a solid effort nevertheless. I found myself going through the book while examining the examples at a browser. Each step has source code and resources associated to it in a neatly arranged manner. Especially for beginners the combination of step-by-step sort of approach combined with some theory seems like a good idea. For more advanced people the beef might lie in part two in which the app store related workflow is covered in detail.

The value proposition of the lowest tier ($29) seems quite alright. Other books are in the same range although they will provide more content for the money. I think the strength of this offering lies within its relative simplicity. It all depends on how far in the rabbit hole you wish to go. There is also a money back guarantee which seems like a nice touch.

Conclusion

From Zero to the Appstore by Olaf Horstmann achieves what it promises. After reading the book and going through the associated material you should have a good idea of what it takes to get a game to an app store. It won't delve very deep into the rabbit hole but at least it's a decent starting point.

The book could have benefitted from some editing. The approach, step-by-step mixed with theory, seems quite valid. I would, however, consider porting the first part of the book into a more interactive form. Besides being cool that might make it easier to absorb the information.

If you dig around, you'll see there are a lot of game development related books out there. Especially if you are a beginner you will likely benefit from Olaf's offering, though. More advanced users familiar with the workflow already might want to pick up something else.

Tuesday, May 7, 2013

My Favorite Business and Startup Blogs

I have quite a few interesting blogs set up to my RSS reader (happens to be Newsblur by the way). I thought it might be fun to share some of those related to business and startups. Enjoy the list below!

  • Steve Blank - He is the guy behind the concept of Customer Development. This is one of those things that lead Eric Ries to write his influential book on Lean Startup.
  • Lean Startup Circle - These guys post highly useful "weekly lean startup tweets" type of summaries. There is some other content every once in a while.
  • Chuck Blakeman - The guy has some very valid points on how to grow one's business. Lately he has been going through "industrialist vs. capitalist" kind of discussion. I find this particularly interesting as it highlights well the changes we're going through at the moment.
  • The Startup Toolkit - If you have a startup related problem, this blog is a very good starting point.
  • Business Model Alchemist - Business Model Canvas and all that. Get a better idea on how to design your business.
  • Ash Maurya - Another business model related blog. A bit different approach and canvas, though. Overall worthwhile thoughts.
  • Kevin Dewalt - Great startup advice. For people in established companies too.
  • Gabriel Weinberg - On resonance, traction and all that. He is working on a book about traction by the way.
  • Saint Sal - I haven't figured out why "saint" yet. Anyway, the content is good so perhaps that has something to do with it.
  • 10,000 Startup Hours by David Cummings - Just rediscovered this hidden gem myself. A lot of topics covered seem to resonate quite well.

As a bonus I'll throw you a recently published article "Why the Lean Start-Up Changes Everything" by Steve Blank. It was published in Harvard Business Review and should give a pretty good idea of what the fuzz is about. Interesting times.

In case you think I'm missing some kick ass blog from my reading list, let me know over at comments. I hope you found something interesting in the list.

Monday, May 6, 2013

Linkdump 13 - Art, Business, Programming, Startups, Web...

Half a year has passed since the previous linkdump and it's time to go dig up some jewels. Note that it's probably easier to keep a track of these simply by following me over at Twitter. I tweet around two or three times a day and try to keep it interesting.

Art


Business


JavaScript


Misc.


Music


OS X


Programming


Startups


Web Design


Web Development