My Blog

Ipython the better Pythonshell

posted Jul 20, 2010, 1:49 AM by Simon Pfeiffer   [ updated Aug 11, 2010, 12:06 AM ]

If you are programming in python or  you are learning it right now and have not found ipython yet this post is for you.

So what is this Ipython all about? Which features make it superior to the standardInterpreter?

Just a quick overview:
  • Historymechanisem 
  • Magicfunctions to %run (Debugging)
  • Interactive class inspection (~globbing namespaces)
Especially the last bullet is of special intrest for me because I hate to read documentation ;) and normally try to figure things out myself. So thats enough theory. Some examples:

Python 2.5.2 (r252:60911, Jan 20 2010, 21:48:48) 
Type "copyright", "credits" or "license" for more information.

IPython 0.8.1 -- An enhanced Interactive Python.
?       -> Introduction to IPython's features.
%magic  -> Information about IPython's 'magic' % functions.
help    -> Python's own help system.
object? -> Details about 'object'. ?object also works, ?? prints more.

In [1]: #Lets assume u have no idea what methods a string object has

In [2]: #--> or u cant remember the name or invocation (This is the case for me) 

In [3]: somestring="hello my friends out there on the Web"

In [4]: #I now use the dot notation somestring.[TAB]

In [5]: somestring.
------------Shortend for brevity !------------------
somestring.__repr__          somestring.split
somestring.__rmod__          somestring.splitlines
somestring.__rmul__          somestring.startswith
somestring.__setattr__       somestring.strip
somestring.__str__           somestring.swapcase
somestring.capitalize        somestring.title            somestring.translate
somestring.count             somestring.upper
somestring.decode            somestring.zfill

In [6]: # This is a nice list isn't it :) so lets see what capitalize does

In [7]: somestring.capitalize?
Type: builtin_function_or_method
Base Class: <type 'builtin_function_or_method'>
String Form: <built-in method capitalize of str object at 0xb7915ee0>
Namespace: Interactive
    S.capitalize() -> string
    Return a copy of the string S with only its first character

In [8]: #Ahh okay does not need any argumetns great and spits back string

In [9]: somestring.capitalize()
Out[9]: 'Hello my friends out there on the web'

In [10]: somestring.swapcase?
Type: builtin_function_or_method
Base Class: <type 'builtin_function_or_method'>
String Form: <built-in method swapcase of str object at 0xb7915ee0>
Namespace: Interactive
    S.swapcase() -> string
    Return a copy of the string S with uppercase characters
    converted to lowercase and vice versa.

In [11]: somestring.swapcase()

In [12]: # The [TAB] Key is your friend in Ipython it does autocompletion

In [13]: #whenever it can find a match in the namespace (try dir() to see what the namespaceis)

So if u don't have any Idea how to obtain Ipython use the Packagemanagment of your Linuxdistribution or use the python tool "pip" like that:(bash or windows cmd)

$ pip install ipython

Alternate method:

$ easy_install ipython

I will cover the other two features on my list in a upcoming post about debugging. For now you can also have a look at the Ipython-Website.

Print Cursorposition on Linux with Pythonxlib

posted Jul 18, 2010, 11:06 PM by Simon Pfeiffer   [ updated Jul 18, 2010, 11:21 PM ]

Some years ago I had the some problems with an legacy programm that had no API at all. So the only thing I could do was emulating mouseclicks to do get the job automated. Therefore I had to know positions of buttons etc... It turnend out that it was quite hard to get x,y coordinates on linux. After some 2 hours of trial and error I found a solution:

from Xlib import X, display
d = display.Display().screen().root.query_pointer()._data

print "x="
print d["root_x"]
print "y="
print d["root_y"]
Just put it in a file and name it then u can fire it up with python .
I did not write a loop for continous updates because it is very annoying that the cmd will be floded with numbers.
This snippet can also be found on Snipplr. For those who don't want to copypaste I have attached a file containing the script.

I'am moving some old stuff

posted Jul 18, 2010, 11:00 PM by Simon Pfeiffer

The next few days i will move some old things to this new location. For example the codesnippet that lets u figure out mouseposition on Linux etc.. I will also update my links from snipplr so that everyone can find my new location.
I hope u will enjoy the new site because it gives me the freedom to do more then just blogging.



1-3 of 3