Client-Side JavaScript Timeline

Given below is the JavaScript timeline when a webpage is loaded in a browser :-

    1. The web browser creates a Document object and begins parsing the web page, adding Element objects and Text nodes to the document as it parses HTML elements and their textual content. The document.readyState property has the value “loading” at this stage.
    2. When the HTML parser encounters <script> elements that have neither the async nor defer attributes, it adds those elements to the document and then exe- cutes the inline or external script. These scripts are executed synchronously, and the parser pauses while the script downloads (if necessary) and runs. Scripts like these can use document.write() to insert text into the input stream. That text will become part of the document when the parser resumes. Synchronous scripts often simply define functions and register event handlers for later use, but they can traverse and manipulate the document tree as it exists when they run. That is, synchronous scripts can see their own <script> element and document content that comes before it.
    3. When the parser encounters a <script> element that has the async attribute set, it begins downloading the script text and continues parsing the document. The script will be executed as soon as possible after it has downloaded, but the parser does not stop and wait for it to download. Asynchronous scripts must not use the document.write() method. They can see their own <script> element and all document elements that come before it, and may or may not have access to additional document content.
    1. When the document is completely parsed, the document.readyState property changes to “interactive”.
    2. Any scripts that had the defer attribute set are executed, in the order in which they appeared in the document. Async scripts may also be executed at this time. Deferred scripts have access to the complete document tree and must not use the document.write() method.
    3. The browser fires a DOMContentLoaded event on the Document object. This marks the transition from synchronous script execution phase to the asynchronous event-driven phase of program execution. Note, however, that there may still be async scripts that have not yet executed at this point.
    4. The document is completely parsed at this point, but the browser may still be waiting for additional content, such as images, to load. When all such content finishes loading, and when all async scripts have loaded and executed, the document.readyState property changes to “complete” and the web browser fires a load event on the Window object.
    5. From this point on, event handlers are invoked asynchronously in response to user input events, network events, timer expirations, and so on.

    Courtesy: JavaScript The Definitive Guide, By David Flanagan

Difference between ArrayList and LinkedList in Java

ArrayList and LinkedList both implements List interface and their methods and results are almost identical. However there are few differences between them which make one better over another depending on the requirement.

ArrayList Vs LinkedList

1) Search: ArrayList search operation is pretty fast compared to the LinkedList search operation. get(int index) in ArrayList gives the performance of O(1)while LinkedList performance is O(n).

Reason: ArrayList maintains index based system for its elements as it uses array data structure implicitly which makes it faster for searching an element in the list. On the other side LinkedList implements doubly linked list which requires the traversal through all the elements for searching an element.

2) Deletion: LinkedList remove operation gives O(1) performance while ArrayList gives variable performance: O(n) in worst case (while removing first element) and O(1) in best case (While removing last element).

Conclusion: LinkedList element deletion is faster compared to ArrayList.

Reason: LinkedList’s each element maintains two pointers (addresses) which points to the both neighbor elements in the list. Hence removal only requires change in the pointer location in the two neighbor nodes (elements) of the node which is going to be removed. While In ArrayList all the elements need to be shifted to fill out the space created by removed element.

3) Inserts Performance: LinkedList add method gives O(1) performance while ArrayList gives O(n) in worst case. Reason is same as explained for remove.

4) Memory Overhead: ArrayList maintains indexes and element data while LinkedList maintains element data and two pointers for neighbor nodes hence the memory consumption is high in LinkedList comparatively.

There are few similarities between these classes which are as follows:

  1. Both ArrayList and LinkedList are implementation of List interface.
  2. They both maintain the elements insertion order which means while displaying ArrayList and LinkedList elements the result set would be having the same order in which the elements got inserted into the List.
  3. Both these classes are non-synchronized and can be made synchronized explicitly by using Collections.synchronizedList method.
  4. The iterator and listIterator returned by these classes are fail-fast (if list is structurally modified at any time after the iterator is created, in any way except through the iterator’s own remove or add methods, the iterator will throw a ConcurrentModificationException).

When to use LinkedList and when to use ArrayList?

1) As explained above the insert and remove operations give good performance (O(1)) in LinkedList compared to ArrayList(O(n)). Hence if there is a requirement of frequent addition and deletion in application then LinkedList is a best choice.

2) Search (get method) operations are fast in Arraylist (O(1)) but not in LinkedList (O(n)) so If there are less add and remove operations and more search operations requirement, ArrayList would be your best bet.

Courtesy: http://beginnersbook.com/2013/12/difference-between-arraylist-and-linkedlist-in-java/

Python: With command

Given below is an example of opening a file, manipulating the file, then closing it:

with open('output.txt', 'w') as f:
    f.write('Hi there!')

The above with statement will automatically close the file after the nested block of code. The advantage of using a with statement is that it is guaranteed to close the file no matter how the nested block exits. If an exception occurs before the end of the block, it will close the file before the exception is caught by an outer exception handler. If the nested block were to contain a return statement, or a continue or break statement, the with statement would automatically close the file in those cases, too.

Python – Accessing Command Line Arguments

Python provides a getopt module that helps you parse command-line options and arguments.

$ python test.py arg1 arg2 arg3

The Python sys module provides access to any command-line arguments via the sys.argv. This serves two purpose:

  • sys.argv is the list of command-line arguments.
  • len(sys.argv) is the number of command-line arguments.

Here sys.argv[0] is the program ie. script name.

Example:

Consider the following script test.py:

#!/usr/bin/python

import sys

print 'Number of arguments:', len(sys.argv), 'arguments.'
print 'Argument List:', str(sys.argv)

Now run above script as follows:

$ python test.py arg1 arg2 arg3

This will produce following result:

Number of arguments: 4 arguments.
Argument List: ['test.py', 'arg1', 'arg2', 'arg3']

NOTE: As mentioned above, first argument is always script name and it is also being counted in number of arguments.

Courtesy : http://www.tutorialspoint.com/python/python_command_line_arguments.htm

Additional notes from VJ : –

If there are arguments that are provided along with the python file, then within the python file this argument can be accessed like this : –

sys.argv[1]  -> for 2nd argument
sys.argv[2] -> for 3rd argument

Note: sys.argv[0] is the python script name as mentioned above

Regards,
VJ

Stronger Fundamentals : tee command in shell scripts

Tee Command Usage Examples

Tee command is used to store and view (both at the same time) the output of any other command.

Tee command writes to the STDOUT, and to a file at a time as shown in the examples below.

Example 1: Write output to stdout, and also to a file

The following command displays output only on the screen (stdout).

$ ls

The following command writes the output only to the file and not to the screen.

$ ls > file

The following command (with the help of tee command) writes the output both to the screen (stdout) and to the file.

$ ls | tee file

Example 2: Write the output to two commands

You can also use tee command to store the output of a command to a file and redirect the same output as an input to another command.

The following command will take a backup of the crontab entries, and pass the crontab entries as an input to sed command which will do the substituion. After the substitution, it will be added as a new cron job.

$ crontab -l | tee crontab-backup.txt | sed 's/old/new/' | crontab –

Misc Tee Command Operations

By default tee command overwrites the file. You can instruct tee command to append to the file using the option –a as shown below.

$ ls | tee –a file

You can also write the output to multiple files as shown below.

$ ls | tee file1 file2 file3

***********************************************************************************************************************************************************

Courtesy: http://linux.101hacks.com/

Stronger Fundamentals: The null Device in Unix Systems

/dev/null is a simple device (implemented in software and not corresponding to any hardware device on the system).

  • /dev/null looks empty when you read from it.
  • Writing to /dev/null does nothing: data written to this device simply “disappear.”

Often a command’s standard output is silenced by redirecting it to /dev/null, and this is perhaps thenull device’s commonest use in shell scripting:

command > /dev/null

You’re using /dev/null differently. cat /dev/null outputs the “contents” of /dev/null, which is to say its output is blank. > messages (or > wtmp) causes this blank output to be redirected to the file on the right side of the > operator.

Since messages and wtmp are regular files (rather than, for example, device nodes), they are turned into blank files (i.e., emptied).

You could use any command that does nothing and produces no output, to the left of >.

An alternative way to clear these files would be to run:

echo -n > messages
echo -n > wtmp

The -n flag is required, or echo writes a newline character.

(This always works in bash. And I believe the default sh in every GNU/Linux distribution and other Unix-like system popularly used today supports the -n flag in its echo builtin. But jlliagre is right thatecho -n should be avoided for a truly portable shell script, as it’s not required to work. Maybe that’s why the guide you’re using teaches the cat /dev/null way instead.)

The echo -n way is equivalent in its effects but arguably is a better solution, in that it’s simpler.
cat /dev/null > file opens three “files”:

  • The cat executable (usually /bin/cat), a regular file.
  • The /dev/null device.
  • file

In contrast, echo -n > file opens only file (echo is a shell builtin).

Although this should be expected to improve performance, that’s not the benefit–not when just running a couple of these commands by hand, anyway. Instead, the benefit is that it’s easier to understand what’s going on.

Redirection and the trivial (blank/empty) command.

As jlliagre has pointed out (see also jlliagre’s answer), this can be shortened further by simply omitting the command on the left of > altogether. While you cannot omit the right side of a > or >>expression, the blank command is valid (it’s the command you’re running when you just press Enter on an empty prompt), and in omitting the left side you’re just redirecting the output of that command.

  • Note that this output does not contain a newline. When you press Enter on a command prompt–whether or not you’ve typed anything–the shell (running interactively) prints a newline before running the command issued. This newline is not part of the command’s output.

Redirecting from the blank command (instead of from cat /dev/null or echo -n) looks like:

>messages

>wtmp

******************************************************************************************************************************************************

Courtesy: http://askubuntu.com/questions/514748/what-does-dev-null-mean-in-a-shell-script