Tuesday, September 18, 2012

Python unittest, assertEquals V.S. assertTrue

It's better that using assertEquals than assertTrue since that it gives better feedback which shows the first and the second parameters and shows how it different. For assertTrue, it only shows that true or false.

Tuesday, July 31, 2012

View source code in Safari 6 (Mountain Lion)

Safari6 hides some development tool such like "view source code" by default. To enable that, go to

  • Preferences 
  • => Advanced Settings 
  • => enable "Show Develop menu in menu bar".

After that, Develop tab will be shown in menu bar, of course you can view source code now.

Friday, July 27, 2012

Enable gcc command line tools for Mountain Lion

Seems Mountain Lion (Xcode 4.4) disable gcc command line tools by default.
Followings are the way to install it:

Xcode -> Preferences -> Download -> Components -> Command Line Tools -> install

Monday, June 25, 2012

SSH login without password

Every time using SSH to login, we should type enter our passwords in to login successfully. Here is a method which make SSH login without entering password.

First, use ssh-keygen to generate the public/private RSA key pair

willy@LOCAL_COMPUTER:~$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/willy/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/willy/.ssh/id_rsa.
Your public key has been saved in /home/willy/.ssh/id_rsa.pub.
The key fingerprint is:
8c:ee:fc:2f:62:b8:84:78:43:0b:1b:d7:7b:72:32:cd willy@willy-System-Product-Name
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|                 |
|                 |
|   .   o         |
|o o . . S        |
| B o =           |
|o = *.E          |
| . o.Oo .        |
|    .ooo.o.      |
+-----------------+

Second, copy the public key ~/.ssh/id_rsa.pub to the remote server

willy@LOCAL_COMPUTER:~$ sftp willy@REMOTE_SERVER
sftp> put id_rsa.pub
sftp> quit

Third, login the REMOTE_SERVER and append the id_rsa.pub to ~/.ssh/authorized_keys

willy@LOCAL_COMPUTER:~$ ssh willy@REMOTE_SERVER
willy@REMOTE_COMPUTER:~$ cat id_rsa.pub ~/.ssh/authorized_keys
willy@REMOTE_COMPUTER:~$ exit

Finally, login again, this time, we need not to enter the password.






Tuesday, June 19, 2012

Conversion between NTP time and UNIX struct timeval

First, let's understand the formats:
  • A NTP timestamp consists of a 32-bit part for second and a 32-bit part for fraction second. And the unit of the fraction part is 2-32 second.
  • A UNIX Struct timeval consists of a 32-bit part for second and a 32-bit part for micro-second.
Second, let's see the meaning of NTP timestamp and UNIX struct timeval
  • A NTP timestamp is defined as the number of seconds have elapsed since Jan 1, 1900.
  • A UNIX struct timeval is defined as the number of seconds have elapsed since Jan 1, 1970.
Assume the structure of NTP is
struct ntp_time_t {
    uint32_t   second;
    uint32_t    fraction;
};

And the knowned timeval structure of UNIX is
struct timeval {
    uint32_t   tv_sec;
    uint32_t    tv_usec;
};

To convert from NTP timestamp to UNIX timeval, we need to offset the second field and
transform the  fraction part:
void convert_ntp_time_into_unix_time(struct ntp_time_t *ntp, struct timeval *unix)
{
    unix->tv_sec = ntp->second - 0x83AA7E80; // the seconds from Jan 1, 1900 to Jan 1, 1970
    unix->tv_usec = (uint32_t)( (double)ntp->fraction * 1.0e6 / (double)(1LL<<32) );
}

void convert_unix_time_into_ntp_time(struct timeval *unix, struct ntp_time_t *ntp)
{
    ntp->second = unix->tv_sec + 0x83AA7E80;
    ntp->fraction = (uint32_t)( (double)(unix->tv_usec+1) * (double)(1LL<<32) * 1.0e-6 );
}

Example:
#include <sys/time.h>
#include <stdio.h>
#include <stdint.h>

struct ntp_time_t {
    uint32_t   second;
    uint32_t    fraction;
};

void convert_ntp_time_into_unix_time(struct ntp_time_t *ntp, struct timeval *unix)
{
    unix->tv_sec = ntp->second - 0x83AA7E80; // the seconds from Jan 1, 1900 to Jan 1, 1970
    unix->tv_usec = (uint32_t)( (double)ntp->fraction * 1.0e6 / (double)(1LL<<32) );
}

void convert_unix_time_into_ntp_time(struct timeval *unix, struct ntp_time_t *ntp)
{
    ntp->second = unix->tv_sec + 0x83AA7E80;
    ntp->fraction = (uint32_t)( (double)(unix->tv_usec+1) * (double)(1LL<<32) * 1.0e-6 );
}

int main()
{
    struct ntp_time_t ntp;
    struct timeval unix;

    // get time unix time via gettimeofday
    gettimeofday(&unix, NULL);
    printf("UNIX Time: %ld %ld\n", unix.tv_sec, unix.tv_usec);

    // convert unix time to ntp time
    convert_unix_time_into_ntp_time(&unix, &ntp);
    printf("NTP Time: %ld %ld\n", ntp.second, ntp.fraction);

    // convert ntp time back to unix time to see whether they are same
    convert_ntp_time_into_unix_time(&ntp, &unix);
    printf("UNIX Time: %ld %ld\n", unix.tv_sec, unix.tv_usec);
}

Output:
UNIX Time: 1340097242 933680
NTP Time: 3549086042 4010129359
UNIX Time: 1340097242 933680

Tuesday, June 5, 2012

Cursor Movement in Mac Terminal (Home and End Key in Linux)

In Mac Terminal, there is no default keyboard shortcut to move your cursor  back to the start of the line or forward to the end of the line. Let's customize our own keyboard shortcuts.

In this example, we will setup two new shortcuts.

  • "shift + cursor left" to back to the start of the line
  • "shift + cursor right" to forward to the end of the line

First go to Terminal => Preference
and choose the Keyboard Tab.

Hit the + button, set followings:

  • Key: cursor left
  • Modifier: shift
  • Action: send string to shell:
  • \001

Note that we should hold ctrl button and press a to produce \001 .
press OK.

Again, hit the + button, set followings:

  • Key: cursor right
  • Modifier: shift
  • Action: send string to shell:
  • \005

This time, hold ctrl button and press e to produce \005 .
Press OK.

Now, return to your terminal, and enjoy your new keyboard shortcuts.

Wednesday, May 23, 2012

Setup Your Own Git Server In 5 Minutes

Sometimes, we need to build up a small project together with our co-workes.
But the modules in this project are highly dependent, it might takes much time to integrate our codes.
It’s a good idea to create a simple git server to help to handle that annoying work.

First, login our remote server, and then create a git user:
$ ssh root@REMOTE_SERVER
$ sudo adduser git
$ exit

Secondly, login as git, create a repository
$ ssh git@REMOTE_SERVER
$ mkdir test.git
$ cd test.git
$ git --bare init
$ exit

Now, you can commit your local codes to the remote git server
$ mkdir test 
$ cd test
$ git init
$ touch README
$ git add README
$ git commit -m "first commit"
$ git remote add origin git@REMOTE_SERVER:test.git
$ git push origin master

Finally, ask your co-worker to clone it!
$ git clone git@REMOTE_SERVER:test.git