- Python 2.7 not available for direct download for Cygwin. Follow the directions at Python 2.7.x under Cygwin to install it. It assumes you have installed
gcc/g++andmakein Cygwin. - This was the clearest explanation of Unicode on Python I could find: Unicode in Python, Demystified.
Tuesday, November 27, 2012
Python Notes
Wednesday, November 21, 2012
Name Notes
| Firefox 17 | Chrome 23 | IE 9 | Opera 12 | |
| Layout Engine | Gecko | WebKit | Trident | Presto |
| JavaScript Engine | SpiderMonkey (in C++) Rhino (in Java, not used within browser) Compiles into byte-code |
V8 (in C++) Compiles into assembly |
Chakra | Carakan (in C++) |
The official name for what is normally called JavaScript is ECMAScript. JavaScript was a trademark from Sun and Netscape. Microsoft called its version JScript. See the Wikipedia article ECMAScript for a history.
Monday, November 19, 2012
Rectification of Names (正名)
齊景公問政於孔子。 孔子對曰:「君君,臣臣,父父,子子。」
Duke Jǐng of Qí asked Confucius about government. Confucius replied "Let the ruler be a ruler; the minister, a minister; the father, a father; the son, a son."
-- The Analects 「論語」, 12:11.
Duke Jǐng of Qí asked Confucius about government. Confucius replied "Let the ruler be a ruler; the minister, a minister; the father, a father; the son, a son."
-- The Analects 「論語」, 12:11.
Javascript Note 3
- In Mozilla, the order of events for keystrokes are
- keydown event. At this point only modification keys are recorded.
- keypress event. At this point the main key is recorded.
- keyup event. At this point only modification keys are available.
- Element updated.
keyCodeattribute of printable keys in keydown and keyup event handlers should not be used.
Examples
- Notice the
keyCodeis G even though thecharCodeis g.
keydown :ALT keyCode[ (18)], charCode[(0)] keydown :ALT-SHIFT keyCode[ (16)], charCode[(0)] keydown :ALT-SHIFT keyCode[G(71)], charCode[(0)] keypress:ALT-SHIFT keyCode[(0)], charCode[G(71)] keyup :ALT-SHIFT keyCode[G(71)], charCode[(0)] keyup : keyCode[ (16)], charCode[(0)] keydown :CTRL keyCode[ (17)], charCode[(0)] keydown :CTRL-ALT keyCode[ (18)], charCode[(0)] keydown :CTRL-ALT keyCode[G(71)], charCode[(0)] keypress:CTRL-ALT keyCode[(0)], charCode[g(103)] keyup :CTRL-ALT keyCode[G(71)], charCode[(0)] keyup :ALT keyCode[ (17)], charCode[(0)]
- Typing apostophe (') (ASCII code 39) corresponds to DOM keycode 222. Javascript's
String.fromCharCodetranslates numeric codes to UTF-8 characters. In UTF-8, 222 corresponds to capital Thorn (Þ) which is a letter used in Old English and still used in Icelandic.
keydown : keyCode[Þ(222)], charCode[(0)] keypress: keyCode[(0)], charCode['(39)] keyup : keyCode[Þ(222)], charCode[(0)]
- Typing "one" results in
keydown : keyCode[O(79)], charCode[(0)] value[] keypress: keyCode[(0)], charCode[o(111)] value[] keydown : keyCode[N(78)], charCode[(0)] value[o] keypress: keyCode[(0)], charCode[n(110)] value[o] keyup : keyCode[O(79)], charCode[(0)] value[on] keyup : keyCode[N(78)], charCode[(0)] value[on] keydown : keyCode[E(69)], charCode[(0)] value[on] keypress: keyCode[(0)], charCode[e(101)] value[on] keyup : keyCode[E(69)], charCode[(0)] value[one]
Notice, the full value of the input field is available only on keyup.
<html> <head> <style> b { color: blue; } </style> <script type="text/javascript"> function msg(e, txt) { var mydiv = document.getElementById("result"); var modifier = []; [[e.ctrlKey,"CTRL"], [e.altKey, "ALT"], [e.metaKey, "META"], [e.shiftKey, "SHIFT"], [e.getModifierState("OS"), "OS"], [e.getModifierState("CapsLock"),"Caps"]].forEach( function (x) { if (x[0]) { modifier.push(x[1]); } } ); var txt = mydiv.innerHTML + "<br/>" + txt + ":<b>" + modifier.join("-") + "</b>" + " keyCode[<b>" + String.fromCharCode(e.keyCode) + "</b>(" + e.keyCode +")]," + " charCode[<b>" + String.fromCharCode(e.charCode) + "</b>(" + e.charCode+")]"; if (document.getElementById("includeValue").checked) { txt = txt + " value[" + document.getElementById("maininput").value + "]"; } mydiv.innerHTML = txt; } function clearResult(e) { document.getElementById("result").innerHTML = ""; document.getElementById("maininput").value = ""; } </script> </head> <body> <form> <input id="maininput" type="text" size="80" onkeyup= "msg(event, 'keyup ')" onkeydown= "msg(event, 'keydown ')" onkeypress="msg(event, 'keypress')"> </input> <input id="includeValue" type="checkbox">Include Value</input> <input type="button" value="Clear" onclick="clearResult(event)"/> <br/> <div id="result" style="font-family:monospace"> </div> </form> </body> </html>
Wednesday, November 14, 2012
Javascript Note 2
argumentsspecial variable available in the scope of a function. It contains all the arguments passed to a function. For example,
function p() { ... } function q(x) { ... } p(1,2,3,4); // arguments.length = 4 q(1,2,3,4); // arguments.length = 4. // Ie, the first argument is not swallowed by x.
Function.prototype.call(t[, args]*). Calls the function passing alongargsin a content wherethisrefers tot.
Function.prototype.apply(t, arg). Same ascallexcept the argument is an array. Coming from R, I had thought this meant the function was applied to each element ofarg.
Array.prototype.map(f). Applies functionfto each element of the array, returning a new array.
Array.prototype.forEach(f). Applies functionfto each element of the array but returnsundefined. In other words, basically used only for side effects such as printing to screen, writing to filesystem, etc.
Be aware that
1 + "5"; // "15"
"1" + 5; // "15"
"5" & 20; // 0
"5" & 21; // 5
"1" & "a"; // 0 (Octal of ASCII "1" is 061)
"1" & "b"; // 0
var x = { 1:"b", "5":"e" };
x["1"] == x[1]; // TRUE
x[5] == x["5"]; // TRUE
Tuesday, November 13, 2012
Javascript Note 1
Things I learned while writing a Thai transcription script in Javascript.
- The "keys" of an array are the indices!
var myarray = [ "potatoes" "tomatoes" "celery" ]; var sample = []; for (var x in myarray) { sample.push(x); } // sample = [ 0 1 2 ] NOT [ "potatoes" "tomatoes" "celery" ]
- Primitives are passed by value and the references of Objects are passed by value.
thisin a method refers to the object from which the method was invoked. See JavaScript “this” keyword.
var m = function() { this.value = this.value +1; } var a = { "value":1, "m":m }; var b = { "value":100, "a":a }; a.m(); // a.value = 2 b.a.m(); // a.value = 3, b.value = 100 b.a.m.call(b); // b.value = 101
- Objects in Javascript are associative arrays. To create an object is to create an associative array. One way of doing this is through the use of functions.
function f() { var x = 1; this.y = 100; this.addup = function addup(v) { this.y = this.y + v; }; return this; } var func1 = f(); var func2 = f(); func1.addup(1); [ func1.y, func2.y ]; // [ 101, 101 ] func2 = new f(); func2.addup(5); [ func1.y, func2.y ]; // [ 101, 105 ] [ func1.x, func2.y ]; // [ NaN, undefined ]
The last line is strange (Mozilla). I personally think it's a bug. I was expectingundefined. See Which way is best for creating an object in javascript? is “var” necessary before variable of object?.
undefinedis not a keyword in Javascript. It is just another variable that generally is undefined. The safest way to test if variablexis undefined istypeof(x) == "undefined". Note
var a = "welcome"; var b = { "a":"apple", "k":"kite", "z":"zebra" }; b["b"] == null; // TRUE b["b"] == undefined; // TRUE b["b"] === null; // FALSE b["b"] === undefined; // TRUE
However starting in Javascript 1.8.5,undefinedis a global non-writable variable whose type isundefined. So
var undefined = "trickster"; // doesn't do anything undefined === undefined; // TRUE typeof(undefined) == "undefined"; // TRUE
Subscribe to:
Comments (Atom)