Thursday, December 27, 2012

[OS X] How to Create a Tar GZip File from the Command Line




Everyone uses built-in compression functionality of Mac OS X with a simple right click. This is useful when you need to compress certain files and send them over email or store them using time machine. However, there are times when you need some advanced options during compression. It’s mostly required when you need to take backup out of Time Machine or you want to transfer data to some other backup server. We are going to discuss one such compression command called “tar”. The command to create a tar / gzip file from the command line is exactly same as we use in Unix.
Open the Terminal window  from /Applications/Utilities/Terminal and follow the instructions below -

How to Create a Tar GZip File from the Command Line

tar -cvzf compressedfile.tar.gz foldername
The command above looks quite easy, but the flags -cvzf have all the secret inside. Let’s understand these one by one -
  • -c  means create an archive
  • -v  means verbose mode, if specified you will see the list of files running through your screen.
  • -z means gzip compression, if specified it will filter your zipped file i.e. tar files via gzip compression.
  • -f means filename
In the above syntax is not really required, you can create an archive without having to specify gzip compression. But it’s very useful, as it’s responsible for actually compressing the size of the resultant file.These two commands can also be used separately.

How to unzip or untar tar.gz files

Now it’s time to see how can we unzip or extract theme compressed files. You can always use the unarchiver app in Mac, but let’s see how it can be done from command line.
gunzip compressedfile.tar.gz
and then..
tar -xvf compressedfile.tar
We can combine these two commands similar to what we do in Unix. And the above two commands will look like -
gunzip compressedfile.tar.gz | tar -xvf compressedfile.tar
Hope that helps!
Jigar


Monday, June 4, 2012

Guake Terminal Awasome Tool

Last Day I was looking on Ubuntu App and I found excellent terminal tool "Guake" Termina.

Guake is a drop-down terminal for GNOME Desktop Environment. Like similar terminals, it is invoked with a single keystroke, and hidden by pressing the same keystroke again.
Running Guake is faster than launching a new terminal with a keyboard shortcut because the program is already loaded into memory, and so can be useful to people who frequently find themselves opening and closing terminals for odd tasks.

Once you Run it. It is Very easy to invoke Just Press F12 and you have terminal dropping down from up and if youw ant full screen same screen press F11 and Ease of USB is you still have that old feature Ctrl+Shift+T New tab, or Tab up and Tab Down.

Loved it. :)


Sunday, June 3, 2012

Brief About Object-Oriented Programming and Python



     
 Mostly we have designed our python program around functions or blocks of statements which manipulate data. This is called the procedure-oriented way of programming. 

     There is another way of organizing your program which is to combine data and functionality and wrap it inside what is called an object. This is called the object oriented programming paradigm. Most of the time you can use procedural programming but sometimes when you want to write large programs or have a solution that is better suited to it, you can use object oriented programming techniques.

Classes and objects are the two main aspect of object oriented programming. 

  • class creates a new type where objects are instances of the class. An analogy is that you can have variables of type int which translates to saying that variables that store integers are variables which are instances (objects) of the int class.
  • Objects can store data using ordinary variables that belong to the object. Variables that belong to an object or class are called as fields
  • Objects can also have functionality by using functions that belong to a class. Such functions are called methods of the class. 
  • This terminology is important because it helps us to differentiate between functions and variables which are separate by itself and those which belong to a class or object. 
  • Collectively, the fields and methods can be referred to as the attributes of that class.

Fields are of two types - they can belong to each instance/object of the class or they can belong to the class itself. They are called instance variables and class variables respectively.

     A class is created using the class keyword. The fields and methods of the class are listed in an indented block.

Now Python Class Have One special SELF parameter so let's understand it first :

The self

     Class methods have only one specific difference from ordinary functions - they must have an extra first name that has to be added to the beginning of the parameter list, but you do do not give a value for this parameter when you call the method, Python will provide it. 

      This particular variable refers to the object itself, and by convention, it is given the name self.

     Although, you can give any name for this parameter, it is strongly recommended that you use the name self - any other name is definitely frowned upon. There are many advantages to using a standard name - any reader of your program will immediately recognize it and even specialized IDEs (Integrated Development Environments) can help you if you use self.

How it works :
     You must be wondering how Python gives the value for self and why you don't need to give a value for it. 
     An example will make this clear. Say you have a class called MyClass and an instance of this class called MyObject. When you call a method of this object as MyObject.method(arg1, arg2), this is automatically converted by Python into MyClass.method(MyObject, arg1, arg2 - this is what the special self is all about.

     This also means that if you have a method which takes no arguments, then you still have to define the method to have a self argument.


Now Classes

The simplest class possible is shown in the following example. Example Creating a Class


#!/usr/bin/python
class Person:
pass # An empty block
p = Person()
print p

Output

$ python simplestclass.py
<__main__.Person instance at 0xf6fcb18c>


How It Works

     We create a new class using the class statement followed by the name of the class. This follows an indented block of statements which form the body of the class. In this case, we have an empty block which is indicated using the pass statement.
    Next, we create an object/instance of this class using the name of the class followed by a pair of parentheses. For our verification, we confirm the type of the variable by simply printing it. It tells us that we have an instance of the Person class in the __main__ module.

    Notice that the address of the computer memory where your object is stored is also printed. The address will have a different value on your computer since Python can store the object wherever it finds space.


object Methods

     We have already discussed that classes/objects can have methods just like functions except that we have an extra self variable. We will now see an example.

Example Using Object Methods

#!/usr/bin/python
class Person:
    def sayHi(self):
        print 'Hello, how are you?'
p = Person()
p.sayHi()
# This short example can also be written as Person().sayHi()   

Output

$ python method.py
Hello, how are you?   



How It Works


     Here we see the self in action. Notice that the sayHi method takes no parameters but still has the self in the function definition.


Now this was very basic  and important towards OOPS Programming with Python for more you can refer the below link :


Regards