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 Errrorexception | raises ImportErrorif library not found; exceptions generated when parsing library propagate to client | requireand require_onceraise fatal error if library not found; includeand include_once emit warnings | raises LoadErrorif 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 globalkeyword | 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 | foomust 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_accessorto 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: definit(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 objectfor 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 FALSEif 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 evalmust be an expression: while True: print(eval(sys.stdin.readline())) | evalevaluates to argument of returnstatement 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 | |
| ____________________________________________________ | ____________________________________________________ | ____________________________________________________ | ____________________________________________________ | |
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 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 forrtrim.
python:
Python strings are immutable.rstrip returns a modified copy of the string.rstrip('\r\n') is not identical tochomp 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.
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 2print parses as a keyword and parentheses are not required:
print "Hello, World!"ruby:
puts appends a newline to the output.print does not.
How to format variables and write them to standard out.
The functionprintf 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 ofprintf.
How to write a string to standard out.
How to open a file for reading.
python:
Theopen function returns an IO object with aread method which returnsstr 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 aread method which returnsunicode objects, usecodecs.open():
import codecsf = codecs.open('/etc/hosts', encoding='utf-8')ruby:
WhenFile.open is given a block, the file is closed when the block terminates.
How to read up to the next newline in a file.
How to iterate over a file line by line.
How to put the contents of a file into a single string.
How to put the lines of a file into an array of strings.
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.
How to read a character from a stream.
All the languages in this stream represent characters with strings of length one.
How to read serialized data from a file, using a language-specific serialization method.
How to open a file for writing. If the file exists its contents will be overwritten.
How to open a file with the seek point at the end of the file. If the file exists its contents will be preserved.
How to write a string to a file handle.
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 inos.linesep.
How to close an open file.
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.
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.
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.
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, usedTemporaryFile instead ofNamedTemporaryFile:
import tempfilef = tempfile.TemporaryFile()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()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).
How to get the file size in bytes.
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)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 Unixchmod command. It uses bitmasking to get the various permissions which is why it is normally an octal literal.
The mode argument shouldnot 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.
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.
How to copy a file; how to remove a file; how to rename a file.
How to create a symlink; how to test whether a file is a symlink; how to get the target of a symlink.
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.
How to parse a CSV file and iterate through the rows.
How to generate a CSV file from an array of tuples.
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.
How to encode data as a JSON string.
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.
How to generate a string of YAML.
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.textendHow 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.
How to parse an HTML document.
How to get and set the working directory.
How to construct a pathname without hard coding the system file separator.
How to extract the directory portion of a pathname; how to extract the non-directory portion of a 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 isfoo
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 synonymopen(), however.
os.listdir() does not return the special files . and .. which represent the directory itself and the parent directory.
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
php:
glob takes a second argument for flags. The flagGLOB_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 pathendHow 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.
How to perform a recursive copy. If the source is a directory, then the directory and all its contents will be copied.
How to remove an empty directory. The operation will fail if the directory is not empty.
How to remove a directory and all its contents.
How to determine if a pathname is a 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:
WhenDir.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")endThe 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.
How to access arguments provided at the command line when the script was run; how to get the name of the script.
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.
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$$.
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.euidpython:
It is possible to register code to be executed upon exit:
import atexitatexit.register(print, "goodbye")It is possible to terminate a script without executing registered exit code by callingos._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 callingexit!.
How to register a signal handling function.
How to execute an external command.
How to prevent shell injection.
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 }.
How to process command line options.
We describe the style used bygetopt_long from the C standard library. The characteristics of this style are:
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.
How to define a option flag which sets a boolean variable.
How to define an option flag which takes an argument and sets a string variable.
How to define an option flag which takes an argument and sets a numeric variable.
Terminology used in this sheet:
Execute the specified file. Normally this is used on a file which only contains declarations at the top level.
php:
include_once behaves likerequire_once except that it is not fatal if an error is encountered executing the library.
How to load a library in a subdirectory of the library path.
How to reload a library. Altered definitions in the library will replace previous versions of the definition.
php:
Alsoinclude.
How errors which are encountered while loading libraries are handled.
How to put code in a library which executes only when the file is run as a top-level script.
The library path is a list of directory paths which are searched when loading libraries.
How to augment the library path by setting an environment variable before invoking the interpreter.
How to augment the library path by providing a command line option when invoking the interpreter.
The punctuation used to separate the labels in the full name of a subnamespace.
How to declare a section of code as belonging to a namespace.
How to declare a section of code as belonging to a subnamespace.
How to import all the definitions in a namespace.
How to import specific definitions from a namespace.
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 usingdistutils, which is in the Python standard library. The code is placed in a directory with asetup.py file. The code is installed by running the Python interpreter onsetup.py:
The format of the file used to specify a package.
python:
How to create a Python package usingdistutils. Suppose that the filefoo.py contains the following code:
def add(x, y): return x+yIn the same directory asfoo.py createsetup.py with the following contents:
#!/usr/bin/env pythonfrom distutils.core import setupsetup(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.tarTo install a tar, perform the following:
$ tar xf foo-1.0.tar.gz$ cd foo$ sudo python setup.py installIf you want people to be able to install the package withpip, upload the tarball to thePython Package Index.
ruby:
For an example of how to create a gem, create a directory calledfoo. Inside it create a file calledlib/foo.rb which contains:
def add(x, y) x + yendThen create a file calledfoo.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']endTo create the gem, run this command:
$ gem build foo.gemspecA file calledfoo-1.0.gem is created. To installfoo.rb run this command:
$ gem install foo-1.0.gemAnobject is a set of functions calledmethods which have shared access to the object'sinstance variables. An object's methods and instance variables are collectively called itsmembers. If a member of an object can be accessed or invoked by code which is not in a member of the object, it ispublic. Otherwise it isprivate.
Aclass is a set of objects which have the same method definitions. The objects in the set areinstances 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 afactory method. If there is class method which is responsible for creating all instances, it is called aconstructor. 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 containclass variables. These are global variables defined in the namespace of the class.
A method which returns the value of an instance variable is called agetter. A method which sets the value of an instance variable is called asetter. 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 theUniform 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 directivesattr_reader,attr_writer, andattr_accessor for defining getters and setters. Ruby classes are objects and in particular they are instances of theModule class. The directivesattr_reader,attr_writer, andattr_accessor are instance methods defined in theModule class which execute when the class block executes.
php:
Properties (i.e. instance variables) must be declaredpublic,protected, orprivate. Methods can optionally be declaredpublic,protected, orprivate. 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:
How to create an object.
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'] = vvalue = property(fget=getValue, fset = setValue)How instance variable access works.
How to define a method.
How to invoke a method.
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.
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. Thedel statement is used to delete an attribute.
ruby:
Define the methodself.method_missing to handle calls to undefined class methods.
How to invoke a class method.
How to create an alias for a method.
ruby:
Ruby provides the keywordalias and the methodalias_method in the classModule. Inside a class body they behave idenitically. When called from inside a methodalias has no effect butalias_method works as expected. Hence some recommend always usingalias_method.
Asubclass is a class whose objects contain all of the methods from another class called thesuperclass. 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 calledoverriding.
It is sometimes useful to define superclass which is never instantiated. Such a class is called anabstract 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.
How to get an identifier for an object or a value.
php:
The PHP manual says that the strings returned bygettype are subject to change and advises using the following predicates instead:
is_nullis_boolis_numericis_intis_floatis_stringis_arrayis_objectis_resourceAll possible return values ofgettype are listed.
How to get the class of an object.
#"inspect-class-hierarchy-note">
python:
hasattr(o,'reverse') will returnTrue if there is an instance variable named 'reverse'.
#"eval-note">
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, theeval evaluates asNULL.
python:
The argument ofeval must be an expression or aSyntaxError 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.
python:
dir(o) returns methods and instance variables.
How to display the contents of a data structure for debugging purposes.
How to get the current line number and file name of the source code.
How to get documentation from the command line.
ruby:
Searching forMath.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#atan2How 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 theuname 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.
How to make an HTTP GET request and read the response into a string.
A command line invocation to start a single process web server which serves the working directory athttp://localhost:8000.
$ sudo cpan -i IO::All$ perl -MIO::All -e 'io(":8000")->fork->accept->(sub { $_[0] < io(-x $1 ? "./$1 |" : $1) if /^GET \/(.*) / })'How to construct an absolute URL from a base URL and a relative URL as documented inRFC 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 aPHP function which computes absolute urls.
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.paramsruby:
How to parse an FTP URL:
up = URI('ftp://foo:bar@google.com/baz;type=binary')# "foo" up.user# up.password"bar"# "binary"up.typecodeHow to URL encode and URL unencode a string.
URL encoding, also called percent encoding, is described inRFC 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 isapplication/x-www-form-urlencoded.
python:
In Python 3 the functionsquote_plus,unquote_plus,quote, andunquote moved fromurllib tourllib.parse.
urllib.quote replaces a space character with%20.
urllib.unquote does not replace+ with a space character.
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<,>, and&.
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" and'. It is not necessary to escape the characters<,>, and& inside quoted attribute values.
php:
The flagENT_NOQUOTES to the functionhtmlspecialchars causes double quotes" to be escaped.
The flagENT_QUOTES causes single quotes' to be escaped.
How to encode binary data in ASCII using the Base64 encoding scheme.
A popular Base64 encoding is the one defined byRFC 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.
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.
How to run all the tests in a test class; how to run a single test from the test class.
How to test for equality.
python:
Note thatassertEquals 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.
How to assert that two floating point numbers are approximately equal.
How to test that a string matches a regex.
How to test whether an exception is raised.
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 methodassert_called_with can be used in place ofasert_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:
Thewith 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. Theonce method can be replaced bynever ortwice. If there is uncertainty about how often the method will be called one can usedat_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) endThere is also a method calledyields 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.
How to define a setup method which gets called before every test.
How to define a cleanup method which gets called after every test.
How to check the syntax of code without executing it.
How to perform static analysis on the code to detect probably errors.
How to detect or remove semantically insignificant variation in the source code.
How to run a script under the debugger.
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 bfunction | b [file:]line bclass[.method] | b [file:]line |
| list breakpoints | breakpoints | b | info b | i b |
| delete breakpoint | cb(file,line) | clnum | delnum | dnum |
| 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. | pexpr | pexpr | pexpr |
| (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;How to run a snippet of code repeatedly and get the user, system, and total wall clock time.
How to run the interpreter on a script and get the number of calls and total execution time for each function or method.