Next Spaceship

Let's drive it into the future

A Python Implementation of Segment Tree

| Comments

Today I need to use a data structure called Segment Tree. A segment tree for a set I of n intervals uses O(n log n) storage and can be built in O(n log n) time. Segment trees support searching for all the intervals that contain a query point in O(log n + k), k being the number of retrieved intervals or segments.

While I search for a Python implementation of segment tree, there is no good ones. So I write one. Here is the code.

Disable Third-Party Cookies

| Comments

More and more websites and abusing web cookies to track users and get private information which they shouldn’t get. Though we can trust some websites, like Google, to coollect user info for analytics and providing better service, we can not trust all websites, especially some in bad reputation. Some company even sell user info. So we have to protect ourselves.

To disable 3-party cookies if one of the most effective way to protect your privacy when surfing the web. How to do it? Follow me and I will guide you.

Disable 3-party cookies in your browser

Here we use Chrome as an example. Go to the setting page, and click Show advanced settings....

Find Privacy, and click Content settings... button.

Check Block third-party cookies and site data as the following screenshot shows and click Done.

Disable Cookies Screenshot

Delete and Disable flash cookies

Flash cookie, also known as Local Shared Object, is exactly like a browser cookie, except that it can also store data more complex than simple text.

Unlike regular Web cookies, which can be disabled via your browser preferences, Flash cookies can only be accessed through the Adobe Flash Player Settings Manager. For Mac users, you can find it in System Preferences.

Do as the following picture shows. Choose Ask me before allowing new sites to save information on this computer or Block all sites from storing information on this computer.

Disable Flash Cookie

Don’t forget to Delete all existed flash cookies.

If you want to delete all flash data from your hard drive, they are under folder:

  • Windows –> C:\Documents and Settings[username]\Application Data\Macromedia\Flash Player
  • Macintosh OSX –> /Users/[username]/Library/Preferences/Macromedia/Flash Player

GitHub Workflow for Submitting Pull Requests

| Comments

The original source of this document is from OpenShift.

This document describes the recommended workflow to maintain a fork of a GitHub repository and how to submit pull requests for new features or bug fixes. The examples used on this page can be used for any GitHub repository.

Forking a repository

Before you can begin editing code you must first create a fork of the GitHub repository you wish to contribute to.

Navigate to the GitHub page of the repository you wish to fork. Click on the fork button on the top right corner of the page.

This creates a copy of the repository in your own GitHub space.

Cloning your fork

Clone the repository from your fork

1
2
git clone git@github.com:<username>/<repo-name>.git
git clone git@github.com:kraman/crankcase.git

Add the upstream repo so that you can pull changes

1
2
git remote add upstream <original repo git url>
git remote add upstream git@github.com:openshift/crankcase.git

Working on a topic branch

Always try to avoid working on the master branch. It usually results in merge conflicts and/or update issues. Instead, work on a bug/feature/topic branch whenever possible.

1
2
3
4
#start from the master branch
git checkout master
#create a new branch
git branch dev/kraman/bug/12345
1
2
3
#switch to the new branch
git checkout dev/kraman/bug/12345
#...make changes...

Staying updated

Once a fork has been created, it does not automatically track the upstream repository. Follow the steps outlined below to keep the master branch up-to-date.

1
2
3
#pull the latest changes from upstream
git fetch upstream
git fetch upstream --tags
1
2
#make sure there are no un-committed changes
git stash
1
2
#make sure we are working on the master branch
git checkout master
1
2
#merge the latest changes
git merge upstream/master
1
2
3
#push the updates changes to our GitHub fork
git push origin master
git push origin --tags
1
2
#return to your bug/feature branch
git checkout dev/kraman/bug/12345
1
2
#rebase this branch onto latest code from master (expect conflicts)
git rebase master
1
#... resolve conflicts
1
2
#push the rebased branch back to your fork
git push origin dev/kraman/bug/12345 -f
1
2
#Restore any un-committed changes
git stash pop

NOTE: The git stash steps are optional. It is easier if you commit all changes before attempting a rebase.

Submitting a patch

Once your code is ready to be submitted, you will need to submit a pull request with your changes.

  • Update your branch and make sure you are rebased off the latest upstream/master
  • Squash your commits onto a single revision
  • Submit a pull request on GitHub

Squashing your changes into one revision

Before you can submit a request, rebase all your changes on to a single commit. This makes it easier to review and also makes reverting the code easier in case of any build breakages.

1
2
git rebase -i master
#... squash commits ...

Creating a pull request

GitHub instructions on creating a pull request

If you need to make changes to your commit after a pull request has been issues, you can go back to the pull request page and update the commit range rather than submit a new pull requests.

Reasons to Start a Website

| Comments

Many people dream about starting a website. Having their own space on the web they can call their own. There are lots of reasons you should give it a go. Even if you are not particularly great with technology, you’ll still be able to make one.

You learn something new

Building a website from scratch is not as easy as ABC or 123. Nevertheless the time and effort it takes to learn how create one, is well worth learning. You might not be at the level to make a site like http://www.partypoker.com/ for a while, but you’ll get there. When you consider how much business there is on the internet in 2013, it’s clear that knowing how to make a website is an invaluable skill which could benefit you greatly.

Keeping in touch

A website of your own can be a great way to keep in contact with people you actually know. Distant family members will have a space to catch up on what you are doing with your life, and maintain communication with you. These types of websites are very simple spaces for uploading photos, writing about experiences, and even video diaries.

Meeting people

Blogs and personal websites are a great way to meet like-minded individuals from all over the world. Perhaps your website is about your favourite hobby, in which case you will attract people who like the same thing. A website can literally be about whatever you want it to be. Joining a web-ring on your particular topic of choice, will ensure more people like you find your blog.

Web presence for your business

If you want to find someone to do some work for you, where do you look? The internet. There’s no greater promotion for your business than a website, especially if you are the one controlling it, because it’ll say exactly what you want it to. You can tell people what you do, where you do it, how you do it and how people can contact you, just from a few relevant pages.

How to Set Up an OpenGL Project With XCode

| Comments

If you search this topic, you find few people are talking about this. Why? because it’s too easy to set up an OpenGL project for Mac. But I still write this article, because maybe someone are finding something like a quick start.

Unlike other platforms, in Mac, you don’t need to install OpenGL independently. To use OpenGL in Mac, you need to install XCode first.

Download & Install XCode

Download XCode freely from apple’s offical website: http://developer.apple.com/xcode/.This may cost you some time, because XCode is huge.

Create a Project

Create a new OS X Application. Choose Command Line Tool.

OpenGL with XCode step 1

Input a product name.

OpenGL with XCode step 2

Then choose a location for you project and click Next.

The Formula of Annual Leave Days

| Comments

Company D recently changed the policy of annual leave days, which confused many people. So I make up these mathematical formulas to make it more clear.

Assume Amy’s entry date is year x0, month y0, day z0, then F(x), the annual leave days for year x is

formular 1

The current available annual leave days G(x, y, z), for year x, month y, day z, is

formular 2

The above forumlas are edited with LaTeXiT.

The source code is here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
$F(x) = \frac{\lfloor2(f(x) + \frac{1}{4})\rfloor}{2}$


$f(x) =
\begin{cases}
\frac{(x - x_0 + 9)(y_0 - 1) + (x - x_0 + 10)(12 - y_0 + 1)}{12}, &\text{if $x > x_0$;}\\
\frac{10(13 - y0)}{12},                                           &\text{if $x = x_0 , z_0 <= 15$;}\\
\frac{10(12 - y0)}{12},                                           &\text{if $x = x_0, z_0 > 15$;}\\
\end{cases}
$


$G(x, y, z) = \frac{\lfloor2(g(x, y, z) + \frac{1}{4})\rfloor}{2} + M(x, y, z) - N$

$g(x, y, z) = 
\begin{cases}
\frac{f(x)(y - 1)}{12},               &\text{if $x > x_0$, $z$ is not the last day of month $y$;}\\
\frac{f(x)y}{12},                     &\text{if $x > x_0$, $z$ is the last day of month $y$;}\\
\max(0, \frac{10(y - y_0 - 1)}{12}),  &\text{if $x = x_0$, $z0 > 15$, $z$ is not the last day of month $y$;}\\
\frac{10(y - y_0)}{12},               &\text{if $x = x_0$, $z0 <= 15$, $z$ is not the last day of month $y$;}\\
\frac{10(y - y_0)}{12},               &\text{if $x = x_0$, $z0 > 15$, $z$ is the last day of month $y$;}\\
\frac{10(y - y_0 + 1)}{12},           &\text{if $x = x_0$, $z0 <= 15$, $z$ is the last day of month $y$;}\\
\end{cases}
$

$M(x, y, z) =
\begin{cases}
G(x - 1, 12, 31),   &\text{if $(y, z) <= (6, 30)$;}\\
0,                  &\text{if $(y, z) > (6, 30)$;}\\
\end{cases}
$

$N = \text{Annual leave days have been used for this year}$

Django + uWSGI + Nginx

| Comments

If you haven’t installed virtualenv, do it first.

Install Django, uWSGI, Nginx

Create a virtual environment, and activate it

1
2
virtualenv en1
source en1/bin/activate

Use pip to install Django and uWSGI

1
2
pip install django
pip install uwsgi

Install Nginx

Use your package manager to install Nginx, like apt-get install nginx, yum install nginx, or emerge nginx.

Configuration

Django and uWSGI

Since Django is a WSGI already, it’s easy to configure. What you need to do is creat a uwsgi.ini file. Here is an example.

1
2
3
4
5
6
7
8
9
[uwsgi]
socket=/var/run/uwsgi.sock
virtualenv=/home/sunliang/en1/
chdir=/home/sunliang/mysite
module=mysite.wsgi:application
master=True
workers=8
pidfile=/home/sunliang/mysite/uwsgi-master.pid
max-requests=5000

Migrated From Wordpress to Octopress

| Comments

Why?

Compared with Wordpress, Octopress has a lot of overwhelming advantages.

  • No database, so you don’t need to back up database, and you can use github or bitbucket to control your all files.
  • No dymaic page, so you don’t PHP server. All you need is to set up Nginx. This allows more concurrent access to your website.
  • Posting more effitiently. I like the process and interaction of posting an article with Octopress. Use command rake new_post["BLOG TITLE"] to generate a template, and with any editor like emacs or vim to edit my article. Use rake generate to generate html file according to the source file, and then rake deploy to deploy to the server. Don’t you think that’s so cool? Don’t forget to commit to your version control system.
  • Markdown format. This is a format easier to write and read than html for human being. Use it. Trust me.

How?

If you start a new blog, just use Octopress, but if you already have a wordpress blog like me, it’s not so easy to migrate to Octopress. At least don’t forget to do the following things:

  • Use exitwp to convert the XML file, which is exported from wordpress, to Markdown format files.
  • Use Disqus to manage comments in your blog.
  • Modify code block. http://octopress.org/docs/blogging/code/
  • Define each post with at most one category, if you prepare to configure your blog urls with categories as a part.
  • Write a script to check each link in your blog valid. Use Nginx rewrite to forward old links to new links. Also write a URL Maper file (mapping from old links to new links) and submit it to Disqus, where there is a migratino tool to do such things.
  • Use Google Analytics and Google Webmaster Tools to minitor your website. Correct warnings and errors they inform you.

Orion’s Belt

| Comments

I suddenly realize new year is coming. It’s another year. I want to look at the Orion’s belt to make sure it is coming and learn how far is it, because when the three stars of Orion’s belt are in a line, it’s the start a new year. But when I raise my head and look up at the sky, I find nothing except polluted air.

How to Add Pinterest, Google +1, Facebook Like, Tweet to Prestashop

| Comments

First, add the following code to where you want to show Pin it button, Google +1 button, Facebook like button and Tweet button (for example, in the product.tpl file):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!-- Pinterest Pin it button -->
<div>
    <a href="<a href="http://pinterest.com/pin/create/button/?url={">http://pinterest.com/pin/create/button/?url={</a>$link->getProductLink($product->id, $product->link_rewrite)}&media={$link->getImageLink($product->link_rewrite, $cover.id_image, 'large')}&description={$product->name|escape:'htmlall':'UTF-8'}" class="pin-it-button" count-layout="horizontal" always-show-count="true"><img border="0" src="//assets.pinterest.com/images/PinExt.png" title="Pin It" /></a>
</div>

<!-- Facebook like button -->
<div>
    <div class="fb-like" data-href="{$link->getProductLink($product->id, $product->link_rewrite)}" data-send="false" data-layout="button_count" data-width="100" data-show-faces="true"></div>
</div>
<!-- Twitter tweet button -->
<div>
    <a href="<a href="https://twitter.com/share"">https://twitter.com/share"</a> class="twitter-share-button" data-lang="en" data-url="{$link->getProductLink($product->id, $product->link_rewrite)}" data-text="{$product->name}">Tweet</a>
</div>
<!-- Google +1 button -->
<div>
    <g:plusone href="{$link->getProductLink($product->id, $product->link_rewrite)}" size="medium"></g:plusone>
</div>