Saturday, September 4, 2010

HTML5 : The canvas element

The canvas element is actually a rectangle of space, ie a two dimensional grid. With this canvas element we can render graphs, game graphics and other visual images and use javascript. It has no content and border of its own.

The mark up for creating a canvas,

canvas id = "a" width = "200" height = "200" /canvas

Define a variable and assign the canvas element by id.

var a_canvas = document.getElementById("a");

After finding the canvas element in the DOM invoke its getcontext method, the context is where all the actions on canvas take place. Pass a string 2d to the function. Now we got the context.

var a_context = a_canvas.getContext("2d");

Now we got the canvas and its drawing context, where all the drawing methods are defined like fillStyle, fillRect,fillStroke etc. Now draw a rectangle in the canvas.

a_context.fillRect(0,0,200,200);

The parameters indicate starting x,y coordinates and width and height.

HTML5 : Introduction

It is not a totally new version of HTML 4 . It support the features of HTML 4 and is a collection of individual features like canvas, video, geolocation etc. We can interacts with javascript through DOM (Document Object Model). It does not have any special notation to handle features like video. The API defines this. Upgrading to HTML5 need only change the doctype.

!DOCTYPE html

It defines new semantics like article, section, footer etc.

Local Storage :

HTML5 provides local storage. It stores information on the mechine, this allows the sites to retrieve it later.

Friday, September 3, 2010

Programming in UNIX Environment : Using the shell

A greater than sign '>' followed by a name makes a file of that name. the -n option of echo command is for omiting the newline. the '-l' option for the 'who' command shows the details of users. The shell is a command like all unix commands. It is represented by 'sh'.

$ > file //creates a file named file.
$ echo -n you are lucky
$ echo /* //prints the all the folders in root (not recursively)
$ echo \* //prints a '*' character
$ who -l

Programming in python : Classes

Create a class as follows,

>>> class abc():
... a = 1
... b = 2
...
create x as an instance of the class abc,

>>> x = abc()
>>> x
<__main__.abc instance at 0xb7fab3ac>
>>> x.b
2
Create another class with a function definition,

>>> class abc():
... a = 3
... b = 4
... def fun():
... print 'hello'
...
>>> x = abc()
>>> x.a
3
>>> x.b
4
>>> x.fun()
Traceback (most recent call last):
File "", line 1, in
TypeError: fun() takes no arguments (1 given)

We cannot call the function in class abc, as an attribute of x. x has no such an attribute. But the address of x is already given by the interpreter as a parameter to that function. Usually we give an extra parameter 'self', have no special meaning.

>>> class abc():
... a = 1
... b = 2
... def fun(self):
... p = 5
... q = 6
... print 'hello'
...
>>> x = abc()
>>> x.a
>>> 1
>>> x.p
>>> x.fun()
The above statement causes an error. Because x has no attribute p. 'x.fun()' cannot print hello, so little changes are needed to our code and is shown below.

>>> class abc():
... x = 1
... y = 2
... def fun(self):
... self.p = 3
... self.q = 4
... print 'Hello'
...
>>> m = abc()
>>> m.p
Traceback (most recent call last):
File "", line 1, in
AttributeError: abc instance has no attribute 'p'

Here the interpreter says that the abc instance has no attribute 'p'.'p' is an attribute of the function 'fun'. So create an instance of abc and pass the address of m to 'self'.

>>> m = abc()
>>> m .fun()
Hello

Thursday, September 2, 2010

Programming in UNIX Environment : Commands - sort, tail, cmp, diff

The command 'sort' is for sorting the line of the text. 'tail' prints the last ten lines of the file. 'cmp' for showing the difference between the files. It shows only the first difference. But 'diff' shows all the difference between two files.

sort filename1 file2
sort -r // reverse normal order.
sort -n // numeric order.
sort -f // fold upper and lower case together.
sort -b // Ignore the leading blanks.

tail filename // prints last 10 lines.
tail -3 filename // print last 3 lines.

cmp filename1 filename2 // prints only the 1st difference.

diff filen1 filen2 // prints all differences.

pwd - For printing current working directory.
cp - For copying a file.

cp /home/abhi/Pclass/num.c num.c

ed /home/abhi/Pclass/num.c

Programming in UNIX Environment : Commands - mv, rm, wc, grep

The 'mv' command is using for renaming. And 'rm' command for removing files. 'wc' for counting lines,words,characters. The 'grep' command is for searching a word. '-v' for searching all words except the indicating word in a line.

mv filename newname

rm filename1 filename2

wc filename1 filename2 // count lines,words,characters.
wc -l //only counts the new lines.
wc -w //print the word count.
wc -m //print the character counts.
wc -c //printf the byte counts.

grep int filename // for searching the word int.
grep -v int filename // all except int.

Programming in UNIX Environment : Commands - kill, ps, nohup, nice

The ampersand at the end of the command says that run this command and also take another from the terminal without stopping the first. It prints the process id. For stopping a process we use 'kill' command followed by process id. 'ps' command prints what that user running. Using 'nohup', the command will run even if the user logged out. 'nice' is for using another system to run our job.

$wc a* >wc.out &

$kill 6578

$ps

$ps -ag // all process that are currently running.

$nohup command & // No hangup for this command even if user logged out.

$nice expensive-command & // if your command take a lot of processor resources.

The 'at' command followed by time is to run our job at whatever time we want.

$ at 2340
There is a file '.profile' in the user's login directory, this file contains commands which the user want to run when the user logged in. A user can set things in it.

a=/long/directory/name

$cd $a

We can put 'a' as given above in our profile. So we can refer that directory by the value of shell variable.