iwiki Documentation Release 1.0 jch January 31, 2014
Contents i
ii
Contents: Contents 1
2 Contents
CHAPTER 1 Python 1.1 Python Core 1.1.1 Strings 1.1.2 Functions Argument Lists *args tuple/list **kwargs dictionary def foo(*args): print args Unpacking 1.1.3 Tuples Tuple assignment a, b = b, a Tuple comparison 1.1.4 Lists 1.1.5 Dictionaries Sorting from operator import itemgetter d = { spam :7, eggs : 14, ham :6} #sort by key print sorted(d.iteritems(), reverse=true) # return a list of tuples print sorted(d.keys(), reverse=true) # return just a list of keys 3
# sort by value print sorted(d.iteritems(), key=itemgetter(1), reverse=true) #return a list of tuples print sorted(d.iteritems(), key=lambda x: x[1], reverse=true) print sorted(d, key=d. getitem, reverse=true) #return just a list of values 1.1.6 Files 1.1.7 Regular Expressions re.search(pat, str, flags=0) returns a match object or None otherwise. re.findall(pat, str, flags=0) returns a list of all matches. re.sub(pat, repl, str, count=0, flags=0) the replacement string can include \1, \2 which refer to the text from group(1), group(2) and so on from the original text: str = spam foo@bar.com ham baz@qux.com eggs match = re.search(r ([\w.-]+)@([\w.-]+), str) # using raw string is convinient if match: print match:, match.group() # foo@bar.com print submatch:, match.group(1) # foo else: print not found print re.findall(r [\w.-]+@[\w.-]+, str) # [ foo@bar.com, baz@qux.com ] # return a list of tuples instead of strings submatches > 1 print re.findall(r ([\w.-]+)@([\w.-]+), str) # [( foo, bar.com ), ( baz, qux.com )] print re.sub(r ([\w.-]+)@([\w.-]+), r \1@quxx.com, str) # spam foo@quxx.com ham baz@quxx.com eggs 1.1.8 OOP init str Operator Overloading add and radd ref Polymorphism Inheritance 1.1.9 Standard Library 4 Chapter 1. Python
CHAPTER 2 Javascript 2.1 Javascript Core 2.1.1 Objects Creation Literal With new Object.create() Note: Introduced in ECMAScript 5 Object.create(proto [, propertiesobject]) var o; o = Object.create(null); //with no prototype o = {}; // is equivalent to: o = Object.create(Object.prototype); function Constructor(){} o = new Constructor(); // is equivalent to: o = Object.create(Constructor.prototype); o = Object.create({}, { p: { value: 42 } }) // by default properties ARE NOT writable, enumerable or configurable: o.p = 24 o.p //42 o.q = 12 for (var prop in o) { console.log(prop) //"q" } delete o.p //false //to specify an ES3 property o2 = Object.create({}, { p: { value: 42, writable: true, enumerable: true, configurable: true } }); 5
Polyfill: if (typeof Object.create!== function ) { Object.create = function (o, props) { function F() {} F.prototype = o; } if (typeof (props) === "object") { for (prop in props) { if (props.hasownproperty((prop))) { F[prop] = props[prop]; } } } return new F(); }; Properties Querying and Setting obj.prop obj["prop"] Deleting delete Testing prop in obj return true for own or inherited properties obj.hasownproperty(prop) return false for inherited properties obj.propertyisenumerable(prop) return true for own property and its enumerable attribute is true. obj.prop!==undefined Enumerating for/in loops (enumerable): for (p in o) { if (!o.hasownproperty(p)) continue; } //filter inherited properties for (p in o) { if (typeof o[p] === "function") continue; } //filter methods Note: Introduced in ECMAScript 5 6 Chapter 2. Javascript
Object.keys(obj) return enumerable own properties Object.getOwnPropertyNames(obj) return enumerable and non-enumerable own properties Getters and Setters Note: Introduced in ECMAScript 5 Property Attributes Note: Introduced in ECMAScript 5 Object.getOwnPropertyDescriptor(obj, prop) Object.defineProperty(obj, prop, descriptor) Object Attributes prototype class extensible Serializing Methods 2.1.2 Functions Invocation Method Invocation Function Invocation Constructor Invocation Apply Invocation In this case, this in the nested function is bound to the global object: var o = { value: 1, outer: function () { var inner = function () { console.log(this); }; inner(); //bound to global object 2.1. Javascript Core 7
} }; o.outer(); Workaround: var o = { value: 1, outer: function () { var that = this; //assign this to a variable var inner = function () { console.log(that); console.log(that.value); }; inner(); } }; o.outer(); //have access to outer s variable Arguments Return Exceptions If return value is not specified, then undefined is returned. If the function is invoked with new and the return value is not an object, then this (the new object) is returned. 2.1.3 Inheritance The true prototype of an object is held by [[Prototype]] internal property: function Foo () {} var bar = new Foo(); //the [[Prototype]] of bar is Foo.prototype function Baz () {} // Baz.prototype = new Foo(); //the [[Prototype]] of Baz.prototype is changed to Foo.prototype. 8 Chapter 2. Javascript
Foo Function prototype constructor [[Prototype]] [[Prototype]] constructor prototype Foo.prototype Function.prototype [[Prototype]] [[Prototype]] Object.prototype [[Prototype]] [[Prototype]] constructor prototype null Object obj instanceof Constructor tests whether obj is in Constructor.prototype s prototype chain. proto.isprototypeof(obj) tests whether obj is in proto s prototype chain. Note: Introduced in ECMAScript 5 Object.getPrototypeOf(obj) Warning: Not standard obj. proto 2.1. Javascript Core 9
10 Chapter 2. Javascript
CHAPTER 3 CSS 3.1 CSS Core 11
12 Chapter 3. CSS
CHAPTER 4 HTML 4.1 HTML Core 13
14 Chapter 4. HTML
CHAPTER 5 Database 5.1 SQL 15
16 Chapter 5. Database
CHAPTER 6 Git 6.1 Git 17
18 Chapter 6. Git
CHAPTER 7 Testing 7.1 Testing Basic 7.2 Selenium 19
20 Chapter 7. Testing
CHAPTER 8 Linux 8.1 Linux Basic 8.2 Arch Linux 21
22 Chapter 8. Linux
CHAPTER 9 Indices and tables genindex modindex search 23