Posts

Showing posts from April, 2013

Coffee Script : Overview

Image
     CoffeeScript is a little language that compiles into JavaScript . Underneath that awkward Java-esque patina, JavaScript has always had a gorgeous heart. CoffeeScript is an attempt to expose the good parts of JavaScript in a simple way.     The golden rule of CoffeeScript is: "It's just JavaScript". The code compiles one-to-one into the equivalent JS, and there is no interpretation at runtime. You can use any existing JavaScript library seamlessly from CoffeeScript (and vice-versa). The compiled output is readable and pretty-printed, passes through JavaScript Lint without warnings, will work in every JavaScript runtime, and tends to run as fast or faster than the equivalent handwritten JavaScript.   Latest Version: 1.6.2                sudo npm install -g coffee-script Overview CoffeeScript on the left, compiled JavaScript output on the right.   Installation      The CoffeeScript compiler is itself written in CoffeeScript , usin

Python Tutorial 8 : Python Utilities

In this section, we look at a few of Python's many standard utility modules to solve common problems. File System -- os, os.path, shutil The *os* and *os.path* modules include many functions to interact with the file system. The *shutil* module can copy files. os module docs filenames = os.listdir(dir) -- list of filenames in that directory path (not including . and ..). The filenames are just the names in the directory, not their absolute paths. os.path.join(dir, filename) -- given a filename from the above list, use this to put the dir and filename together to make a path os.path.abspath(path) -- given a path, return an absolute form, e.g. /home/nick/foo/bar.html os.path.dirname(path), os.path.basename(path) -- given dir/foo/bar.html, return the dirname "dir/foo" and basename "bar.html" os.path.exists(path) -- true if it exists os.mkdir(dir_path) -- makes one dir, os.makedirs(dir_path) makes all the needed

Python Tutorial 7 : Python Regular Expressions

Regular expressions are a powerful language for matching text patterns. This page gives a basic introduction to regular expressions themselves sufficient for our Python exercises and shows how regular expressions work in Python. The Python "re" module provides regular expression support. In Python a regular expression search is typically written as:   match = re . search ( pat , str )   The re.search() method takes a regular expression pattern and a string and searches for that pattern within the string. If the search is successful, search() returns a match object or None otherwise. Therefore, the search is usually immediately followed by an if-statement to test if the search succeeded, as shown in the following example which searches for the pattern 'word:' followed by a 3 letter word (details below): str = 'an example word:cat!!' match = re . search ( r 'word:\w\w\w' , str ) # If-statement after search() tests if it suc

Python Tutorial 6 : Python Dict and File

Image
Dict Hash Table Python's efficient key/value hash table structure is called a "dict". The contents of a dict can be written as a series of key:value pairs within braces { }, e.g. dict = {key1:value1, key2:value2, ... }. The "empty dict" is just an empty pair of curly braces {}. Looking up or setting a value in a dict uses square brackets, e.g. dict['foo'] looks up the value under the key 'foo'. Strings, numbers, and tuples work as keys, and any type can be a value. Other types may or may not work correctly as keys (strings and tuples work cleanly since they are immutable). Looking up a value which is not in the dict throws a KeyError -- use "in" to check if the key is in the dict, or use dict.get(key) which returns the value or None if the key is not present (or get(key, not-found) allows you to specify what value to return in the not-found case).   ## Can build up a dict by starting with the the empty dict {}   ##

Python Tutorial 5: Python Sorting

Image
The easiest way to sort is with the sorted(list) function, which takes a list and returns a new list with those elements in sorted order. The original list is not changed.   a = [ 5 , 1 , 4 , 3 ]   print sorted ( a )   ## [1, 3, 4, 5]   print a   ## [5, 1, 4, 3]   It's most common to pass a list into the sorted() function, but in fact it can take as input any sort of iterable collection. The older list.sort() method is an alternative detailed below. The sorted() function seems easier to use compared to sort(), so I recommend using sorted(). The sorted() function can be customized though optional arguments. The sorted() optional argument reverse=True, e.g. sorted(list, reverse=True), makes it sort backwards.   strs = [ 'aa' , 'BB' , 'zz' , 'CC' ]   print sorted ( strs )   ## ['BB', 'CC', 'aa', 'zz'] (case sensitive)   print sorted ( strs , reverse = True )   ## ['zz', &#

Python Tutorial 4: Python Lists

Image
Python has a great built-in list type named "list". List literals are written within square brackets [ ]. Lists work similarly to strings -- use the len() function and square brackets [ ] to access data, with the first element at index 0. (See the official python.org list docs .)   colors = [ 'red' , 'blue' , 'green' ]   print colors [ 0 ]     ## red   print colors [ 2 ]     ## green   print len ( colors )   ## 3   Assignment with an = on lists does not make a copy. Instead, assignment makes the two variables point to the one list in memory.   b = colors   ## Does not copy the list   The "empty list" is just an empty pair of brackets [ ]. The '+' works to append two lists, so [1, 2] + [3, 4] yields [1, 2, 3, 4] (this is just like + with strings). FOR and IN Python's *for* and *in* constructs are extremely useful, and the first use of them we'll see is with lists. The *for* construct