Compile Async-Await to Generators and Promises

Async/Await is introduced in ES2017. In order to let ES2017 code run in very old browsers/platforms, ES2017 code need to be transpiled/compiled to earlier versions. Async/await need to be transpiled to older-version JavaScript code. In this post, I will list three ways to do the same thing: 1) async/await [ES2017+], 2) generator function + promise … Read more

Most Frequently Used Python Code Fragments

Python syntax may seem strange at the beginning for Java developers. Wouldn’t it be nice if we first see how Python does the common jobs that we have done using other programming languages, like Java? The common code fragments are call “code idioms”. Reading the code idioms of a programming language is often helpful, and can be served as a shortcut for learning a new programming language.

Read more

Get Programming Language Keywords By Using Regular Expression in Java

We can use regular expression to get all Java keywords in a program. The key is using word boundary correctly. For example, given “static staticField”, the first word should be recognized as a keyword but the second should not.

Read more

R read data from csv files

Reading data from csv files is very useful as csv file is a standard output of many programs. In R, you can read csv file in one line: df <- read.csv("/path/to/file.txt", row.names=1,head=FALSE, sep=",")df <- read.csv("/path/to/file.txt", row.names=1,head=FALSE, sep=",") The code above specifies the first column is just a name by using row.names=1. By using head=FALSE, it … Read more

Python read data from MySQL database

The following code can read data from MySQL database. import MySQLdb   # Open database connection db = MySQLdb.connect("localhost","username","password","DBName")   # prepare a cursor object using cursor method cursor = db.cursor()   sql = "select * from table1" cursor.execute(sql) results = cursor.fetchall() for row in results: print row[0]   # disconnect from server db.close()import MySQLdb … Read more

How to install Latex on Ubuntu 14.04LTS

You may often get error messages like “missing subfigure.sty”, “missing url.sty”, or missing other .sty files. The problem is caused by missing packages. This post outlines the steps to correctly install latex on Ubuntu latest version 14.04LTS. It should also work for most previous other versions. 1. Install Latex on Ubuntu In the terminal, type … Read more

Java Runtime.exec() Linux Pipe

If you want to utilize Linux pipe advantages, you can do it in Java. You may want to do the following, which is wrong. String cmd = "ls" Process p = Runtime.getRuntime().exec(cmd);String cmd = "ls" Process p = Runtime.getRuntime().exec(cmd); The correct way of executing shell command is the following: String[] cmd = { "/bin/sh", "-c", … Read more