Saturday, 19 April 2014

Python is compiled language or interpreted language ?

Posted by Amit Khomane 5 Comments

So you can code into python with ease (kudos to its simplicity). So next is to go deep and learn more about decorators, __add__ method, __get__ method, and more exciting stuff, that's great. But when somebody (most probably in interviews) ask you what exactly is python, compiled or interpreted language? You stuck for some time and explain them (or try to explain) with some heck of technical terms. With my personal experience, Yes! people do get stuck in this question even if they can code smoothly in python (I personally did).
Therefore, I decided to sit and explore about it. With some understanding, I prefer simpler way to explain it to you.

Which Python?
Confused with question? Then you should read this article. We are going to consider CPython for this time (or it doesn't matter to understand concept here).

What is Python?
Python we refer commonly as language is Cpython(original and most-maintained implementation of Python) and 'Python' (as language reference) is considered to be merely a interface to implement different version of the Python. In rough sense its considered to be interface rather than language (What the heck). Yes TRUE, its merely an interface for language developers. The reference page for python specification tries to say what and how Python should behave, as we do with any interface implementation. And there are many implementation of the python, as with any interface implementations. That does not mean it doesn't qualify to be called 'language', but it clarifies that there are variety of python available to do same work which are based on the 'implementation' guidelines provided by the Python Language Reference. You may be aware of interface in Java or C#. As with interfaces, it gives you an abstraction about how particular functionality should work leaving implementation work for you (or for your class). In simple words it contains behavior that class will implement.
Being interpreted or compiled is not relative terms to interface rather it is related to implementation. So considering Python, which is merely an interface cannot be compiled or interpreted.
Rather, if you you ask, is Cpython compiled or interpreted ? We can say that its interpreted with some compilation (I know bit confusing but have patience for few more lines please).

You need to understand….

How your python code gets executed?
The python code you write is compiled into python bytecode, which creates file with extension .pyc. If compiles, again question is, why not compiled language.

Note that this isn't compilation in the traditional sense of the word. Typically, we’d say that compilation is taking a high-level language and converting it to machine code. But it is a compilation of sorts. Compiled in to intermediate code not into machine code (Hope you got it Now).

Back to the execution process, your bytecode, present in pyc file, created in compilation step, is then executed by appropriate virtual machines, in our case, the CPython VM (actually we call it interpreter, right?).

Execution of Python Code
So for Cpython, we can say that its interpreted language. Aha, So that made to confuse you as Python is an "interpreted language"(which in term True for Cpython, a most famous implementation of python).

So my pyc file contains cross platform code right?. Yes, your bytecode is cross platform but its version dependent ( python 2.x or 3.x).

Is .pyc created every time I run code?
Answer is No. Actually it depends on your modification in py file. The time-stamp (called as magic number) is used to validate whether .py file is changed or not, depending on that new pyc file is created. If pyc is of current code then it simply skips compilation step.

Basically the way the programs are run is always the same. The compiled code is interpreted. The way the programs are loaded differs. If there is a current pyc file, this is taken as the compiled version, so no compile step has to be taken before running the command. Otherwise the py file is read, the compiler has to compile it (which takes a little time) but then the compiled version in memory is interpreted just the same way as always.

Again Its all same for them also, all of them have a typical implementation strategy of producing bytecode first, then executing it via a VM/interpreter. Difference is only in VM they use. Jython use JVM where as Ironpython use CLR.

Is there any python compiler?
Now we are talking about compilers (according to above definition).You may have heard about PyPy it is JIT compiler for python code. Nuitka, is one of the notable compilers. Nuitka attempts to translate pure Python not into bytecode, but into machine code (via C++ compiler), while using libpython at run time. Another one is ShedSkin. It compiles implicitly statically typed Python to C++, stand-alone programs or extension modules.

    Hence, perfect answer to the question, Python is compiled language or interpreted language ?, It totally depends upon which python is in consideration?, After that you can proceed with further explanation as per python in consideration (mostly answer is interpreter), and you can explain the process to clarify. I hope this article helped you.

Read More

Sunday, 6 April 2014

Linux Filesystem Commands

Posted by Mahesh Doijade 2 Comments
Linux Filesystem simplified, Linux Filesystem Commands
df [options] [device name] : It displays the filesystem usage related information
-a : Displays all the filesystems
-i : Gives inode usage information
-h : Displays in human readable format. Shows quantified byte information

Example -:
$ df -a
Filesystem       1K-blocks    Used Available Use% Mounted on
/dev/sda1          7608792 2469924   4729320  35% /

proc                     0       0         0    - /proc
sysfs                    0       0         0    - /sys
none                     0       0         0    - /sys/fs/fuse/connections
none                     0       0         0    - /sys/kernel/debug
none                     0       0         0    - /sys/kernel/security
udev                244408       4    244404   1% /dev
devpts                   0       0         0    - /dev/pts
tmpfs               101288     772    100516   1% /run
none                  5120       0      5120   0% /run/lock
none                253216     124    253092   1% /run/shm
gvfs-fuse-daemon         0       0         0    - /home/mahesh/.gvfs
/dev/sr0             62658   62658         0 100% /media/VBOXADDITIONS_4.3.6_
du [options] [pattern]  :  Displays space usage on files and directories
-c : Displays grand total for all the arguments
-h : Displays in human readable format. Shows quantified byte information
Example -:
$ du -ahc
0 ./centos/6
8.0K ./centos
4.0K ./samplefile.txt
0 ./ubuntu/ub10
4.0K ./ubuntu
16K .
16K total

ls [options] [filepattern] : Lists out directories and file entries from the given pattern
-a : Displays all the files including . &  ..
-r : Lists all the files in directories recursively
-l : Displays long list consisting of permissions on each file and other details
-d : Lists directories and not their content
-x : Displays sorted list by extension of the file
-s : Sorts output according to file size
-u : Displays sorted list by the access time
Example -:
$ ls -la
total 160
drwxr-xr-x 20 mahesh mahesh  4096 Mar 24 00:00 .
drwxr-xr-x  3 root   root    4096 Mar  9 01:23 ..
-rw-r--r--  1 mahesh mahesh   220 Mar  9 01:23 .bash_logout
-rw-r--r--  1 mahesh mahesh  3486 Mar  9 01:23 .bashrc
drwx------ 14 mahesh mahesh  4096 Mar 24 00:00 .cache
drwx------ 10 mahesh mahesh  4096 Mar 24 00:11 .config
drwx------  3 mahesh mahesh  4096 Mar  9 01:36 .dbus
drwxr-xr-x  2 mahesh mahesh  4096 Mar 24 00:11 Desktop
-rw-r--r--  1 mahesh mahesh    25 Mar 23 19:50 .dmrc
drwxr-xr-x  2 mahesh mahesh  4096 Mar  9 01:36 Documents
drwxr-xr-x  2 mahesh mahesh  4096 Mar  9 01:36 Downloads
-rw-r--r--  1 mahesh mahesh  8445 Mar  9 01:23 examples.desktop
drwx------  3 mahesh mahesh  4096 Mar 23 19:51 .gconf
drwx------  4 mahesh mahesh  4096 Mar  9 01:37 .gnome2
-rw-rw-r--  1 mahesh mahesh   142 Mar 23 19:51 .gtk-bookmarks
dr-x------  2 mahesh mahesh     0 Mar 23 19:51 .gvfs
-rw-------  1 mahesh mahesh   724 Mar 23 19:51 .ICEauthority
drwxr-xr-x  3 mahesh mahesh  4096 Mar  9 01:36 .local
drwx------  3 mahesh mahesh  4096 Mar  9 01:37 .mission-control
drwx------  4 mahesh mahesh  4096 Mar 24 00:00 .mozilla
drwxr-xr-x  2 mahesh mahesh  4096 Mar  9 01:36 Music
drwxr-xr-x  2 mahesh mahesh  4096 Mar  9 01:36 Pictures
-rw-r--r--  1 mahesh mahesh   675 Mar  9 01:23 .profile
drwxr-xr-x  2 mahesh mahesh  4096 Mar  9 01:36 Public
drwx------  2 mahesh mahesh  4096 Mar 23 19:51 .pulse
-rw-------  1 mahesh mahesh   256 Mar  9 01:36 .pulse-cookie
drwxr-xr-x  2 mahesh mahesh  4096 Mar  9 01:36 Templates
drwxr-xr-x  2 mahesh mahesh  4096 Mar  9 01:36 Videos
-rw-------  1 mahesh mahesh    62 Mar 23 19:50 .Xauthority
-rw-------  1 mahesh mahesh 23406 Mar 24 02:44 .xsession-errors
-rw-------  1 mahesh mahesh 12494 Mar 16 15:11 .xsession-errors.old 

mkdir [options] [directory-name] : Creates a new directory by the given directory-name
-m : Sets file mode
-p  : Creates parent directories if it does not exists.
Example -:
$ mkdir testdir
The above command will create a directory named "testdir"

 touch [options] [pattern] : Updates the timestamp of a file, If the file does not exists by default options creates it.
-a : Changes the access time only
-c : Do not create the file
-t  : Give a timestamp to use instead of current time
Example -:
$ touch testfile.txt
Creates "testfile.txt" if it does not exists, else it only updates timestamp of the "testfile.txt".

shred [options] [file_pattern] : Enables to overwrite a file to hide its content, also allows to delete file data in secured manner.
-s : This followed by number of bytes, will shred those many bytes
-n : Number of pattern iterations to run
-z : Add a final overwrite with zero to hide shredding
-u : Truncate and remove the file after overwriting
 Example -:
$ shred file1.txt file2.txt
The above command will destroy file1.txt and file2.txt completely, so not be able to recover even using any recovery utilities

rm [options] [file_pattern] : Remove the file
-f : Force removal
-i : Prompt before removal of each file 
-r : Deletes directories and their contents recursively.
Example -:
$ rm -rf testdir
Removes forcefully and recursively all the files in "testdir" as well as removes "testdir" directory.

mv [options] [from_pattern] [to_file] : Move or Rename a file
-f  : Do not prompt before overwriting
-i  : Interactive, prompts before moving
-n : Do not overwrite an existing file
Example -:
$ mv old_name new_name
Above command renames the "old_name" file/directory to "new_name".
$ mv alphonso basket/mango
Moves a file to another directory and rename it:

tar [options] [tar_file] [pattern] : Creates, Extracts an archive.
-c  : Creates a new tar archive
-f  : Specify an tar archive file to use
-t  : List the contents
-v : Verbose mode
-x : Extracts the archive contents
-z : Compress/Decompress through gzip
-j : Compress/Decompress through bzip2
Example -:
$ tar -cvf techdarting-04-14.tar /home/techdarting/

cd [directory] : Change directory

chown [options]  [mode] [file_name] : Enables to change the ownership of a directory, file etc.
-R : Recursively change ownership.

chmod [options] [user] [file_name] :  Enables to change the permission of a file, directory, etc.
-R : Recursively change permissions

Read More