Search This Blog

Showing posts with label ipython. Show all posts
Showing posts with label ipython. Show all posts

Tuesday, May 20, 2014

How to install ipython on XenServer and test XAPI

We've been using the more user friendly shell to interact with python before: ipython. The example below are showing first how to install and enable EPEL repository to be able to install ipython. Next we are going to write a simple XAPI demo program.

Install ipython
  • Find the distro your XenServer is based on
cat /etc/issue.net
CentOS release 5.7 (Final)
Kernel \r on an \m
  • Check enabled repository 
yum repolist
  • From the EPEL install the relevant rpm packets that will add new repository to your yum
# http://fedoraproject.org/wiki/EPEL
# http://www.mirrorservice.org/sites/dl.fedoraproject.org/pub/epel/5/i386/repoview/epel-release.html

rpm --force -i http://www.mirrorservice.org/sites/dl.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm
  • Update the repo info
yum list 
yum list | grep ipython
  • Install ipython
yum install ipython.noarch
  • Start and verify that ipython is working fine
ipython

In [3]: import sys
In [4]: sys.version
Out[4]: '2.4.3 (#1, Sep 21 2011, 20:06:00) \n[GCC 4.1.2 20080704 (Red Hat 4.1.2-51)]'

Xapi example using ipython

References

XAPI:
http://blogs.citrix.com/2011/05/18/so-what-is-xenserver-xapi/
http://docs.vmd.citrix.com/XenServer/6.2.0/1.0/en_gb/sdk.html#language_bindings-python

Packages:
http://xmodulo.com/2012/05/how-to-install-additional-packages-in.html
http://thomas-cokelaer.info/blog/2012/01/installing-repositories-under-centos-6-2-to-get-ipython-r-and-other-packages/
http://fedoraproject.org/wiki/EPEL#How_can_I_use_these_extra_packages.3F


Sunday, April 28, 2013

How to use pyrax and ipython together to test and learn the library api

When testing pyrax module using interactive ipython session I find myself often trying to inspect a single list value. In practice as my lists usually were build out of the same objects what I wanted is to inspect a single object from the list to discover its attributes and functions.

To overcome the ipython object limitation when it comes to introspection or reflection features I use this little helper script described here Custom magic function in IPython session. Below is an example session howto use in practice (in line you press TAB to let the ipython introspect the object)

ipython -i bpython-pyrax.py
In [1]: cs.images.list()                                                                                                            19:51:28
Out[1]:
[<Image: Ubuntu 13.04 (Raring Ringtail)>,
 <Image: Gentoo 13.1>,
 <Image: Fedora 18 (Spherical Cow)>,
truncated ...

In [2]: rl
------> rl()
created variable l0 = <Image: Ubuntu 13.04 (Raring Ringtail)>
created variable l1 = <Image: Gentoo 13.1>
created variable l2 = <Image: Fedora 18 (Spherical Cow)>
created variable l3 = <Image: FreeBSD 9.1>
created variable l4 = <Image: Ubuntu 12.10 (Quantal Quetzal)>
created variable l5 = <Image: Ubuntu 12.04 LTS (Precise Pangolin)>
created variable l6 = <Image: Ubuntu 10.04 LTS (Lucid Lynx)>
created variable l7 = <Image: Red Hat Enterprise Linux 6.3>
list cs.images.list() has 45 elements but only 7 was printed

In [3]: l0. 
l0.HUMAN_ID              l0.created               l0.id                    l0.metadata              l0.progress
l0.NAME_ATTR             l0.delete                l0.is_loaded             l0.minDisk               l0.set_loaded
l0.OS-DCF:diskConfig     l0.get                   l0.links                 l0.minRam                l0.status
l0.OS-EXT-IMG-SIZE:size  l0.human_id              l0.manager               l0.name                  l0.updated

In [5]: l0.name
Out[5]: u'Ubuntu 13.04 (Raring Ringtail)'
References
  1. http://rtomaszewski.blogspot.co.uk/2013/04/how-to-dynamically-create-variable-name.html
  2. https://github.com/rtomaszewski/api-challenge

Saturday, April 27, 2013

Custom magic function in IPython session

I needed some additional functionality when running my interactive ipython session. As ipython session is easily extendable with a help of custom python base functions I decided to write one as an example.

The function searches for the last outputed list object and creates variables for easy access (latest version can be found on github). Instead of typing l[_index_] you can simple tame l__index_. See the example below.
 
~/.config/ipython/profile_default/startup# cat rl.py

def object_name(obj, ret=None):
    g=globals()
    ret=[]
    obj_id=id(obj)
    for i in g.keys() :
        if id(g[i]) == obj_id :
            print i, g[i]
            ret.append(i)
    return ret


def rl (mylist=None, prefix=None, limit=7):
    prefix = prefix if prefix else "l"
    limit = limit if limit else 7

    aux=list(Out.keys()) # we want to make a copy
    aux.sort()
    aux.reverse()
    last_index = aux[0]

    if mylist == None :
        mylist = Out[last_index]
        mylist_name=In[last_index]

        if type(mylist) is not  list :
            for i in aux :
                # print i, Out[i],
                if type(Out[i]) is list :
                    mylist = Out[i]
                    mylist_name=In[i]
                    print("using last list object: %s" % mylist_name)
                    break
    else :
        mylist_name = filter( lambda x: not x.startswith("_"), object_name(l))

    if type(mylist) is not  list :
        print("can't find any list or specified object is not a list")
        return

    glo=globals()

    for k, val in enumerate(mylist) :
        if k <= limit :
            name=prefix+str(k)
            glo[name]=val
            print("created variable %s = %s" % (name, val) )
        else :
            print("list %s has %d elements but only 7 was printed" % ( mylist_name, len(mylist) ) )
            break

def rla (**kargs) :
  rl(prefix="a", **kargs)

def rlb (**kargs) :
  rl(prefix="b", **kargs)

print
print("auxiliary functions `rl` `rla` and `rlb` have been defined")

Example how this works
 
$ ipython='ipython  --colors Linux --autocall=2

In [17]: l=[1,2,3,1,2,3,1,2,3,1,2,3]

# instead of typing l[0] or l[1] etc we can now use this variables
In [18]: rl
-------> rl()
created variable l0 = 1
created variable l1 = 2
created variable l2 = 3
created variable l3 = 1
created variable l4 = 2
created variable l5 = 3
created variable l6 = 1
created variable l7 = 2
list l has 24 elements but only 7 was printed

In [2]: a=l    

In [6]: a
Out[6]: [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]

In [7]: rl(a)
__ [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]
_ [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]
a [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]
_6 [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]
_4 [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]
l [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]
created variable l0 = 1
created variable l1 = 2
created variable l2 = 3
created variable l3 = 1
created variable l4 = 2
created variable l5 = 3
created variable l6 = 1
created variable l7 = 2
list ['a', 'l'] has 12 elements but only 7 was printed

In [10]: a
Out[10]: [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]

In [11]: rlb
-------> rlb()
created variable b0 = 1
created variable b1 = 2
created variable b2 = 3
created variable b3 = 1
created variable b4 = 2
created variable b5 = 3
created variable b6 = 1
created variable b7 = 2
list a has 12 elements but only 7 was printed

References
  1. https://github.com/rtomaszewski/dotfiles
  2. http://rtomaszewski.blogspot.co.uk/2013/04/how-to-find-list-variable-name.html
  3. http://en.wikipedia.org/wiki/Reflection_(computer_science)
  4. http://en.wikipedia.org/wiki/Type_introspection

Monday, April 22, 2013

How to paste into Python interpreter a code snippet for testing

While working on a bigger Python code module you would like to test a small part of it quickly. When you try to copy it directly to your Python interpreter (python, bpython, ipython) you get IndentationError: unexpected indent or SyntaxError: invalid syntax error messages.

Problem

How to copy a code snippet into ipython.

Solution

Example 1
 
a=True
if a :
    print "true"

else:
    print "false"

Example 2
 
class Test:
    def f(self):
        print("hello f")

    def f2(self):
        print("hello f2")

c=Test()
c.f()

To test example 1 or 2 code we are going to use ipython. To instruct the interpreter that it shouldn't  auto-format or auto-indent we use the %cpaste magic. An example below shows how to use it.
 
user@server:# ipython
Python 2.7.3 (default, Aug  1 2012, 05:14:39)
Type "copyright", "credits" or "license" for more information.

IPython 0.12.1 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: %cpaste
Pasting code; enter '--' alone on the line to stop or use Ctrl-D.
:a=True
:if a :
:    print "true"
:
:else:
:    print "false"
:
:--

true

References
  1. http://stackoverflow.com/questions/10886946/how-does-ipythons-magic-paste-work