misc quick notes

JRE: Java runtime environment, consists of JVM (abstract computing machine, have an instruction set that uses memory), Java platform core classes, supporting Java platform libs.

JIT: just in time compiler, improvement execution performance, especially for long running, compute-intense programs. If JIT env var is on, JVM passes .class file to JIT. JIT compiles the bytecode to native code (e.g. Intel machine code) for the platform on which it is running.

Native code: the code that is compiled to run with a particular processor and its set of instructions. Unmanaged code: program that get directly compiled into machine code, like C, C++. Managed code, program runs in a virtual environment, like Java, C#.

CGI files: common gateway interface, generate dynamci content on web pages and web applications. When implemened on web servers, provide an interface between web servers and programs (CGI scripts) that generate the web content. Containing code in perl, etc.

RSA: public key cryptographic algorithm. Bases on the difficulty of interger factorization of large numbers.

Open window from command line in Ubuntu: nautilus .

Read input from stdin c++: 

string s = "";
getline(cin, s); // Read entire line. Allow spaces
// Or
cin >> s; // read a word. Do not allow spaces.

Primary data types are allocated on the stack and dynamic data types are allocated on the heap. A char array is allocated on the stack, if return it from a function, the user that uses this char array may not see it because after function, the stack is cleared out. 

Determine if two integers have the same sign: (dividend ^ divisor) >> 31. 

1 GB = 2^10 MB = 2^20 KB = 2^30Byte. 4 GB is about 4 billion (2^32) bytes.

P and NP: P–>An exclusive club a problem can join if there exists a polynomial-time algorithm to solve it. (shortest path, minimum spanning tree). NP–> A less exclusive club for algorithmic problems whose solutions can be verified in polynomial-time. (TSM, satisfiability, vertex cover) (P get free pass to NP).

Lexicographical ordering: First, sort by length; Then, sort by dictionary order.

Bit shift in C++: bit shift returns a value.

int n = 1;
n = n << 1; // Correct, multiple n by 2, thus, n is 2
n << 1; // Won't update n, n is still 1

Copy constructor in c++: ClassName(const ClassName& otherObj); . Since pass by reference, members can be access using “.” . Such as int x = otherObj.value;
For class with pointers data members, usually
— Include destructor;
— Include copy constructor;
— Overload assignment operator;

Polymorphism Java vs C++: Java: routines are overridable by default. Routine must declared as final to avoid derived classes overriding it. C++: routines are not overridable  by default. Routine must declared as virtual in base class to be overridable.

Abstraction vs Encapsulation: Abstraction: manage complexity by providing models that allows you to ignore implementation details. Encapsulation: prevents you from looking at details even if you want to. Friend classes violate encapsulation.

Abstract Data Type: a collection of data and operations that work on that data.

Asynchronous: Events occur independently of the main program flow. Actions executed in a non-blocking scheme, allowing the main program flow to continue processing (AMD). Synchronous vs asynchronous: a synchronous operations block a process until operation is finished. An asynchronous operation is non-blocking and only initiate the operation.

Pass by reference and by value in Java: Java does manipulate objects by reference, and all object variables are references. However, Java doesn’t pass method arguments by reference; it passes them by value.

StringBuffer: thread safe (manipulating shared data structure guarantee safe execution of multiple threads at the same time). StringBuilder: non thread safe. Both create immutable strings in Java.

Better rounding: add 0.5 before truncating to nearest integer results a better rounding. Instead 9.99->9, we have 9.99 + 0.5 -> 10.

Endless loop: for(;;) { statement; }

Cross Compiler: a compiler that is capable of creating executable for a platform other than the one on which the compiler is running. Often used to compile code for embedded systems since it is infeasible to do the compiling on such systems.

Native code (machine code): atomic instructions (operations,  jump) executed directly by a CPU. However, bytecode is executed by an interpreter.

Interpreter: a computer program that executes instructions written in a programming language.  1) executes source code directly (Lisp); 2) translates source to efficient intermediate code and execute (Perl, Python, Matlab, Ruby); 3) execute precompiled code.

Memory leak occurs:

  • when a compute program consumes memory but is unable to release it back to the operating system.
  • when an object is stored in memory but cannot be accessed by the running code.

Check if x is power of 2:

int isPowerOfTwo (unsigned int x)
while (((x % 2) == 0) &amp;amp;amp;amp;&amp;amp;amp;amp; x &amp;amp;amp;gt; 1) /* While x is even and &amp;amp;amp;gt; 1 */
x /= 2;
return (x == 1);

In order to include certain jar files for java to use, you need to set environment variable CLASSPATH to include the path to that jar file.
export CLASSPATH=~/myjarfile.jar:$CLASSPATH


  • Formal parameter: the variable as found in the function definition (parameter).
  • Actual parameter: the actual value passed (argument).

Function like macro: 
#define getmax(a, b) a>b?a:b

De facto standard:
A de facto standard is a system that has achieved a dominant position by public acceptance or market forces.

Anonymous namespace in C++:

namespace{ ... }

Anonymous namespaces are to C++ what the static keyword is to C. It makes the things declared inside of the anonymous namespace visible only to the current file.

Posted in Miscellaneous | Leave a comment

Things must know about Linux

Linux directory structure:
Explanation of Linux directory structure.

Install packages on Linux:
– Extract the package using tar
– Go to extracted folder and do (usually install under /usr/local directory). And then compile and install.

./configure --prefix=/usr/local
sudo make install

Note that the last command actually install the binary into the desired location.

Find out platforms:

uname -a # print basic information currently available from the system.

The output maybe something like:

Linux ubuntu 3.2.0-29-generic #46-Ubuntu SMP Fri Jul 27 17:04:05 UTC 2012 i686 i686 i386 GNU/Linux

The last two fields maybe different, and they tell your system platform:
64-bit compliant: x86_64 GNU/Linux & ia64 GNU/Linux
32-bit platform: i386 GNU/Linux

Explanation of each fields:
“Linux”: the machine’s kernel name.
“ubuntu”: the machine’s node name.
“3.2.0-29-generic”: the kernel version.
“#46-Ubuntu SMP Fri Jul 27 17:04:05 UTC 2012”: kernel version and build time.
“i686 i686”: the processor type and hardware platform.
“i386”: the architecture of the processor.
“GNU/Linux”: the operating system name.

Auto display the current path in the terminal:
Type PS1="\`pwd\` \$ " OR
Type export PS1='$(hostname):$(PWD)$ ' in the terminal.
Which can be put into ~/.bash_profile file so don’t need to type every time.
Also, export PS1='\u:\w$'

In zsh shell: the command to display current path at prompt is:
setenv PROMPT '[%/]%# ', which can be put in .zshrc file to avoid type it each time.
The prompt would become something like: [/workplace/dongpjin/ERCCS]%

Disable webpages from access
chmod o-x ./webpages

dig command:

dig . ns # find root name servers
dig cse.unl.edu any # show any recoreds
dig cse.unl.edu +short # find ip of given domain
dig google.com ns +short # find name servers of google.com
dig @lexington.unl.edu. cse.unl.edu +short # search the given name server for the ip for the given domain

Change to dir before reading Makefile:
make -C dir

Show dist usage:
df -h # Show disk usage.
The following is the output:

Filesystem Size Used Avail Use% Mounted on
/dev/sda1 18G 13G 4.3G 76% /
udev 1000M 4.0K 1000M 1% /dev
tmpfs 403M 792K 402M 1% /run
none 5.0M 0 5.0M 0% /run/lock
none 1007M 300K 1007M 1% /run/shm

Display the architecture of the machine:

Display all process owned by current user and kill a job:
ps ux
kill -9 1234 # kill job with PID 1234, -9 ensures execution

Copy everything under ~/usr/ to ~ on firefly:
scp -r ~/usr/ djin@ff.unl.edu:~

View previous commands:
history | more

List all files included in a package without installing or downloading it:
apt-file update # have its database created
apt-file search stubs-32.h # query database for a list of packages that contain a file
apt-file list packagename # list the content of an uninstalled package

Create parent directories (iff not exists) when creating a new directory:
mkdir -p /path/to/file

Display information about the system architecture:

Run application (bash) as root:
sudo bash
root% echo 0 > /proc/sys/kernel/yama/ptrace_scope
root% exit # exit root environment

Set PATH in shell, add to .bashrc or .zshrc:
export PATH=$PATH:/your/path/bin

Set PATH in Windows:

set PATH=%PATH%;c:\...\... # Windows, %PATH% refers to current path
set PATH=%PATH%;C:\Program Files (x86)\Java\jdk1.7.0_02\bin # add javac to the path

Send email (with attachment) from Linux command line:
$ mail -a my_attachment.txt -s "My Subject" myemail@somedomain.com < /dev/null

Posted in Linux | 1 Comment

Logging in Python

This example demonstrates how to setup logging in Python. TimedRotatingFileHandler rotate log files at specific time. i.e. in this example, it rotates log file /my/log/file.log at midnight UTC time. Therefore, log lines are kept append to the same log file till midnight UTC and then the log file get rotated. The log lines are formatted too with timestamp, name, level, and message.

Posted in Default | Leave a comment

Submit HTTPS Request with Client Cert in Python

The code snippet below demonstrate how to submit HTTPS request with client cert. Line 14 clears the session if you don’t want the session to persist after call finishes, which may blocked other requests.

Posted in Default | Leave a comment

Investing Terminologies

A list of explanations on investing terms.

Revenue: income a business has from its normal business activities.

Revenue Per Share: a ratio that computes the total revenue earned per share over a 12 month period. i.e. 10.96.

Gross Profit: total revenue minus the cost of goods sold.

Total Debt: total amount of debt.

Debt Ratio: ratio of total debt to total assets. The higher the ratio, the greater the financial risk of the company.

Market Cap: market capitalization. The market value of a company’s outstanding shares. It is calculated by taking the stock price and multiplying it by the total number of shares outstanding.

Volume: the number of shares changed hands during a given day.

Avg. Volume: the total amount of traded in a period, divided by the length of the period.

TTM (Trailing Twelve Months): a timeframe of past 12 months used for reporting financial figures.

MRQ (Most Recent Quarter). 

Beta: A beta of less than 1 means that the security is theoretically less volatile than the market. A beta of greater than 1 indicates that the security’s price is theoretically more volatile than the market. For example, if a stock’s beta is 1.2, it’s theoretically 20% more volatile than the market.

EPS (Earnings Per Share): the portion of a company’s profit allocated to each outstanding share of common stock. Indicates company’s profitability. It is calculated as: (Net Income – Dividends on Preferred Stock) / Average Outstanding Shares.

P/E ratio: measure a company current share price relative to its per-share earnings. It is calculated as: Market Value per Share / Earnings per Share. i.e. 43/1.95, or 22.05.

Trailing P/E: current stock price divided by the trailing earning per share for the past 12 month. i.e. 30.65

Forward P/E: current stock price divided by the forecast earning per share for the next 12 month. (not reliable) i.e. 19.69

PEG ratio:price/earnings to growth ratio. The PEG ratio is used to determine a stock’s value while taking the company’s earnings growth into account, and is considered to provide a more complete picture than the P/E ratio. In general, the P/E ratio is higher for a company with a higher growth rate.

Dividend Yield: how much a company pays out in dividends each year relative to its share price. It is calculated as: Annual Dividends Per Share / Price Per Share. i.e. 1.56 (2.46%)

Payout Ratio: determines the sustainability of a company’s dividend payments. A lower payout radio is generally preferable to a higher payout ratio. A ratio greater than 100% meaning the company is paying more in dividends than it makes in net income.

Price/Sales: calculated by 1) dividing company’s market cap by the revenue in the most recent year; 2) divide per-share stock price by the per-share revenue. i.e. 5.77

Price/Book: calculated by dividing current closing price of stock by the latest quarter’s book value per share. A low P/B ratio could mean that the stock is undervalued. i.e. 7.08

Book Value: is the net asset of a company. Calculated as total assets minus intangible assets (patents, goodwill) and liabilities. It serves as the total value of the company’s assets that shareholders would theoretically receive if a company were liquidated. When compared to the company’s market value book value can indicate whether a stock is under- or overpriced.

Profit Margin: a measure how much of every dollar of sales a company actually keeps in earnings. A 20% profit margin means the company has a net income of $0.20 for each dollar of total revenue earned. Calculated as: Net Income / Net Sales (revenue).

Operating Margin: measure a company’s pricing strategy and operating efficiency. A measure of what portion of a company’s revenue is left over after paying for variable costs of production such as wages, raw materials, etc. i.e. 24.36% of revenue is left after paying variable costs.

Return on Assets: how efficient management is at using its assets to generate earnings. Dividing a company’s annual earnings by its total assets. i.e. 6.45%

Posted in Default, Investing | Leave a comment

Execute a subprocess with timeout in Python

In Python 2.6+, normally we use a subprocess to execute a command line. To add timeout capability, we can take advantage of threading where we run the command in a thread and kill the thread if timeout. Below is an example snippet.

import subprocess;
import threading;

class Command:
    def __init__(self, cmd):
        self.cmd = cmd;
        self.process = None;

    def run(self, timeout):
        def worker():
            print("Thread started...");
            self.process = subprocess.Popen(self.cmd, shell=True);
            print("Thread finished.");

        # Start child thread
        thread = threading.Thread(target=worker);

        # When timeout, join() would return but process will be alive, thus need to terminate.
        if thread.isAlive():
            print "Terminating process...";
        print self.process.returncode;

command = Command("echo 'Excuting command: '; sleep 5; echo 'Command finished'");

Output is:

Thread started...
Excuting command:
Command finished
Thread finished.
Thread started...
Excuting command:
Terminating process...
Thread finished.

More comments on join():
join([timeout]) Wait until the thread terminates. This blocks the calling thread until the thread whose join() method is called terminates – either normally or through an unhandled exception – or until the optional timeout occurs.

Posted in Python | Leave a comment

Submit REST request in Python

The code snippet below illustrates how to submit REST requests in Python.

import urllib2
def send_request(request):
    parsed_json = None
    for i in range(RETRIES):
            response = urllib2.urlopen(request).read()
            parsed_json = json.loads(response)
            print("Response: {0}".format(parsed_json))
        except Exception:
            if i < RETRIES - 1:
                print("Fail to send request: {0} after: {1} retries.".format(request, i))
    return parsed_json
Posted in Default, Python | Leave a comment

How to ping a host in Python

One could write a small function in Python to ping a host. Below is the code snippet.

def ping_host(hostname):
    # Strip HTTP prefix and trailing port number if any
    hostname = re.sub(r'https?://', "", hostname.lower())
    index = hostname.rfind(":")
    if index != -1:
        hostname = hostname[:index]
    # Ping once and timeout after 10 sec
    response = os.system("ping -c 1 -W 10 " + hostname)
    if response == 0:
        print("Host: {0} is up".format(hostname))
        print("Host: {0} is down".format(hostname))
Posted in Python | Leave a comment

Python Regex Examples

This post illustrates a list of examples of what you can do using regex in python.

Strip out the leading http(s) prefix.

hostname = re.sub(r'https?://', "", hostname.lower())

Match a string with regex:

if re.match(regex, value, flags=re.IGNORECASE) is None:
print("Fail to match regex {0} with string {1}".format(regex, value))

Empty, whitespace, or a list emails


-Google tutorial:

-TutorialPoint tutorial:

-re operator:

– Python regex online editor:

Posted in Python | Leave a comment