Monday, 18 May 2015

IPYTHON [ Advance And Core Python ]

What Is IPython?

IPython is a richly featured replacement for the standard python interpreter. It offers a wider range of functionality, that the standard
interpreter, which generally ships with python, sorely lacks. It excels as a quick Python object reference, and for testing snippets of code on-the-fly. But these are just two of the many features useful to the Python scripter. In this brief introduction, I will show a few of it's most useful features. IPython is available for Linux, Mac and Windows, and comes pre-installed on Kali linux!
In this article, I am assuming some familiarity with Python fundamentals

Where Is IPython?

You can start it from the command line. In my examples, I will be using Kali and the IPython version for Python 2.7. However, if you're trying this on windows, you'll need to install it first. If you already have Python, and have some experience installing packages, you can get it with the command:
pip install ipython
If that line makes zero sense to you, then I recommend you have a look here:
https://pypi.python.org/pypi/pip
and here:
http://ipython.org/install.html .
And here :):

To start IPython, open a command prompt, and type ipython
You should see this new prompt in the terminal:


Why IPython?

Exploring objects
IPython makes exploring and learning more about python objects a breeze. See for yourself. Let's say you wanted to find out more about the itertools module. IPython makes this simple:


The command takes the form object_name?
Now hit <return>, and Boom!



We get the modules docstring, and we find out that itertools contains "Functional tools for creating and using iterators"... Great!
Pressing <Enter> or the up/down directional keys will navigate through the rest of the docstring. We can escape back to IPython by pressing 'q.'
However, it's not just modules you can do this with, you can also do this for pythons types(str, list, tuple, dict ... etc), variables you have created and ... well pretty much any object in python... and that's pretty useful, because in python, everything is an object!





Tab Completion
Another great feature of IPython is it's Tab-completion. We can find out what methods itertools has by typing object_name.<TAB> (<TAB> = press the TAB key):


 Now I want to know more about itertools.permutations, again I can use object_name?


 This is also useful if you can't quite remember the name of the method you want, but you do remember for instance, the letter it started with. In this instance we could type, object_name.first_letter<TAB> which gives you all the methods starting with that letter


In short, typing object_name.<TAB> will work for almost any object, displaying it's attributes and methods, if it has any. This also works on file and directory names and any objects you may have created yourself!
Magic Functions
These are 'magic' predefined functions whose syntax is much like that of a command line call. There are two types, line magics and cell magics.
Line magics syntax takes the form
%func_name <args>
where <args> is the rest of the line, without quotes or parentheses.
Cell magics syntax is
%%func_name <args>
<args>
<args>
...

where <args> are both the rest of the line, and in a seperate argument, the lines that follow it. Note that we use '%' twice for cell magics, once for line magics.
A brief example:




A full list of magic functions can be seen by typing %lsmagic
Finally, to exit IPython, simply type exit and hit <Enter>

In Conclusion

IPython is a very useful tool for scripting with python as a quick, easy, suitably in-depth reference, scratchpad(for testing snippets of code on the the fly) and last, but not least, for expanding your knowledge of python. I hope this will prove useful, it's my first post, and I appreciate any comments or constructive criticism. Thanks for reading!
Explore Object:
object_name?
TAB-completion:
object_name.<TAB>
Magic Functions:
%line_function <args>


%%cell_function <args>
<args>

No comments:

Post a Comment