AngularJS tutorial

Here is a very nice step-by-step intro tutorial for AngularJS, from the AngularJS official site. You will see how the angularjs code is added to the sample project and for what purpose. I highly recommend this tutorial for anyone wanting to try out AngularJS.

http://docs.angularjs.org/tutorial

At the end of the tutorial, there are more useful links for development. For example, the angular-seed project, and the AngularJS cookbook. Also, try out this ng-boilerplate project to kickstart your AngularJS projects.

http://docs.angularjs.org/tutorial/the_end

P.S. This http://www.egghead.io/ is also an awesome tutorial site!

PHP on Google App Engine

You can follow the official install page to install Google App Engine for PHP, or follow my steps here. I am using osx 10.8.3 and I use brew instead of macport.

First you will need to get php 5.4. What you have is probably 5.3. You can verify by php -v, and something like this comes out.

PHP 5.3.15 with Suhosin-Patch (cli) (built: Aug 24 2012 17:45:44) 
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies

That’s the default/latest php version for osx 10.8.3. To install php 5.4 using brew, according to https://github.com/josegonzalez/homebrew-php, do the following.

brew tap homebrew/dupes
brew tap josegonzalez/homebrew-php
brew install php54

You may run into permission issues when running brew install php54. You will need to use chown and chgrp to give brew access to some of those directories under /usr/local/.

After installing php54, double check by issuing ls /usr/local/bin/php*. You should see php and php-cgi there. Run /usr/local/bin/php-cgi -v and you should see something like the following.

PHP 5.4.15 (cgi-fcgi) (built: May 15 2013 18:27:50)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies

Confirming it’s 5.4. Then move on.

Download and unzip the SDK in your home directory. (or you can pick any other directory, doesn’t matter).

cd
curl -O http://commondatastorage.googleapis.com/appengine-php/appengine-php-sdk-1.8.0.zip
unzip appengine-php-sdk-1.8.0.zip

You will then have the google_appengine folder. Here, to avoid confusion in the future, I prefer to rename this folder in case you may do app engine programming in other languages.

mv google_appengine google_appengine_php

Now we can write and test the hello world program. Create a directory for your app.

mkdir helloworld
cd helloworld

Then create the app.yaml file. This is standard for any app engine projects. For yaml reference look here.

application: helloworld
version: 1
runtime: php
api_version: 1

handlers:
- url: /.*
  script: helloworld.php

Then create your helloworld.php file.

<?php
  echo 'Hello, World!';

Yea, it’s without the closing ?>. I am not sure why either. But if you add the closing tag back in it will work as well.

Now, issue the following command to start your web server.

~/google_appengine_php/dev_appserver.py --php_executable_path=/usr/local/bin/php-cgi ~/helloworld/

If everything goes well, you should see your hello world text at http://localhost:8080/.

If you are thinking about deploying your app, make sure you create your app at https://appengine.google.com/ and go to https://gaeforphp.appspot.com/ to register first. The waitlist is building up. I see 73 applications on the list as of now May 15th, 2013 at around 7:30pm. This registration process hopefully will go away soon.

One more thing, there are more php54 libraries. See the list by brew search php54. They may be useful/crucial for your new app!

Happy app engining!

AdMob error

I got this mysterious runtime error below while trying to integrate AdMob into my xcode project.

2013-03-21 22:02:32.646 my_app[41152:c07] *** Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘-[GADObjectPrivate changeState:]: unrecognized selector sent to instance 0x95945e0’
*** First throw call stack:
(0x19a6012 0x1719e7e 0x1a314bd 0x1995bbc 0x199594e 0x217d2 0x2307e 0x20d87 0x90ce 0x6248c3 0x624323 0x632668 0x621b73 0x172d6b0 0x62d7b3 0x62f264 0x62feb8 0x1e7a53f 0x1e8c014 0x1e7c7d5 0x194caf5 0x194bf44 0x194be1b 0x27ae7e3 0x27ae668 0x66165c 0x2cbd 0x2be5)
libc++abi.dylib: terminate called throwing an exception

It turns out there is a flag to be added… argh…

You can read more from this link, or just add the -ObjC flag like the screenshot shows.

admob_xcode_objc_setting

Keep pressing on!

Excel to JSON

Here is my little side project to convert excel files to json objects. I mainly used the Google App Engine and the Python xlrd module.

Feel free to try it out at http://excel2json.appspot.com/.

curl -F f=@my_file.xls http://excel2json.appspot.com/2json

All you need is just one line of code!

Working with Excel files in Python

The libraries needed are: xlutils, xlrd and xlwt. They are all under the package called xlutils. So just issue the following command.

sudo pip install xlutils

To verify:

localhost:~ birdchan$ python
Python 2.7.2 (default, Jun 16 2012, 12:38:40) 
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import xlutils
>>> 

Should have no error messages.

Trigger a click event on a legend item in Highchart

Sometimes you have a few series in your chart and you want to turn off some of them, maybe because they are too big thus skewing the rest of the data. See below for example, the green bar is just too long compared to the other two.

highchart_bar_legend_on

We know that mouse-clicking on a legend item can turn series on and off. Here after clicking on the green legend item, we see:

highchart_bar_legend_off

To do that programmatically, here is how you do it.


var chart;
$(document).ready(function() {
  chart = new Highcharts.Chart({
    // blah blah blah...
  });

  // auto click on the 3rd legend label to disable that series from showing up
  $(chart.series[2].legendItem.element).trigger('click');
});

Datatable clear filtering

Here is how to clear all filtering in datatable.

function fnResetAllFilters() {
  var oSettings = oTable.fnSettings();
  for(iCol = 0; iCol < oSettings.aoPreSearchCols.length; iCol++) {
    oSettings.aoPreSearchCols[ iCol ].sSearch = '';
  }
  oTable.fnDraw();
}

So to clear just one filter, do:

function reset_filter_with_index(iCol){
  var oSettings = oTable.fnSettings();
  oSettings.aoPreSearchCols[ iCol ].sSearch = '';
  oTable.fnDraw();
}

Ref link: http://www.datatables.net/forums/discussion/997/fnfilter-how-to-reset-all-filters-without-multiple-requests./p1

splitting and joining huge files

Today I wanted to sftp a 10Gb file over the wire. Knowing that the transfer would fail in the middle of it, I decided to split this file into small pieces beforehand. Below I think is the easiest way on osx.

// to split my huge file into 100mb files, having the prefix "bak_"
split -b 100m my_huge_file.ext bak_

// to re-construct my huge file
cat `ls bak_*` > my_huge_file2.ext

After running the split command, you will see some files named bak_aa, bak_ab, etc in your directory. These are the chucks of your original huge file. So just transfer these small bak_* files over the wire.

sftp doesn’t have mput… sigh…

Also the original file name is gone missing in the process… sigh… That’s unfortunate. What I decided to do is to write up a small readme file to store the above two commands, so later on I know what output filename to use when running the second command.

PHP explode

Say you store a csv formatted string somewhere to represent an array. For example, Johnny has items “apple|orange|banana”. Then in your code, you may parse out the items as follows:

$items = "apple|orange|banana"; // retrieve it from somewhere
$my_arr = explode("|", $items);
if (count($my_arr) > 0){
  print "Johnny has something.";
}else{
  print "Johnny has nothing.";
}

It works fine as it seems.

Well, actually, you will never see the “Johnny has nothing” print out. Surprisingly, even if $items is an empty string, explode will still return an array with one element. That array is:

Array
(
  [0] =>
)

Sigh… to avoid this, if you have to use the csv format, then add an if statement to double check the string length.

$items = "apple|orange|banana"; // retrieve it from somewhere
if ($items != ""){
  $my_arr = explode("|", $items);
  print "Johnny has something.";
}else{
  print "Johnny has nothing.";
}

Best practice I think is to store such an array in json format. That way you can completely avoid this surprise!

Ref: http://www.php.net/manual/en/function.explode.php#99830

Formatting mysql datetime with php

Very useful, so posting here. Read the php date function page for more details.

$mysql_datetime = date("Y-m-d H:i:s");

Ref: http://stackoverflow.com/questions/136782/format-mysql-datetime-with-php