Scripting Languages I: Node.js, Python, PHP, Ruby

a side-by-side reference sheet

sheet one: version | grammar and invocation | variables and expressions | arithmetic and logic | strings | regexes | dates and time | arrays | dictionaries | functions | execution control | exceptions | threads

sheet two: streams | asynchronous events | files | file formats | directories | processes and environment | option parsing | libraries and namespaces | objects | reflection | net and web | databases | unit tests | debugging

streams
node.js python php ruby
read from stdin
 
let readline = require('readline');

let rl = readline.createInterface({
  input: process.stdin
});
rl.on('line', (line) => {
  // this code executes once for each line
});
line = sys.stdin.readline()
s = line.rstrip('\r\n')
$stdin = fopen('php://stdin', 'r');
$line = rtrim(fgets($stdin));
line = gets
s = line.chomp
write to stdout
 
console.log('Hello, World!'); print('Hello, World!')

sys.stdout.write('Hello, World!\n')
echo "Hello, World!\n"; puts "Hello, World!"

$stdout.write("Hello, World!\n")
write format to stdout import math

print('%.2f' % math.pi)
printf("%.2f\n", M_PI); printf("%.2f\n", Math::PI)
write to stderr console.error('bam!'); sys.stderr.write('bam!\n') $stderr = fopen('php://stderr', 'w');
fwrite($stderr, "bam!\n");
$stderr.puts "bam!"

$stderr.write("bam!\n")
open for reading
 
f = open('/etc/hosts',
  encoding='utf-8')

# Python 2: use codecs.open()
$f = fopen("/etc/hosts", "r"); f = File.open("/etc/hosts", "r:utf-8")
open for reading bytes let fs = require('fs');

let f = fs.openSync("/etc/hosts", "r");
f = open('/etc/hosts', 'rb') $f = fopen("/etc/hosts", "r"); f = File.open("/etc/hosts", "rb")
read line
 
f.readline() $line = fgets($f); f.gets
iterate by line
 
const readline = require('readline');
const fs = require('fs');

let f = fs.createReadStream('/etc/hosts');
const rl = readline.createInterface({
  input: f
});
rl.on('line', (line) => {
  console.log(line);
});
for line in f:
  print(line)
while (!feof($f)) {
  $line = fgets($f);
  echo $line;
}
f.each do |line|
  print(line)
end
read file into string let fs = require('fs');

let s = fs.readFileSync('/etc/hosts', 'utf8');
s = f.read() $s = file_get_contents(
  "/etc/hosts");
s = f.read
read file into array of strings a = f.readlines() $a = file("/etc/hosts"); a = f.lines.to_a
read fixed length let buf = Buffer.alloc(100);
// 3rd arg is offset into buf:
let n = fs.readSync(f, buf, 0, 100);
let s = buf.toString('utf-8', 0, n);
s = f.read(100) $s = fread($f, 100); s = f.read(100)
read char
 
ch = f.read(1) $ch = fread($f, 1); ch = f.readchar
read serialized data let fs = require('fs');

let s = fs.readFileSync("/tmp/data.json");
let data = JSON.parse(s);
import pickle

with open('/tmp/data.pickle', 'rb') as f:
  data = pickle.load(f)
$s = file_get_contents("/tmp/data");
$data = unserialize($s);
File.open('/tmp/data.marshal') do |f|
  data = Marshal.load(f)
end
open for writing
 
let fs = require('fs');

let f = fs.openSync("/tmp/test", "w");
f = open('/tmp/test', 'w'
  encoding='utf-8')

# Python 2: use codecs.open()
$f = fopen("/tmp/test", "w"); f = File.open("/tmp/test", "w:utf-8")
open for writing bytes let fs = require('fs');

let f = fs.openSync("/tmp/test", "w");
f = open('/tmp/test', 'wb') $f = fopen("/tmp/test", "w"); f = File.open("/tmp/test", "wb")
open for appending et fs = require('fs');

let f = fs.openSync("/tmp/test", "a");
f = open('/tmp/err.log', 'a') $f = fopen("/tmp/test", "a"); f = File.open("/tmp/err.log", "a")
write string
 
fs.writeSync(f, 'lorem ipsum');

// writeSync() takes String or Buffer arg.
// A String is encoded as UTF-8.
f.write('lorem ipsum') fwrite($f, "lorem ipsum"); f.write("lorem ipsum")
write line
 
fs.writeSync(f, 'lorem ipsum\n'); f.write('lorem ipsum\n') fwrite($f, "lorem ipsum\n"); f.puts("lorem ipsum")
write format import math

f.write('%.2f\n', math.pi)
fprintf($f, "%.2f\n", M_PI); f.printf("%.2f\n", Math::PI)
write char
 
f.write('A') fwrite($f, "A"); f.putc('A')
write serialized data let fs = require('fs');

let s = JSON.stringify({foo: [1, 2, 3]});
fs.writeFileSync("/tmp/data.json", s);
import pickle

with open('/tmp/data.pickle', 'wb') as f:
  pickle.dump({'foo': [1, 2, 3]}, f)
$f = fopen("/tmp/data", "w");
$data = ["foo" => [1, 2, 3]];
fwrite($f, serialize($data));
fclose($f);
File.open('/tmp/data.marshal', 'w') do |f|
  Marshal.dump({'foo': [1, 2, 3]}, f)
end
close
 
fs.closeSync(f); f.close() fclose($f); f.close
close on block exit with open('/tmp/test', 'w') as f:
  f.write('lorem ipsum\n')
none File.open("/tmp/test", "w") do |f|
  f.puts("lorem ipsum")
end
flush
 
writeSync() isn't buffered f.flush() # CLI output isn't buffered
fflush($f);
f.flush
position

get, set
// no get

let buf = Buffer.alloc(100);
// 5th arg is where in file to start read:
fs.readSync(f, buf, 0, 100, 0);
// 3rd arg is where in file to start write:
fs.writeSync(f2, buf, 0);
f.tell()
f.seek(0)
ftell($f)
fseek($f, 0);
f.tell
f.seek(0)

f.pos
f.pos = 0
open temporary file // npm install tmp
let tmp = require('tmp');
let fs = require('fs');

let file = tmp.fileSync();
fs.writeSync(file.fd, 'lorem ipsum');
console.log(`tmp file: ${file.name}`);
fs.closeSync(file.fd);
import tempfile

f = tempfile.NamedTemporaryFile()
f.write('lorem ipsum\n')
print("tmp file: %s" % f.name)
f.close()

# file is removed when file handle is closed
$f = tmpfile();
fwrite($f, "lorem ipsum\n");
# no way to get file name
fclose($f);

# file is removed when file handle is closed
require 'tempfile'

f = Tempfile.new('')
f.puts "lorem ipsum"
puts "tmp file: #{f.path}"
f.close

# file removed when file handle
# garbage-collected or interpreter exits
open in memory file import io

f = io.StringIO()
f.write('lorem ipsum\n')
s = f.getvalue()

# Python2: in StringIO module
$meg = 1024 * 1024;
$mem = "php://temp/maxmemory:$meg";
$f = fopen($mem, "r+");
fputs($f, "lorem ipsum");
rewind($f);
$s = fread($f, $meg);
require 'stringio'

f = StringIO.new
f.puts("lorem ipsum")
f.rewind
s = f.read
asynchronous events
node.js python php ruby
start event loop # Python 3.4 and later:
import asyncio

asyncio.BaseEventLoop.run_forever()
read file asynchronously
write file asynchronously
promise
files
node.js python php ruby
file exists test, file regular test
 
let fs = require('fs');

let exists = fs.existsSync('/etc/hosts');
let stat = fs.statSync('/etc/hosts');
let regular = stat.isFile();
os.path.exists('/etc/hosts')
os.path.isfile('/etc/hosts')
file_exists("/etc/hosts")
is_file("/etc/hosts")
File.exists?("/etc/hosts")
File.file?("/etc/hosts")
file size
 
let fs = require('fs');

let stat = fs.statSync('/etc/hosts');
let size = stat.size;
os.path.getsize('/etc/hosts') filesize("/etc/hosts") File.size("/etc/hosts")
is file readable, writable, executable let fs = require('fs');

// no return values; exception thrown
// if not readable, writable, or executable:

fs.accessSync('/etc/hosts',
  fs.constants.R_OK);
fs.accessSync('/etc/hosts',
  fs.constants.W_OK);
fs.accessSync('/etc/hosts',
  fs.constants.X_OK);
os.access('/etc/hosts', os.R_OK)
os.access('/etc/hosts', os.W_OK)
os.access('/etc/hosts', os.X_OK)
is_readable("/etc/hosts")
is_writable("/etc/hosts")
is_executable("/etc/hosts")
File.readable?("/etc/hosts")
File.writable?("/etc/hosts")
File.executable?("/etc/hosts")
set file permissions
 
let fs = require('fs');

fs.chmodSync('/tmp/foo', parseInt('755', 8));
os.chmod('/tmp/foo', 0755) chmod("/tmp/foo", 0755); File.chmod(0755, "/tmp/foo")
last modification time let fs = require('fs');

let stat = fs.statSync('/etc/hosts');
let dt = stat.mtime;
from datetime import datetime as dt

# unix epoch:
t = os.stat('/etc/passwd').st_mtime

# datetime object:
t2 = dt.fromtimestamp(t)
# unix epoch:
$t = stat('/etc/passwd')['mtime'];

# DateTime object:
$t2 = new DateTime('UTC');
$t2->setTimestamp($t);
# Time object:
t2 = File.stat('/etc/passwd').mtime

# unix epoch:
t = t2.to_i
copy file, remove file, rename file // npm install fs-extra
let fs = require('fs-extra');

fs.copySync('/tmp/foo', '/tmp/bar');
fs.unlinkSync('/tmp/foo');
fs.renameSync('/tmp/bar', '/tmp/foo');
import shutil

shutil.copy('/tmp/foo', '/tmp/bar')
os.remove('/tmp/foo')
shutil.move('/tmp/bar', '/tmp/foo')
copy("/tmp/foo", "/tmp/bar");
unlink("/tmp/foo");
rename("/tmp/bar", "/tmp/foo");
require 'fileutils'

FileUtils.cp("/tmp/foo", "/tmp/bar")
FileUtils.rm("/tmp/foo")
FileUtils.mv("/tmp/bar", "/tmp/foo")
create symlink, symlink test, readlink let fs = require('fs');

fs.symlinkSync('/etc/hosts', '/tmp/hosts');
let lstat = fs.lstatSync('/tmp/hosts');
let isLink = lstat.isSymbolicLink();
let path = fs.readlinkSync('/tmp/hosts');
os.symlink('/etc/hosts', '/tmp/hosts')
os.path.islink('/tmp/hosts')
os.path.realpath('/tmp/hosts')
symlink("/etc/hosts", "/tmp/hosts");
is_link("/etc/hosts")
readlink("/tmp/hosts")
File.symlink("/etc/hosts", "/tmp/hosts")
File.symlink?("/etc/hosts")
File.realpath("/tmp/hosts")
generate unused file name // npm install tempfile
let tempfile = require('tempfile');
let path = tempfile();
import tempfile

f, path = tempfile.mkstemp(
  prefix='foo',
  dir='/tmp')
$path = tempnam("/tmp", "foo");
$f = fopen($path, "w");
file formats
node.js python php ruby
parse csv let fs = require('fs');
// npm install csv
let csv = require('csv');

let path = 'no-header.csv';
let f = fs.createReadStream(path);
f.pipe(csv.parse()).pipe(
  csv.transform(function (record) {
    console.log(record.join('\t'));
  })
);
import csv

with open('foo.csv') as f:
  cr = csv.reader(f)
  for row in cr:
    print('\t'.join(row))
$f = fopen("no-header.csv", "r");
while (($row = fgetcsv($f)) != FALSE) {
  echo implode("\t", $row) . "\n";
}
require 'csv'

CSV.foreach("foo.csv") do |row|
  puts row.join("\t")
end
generate csv import csv

with open('foo.csv', 'w') as f:
  cw = csv.writer(f)
  cw.writerow(['one', 'une', 'uno'])
  cw.writerow(['two', 'deux', 'dos'])
require 'csv'

CSV.open("foo.csv", "w") do |csv|
  csv << ["one", "une", "uno"]
  csv << ["two", "deux", "dos"]
end
parse json let s = '{"t":1,"f":0}';
let data = JSON.parse(s);
import json

d = json.loads('{"t":1,"f":0}')
$s1 = '{"t":1,"f":0}';
$a1 = json_decode($s1, TRUE);
require 'json'

d = JSON.parse('{"t":1,"f":0}')
generate json let data = {'t': 1, 'f': 0};
let s = JSON.stringify(data);
import json

s = json.dumps({'t': 1, 'f': 0})
$a2 = array("t" => 1, "f" => 0);
$s2 = json_encode($a2);
require 'json'

s = {'t' => 1,'f' => 0}.to_json
parse yaml # sudo pip install PyYAML
import yaml

data = yaml.safe_load('{f: 0, t: 1}\n')
# sudo gem install safe_yaml
require 'safe_yaml'

data = YAML.safe_load("---\nt: 1\nf: 0\n")
generate yaml # sudo pip install PyYAML
import yaml

s = yaml.safe_dump({'t': 1, 'f': 0})
# sudo gem install safe_yaml
require 'safe_yaml'

s = YAML.dump({'t' => 1, 'f' => 0})
parse xml
all nodes matching xpath query; first node matching xpath query
// npm install xmldom xpath
let dom = require('xmldom').DOMParser;
let xpath = require('xpath');

let xml = '<a><b><c ref="3">foo</c></b></a>';
let doc = new dom().parseFromString(xml);
let nodes = xpath.select('/a/b/c', doc);
nodes.length;
nodes[0].firstChild.data;
from xml.etree import ElementTree

xml = '<a><b><c ref="3">foo</c></b></a>'

# raises xml.etree.ElementTree.ParseError
# if not well-formed:

doc = ElementTree.fromstring(xml)

nodes = doc.findall('b/c')
print(len(nodes))
print(nodes[0].text)

node = doc.find('b/c')
print(node.text)
print(node.attrib['ref'])
$xml = "<a><b><c ref='3'>foo</c></b></a>";

# returns NULL and emits warning if not
# well-formed:

$doc = simplexml_load_string($xml);

$nodes = $doc->xpath("/a/b/c");
echo count($nodes);
echo $nodes[0];

$node = $nodes[0];
echo $node;
echo $node["ref"];
require 'rexml/document'
include REXML

xml = "<a><b><c ref='3'>foo</c></b></a>"

# raises REXML::ParseException if
# not well-formed:

doc = Document.new(xml)

nodes = XPath.match(doc,"/a/b/c")
puts nodes.size
puts nodes[0].text

node = XPath.first(doc,"/a/b/c")
puts node.text
puts node.attributes["ref"]
generate xml // npm install xmlbuilder
let builder = require('xmlbuilder');

let xml = builder.create('a').ele('b', {id: 123}, 'foo').end();
import xml.etree.ElementTree as ET

builder = ET.TreeBuilder()
builder.start('a', {})
builder.start('b', {'id': '123'})
builder.data('foo')
builder.end('b')
builder.end('a')
et = builder.close()

# <a><b id="123">foo</b></a>:
print(ET.tostring(et))
$xml = "<a></a>";
$sxe = new SimpleXMLElement($xml);
$b = $sxe->addChild("b", "foo");
$b->addAttribute("id", "123");

# <a><b id="123">foo</b></a>:
echo $sxe->asXML();
# gem install builder
require 'builder'

builder = Builder::XmlMarkup.new
xml = builder.a do |child|
  child.b("foo", :id=>"123")
end

# <a><b id="123">foo</b></a>:
puts xml
parse html # pip install beautifulsoup4
import bs4

html = open('foo.html').read()
doc = bs4.BeautifulSoup(html)

for link in doc.find_all('a'):
  print(link.get('href'))
$html = file_get_contents("foo.html");
$doc = new DOMDocument;
$doc->loadHTML($html);
$xpath = new DOMXPath($doc);

$nodes = $xpath->query("//a/@href");
foreach($nodes as $href) {
  echo $href->nodeValue;
}
# gem install nokogiri
require 'nokogiri'

html = File.open("foo.html").read
doc = Nokogiri::HTML(html)
doc = doc.xpath("//a").each do |link|
  puts link["href"]
end
directories
node.js python php ruby
working directory let old_dir = process.cwd();

process.chdir("/tmp");
old_dir = os.path.abspath('.')

os.chdir('/tmp')
$old_dir = getcwd();

chdir("/tmp");
old_dir = Dir.pwd

Dir.chdir("/tmp")
build pathname let path = require('path');

path.join('/etc', 'hosts');
os.path.join('/etc', 'hosts') "/etc" . DIRECTORY_SEPARATOR . "hosts" File.join("/etc", "hosts")
dirname and basename let path = require('path');

path.dirname('/etc/hosts')
path.basename('/etc/hosts')
os.path.dirname('/etc/hosts')
os.path.basename('/etc/hosts')
dirname("/etc/hosts")
basename("/etc/hosts")
File.dirname("/etc/hosts")
File.basename("/etc/hosts")
absolute pathname
and tilde expansion
# symbolic links are not resolved:
os.path.abspath('foo')
os.path.abspath('/foo')
os.path.abspath('../foo')
os.path.abspath('./foo')
os.path.expanduser('~/foo')
# file must exist; symbolic links are
# resolved:

realpath("foo")
realpath("/foo")
realpath("../foo")
realpath("./foo")
# no function for tilde expansion
# symbolic links are not resolved:
File.expand_path("foo")
File.expand_path("/foo")
File.expand_path("../foo")
File.expand_path("./foo")
File.expand_path("~/foo")
iterate over directory by file let fs = require('fs');

fs.readdirSync('/etc').forEach(
  function(s) { console.log(s); }
);
for filename in os.listdir('/etc'):
  print(filename)
if ($dir = opendir("/etc")) {
  while ($file = readdir($dir)) {
    echo "$file\n";
  }
  closedir($dir);
}
Dir.open("/etc").each do |file|
  puts file
end
glob paths // npm install glob
let glob = require('glob');

glob('/etc/*', function(err, paths) {
  paths.forEach(function(path) {
    console.log(path);
  });
});
import glob

for path in glob.glob('/etc/*'):
  print(path)
foreach (glob("/etc/*") as $file) {
  echo "$file\n";
}
Dir.glob("/etc/*").each do |path|
  puts path
end
make directory const fs = require('fs');

if (!fs.existsSync('/tmp/foo')) {
  fs.mkdirSync('/tmp/foo', 0755);
}
fs.mkdirSync('/tmp/foo/bar', 0755);
dirname = '/tmp/foo/bar'
if not os.path.isdir(dirname):
  os.makedirs(dirname)
mkdir("/tmp/foo/bar", 0755, TRUE); require 'fileutils'

FileUtils.mkdir_p("/tmp/foo/bar")
recursive copy import shutil

shutil.copytree('/tmp/foodir',
  '/tmp/bardir')
none require 'fileutils'

FileUtils.cp_r("/tmp/foodir",
  "/tmp/bardir")
remove empty directory const fs = require('fs');

fs.rmdirSync('/tmp/foodir');
os.rmdir('/tmp/foodir') rmdir("/tmp/foodir"); File.rmdir("/tmp/foodir")
remove directory and contents import shutil

shutil.rmtree('/tmp/foodir')
none require 'fileutils'

FileUtils.rm_rf("/tmp/foodir")
directory test
 
os.path.isdir('/tmp') is_dir("/tmp") File.directory?("/tmp")
generate unused directory const fs = require('fs');

const dir = fs.mkdtemp('/tmp/foo');
import tempfile

path = tempfile.mkdtemp(dir='/tmp',
  prefix='foo')
require 'tmpdir'

path = Dir.mktmpdir("/tmp/foo")
system temporary file directory import tempfile

tempfile.gettempdir()
sys_get_temp_dir() require 'tmpdir'

Dir.tmpdir
processes and environment
node.js python php ruby
command line arguments
and script name
process.argv.slice(2)
process.argv[1]
// process.argv[0] contains "node"
sys.argv[1:]
sys.argv[0]
$argv
$_SERVER["SCRIPT_NAME"]
ARGV
$PROGRAM_NAME
environment variable

get, set
process.env["HOME"]

process.env["PATH"] = "/bin";
os.getenv('HOME')

os.environ['PATH'] = '/bin'
getenv("HOME")

putenv("PATH=/bin");
ENV["HOME"]

ENV["PATH"] = "/bin"
get pid, parent pid process.pid
none
os.getpid()
os.getppid()
posix_getpid()
posix_getppid()
Process.pid
Process.ppid
user id and name import getpass

os.getuid()
getpass.getuser()
$uid = posix_getuid();
$uinfo = posix_getpwuid($uid);
$username = $uinfo["name"];
require 'etc'

Process.uid
Etc.getpwuid(Process.uid)["name"]
exit
 
process.exit(0); sys.exit(0) exit(0); exit(0)
set signal handler
 
import signal

def handler(signo, frame):
  print('exiting...')
  sys.exit(1)

signal.signal(signal.SIGINT, handler)
Signal.trap("INT",
  lambda do |signo|
    puts "exiting..."
    exit 1
  end
)
external command
 
if os.system('ls -l /tmp'):
  raise Exception('ls failed')
system("ls -l /tmp", $retval);
if ($retval) {
  throw new Exception("ls failed");
}
unless system("ls -l /tmp")
  raise "ls failed"
end
shell-escaped external command
 
import subprocess

cmd = ['ls', '-l', '/tmp']
if subprocess.call(cmd):
  raise Exception('ls failed')
$path = chop(fgets(STDIN));
$safe = escapeshellarg($path);
system("ls -l " . $safe, $retval);
if ($retval) {
  throw new Exception("ls failed");
}
path = gets
path.chomp!
unless system("ls", "-l", path)
  raise "ls failed"
end
command substitution
 
import subprocess

cmd = ['ls', '-l', '/tmp']
files = subprocess.check_output(cmd)
$files = `ls -l /tmp`; files = `ls -l /tmp`
unless $?.success?
  raise "ls failed"
end

files = %x(ls)
unless $?.success?
  raise "ls failed"
end
option parsing
node.js python php ruby
boolean flag // $ npm install commander
program = require('commander');

program.option('-v, --verbose')
  .parse(process.argv);

let verbose = program.verbose;
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('--verbose', '-v',
  dest='verbose',
  action='store_true')
args = parser.parse_args()

if args.verbose:
  print('Setting output to verbose.')
$opts = getopt("v", ["verbose"]);

if (array_key_exists("v", $opts) ||
    array_key_exists("verbose", $opts)) {
  $verbose = TRUE;
}
require 'optparse'

options = {}
OptionParser.new do |opts|
  opts.on('-v', '--verbose') do |arg|
    options[:verbose] = arg
  end
end.parse!

verbose = options[:verbose]
string option // $ npm install commander
program = require('commander');

program.option('-f, --file <file>')
  .parse(process.argv);

let file = program.file;
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('--input', '-i',
  dest='input')
args = parser.parse_args()

if args.input:
  f = open(args.input)
else:
  f = sys.stdin
$opts = getopt("f:", ["file:"]);

$file = $opts["f"] ? $opts["f"] :
  $opts["file"];
require 'optparse'

options = {}
OptionParser.new do |opts|
  opts.on('-i', '--input PATH') do |arg|
    options[:input] = arg
  end
end.parse!

if options[:input].nil?
  f = $stdin
else
  f = File.open(options[:input])
end
numeric option // $ npm install commander
program = require('commander');
program.option('-c, --count <n>',
  'a count', parseInt);
program.option('-r, --ratio <n>',
  'a ratio', parseFloat);
program.parse(process.argv);

if (program.count)
  console.log(program.count);
if (program.ratio)
  console.log(program.ratio);
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('--count', '-c',
  dest='count', type=int)
parser.add_argument('--ratio', '-r',
  dest='ratio', type=float)
args = parser.parse_args()

if args.count:
  print('Count: %d' % args.count)
if args.ratio:
  print('Ratio: %.3f' % args.ratio)
unrecognized option behavior // error message and exit
// with nonzero status
# print usage and exit with nonzero status # Unrecognized options are ignored.
#
# Also, an option declared to have an
# argument is ignored if the argument
# is not provided on the command line.
required option const fs = require('fs');
// $ npm install commander
const program = require('commander');

program.option('-i, --input <input>')
program.parse(process.argv);

if (program.input) {
  let f = fs.openSync(program.input, 'r');
} else {
  console.log('required: --input');
  program.outputHelp();
  process.exit(1);
}
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('--input', '-i',
  dest='input', required=True)
args = parser.parse_args()

f = open(args.input)
default option // $ npm install commander:
const program = require('commander');

program.option('-H, --hosts <hosts>',
  'the hosts file', '/etc/hosts');
program.parse(process.argv);
console.log('hosts: ' + program.hosts);
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('--hosts', '-H',
  dest='hosts', default='/etc/hosts')
args = parser.parse_args()

f = open(args.hosts)
delimited options // $ npm install commander
const program = require('commander');

function comma_split(val) {
  return val.split(',');
}

program.option('-w, --words <words>',
  'comma-delimited', comma_split);
program.parse(process.argv);
console.log(program.words.length);
import argparse

parser = argparse.ArgumentParser()
# Take all arguments up to next flag.
# Use '+' to require at least one argument:

parser.add_argument('--words', '-w',
  dest='words', nargs='*')

# twiddle -w one two three
args = parser.parse_args()
# ['one', 'two', 'three']:
args.words
repeated options // $ npm install commander
const program = require('commander');

function collect(val, memo) {
  memo.push(val);
  return memo;
}

// $ node twiddle.js -w one -w two -w three
program.option('-w, --words <words>',
  'repeatable flag', collect, []);
program.parse(process.argv);
console.log(program.words.length);
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('--word', '-w',
  dest='words', action='append')

# twiddle -w one -w two -w three
args = parser.parse_args()
# ['one', 'two', 'three']:
args.words
positional parameters // Processing stops at
// first positional arg.
//
// Positional arguments are
// in program.args.
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('foo')
parser.add_argument('bar')
args = parser.parse_args()
print('foo: {}'.format(args.foo))
print('bar: {}'.format(args.bar))
# Processing stops at first positional arg.
#
# getopt() does not modify $argv or
# provide means to identify positional
# arguments.
# Options can follow positional args.
#
# After calling OptionParser.parse! only
# positional arguments are in ARGV.
positional parameters as array import argparse

parser = argparse.ArgumentParser()
# '*' for zero or more args;
# '+' for 1 or more args:

parser.add_argument('foo', nargs='*')
args = parser.parse_args()
print(len(args.foo))
usage // $ npm install commander
program = require('commander');

// The flags -h and --help are generated
// automatically.

program.option('-i, --input <input>')
  .parse(process.argv);

let input = program.input;

if (!input) {
  program.outputHelp();
  process.exit(1);
}
import argparse

parser = argparse.ArgumentParser(
  description='Twiddles data')
parser.add_argument('--input', '-i',
  dest='input', help='path of input file')
args = parser.parse_args()

if not args.input:
  parser.print_help()
  sys.exit(1)
$usage = "usage: " .
  $_SERVER["SCRIPT_NAME"] .
  " [-f FILE] [-v] [ARG ...]\n";

$opts = getopt("i:h",
  array("input:", "help"));

if (array_key_exists("h", $opts) ||
    array_key_exists("help", $opts)) {
  echo $usage;
  exit(1);
}

$input = $opts["i"] ? $opts["i"] :
  $opts["input"];
require 'optparse'

options = {}
OptionParser.new do |opts|
  opts.banner =
    "usage: #{$PROGRAM_NAME} [OPTIONS] [ARG ...]"
  opts.on('-i', '--input PATH') do |arg|
    options[:input] = arg
  end
  if options[:input].nil?
    puts opts
    exit 1
  end
end.parse!
subcommand import argparse

def show(args):
  print('showing...')

def submit(args):
  print('submitting {}'.format(args.name))

parser = argparse.ArgumentParser()
sub_ps = parser.add_subparsers()

show_p = sub_ps.add_parser('show')
show_p.set_defaults(func=show)

submit_p = sub_ps.add_parser('submit')
submit_p.add_argument('--name', '-n',
  dest='name', required=True)
submit_p.set_defaults(func=submit)

args = parser.parse_args()
if hasattr(args, 'func'):
  args.func(args)
else:
  parser.print_help()
  sys.exit(1)
libraries and namespaces
node.js python php ruby
load library
 
let foo = require('./foo.js');

let foo = require('foo');
# searches sys.path for foo.pyc or foo.py:
import foo
require_once("foo.php"); require 'foo.rb'

# searches $LOAD_PATH for foo.rb, foo.so,
# foo.o, foo.dll:

require 'foo'
load library in subdirectory let bar = require('./foo/bar.js'); # foo must contain __init__.py file
import foo.bar
require_once('foo/bar.php'); require 'foo/bar.rb'

require 'foo/bar'
hot patch
 
delete require.cache[require.resolve('./foo.js')];
let foo = require('./foo.js');
reload(foo) require("foo.php"); load 'foo.rb'
load error raises Errror exception raises ImportError if library not found; exceptions generated when parsing library propagate to client require and require_once raise fatal error if library not found; include and include_once emit warnings raises LoadError if library not found; exceptions generated when parsing library propagate to client
main routine in library if (require.main == module) {
  code
}
if __name__ == '__main__':
  code
none if $PROGRAM_NAME == __FILE__
  code
end
library path none sys.path

sys.path.append('/some/path')
$libpath = ini_get("include_path");

ini_set("include_path",
  $libpath . ":/some/path");
# $: is synonym for $LOAD_PATH:
$LOAD_PATH

$LOAD_PATH << "/some/path"
library path environment variable $ NODE_PATH=~/lib node foo.js $ PYTHONPATH=~/lib python foo.py none $ RUBYLIB=~/lib ruby foo.rb
library path command line option none none none $ ruby -I ~/lib foo.rb
simple global identifiers built-in functions variables defined outside of functions or with global keyword variables which start with $
multiple label identifiers modules classes, interfaces, functions, and constants constants, classes, and modules
label separator
 
foo.bar.baz() \Foo\Bar\baz(); Foo::Bar.baz
root namespace definition none \foo # outside of class or module; only
# constants in root namespace:

FOO = 3

# inside class or module:
::FOO = 3
namespace declaration
 
put declarations in foo.py namespace Foo; class Foo
  # class definition
end

module Foo
  # module definition
end
child namespace declaration foo must be in sys.path:
$ mkdir foo
$ touch foo/__init__.py
$ touch foo/bar.py
namespace Foo\Bar; module Foo::Bar
  # module definitions
end

module Foo
  module Bar
    # module definitions
  end
end

# classes can nest inside classes or
# modules; modules can nest in classes
import definitions
 
from foo import bar, baz only class names can be imported none
import all definitions in namespace
 
from foo import * none # inside class or module:
include Foo
import all subnamespaces # subnamespaces in list __all__ of
# foo/__init__.py are imported

from foo import *
shadow avoidance # rename namespace:
import foo as fu

# rename identifier:
from sys import path as the_path
use Foo as Fu; Fu = Foo.dup

include Fu
list installed packages, install a package
 
$ pip freeze
$ pip install jinja2
$ pear list
$ pear install Math_BigInteger
$ gem list
$ gem install rails
package specification format in setup.py:

#!/usr/bin/env python

from distutils.core import setup

setup(
  name='foo',
  author='Joe Foo',
  version='1.0',
  description='a package',
  py_modules=['foo'])
in foo.gemspec:

spec = Gem::Specification.new do |s|
  s.name = "foo"
  s.authors = "Joe Foo"
  s.version = "1.0"
  s.summary = "a gem"
  s.files = Dir["lib/*.rb"]
end
objects
node.js python php ruby
define class
 
function Int(i) {
  this.value = i === undefined ? 0 : i;
}
class Int(object):
  def __init__(self, v=0):
    self.value = v
class Int
{
  public $value;
  function __construct($int=0)
  {
    $this->value = $int;
  }
}
class Int
  attr_accessor :value
  def initialize(i=0)
    @value = i
  end
end
create object
 
let i = new Int();
let i2 = new Int(7);
i = Int()
i2 = Int(7)
$i = new Int();
$i2 = new Int(7);
i = Int.new
i2 = Int.new(7)
get and set instance variable
 
let v = i.value;
i.value = v + 1;
v = i.value
i.value = v + 1
$v = $i->value;
$i->value = $v + 1;
v = i.value
i.value = v + 1
instance variable visibility public public; attributes starting with underscore private by convention visibility must be declared private by default; use attr_reader, attr_writer, attr_accessor to make public
define method
 
// inside constructor:
this.plus = function(v) {
  return this.value + v;
};

// outside constructor:
Int.prototype.plus = function (v) {
  return this.value + v;
}
def plus(self,v):
  return self.value + v
function plus($i)
{
  return $this->value + $i;
}
def plus(i)
  value + i
end
invoke method
 
i.plus(3); i.plus(7) $i->plus(7) i.plus(7)
define class method @classmethod
def get_instances(cls):
  return Counter.instances
class Foo
  def Foo.one
    puts "one"
  end
end
invoke class method
 
Counter.get_instances() Counter::getInstances() Foo.one
define class variable class Foo:
  instances = 0
class Foo
  @@instances = 1
end
get and set class variable class Foo:
  def init(self):
    Foo.instances += 1
class Foo
  def initialize
    @@instances += 1
  end
end
handle undefined method invocation
 
def __getattr__(self, name):
  s = 'no def: ' + name + ' arity: %d'

  return lambda *a: print(s % len(a))
function __call($name, $args)
{
  $argc = count($args);
  echo "no def: $name " .
    "arity: $argc\n";
}
def method_missing(name, *a)
  puts "no def: #{name}" +
    " arity: #{a.size}"
end
alias method class Point

  attr_reader :x, :y, :color

  alias_method :colour, :color

  def initialize(x, y, color=:black)
    @x, @y = x, y
    @color = color
  end
end
destructor
 
def __del__(self):
  print('bye, %d' % self.value)
function __destruct()
{
  echo "bye, $this->value\n";
}
val = i.value
ObjectSpace.define_finalizer(int) {
  puts "bye, #{val}"
}
subclass
 
class Counter(Int):

  instances = 0

  def __init__(self, v=0):
    Counter.instances += 1
    Int.__init__(self, v)

  def incr(self):
    self.value += 1
class Counter extends Int
{
  private static $instances = 0;
  function __construct($int=0)
  {
    Counter::$instances += 1;
    parent::__construct($int);
  }
  function incr()
  {
    $this->value++;
  }
  static function getInstances()
  {
    return $instances;
  }
}
class Counter < Int

  @@instances = 0

  def initialize
    @@instances += 1
    super
  end

  def incr
    self.value += 1
  end

  def self.instances
    @@instances
  end
end
reflection
node.js python php ruby
object id
 
none id(o) o.object_id
inspect type
 
typeof([]) === 'object' type([]) == list gettype(array()) == "array"

returns object for objects
[].class == Array
basic types number
string
boolean
undefined
function
object

// these evaluate as 'object':
typeof(null)
typeof([])
typeof({})
NoneType
bool
int
long
float
str
SRE_Pattern
datetime
list
array
dict
object
file
NULL
boolean
integer
double
string
array
object
resource
unknown type
NilClass
TrueClass
FalseClass
Fixnum
Bignum
Float
String
Regexp
Time
Array
Hash
Object
File
inspect class // returns prototype object:
Object.getPrototypeOf(o)
o.__class__ == Foo
isinstance(o, Foo)
returns FALSE if not an object:
get_class($o) == "Foo"
o.class == Foo
o.instance_of?(Foo)
inspect class hierarchy let pa = Object.getPrototypeOf(o)
// prototype's of prototype object:
let grandpa = Object.getPrototypeOf(pa)
o.__class__.__bases__ get_parent_class($o) o.class.superclass
o.class.included_modules
has method?
 
o.reverse && typeof(o.reverse) === 'function' hasattr(o, 'reverse') method_exists($o, "reverse") o.respond_to?("reverse")
message passing
 
not a standard feature for i in range(1,10):
  getattr(o, 'phone'+str(i))(None)
for ($i = 1; $i <= 10; $i++) {
  call_user_func(array($o,
    "phone$i"), NULL);
}
(1..9).each do |i|
  o.send("phone#{i}=", nil)
end
eval
 
eval('1 + 1') argument of eval must be an expression:
while True:
  print(eval(sys.stdin.readline()))
eval evaluates to argument of return statement or NULL:
while ($line = fgets(STDIN)) {
  echo eval($line) . "\n";
}
loop do
  puts eval(gets)
end
list object methods
 
[m for m in dir(o)
  if callable(getattr(o,m))]
get_class_methods($o) o.methods
list object attributes
 
dir(o) get_object_vars($o) o.instance_variables
list loaded libraries # relative to directory in lib path:
$LOADED_FEATURES
$"
list loaded namespaces dir() Class.constants.select do |c|
  Module.const_get(c).class == Class
end
inspect namespace import urlparse

dir(urlparse)
require 'uri'

URI.constants
URI.methods
URI.class_variables
pretty-print
 
let d = {"lorem": 1, "ipsum": [2, 3]};
console.log(JSON.stringify(d, null, 2));
import pprint

d = {'lorem':1, 'ipsum':[2,3]}

pprint.PrettyPrinter().pprint(d)
$d = array("lorem"=>1,
  "ipsum"=>array(2,3));

print_r($d);
require 'pp'

d = {'lorem' => 1, 'ipsum' => [2, 3]}

pp d
source line number and file name import inspect

cf = inspect.currentframe()
cf.f_lineno
cf.f_code.co_filename
__LINE__
__FILE__
__LINE__
__FILE__
command line documentation $ pydoc math
$ pydoc math.atan2
none $ ri -c
$ ri Math
$ ri Math.atan2
net and web
node.js python php ruby
get local hostname, dns lookup, reverse dns lookup import socket

host = socket.gethostname()
ip = socket.gethostbyname(host)
host2 = socket.gethostbyaddr(ip)[0]
$host = gethostname();
$ip = gethostbyname($host);
$host2 = gethostbyaddr($ip);
require 'socket'

hostname = Socket.gethostname

ip = Socket.getaddrinfo(
  Socket.gethostname,
  "echo")[0][3]

host2 = Socket.gethostbyaddr(ip)[0]
http get
 
// npm install request
let request = require('request');

request('http://www.google.com',
  function(err, resp, body) {
    if (!err && resp.statusCode == 200) {
      console.log(body);
    }
  }
);
import httplib

url = 'www.google.com'
conn = httplib.HTTPConnection(url)
conn.request("GET", '/')
resp = conn.getresponse()
if resp.status == httplib.OK:
  s = resp.read()
$url = 'http://www.google.com';
$s = file_get_contents($url);
require 'net/http'

url = "www.google.com"
r = Net::HTTP.start(url, 80) do |f|
  f.get("/")
end
if r.code == "200"
  s = r.body
end
http post
 
import httplib
import urllib

url = 'www.acme.com'
conn = httplib.HTTPConnection(url)
data = urllib.urlencode({
  'item': 'anvil',
  'qty': 1})
conn.request('POST', '/orders', data)
resp = conn.getresponse()
if resp.status == httplib.OK:
  s = resp.read()
serve working directory $ python -m http.server 8000 $ php -S localhost:8000 $ ruby -rwebrick -e \
'WEBrick::HTTPServer.new(:Port => 8000, '\
':DocumentRoot => Dir.pwd).start'
absolute url
from base and relative url
import urlparse

urlparse.urljoin('http://google.com',
  'analytics')
none require 'uri'

URI.join("http://google.com", "analytics")
parse url # Python 3 location: urllib.parse
import urlparse

url = 'http://google.com:80/foo?q=3#bar'
up = urlparse.urlparse(url)

protocol = up.scheme
hostname = up.hostname
port = up.port
path = up.path
query_str = up.query
fragment = up.fragment

# returns dict of lists:
params = urlparse.parse_qs(query_str)
$url = "http://google.com:80/foo?q=3#bar";
$up = parse_url($url);

$protocol = $up["scheme"];
$hostname = $up["host"];
$port = $up["port"];
$path = $up["path"];
$query_str = $up["query"];
$fragment = $up["fragment"];

# $params is associative array; if keys
# are reused, later values overwrite
# earlier values

parse_str($query_str, $params);
require 'uri'

url = "http://google.com:80/foo?q=3#bar"
up = URI(url)

protocol = up.scheme
hostname = up.host
port = up.port
path = up.path
query_str = up.query
fragment = up.fragment

# Ruby 1.9; returns array of pairs:
params = URI.decode_www_form(query_str)
url encode/decode
 
# Python 3 location: urllib.parse
import urllib

urllib.quote_plus("lorem ipsum?")
urllib.unquote_plus("lorem+ipsum%3F")
urlencode("lorem ipsum?")
urldecode("lorem+ipsum%3F")
require 'cgi'

CGI::escape("lorem ipsum?")
CGI::unescape("lorem+ipsum%3F")
html escape
escape character data, escape attribute value, unescape html entities
import cgi
from HTMLParser import HTMLParser

s = cgi.escape('<>&')
s2 = cgi.escape('<>&"', True)
s3 = HTMLParser().unescape(s2)
$s = htmlspecialchars("<>&");

$s2 = htmlspecialchars("<>&\"'",
  ENT_NOQUOTES | ENT_QUOTES);

$s3 = htmlspecialchars_decode($s2);
require 'cgi'

s2 = CGI.escapeHTML('<>&"')
s3 = CGI.unescapeHTML(s2)
base64 encode/decode import base64

s = open('foo.png').read()
b64 = base64.b64encode(s)
s2 = base64.b64decode(b64)
$s = file_get_contents("foo.png");
$b64 = base64_encode($s);
$s2 = base64_decode($b64);
require 'base64'

s = File.open("foo.png").read
b64 = Base64.encode64(s)
s2 = Base64.decode64(b64)
databases
node.js python php ruby
mysql # install MySQL dev files, then
# $ sudo pip install MySQL-python

import MySQLdb

conn = MySQLdb.Connect(
  db='customers',
  user='joe',
  passwd='xyz123',
  host='127.0.0.1')
sql = 'select id from cust where name = %s'
cur.execute(sql, ('Bob',))
for row in cur:
  print('id: ' + row[0])
conn.close()
# $ sudo gem install mysql
require 'mysql'

conn = Mysql.new
conn.select_db("customers")
sql = 'select id from cust where name = ?'
stmt = con.prepare(sql)
stmt.execute('Bob')
stmt.each do |row|
  puts row[0]
end
mongodb # $ sudo pip install pymongo
import pymongo

client = pymongo.Connection('localhost')
db = conn['customers']
cur = db.find()
for doc in cur:
  print(doc)
# $ sudo gem install mongo bson_ext
require 'mongo'

host = 'localhost'
port = 27017
conn = Mongo::MongoClient.new(host, port)
db = conn['customers']
coll = db['cust']
coll.find().each {|doc| puts doc}
redis # $ sudo pip install redis
import redis

conn = redis.Redis(
  host='localhost',
  port=6379,
  password='xyz123')
conn.set('Bob', 123)
value = conn.get('Bob')
print(value)
unit tests
node.js python php ruby
test class // npm install -g nodeunit

exports.testFoo = function(test) {
  test.ok(true, 'not true!.');
  test.done();
}
import unittest

class TestFoo(unittest.TestCase):
  def test_01(self):
    self.assertTrue(True, 'not True!')

if __name__ == '__main__':
  unittest.main()
# pear install pear.phpunit.de/PHPUnit

<?php

Class FooTest extends
  PHPUnit_Framework_TestCase
{
  public function test_01()
  {
    $this->assertTrue(true,
      "not true!");
  }
}
?>
require 'test/unit'

class TestFoo < Test::Unit::TestCase
  def test_01
    assert(true, "not true!")
  end
end
run tests, run test method $ nodeunit test_foo.js

$ nodeunit -t testFoo test_foo.js
$ python test_foo.py
$ python test_foo.py TestFoo.test_01
$ phpunit test_foo.php

$ phpunit --filter test_01 test_foo.php
$ ruby test_foo.rb
$ ruby test_foo.rb -n test_01
equality assertion let s = 'do re mi';
test.equals(s, 'do re mi');
s = 'do re me'
self.assertEqual('do re me',
  s,
  's: {}'.format(s))
$s = "do re me";
$this->assertEquals($s, "do re mi");

# also asserts args have same type:
$this->assertSame($s, "do re mi");
s = "do re me"
assert_equal("do re me", s)
approximate assertion x = 10.0 * (1.0 / 3.0)
y = 10.0 / 3.0

# default for delta is 0.1**7
self.assertAlmostEqual(x, y, delta=0.1**6)
$x = 10.0 * (1.0 / 3.0);
$y = 10.0 / 3.0;

$this->assertEquals($x, $y,
  "not within delta",
  pow(0.1, 6));
x = 10.0 * (1.0 / 3.0)
y = 10.0 / 3.0

# default for delta is 0.001
assert_in_delta(x, y, 0.1**6)
regex assertion s = 'lorem ipsum'
# uses re.search, not re.match:
self.assertRegexpMatches(s, 'lorem')
$s = "lorem ipsum";
$this->assertRegExp("/lorem/", $s);
s = "lorem ipsum"
assert_match(/lorem/, s)
exception assertion a = []
with self.assertRaises(IndexError):
  a[0]
class Bam extends Exception {};

public function test_exc {
  $this->SetExpectedException("Bam");
  throw new Bam("bam!");
}
assert_raises(ZeroDivisionError) do
  1 / 0
end
mock method # added in Python 3.3:
from unittest import mock

foo = Foo()
foo.run = mock.MagicMock(return_value=7)

self.assertEqual(7, foo.run(13))
foo.run.assert_called_once_with(13)
$mock = $this->getMock('Foo', ['foo']);
$mock->expects($this->once())
  ->method('foo')
  ->with(13)
  ->will($this->returnValue(7));

$mock->foo(13);
# gem install mocha
require 'mocha'

foo = mock()
foo.expects(:run).returns(7).with(13).once

foo.run(13)
setup exports.setUp = function(callback) {
  console.log('setting up...');
  callback();
}
# in class TestFoo:
def setUp(self):
  print('setting up')
public function setUp()
{
  echo "setting up\n";
}
# in class TestFoo:
def setup
  puts "setting up"
end
teardown exports.tearDown = function(callback) {
  console.log('tearing down...');
  callback();
}
# in class TestFoo:
def tearDown(self):
  print('tearing down')
public function tearDown()
{
  echo "tearing down\n";
}
# in class TestFoo:
def teardown
  puts "tearing down"
end
debugging
node.js python php ruby
check syntax
 
$ node -c foo.js import py_compile

# precompile to bytecode:
py_compile.compile('foo.py')
$ php -l foo.php $ ruby -c foo.rb
check for errors $ npm install -g semistandard
$ semistandard foo.js
$ sudo pip install pylint
$ pylint foo.py
$ sudo gem install rubocop
$ rubocop -D foo.rb
check style $ npm install -g semistandard
$ semistandard foo.js
$ sudo pip install pep8
$ pep8 foo.py
$ sudo gem install rubocop
$ rubocop -D foo.rb
run debugger $ node debug foo.js $ python -m pdb foo.py $ sudo gem install ruby-debug
$ rdebug foo.rb
benchmark code console.time('product');
let n = 1;
for (let i = 1; i < 1000*1000; ++i) {
  ++n;
}
console.timeEnd('product');
import timeit

timeit.timeit('i += 1',
  'i = 0',
  number=1000000)
require 'benchmark'

n = 1_000_000
i = 0
puts Benchmark.measure do
  n.times { i += 1 }
end
profile code $ node --prof foo.js
$ node --prof-process *v8.log
$ python -m cProfile foo.py $ sudo gem install ruby-prof
$ ruby-prof foo.rb
____________________________________________________ ____________________________________________________ ____________________________________________________ ____________________________________________________

Streams

read line from stdin

How to read a line from standard input.

The illustrated function read the standard input stream until a end-of-line marker is found or the end of the stream is encountered. Only in the former case will the returned string be terminated by an end-of-line marker.

php:

fgets takes an optional second parameter to specify the maximum line length. If the length limit is encountered before a newline, the string returned will not be newline terminated.

ruby:

gets takes an optional parameter to specify the maximum line length. If the length limit is encountered before a newline, the string returned will not be newline terminated.

There are both global variable and constant names for the standard file handles:

$stdin STDIN
$stdout STDOUT
$stderr STDERR

remove end-of-line

Remove a newline, carriage return, or carriage return newline pair from the end of a line if there is one.

php:

chop removes all trailing whitespace. It is an alias for rtrim.

python:

Python strings are immutable. rstrip returns a modified copy of the string. rstrip('\r\n') is not identical to chomp because it removes all contiguous carriage returns and newlines at the end of the string.

ruby:

chomp! modifies the string in place. chomp returns a modified copy.

write to stdout

How to write a line to standard out. The line will be terminated by an operating system appropriate end of line marker.

python:

print appends a newline to the output. To suppress this behavior, put a trailing comma after the last argument. If given multiple arguments, print joins them with spaces.

In Python 2 print parses as a keyword and parentheses are not required:

print "Hello, World!"

ruby:

puts appends a newline to the output. print does not.

write format to stdout

How to format variables and write them to standard out.

The function printf from the C standard library is a familiar example. It has a notation for format strings which uses percent signs %. Many other languages provide an implementation of printf.

write to stderr

How to write a string to standard out.

open file for reading

How to open a file for reading.

python:

The open function returns an IO object with a read method which returns str objects. In Python 3, these are strings of Unicode characters, but in Python 2 they are arrays of bytes.

In Python 2, to get an IO object with a read method which returns unicode objects, use codecs.open():

import codecs

f = codecs.open('/etc/hosts', encoding='utf-8')

ruby:

When File.open is given a block, the file is closed when the block terminates.

open for reading bytes

read line

How to read up to the next newline in a file.

iterate by line

How to iterate over a file line by line.

read file into string

How to put the contents of a file into a single string.

read file into array of string

How to put the lines of a file into an array of strings.

read fixed length

How to read up to a pre-specified number of bytes or characters from a file.

node:

Node allows reading a pre-specified max number of bytes. It is possible that the last byte can be an incomplete part of a character. buf.toString('utf-8') returns a malformed string with an incomplete character at the end.

readSync() returns the number of bytes read. If this is less than the length of the buffer, then the remaining bytes are not overwritten.

Buffer.alloc can take an optional 2nd arg to indicate the fill byte value; the bytes are zero-filled by default.

read char

How to read a character from a stream.

All the languages in this stream represent characters with strings of length one.

read serialized data

How to read serialized data from a file, using a language-specific serialization method.

open for writing

How to open a file for writing. If the file exists its contents will be overwritten.

open for writing bytes

open for appending

How to open a file with the seek point at the end of the file. If the file exists its contents will be preserved.

write string

How to write a string to a file handle.

write line

How to write a line to a file handle. An operating system appropriate end-of-line marker is appended to the output.

**php:

Newlines in strings are translated to the operating system appropriate line terminator unless the file handle was opened with a mode string that contained 'b'.

python:

When file handles are opened with the mode strings 'r', 'w', or 'a', the file handle is in text mode. In text mode the operating system line terminator is translated to '\n' when reading and '\n' is translated back to the operating system line terminator when writing. The standard file handles sys.stdin, sys.stdout, and sys.stderr are opened in text mode.

When file handles are opened with the mode strings 'rb', 'rw', or 'ra', the file handle is in binary mode and line terminator translation is not performed. The operating system line terminator is available in os.linesep.

write format

write char

write serialized data

close

How to close an open file.

close on block exit

How to have an open file closed when a block is exited.

python:

File handles are closed when the variable holding them is garbage collected, but there is no guarantee when or if a variable will be garbage collected.

ruby:

File handles are closed when the variable holding them is garbage collected, but there is no guarantee when or if a variable will be garbage collected.

flush

How to flush a file handle that has been written to.

File handles often have buffering built into them. A buffer collects the result of multiple writes and the data is later written to disk with a single system call write. A flush ensures that the data is on disk, or at least in the operating system disk cache, so that other processes can see it.

position

How to get or set the file handle seek point.

The seek point is where the next read on the file handle will begin. The seek point is measured in bytes starting from zero.

open temporary file

How to get a file handle to a file that will be removed automatically sometime between when the file handle is closed and the interpreter exits.

The file is guaranteed not to have existed before it was opened.

The file handle is opened for both reading and writing so that the information written to the file can be recovered by seeking to the beginning of the file and reading from the file handle.

On POSIX operating systems it is possible to unlink a file after opening it. The file is removed from the directory but continues to exist as long as the file handle is open. This guarantees that no other process will be able to read or modify the file contents.

php:

Here is how to create a temporary file with a name:

$path = tempnam(sys_get_temp_dir(), "");
$f = fopen($path, "w+");

python:

To unlink a temporary file on open, used TemporaryFile instead of NamedTemporaryFile:

import tempfile

f = tempfile.TemporaryFile()

open in memory file

How to create a file descriptor which writes to an in-memory buffer.

python:

StringIO also supports the standard methods for reading input. To use them the client must first seek to the beginning of the in-memory file:

f = StringIO()
f.write('lorem ipsum\n')
f.seek(0)
r.read()

Asynchronous Events

Files

file exists test, file regular test

How to test whether a file exists; how to test whether a file is a regular file (i.e. not a directory, special device, or named pipe).

file size

How to get the file size in bytes.

is file readable, writable, executable

How to test whether a file is readable, writable, or executable.

python:

The flags can be or'ed to test for multiple permissions:

os.access('/etc/hosts', os.R_OK | os.W_OK | os.X_OK)

set file permissions

How to set the permissions on the file.

For Perl, Python, and Ruby, the mode argument is in the same format as the one used with the Unix chmod command. It uses bitmasking to get the various permissions which is why it is normally an octal literal.

The mode argument should not be provided as a string such as "0755". Python and Ruby will raise an exception if a string is provided. Perl will convert "0755" to 755 and not 0755 which is equal to 493 in decimal.

last modification time

How to get the last modification time of a file.

For a regular file, the last modification time is the most recent time that the contents were altered.

For a directory, the last modification time is the most recent time that a file in the directory was added, removed, or renamed.

copy file, remove file, rename file

How to copy a file; how to remove a file; how to rename a file.

create symlink, symlink test, readlink

How to create a symlink; how to test whether a file is a symlink; how to get the target of a symlink.

generate unused file name

How to generate an unused file name. The file is created to avoid a race condition with another process looking for an unused file name.

The file is not implicitly deleted.

File Formats

parse csv

How to parse a CSV file and iterate through the rows.

generate csv

How to generate a CSV file from an array of tuples.

parse json

How to decode a string of JSON.

JSON data consists of objects, arrays, and JSON values. Objects are dictionaries in which the keys are strings and the values are JSON values. Arrays contain JSON values. JSON values can be objects, arrays, strings, numbers, true, false, or null.

A JSON string is JSON data encoded using the corresponding literal notation used by JavaScript source code.

JSON strings are sequences of Unicode characters. The following backslash escape sequences are supported:

  \" \\ \/ \b \f \n \r \t \uhhhh.

generate json

How to encode data as a JSON string.

parse yaml

How to parse a string of YAML.

YAML is sometimes used to serialize objects. Deserializing such YAML results in the constructor of the object being executed. The YAML decoding techniques illustrated here are "safe" in that they will not execute code, however.

generate yaml

How to generate a string of YAML.

parse xml

How to parse XML and extract nodes using XPath.

ruby:

Another way of handling an XPath expression which matches multiple nodes:

XPath.each(doc,"/a/b/c") do |node|
  puts node.text
end

generate xml

How to build an XML document.

An XML document can be constructed by concatenating strings, but the techniques illustrated here guarantee the result to be well-formed XML.

parse html

How to parse an HTML document.

Directories

working directory

How to get and set the working directory.

build pathname

How to construct a pathname without hard coding the system file separator.

dirname and basename

How to extract the directory portion of a pathname; how to extract the non-directory portion of a pathname.

absolute pathname

How to get the get the absolute pathname for a pathname. If the pathname is relative the working directory will be appended.

In the examples provided, if /foo/bar is the working directory and .. is the relative path, then the return value is foo

iterate over directory by file

How to iterate through the files in a directory.

In PHP, Perl, and Ruby, the files representing the directory itself . and the parent directory .. are returned.

php:

The code in the example will stop if a filename which evaluates as FALSE is encountered. One such filename is "0". A safer way to iterate through the directory is:

if ($dir = opendir("/etc")) {
  while (FALSE !== ($file = readdir($dir))) {
    echo "$file\n";
  }
  closedir($dir);
}

python:

file() is the file handle constructor. file can be used as a local variable name but doing so hides the constructor. It can still be invoked by the synonym open(), however.

os.listdir() does not return the special files . and .. which represent the directory itself and the parent directory.

glob paths

How to iterate over files using a glob pattern.

Glob patterns employ these special characters:

* matches zero or more characters, the first of which is not . and none of which is /
? matches one character
[ ] matches one character from the list inside the brackets
\ escapes one of the previous characters

Use glob patterns instead of simple directory iteration when

  • dot files, including the directory itself (.) and the parent directory (..), should skipped
  • a subset of the files in a directory, where the subset can be specified with a glob pattern, is desired
  • files from multiple directories, where the directories can be specified with a glob pattern, are desired
  • the full pathnames of the files is desired

php:

glob takes a second argument for flags. The flag GLOB_BRACE enables brace notation.

python:

glob.glob returns a list. glob.iglob accepts the same arguments and returns an iterator.

ruby:

Ruby globs support brace notation.

A brace expression matches any of the comma separated strings inside the braces.

Dir.glob("/{bin,etc,usr}/*").each do |path|
  puts path
end

make directory

How to create a directory.

If needed, the examples will create more than one directory.

No error will result if a directory at the pathname already exists. An exception will be raised if the pathname is occupied by a regular file, however.

recursive copy

How to perform a recursive copy. If the source is a directory, then the directory and all its contents will be copied.

remove empty directory

How to remove an empty directory. The operation will fail if the directory is not empty.

remove directory and contents

How to remove a directory and all its contents.

directory test

How to determine if a pathname is a directory.

generate unused directory

How to generate an unused directory. The directory is created to avoid a race condition with another process looking for an unused directory.

The directory is not implicitly deleted.

ruby:

When Dir.mktmpdir is provided with a block the directory is deleted after the block finishes executing:

require 'tmpdir'
require 'fileutils'

Dir.mktmpdir("/tmp/foo") do |path|
  puts path
  FileUtils.cp("/etc/hosts", "#{path}/hosts")
end

system temporary file directory

The name of the system provided directory for temporary files.

On Linux the directory is often /tmp, and the operating system is often configured to delete the contents of /tmp at boot.

Processes and Environment

command line arguments

How to access arguments provided at the command line when the script was run; how to get the name of the script.

environment variable

How to get and set an environment variable. If an environment variable is set the new value is inherited by child processes.

php:

putenv returns a boolean indicating success. The command can fail because when PHP is running in safe mode only some environment variables are writable.

get pid, parent pid

How to get the process id of the interpreter process; how to get the id of the parent process.

ruby:

The process pid is also available in the global variable $$.

user id and name

How to get the user id of the interpreter process; how to get the username associated with the user id.

When writing a setuid application on Unix, there is a distinction between the real user id and the effective user id. The code examples return the real user id.

The process may be able to determine the username by inspecting environment variables. A POSIX system is required to set the environment variable LOGNAME at login. Unix systems often set USER at login, and Windows systems set %USERNAME%. There is nothing to prevent the user from altering any of these environment variables after login. The methods illustrated in the examples are thus more secure.

python:

How to get the effective user id:

os.geteuid()

ruby:

How to get the effective user id:

Process.euid

exit

python:

It is possible to register code to be executed upon exit:

import atexit
atexit.register(print, "goodbye")

It is possible to terminate a script without executing registered exit code by calling os._exit.

ruby:

It is possible to register code to be executed upon exit:

at_exit { puts "goodbye" }

The script can be terminated without executing registered exit code by calling exit!.

set signal handler

How to register a signal handling function.

external command

How to execute an external command.

shell-escaped external command

How to prevent shell injection.

command substitution

How to invoke an external command and read its output into a variable.

The use of backticks for this operation goes back to the Bourne shell (1977).

python:

A more concise solution is:

file = os.popen('ls -l /tmp').read()

os.popen was marked as deprecated in Python 2.6 but it is still available in Python 2.7 and Python 3.2.

ruby:

%x can be used with any delimiter. If the opening delimiter is (, [, or {, the closing delimiter must be ), ], or }.

Option Parsing

How to process command line options.

We describe the style used by getopt_long from the C standard library. The characteristics of this style are:

  • Options can be short or long. Short options are a single character preceded by a hyphen. Long options are a word preceded by two hyphens.
  • A double hyphen by itself can be used to terminate option processing. Arguments after the double hyphen are treated as positional arguments and can start with a hyphen.
  • Options can be declared to be with or without argument. Options without argument are used to set a boolean value to true.
  • Short options without argument can share a hyphen.
  • Long options can be separated from their argument by a space or an equals sign (=). Short options can be separated from their argument by nothing, a space, or an equals sign (=).

The option processing function should identify the positional arguments. These are the command line arguments which are not options, option arguments, or the double hyphen used to terminate option processing. getopt_long permits options to occur after positional arguments.

boolean flag

How to define a option flag which sets a boolean variable.

string option

How to define an option flag which takes an argument and sets a string variable.

numeric option

How to define an option flag which takes an argument and sets a numeric variable.

unrecognized option behavior

required option

default option

delimited options

repeated options

positional parameters

positional parameters as array

usage

subcommand

Libraries and Namespaces

Terminology used in this sheet:

  • library: code in its own file that can be included, loaded, or linked by client code.
  • client: code which calls code in a separate file.
  • top-level file or top-level script: the file containing the code in the program which executes first.
  • load: to add definitions in a file to the text of a running process.
  • namespace: a set of names that can be imported as a unit.
  • import: to add definitions defined elsewhere to a scope.
  • unqualified import: to add definitions to a scope using the same identifiers as where they are defined.
  • qualified import: to add definitions to a scope. The identifiers in the scope are derived from the original identifiers in a formulaic manner. Usually the name of the namespace is added as a prefix.
  • label: one of the parts of a qualified identifier.
  • alias import: to add a definition to a scope under an identifier which is specified in the import statement.
  • package: one or more libraries that can be installed by a package manager.

load library

Execute the specified file. Normally this is used on a file which only contains declarations at the top level.

php:

include_once behaves like require_once except that it is not fatal if an error is encountered executing the library.

load library in subdirectory

How to load a library in a subdirectory of the library path.

hot patch

How to reload a library. Altered definitions in the library will replace previous versions of the definition.

php:

Also include.

load error

How errors which are encountered while loading libraries are handled.

main routine in library

How to put code in a library which executes only when the file is run as a top-level script.

library path

The library path is a list of directory paths which are searched when loading libraries.

library path environment variable

How to augment the library path by setting an environment variable before invoking the interpreter.

library path command line option

How to augment the library path by providing a command line option when invoking the interpreter.

simple global identifiers

multiple label identifiers

label separator

The punctuation used to separate the labels in the full name of a subnamespace.

root namespace definition

namespace declaration

How to declare a section of code as belonging to a namespace.

subnamespace declaration

How to declare a section of code as belonging to a subnamespace.

import namespace

import subnamespace

import all definitions in namespace

How to import all the definitions in a namespace.

import definitions

How to import specific definitions from a namespace.

list installed packages, install a package

How to show the installed 3rd party packages, and how to install a new 3rd party package.

python

Two ways to list the installed modules and the modules in the standard library:

$ pydoc modules
$ python
>>> help('modules')

Most 3rd party Python code is packaged using distutils, which is in the Python standard library. The code is placed in a directory with a setup.py file. The code is installed by running the Python interpreter on setup.py:

package specification format

The format of the file used to specify a package.

python:

distutils.core reference

How to create a Python package using distutils. Suppose that the file foo.py contains the following code:

def add(x, y):
    return x+y

In the same directory as foo.py create setup.py with the following contents:

#!/usr/bin/env python

from distutils.core import setup

setup(name='foo',
      version='1.0',
      py_modules=['foo'],
     )

Create a tarball of the directory for distribution:

$ tar cf foo-1.0.tar foo
$ gzip foo-1.0.tar

To install a tar, perform the following:

$ tar xf foo-1.0.tar.gz
$ cd foo
$ sudo python setup.py install

If you want people to be able to install the package with pip, upload the tarball to the Python Package Index.

ruby:

gemspec attributes

For an example of how to create a gem, create a directory called foo. Inside it create a file called lib/foo.rb which contains:

def add(x, y)
  x + y
end

Then create a file called foo.gemspec containing:

spec = Gem::Specification.new do |s|
  s.name = 'foo'
  s.authors = 'Joe Foo'
  s.version = '1.0'
  s.summary = 'a gem'
  s.files = Dir['lib/*.rb']
end

To create the gem, run this command:

$ gem build foo.gemspec

A file called foo-1.0.gem is created. To install foo.rb run this command:

$ gem install foo-1.0.gem

Objects

An object is a set of functions called methods which have shared access to the object's instance variables. An object's methods and instance variables are collectively called its members. If a member of an object can be accessed or invoked by code which is not in a member of the object, it is public. Otherwise it is private.

A class is a set of objects which have the same method definitions. The objects in the set are instances of the class. Functions defined in the class namespace which are not object methods are called class methods. A class method which returns instances of the class is called a factory method. If there is class method which is responsible for creating all instances, it is called a constructor. The existence of a constructor does not preclude the existence of other factory methods since they can invoke the constructor and return its return value.

A class may contain class variables. These are global variables defined in the namespace of the class.

A method which returns the value of an instance variable is called a getter. A method which sets the value of an instance variable is called a setter. Getters and setters and seem pointless at first blush as one could make the underlying instance variable public instead. In practice getters and setters make code more maintainable. Consistent use of getters and setters conforms with the Uniform Access Principle and makes the API presented by an object to its clients simpler.

Perl instance variables are private, so Perl enforces a good practice at the cost of requiring boilerplate code for defining getters and setters.

Python instance variables are public. Although this permits concise class definitions, a maintainer of a Python class may find it difficult to replace an instance variable with a derived value when clients are accessing the instance variable directly. With an old-style Python class, the maintainer can't make the change without breaking the client code. With a new-style class the maintainer can replace an instance variable with a getter and setter and mark them with the @property decorator.

Ruby, like Perl, has private instance variables. It has the directives attr_reader, attr_writer, and attr_accessor for defining getters and setters. Ruby classes are objects and in particular they are instances of the Module class. The directives attr_reader, attr_writer, and attr_accessor are instance methods defined in the Module class which execute when the class block executes.

define class

php:

Properties (i.e. instance variables) must be declared public, protected, or private. Methods can optionally be declared public, protected, or private. Methods without a visibility modifier are public.

python:

As of Python 2.2, classes are of two types: new-style classes and old-style classes. The class type is determined by the type of class(es) the class inherits from. If no superclasses are specified, then the class is old-style. As of Python 3.0, all classes are new-style.

New-style classes have these features which old-style classes don't:

  • universal base class called object.
  • descriptors and properties. Also the __getattribute__ method for intercepting all attribute access.
  • change in how the diamond problem is handled. If a class inherits from multiple parents which in turn inherit from a common grandparent, then when checking for an attribute or method, all parents will be checked before the grandparent.

create object

How to create an object.

get and set attribute

How to get and set an attribute.

python:

Defining explicit setters and getters in Python is considered poor style. Extra logic can be achieved without disrupting the clients of the class by creating a property:

def getValue(self):
  print("getValue called")
  return self.__dict__['value']

def setValue(self,v):
  print("setValue called")
  self.__dict__['value'] = v

value = property(fget=getValue, fset = setValue)

instance variable visibility

How instance variable access works.

define method

How to define a method.

invoke method

How to invoke a method.

destructor

How to define a destructor.

python:

A Python destructor is not guaranteed to be called when all references to an object go out of scope, but apparently this is how the CPython implementations work.

ruby:

Ruby lacks a destructor. It is possible to register a block to be executed before the memory for an object is released by the garbage collector. A ruby interpreter may exit without releasing memory for objects that have gone out of scope and in this case the finalizer will not get called. Furthermore, if the finalizer block holds on to a reference to the object, it will prevent the garbage collector from freeing the object.

method missing

How to handle when a caller invokes an undefined method.

php:

Define the method __callStatic to handle calls to undefined class methods.

python:

__getattr__ is invoked when an attribute (instance variable or method) is missing. By contrast, __getattribute__, which is only available in Python 3, is always invoked, and can be used to intercept access to attributes that exist. __setattr__ and __delattr__ are invoked when attempting to set or delete attributes that don't exist. The del statement is used to delete an attribute.

ruby:

Define the method self.method_missing to handle calls to undefined class methods.

define class method

invoke class method

How to invoke a class method.

define class variable

get and set class variable

method alias

How to create an alias for a method.

ruby:

Ruby provides the keyword alias and the method alias_method in the class Module. Inside a class body they behave idenitically. When called from inside a method alias has no effect but alias_method works as expected. Hence some recommend always using alias_method.

subclass

A subclass is a class whose objects contain all of the methods from another class called the superclass. Objects in the subclass should in principle be usable anywhere objects in the superclass can be used. The subclass may have extra methods which are not found in the superclass. Moreover it may replace method definitions in the superclass with its own definitions provided the signature remains the same. This is called overriding.

It is sometimes useful to define superclass which is never instantiated. Such a class is called an abstract class. An abstract class is way to share code between two or more subclasses or to define the API that two or more subclasses should implement.

Reflection

object id

How to get an identifier for an object or a value.

inspect type

php:

The PHP manual says that the strings returned by gettype are subject to change and advises using the following predicates instead:

is_null
is_bool
is_numeric
is_int
is_float
is_string
is_array
is_object
is_resource

All possible return values of gettype are listed.

basic types

inspect class

How to get the class of an object.

javascript:

inspect class hierarchy

has method?

python:

hasattr(o,'reverse') will return True if there is an instance variable named 'reverse'.

message passing

javascript:

The following works in Firefox:

var o = {}
o.__noSuchMethod__ = function(name) { alert('you called ' + name) }
o.whoopsie()

eval

How to interpret a string as code and return its value.

php:

The value of the string is the value of of the return statement that terminates execution. If execution falls off the end of the string without encountering a return statement, the eval evaluates as NULL.

python:

The argument of eval must be an expression or a SyntaxError is raised. The Python version of the mini-REPL is thus considerably less powerful than the versions for the other languages. It cannot define a function or even create a variable via assignment.

list object methods

list object attributes

python:

dir(o) returns methods and instance variables.

pretty print

How to display the contents of a data structure for debugging purposes.

source line number and file name

How to get the current line number and file name of the source code.

command line documentation

How to get documentation from the command line.

ruby:

Searching for Math.atan2 will return either class method or instance method documentation. If there is documentation for both one can be specific with the following notation:

$ ri Math::atan2
$ ri Math#atan2

Net and Web

get local hostname, dns lookup, reverse dns lookup

How to get the hostname and the ip address of the local machine without connecting to a socket.

The operating system should provide a method for determining the hostname. Linux provides the uname system call.

A DNS lookup can be performed to determine the IP address for the local machine. This may fail if the DNS server is unaware of the local machine or if the DNS server has incorrect information about the local host.

A reverse DNS lookup can be performed to find the hostname associated with an IP address. This may fail for the same reasons a forward DNS lookup might fail.

http get

How to make an HTTP GET request and read the response into a string.

http post

serve working directory

A command line invocation to start a single process web server which serves the working directory at http://localhost:8000.

$ sudo cpan -i IO::All

$ perl -MIO::All -e 'io(":8000")->fork->accept->(sub { $_[0] < io(-x $1 ? "./$1 |" : $1) if /^GET \/(.*) / })'

absolute url

How to construct an absolute URL from a base URL and a relative URL as documented in RFC 1808.

When constructing the absolute URL, the rightmost path component of the base URL is removed unless it ends with a slash /. The query string and fragment of the base URL are always removed.

If the relative URL starts with a slash / then the entire path of the base URL is removed.

If the relative URL starts with one or more occurrences of ../ then one or more path components are removed from the base URL.

The base URL and the relative URL will be joined by a single slash / in the absolute URL.

php:

Here is a PHP function which computes absolute urls.

parse url

How to extract the protocol, host, port, path, query string, and fragment from a URL. How to extract the parameters from the query string.

python:

urlparse can also be used to parse FTP URLs:

up = urlparse.urlparse('ftp://foo:bar@google.com/baz;type=binary')

# 'foo'
up.username

# 'bar'
up.password

# 'type=binary'
up.params

ruby:

How to parse an FTP URL:

up = URI('ftp://foo:bar@google.com/baz;type=binary')

# "foo"
 up.user

# up.password
"bar"

# "binary"
up.typecode

url encode/decode

How to URL encode and URL unencode a string.

URL encoding, also called percent encoding, is described in RFC 3986. It replaces all characters except for the letters, digits, and a few punctuation marks with a percent sign followed by their two digit hex encoding. The characters which are not escaped are:

A-Z a-z 0-9 - _ . ~

URL encoding can be used to encode UTF-8, in which case each byte of a UTF-8 character is encoded separately.

When form data is sent from a browser to a server via an HTTP GET or an HTTP POST, the data is percent encoded but spaces are replaced by plus signs + instead of %20. The MIME type for form data is application/x-www-form-urlencoded.

python:

In Python 3 the functions quote_plus, unquote_plus, quote, and unquote moved from urllib to urllib.parse.

urllib.quote replaces a space character with %20.

urllib.unquote does not replace + with a space character.

html escape

How to escape special characters in HTML character data; how to escape special characters in HTML attribute values; how to unescape HTML entities.

In character data, such as what occurs in between a start and end tag, the characters <, >, and & must be replaced by &lt;, &gt;, and &amp;.

Attribute values in HTML tags must be quoted if they contain a space or any of the characters "'`=<>. Attribute values can be double quoted or single quoted. Double quotes and single quotes can be escaped by using the HTMl entities &quot; and &apos;. It is not necessary to escape the characters <, >, and & inside quoted attribute values.

php:

The flag ENT_NOQUOTES to the function htmlspecialchars causes double quotes " to be escaped.

The flag ENT_QUOTES causes single quotes ' to be escaped.

base64 encode/decode

How to encode binary data in ASCII using the Base64 encoding scheme.

A popular Base64 encoding is the one defined by RFC 2045 for MIME. Every 3 bytes of input is mapped to 4 of these characters: [A-Za-z0-9/+].
If the input does not consist of a multiple of three characters, then the output is padded with one or two hyphens: =.

Whitespace can inserted freely into Base64 output; this is necessary to support transmission by email. When converting Base64 back to binary whitespace is ignored.

Unit Databases

Unit Tests

test class

How to define a test class and make a truth assertion.

The argument of a truth assertion is typically an expression. It is a good practice to include a failure message as a second argument which prints out variables in the expression.

run tests; run test method

How to run all the tests in a test class; how to run a single test from the test class.

equality assertion

How to test for equality.

python:

Note that assertEquals does not print the values of its first two arguments when the assertion fails. A third argument can be used to provide a more informative failure message.

approximate assertion

How to assert that two floating point numbers are approximately equal.

regex assertion

How to test that a string matches a regex.

exception assertion

How to test whether an exception is raised.

mock method

How to create a mock method.

A mock method is used when calling the real method from a unit test would be undesirable. The method that is mocked is not in the code that is being tested, but rather a library which is used by that code. Mock methods can raise exceptions if the test fails to invoke them or if the wrong arguments are provided.

python:

assert_called_once_with can takes the same number of arguments as the method being mocked.

If the mock method was called multiple times, the method assert_called_with can be used in place of asert_called_once_with to make an assertion about the arguments that were used in the most recent call.

A mock method which raises an exception:

foo = Foo()
foo.run = mock.Mock(side_effect=KeyError('foo'))

with self.assertRaises(KeyError):
  foo.run(13)

foo.run.assert_called_with(13)

ruby:

The with method takes the same number of arguments as the method being mocked.

Other methods are available for use in the chain which defines the assertion. The once method can be replaced by never or twice. If there is uncertainty about how often the method will be called one can used at_least_once, at_least(m), at_most_once, at_most(n) to set lower or upper bounds. times(m..n) takes a range to set both the lower and upper bound.

A mock method which raises an exception:

    foo = mock()
    foo.expects(:run).
      raises(exception = RuntimeError, message = 'bam!').
      with(13).
      once

    assert_raises(RuntimeError) do
      foo.run(13)
    end

There is also a method called yields which can be used in the chain which defines the assertion. It makes the mock method yield to a block. It takes as arguments the arguments it passes to the block.

setup

How to define a setup method which gets called before every test.

teardown

How to define a cleanup method which gets called after every test.

Debugging

check syntax

How to check the syntax of code without executing it.

check for errors

How to perform static analysis on the code to detect probably errors.

check style

How to detect or remove semantically insignificant variation in the source code.

run debugger

How to run a script under the debugger.

debugger commands

A selection of commands available when running the debugger. The gdb commands are provided for comparison.

cmd node debug python -m pdb rdebug gdb
help help h h h
list list(lines_of_context) l [first, last] l [first, last] l [first, last]
next statement n n n n
step into function s s s s
step out of function o
set breakpoint sb([file, ]line) b [file:]line
b function
b [file:]line
b class[.method]
b [file:]line
list breakpoints breakpoints b info b i b
delete breakpoint cb(file, line) cl num del num d num
continue c c c c
show backtrace bt w w bt
move up stack u u u
move down stack d down do
print expression repl

Inside repl use console.log() to
print expression; ^C to exit.
p expr p expr p expr
(re)run restart restart [arg1[, arg2 …]] restart [arg1[, arg2 …]] r [arg1[, arg2 …]]
quit debugger quit q q q

node:

One can insert a breakpoint by adding this statement to the source code:

debugger;

benchmark code

How to run a snippet of code repeatedly and get the user, system, and total wall clock time.

profile code

How to run the interpreter on a script and get the number of calls and total execution time for each function or method.

issue tracker | content of this page licensed under creative commons attribution-sharealike 3.0