The Chartio team has grown now and several months ago I had to stop coding completely and focus on CEO tasks. I’ve missed programming a lot, and tonight, the night before Thanksgiving and one of the first breaks I’ve gotten, I created a little javascript chess game and AI.

If you’re interested you can play the game here or checkout the source code here.

I utilized jhlywa’s helpful chess.js library for some of the move intelligence and game status storage. It relies on the Forsyth–Edwards Notation (FEN) and I found it quite simple to use. It has an odd implementation of an object constructor so extending the class was difficult, but typically only one instance of the chess object is used anyway so I just kept it simple and appended a few functions on to the instance for now.

The chess.js files come with an ASCII output but no html interface. It was fairly quick to use the chess unicode characters and the drag and drop from jQuery-UI to make up a classic looking board and drag controls. Most of that code can be found in game.js

Lastly, it wasn’t fun at all to play myself so I made my first attempt in many years at writing an AI. I used an alpha beta search which is effectively a minmax algorithm with a few shortcuts. In theory the AI should work pretty well. In practice however its quite dumb. There are two main reasons

    • game status and potential moves are stored as strings
    • I have no openers stored in memory
    • the board score is only based on the pieces that remain

To fix those I would have to write my own Bit based chart engine.  Right now the string based chart.js library can only search a depth of 2 before the wait time starts to get really annoying.  That’s pretty bad. I read that typically a depth of 6 or 7 is desired for intelligent mid-game play. Many AI implementations also have some openers stored in memory. Mine does not and so it pretty much sabotages its game right away by continually choosing its first move (pushing the first pawn) until the game starts to get more interesting.

Anyway, it was fun to get back into coding with a small project.  Especially one that I can play and share with others.  The code is free as in beer, but if you do anything with it please let me know as I’d love to check it out!

Gmail is a frequently used SMTP host for Django apps.  Its free and relatively simple to setup which is ideal for small apps.  There are a few significant downfalls. First, its limited to 500 emails a day with no option to upgrade.  Second, it limits and even re-writes all outgoing emails to the username of the connector.

For example if you’re trying to send an email from welcome@yourhost.com or support@yourhost.com you’re out of luck because all of those emails will actually get sent at dave@yourhost.com or whatever email account you have configured. I’m sure this saves millions of people from millions of spam but it adds a bit of annoyance to developer’s lives.

The recently launched MailGunoffers a great solution.  Its free for up to 200 emails per day and super cheap and dramatically scalable after that.  It also works as a drop in replacement for whatever service you were using for your django smtp server.  Changing will take you less time than reading this article.

The Steps:

  1. Sign up for MailGun
  2. Go to the control panel and click on the YOURHOST.mailgun.org server created for you.
  3. In the upper right you will find your “SMTP Authentication” credentials for this server.
  4. Open the settings.py in your django app and configure your email with the given credentials.  It should look something like this.
    EMAIL_USE_TLS = True
    EMAIL_HOST = 'smtp.mailgun.org'
    EMAIL_HOST_USER = 'postmaster@YOURHOST.mailgun.org'
    EMAIL_HOST_PASSWORD = 'SOMEPASSWORD'
    EMAIL_PORT = 587
    
  5. Test it out!./manage.py shell
    >>> from django.core.mail import send_mail
    >>> send_mail('MailGun works great!', 'It really really does.', 'tester@YOURHOST.com', ['YOUREMAIL@gmail.com'], fail_silently=False)
    

					

I’ve become a really big fan of the PickledObjectField provided by this django snippet.  So much so that I use it in almost every django model I create these days.

Basically it serves as the best way to do an object store in your database and perfectly translates in any JSON conversion.  Its an essential tool in any javascript heavy application.

In my current project, Chart.io we’re aspiring to be google analytics for your database.  We’re basically creating a system that will handle all of your dashboard/analytics needs in the easiest way possible.  This means that we have a LOT of different charts and more in the making.

Its not feasible to put all of those extra parameters that each chart type requires into the model as different columns.  We would end up with an incredible mess in short order.  So I instead create a PickledObjectField called ‘params’ in the model.

class Chart(models.Model):
    ...
    params = PickledObjectField(default={'just': 'some', 'default': 'parameters'} )

The params variable then takes most any dictionary of parameters and automatically converts it to string to be stored in the database.

The following command for example will save a params value of something like “KGRwMQpWa2V……” to the database, but you can still use it just like any dict object.

>>> chart = Chart(params = {"type": "scatter", "dot_size":, 4, "color_list": ["red", "green", "orange"], });
>>> chart.save()
>>> chart.parms
{"type": "scatter", "dot_size":, 4, "color_list": ["red", "green", "orange"], }
#You can also treat the field just like a dict
>>> chart.parms['awesome'] = 'for sure'

Its worth noting that there is a similar snippet to this that uses JSON object to string conversion instead of Pickle.  I find that when using Javascript so heavily its easier to use some other string conversion so as not to get confused and I’ve been really impressed with the way that this particular snippet works.

Its incredibly rare that a django snippet becomes such a major tool.  With the exception of my subdomain middleware, I can’t think of another snippet that I use more regularly which leads me to think that it should really get moved into the core fields that django provides.  Object store is essential element to many applications and the PickledObjectField is the best way to do it.

I’ve been a fanatic about finding the best ways to organize my life and stay “on top of things”.  There are a million articles online on how to go about this. I’ve read my fare share of these and tried a lot of the methods out.  Usually something ends up working decent for a while and then I either burn out or lose discipline in it.  So I pick up some other method or just deal with a more chaotic productivity schedule.

I never get too disappointed when one doesn’t work out as usually said method was proposed by some blogger who seemed to only have recently (and likely temporarily) gotten their shit together and decided to tell the world about it.  So there wasn’t too much of an expectation going in, but its often a bit frustrating to me to have the advice and tips given out so confidently.

The articles are usually written in a way that makes their method sound like the writer has been practicing this for years and achieved great success/peace with the workflow they deal with.  But I have never met a person who has found such success.

There are a thousand different TODO apps on the web and for download.  Each one will tell you that their tool is the best to keep organized and productive.  There are even more authors and bloggers.

In the end nothing has worked near as well for me as a blank notebook and a pen.  Sometimes a planner helps when my days are more about scheduled meetings and less about development time, but planners are just paper with a different set of lines.

So I guess the thing that I learned hear is that keeping organized and on top of things is a constant struggle and there’s no perfect fix for it.  Its just the nature of the game, and if you accept that it takes time and some effort then the whole process becomes a lot less frustrating and a lot less time is wasted trying out so many different tools.

Fancybox is a really nice jQuery library for creating lightboxes.  There are a few simple examples and some documentation on their site but there seems to be no demonstrated method for loading a fancybox dynamically with javascript.  All of their examples require you to embed a link to the lightbox content somewhere in the page source.

The following hack, mostly taken from the method described here, allows you to dynamically any web page into a fancybox straight from javascript.

I’ve documented the following example which should be fairly straight forward.  Basically it embeds a hidden link, that’s controlled by the callFancyBox function.  Make sure you have the right things included and you can simply call

callFancyBox( <your iframed url > );

<html>
<head>

<!--- These are the scripts and styles needed for fancybox to work --->
<script type="text/javascript" src="http://qwisk.com/sbmedia/scripts/libraries/jquery-1.3.2-mod.js" charset="utf-8"></script>
<script type="text/javascript" src="http://qwisk.com/sbmedia/scripts/libraries/jquery.fancybox-1.2.6.js" charset="utf-8"></script>
<link href="http://qwisk.com/sbmedia/css/jquery.fancybox-1.2.6.css" rel="stylesheet" type="text/css" media="all" />

</head>
<body>

<h3> Fancybox Test</h3>

A fancybox will load in 3 seconds

<!--- Currently fancybox only works on links in your page.  This div hides a link, who's href we change dynamically --->
<div id="hidden_clicker" style="display:none">
<a id="hiddenclicker" href="http://asdf.com" >Hidden Clicker</a>
</div>

<script type="text/javascript" >

// Register all links with-flash as fancybox's
$(document).ready(function() {
 $("a.overlay-flash").fancybox({
 'padding': 0,
 'zoomOpacity': true,
 'zoomSpeedIn': 500,
 'zoomSpeedOut': 500,
 'overlayOpacity': 0.75,
 'frameWidth': 530,
 'frameHeight': 400,
 'hideOnContentClick': false
 });
});

// This function allows you to call the fancy box from javascript
// origional source: http://outburst.jloop.com/2009/08/06/call-fancybox-from-flash/
function callBoxFancy(my_href) {
var j1 = document.getElementById("hiddenclicker");
j1.href = my_href;
$('#hiddenclicker').trigger('click');
}

// Call the Fancy Box 3 seconds after the page loads
$(document).ready(function() {
 window.setTimeout("callFancyBox('http://google.com');", 3000)
});
</script>

</body>
</html>

I learned some interesting things about financing retirement yesterday.

I’ve been reading a financial book a friend recommended to me preaching the common methods on “how to get rich the safe way” through managing your 401(k) and IRA’s and such.  It’s somewhat helpful in recommending what banks to use and laying out all of the details of each of the methods of saving and recommending a path.

At the beginning of the book there’s this table similar to many others I’ve seen.  It looks like this:

Smart Sally Dumb Dan
When beginning to invest the person is 25 years old 35 years old
Each person invests $100 for… 10 years 30 years
At 8% interest at 65 they have $200,061 $149,036

The basic gist is, starting young is incredibly beneficial. If the goal was to get to 200k Smart Sally could just stop saving for retirement at the age of 35 where Dumb Dan, at the same rate spent 30 years trying to make it and never did.

When can I be done?

Which got me thinking: what would I have to do to one day just be done saving for retirement? I wrote a script to determine just that. It ignores a few minor details which are probably negligible anyway when compared to the error margins in the assumption of inflation and assumed investment return, but gives good ballpark results.

I assume I’d like to retire on an income of today’s equivalent of 50k after tax which is completely generated by the interest earned on my retirement savings. I assume I can maintain an 8% average earnings and account for a 3% inflation rate.

Without Further delays, here are the results.

Welcome to the Reverse Financial Calculator!

$50,000.00/year is $158,351.35/year when you are 65 assuming 3% inflation
To make that much money from an 8.00% interest you need $1,979,391.86 

You can stop saving up if you reach a total savings of one of the
values in the STOP AT at the corresponding AGE.  If you want to hit
that goal starting from 0 you need to START PAYING the listed value per year.

AGE     STOP AT       START PAYING
26       $98,402.32  $98,402.32/year
27      $106,274.50  $51,093.51/year
28      $114,776.46  $35,355.00/year
29      $123,958.58  $27,508.99/year
30      $133,875.26  $22,819.90/year
31      $144,585.28  $19,709.20/year
32      $156,152.11  $17,500.34/year
33      $168,644.28  $15,855.05/year
34      $182,135.82  $14,585.38/year
35      $196,706.68  $13,578.56/year
36      $212,443.22  $12,762.81/year
37      $229,438.68  $12,090.27/year
38      $247,793.77  $11,527.81/year
39      $267,617.27  $11,051.75/year
40      $289,026.65  $10,644.72/year
41      $312,148.79  $10,293.69/year
42      $337,120.69  $9,988.69/year
43      $364,090.34  $9,721.98/year
44      $393,217.57  $9,487.41/year
45      $424,674.98  $9,280.09/year
46      $458,648.97  $9,096.04/year
47      $495,340.89  $8,932.02/year
48      $534,968.16  $8,785.34/year
49      $577,765.62  $8,653.75/year
50      $623,986.87  $8,535.38/year
51      $673,905.82  $8,428.63/year
52      $727,818.28  $8,332.13/year
53      $786,043.74  $8,244.74/year
54      $848,927.24  $8,165.44/year
55      $916,841.42  $8,093.36/year
56      $990,188.74  $8,027.74/year
57     $1,069,403.83  $7,967.93/year
58     $1,154,956.14  $7,913.33/year
59     $1,247,352.63  $7,863.45/year
60     $1,347,140.84  $7,817.81/year
61     $1,454,912.11  $7,776.03/year
62     $1,571,305.08  $7,737.74/year
63     $1,697,009.49  $7,702.62/year
64     $1,832,770.24  $7,670.38/year
65     $1,979,391.86  $7,640.77/year

Pretty cool right? The results say that if I pay about 20k a year I can stop putting money away for retirement in just 6 years, making my last payment when I’m 31.

When to Start

I also was interested in seeing what it would take to do that same 6 year goal if I put off starting the plan. You can see from the following results that putting it off does have significant cost to it.

START      STOP    PAYMENTS
26           31    $19,709.20/year
27           32    $20,665.96/year
28           33    $21,669.16/year
29           34    $22,721.06/year
30           36    $23,824.02/year

So the plan would cost about 1k more per year for every year I put it off.  That’s a lot, but it doesn’t necessarily mean you should put everything aside and go out and start saving ASAP.  As long as whatever you’re doing, that’s not allowing you to put significant money away is increasing your probable future income by atleast 3k (post tax) per year you should be in good shape.

People who work towards a PhD or an for example MD are not able to save much but will be able to handle that extra 4k/year easily once they’ve got a much higher paying job (schooling aside).

Anyone interested in trying it out themselves or modifying the results slightly can grab the source here.

Fun Fact:

If your parents had gone cheap with your baby clothes, crib and stroller and instead put 10k a year away from the day you were born, you’d be set for retiring at 65 on just your 2nd birthday!

AGE    STOP AT
0       $28,691.65  $28,691.65/year
1       $30,986.98  $14,897.59/year
2       $33,465.94  $10,308.63/year

There are a lot of advantages to using a distributed revision control system as opposed to centralized. There are also some down sides, mostly in that it can be a bit more confusing to manage. A centralized web interface however seems to solve that problem quite nicely.

Currently there are two major competitors in that space, Github and Bitbucket, that manage Git and Mercurial repositories respectively. I’ve used Github for a while and recently have really started using Bitbucket and I thought I’d share some thoughts and comparisons of the two. There are a lot of blogs that compare Git and Mercurial, but this is not one of those blogs. I personally have a preference for mercurial, but for admittedly stupid reasons like it was written in python and the syntax more closely resembles SVN which I’ve become used to. To me, a major differentiator in choosing one over the other is the interface or ‘hub’ that manages it.

Below is my item by item lineup showing the differences between GitHub and Bitbucket

GitHub BitBucket
pull requests yes yes
forking yes yes
command instructions yes yes
distributed revision control yes yes
network graph yes no
inline editing yes no
developer profiles yes yes
wiki yes yes
issues yes yes
rss feeds yes yes
source browser yes yes
monthly price cheap cheaper
free private projects 0 1
# of users lots not lots
happy hours in SF no

There really aren’t a lot of differences.  Personally, I actively use both. Learning both is easy as the syntax difference between the two revision control systems is minimal and both hub interfaces are very similar.

I use git for open projects like TwitTornado where privacy is not an issue and where I’d like to get more developers involved. Its really got a fantastic social network of hackers.

I use bitbucket for my private repositories as it allows 1 free private repository (per user) and has cheaper monthly rates.

I hope this helps others make a decision between the two and try both of them out. Let me know in the comments if I’ve left any parameters out of the table above and I’ll add them in.

Follow

Get every new post delivered to your Inbox.