If you run a website, you may have experienced some down time. Occasionally, the web server can go down. It would be great if we can get an email when the server is down.
Programming Languages
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.
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.
Ubuntu log out GUI from SSH
In Ubuntu, we can log out GUI from SSH: pkill -u username
Migrating Website to Amazon AWS EC2
This post shows how to migrate a php website from 1and1 shared hosting to Amazon AWS. I mainly cover the Linux command lines and various configurations since they are often the difficult part. I will cover the steps for creating instances of AWS because they can be done by following the guide provided by Amazon … 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
Python read file line by line
How to read a text file line by line using Python? Reading files in python is extremely simple to do. The following is the code: with open("/path/to/file.txt") as f: for line in f: linedata = line.split(’\t’)with open("/path/to/file.txt") as f: for line in f: linedata = line.split(‘\t’) Very often, you may want to split each line … 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
Linux shell script read file line by line
The following are different ways of reading a txt file line by line in linux shell. To fun the following scripts, you should create a “test.txt” file under the same directory with your script. Method 1 Use file names as parameter to the shell script. readFile.sh #!/bin/bash while read line do name=$line echo “Text read … Read more
Vector, Array, List and Data Frame in R
Vector, Array, List and Data Frame are 4 basic data types defined in R. Knowing the differences between them will help you use R more efficiently. 1. Vector All elements must be of the same type. For example, the following code create two vectors. name y y $name [1] “Mike” $gender [1] “M” $company [1] … Read more
How to set page title font size in R?
How to set page title font size is not clear in R’s documentation. It can take a lot of time to figure out how to set font size. The following code shows how to change font size. # create a simple data frame a