Tuesday, 20 January 2015

Vagrant up and running on ubuntu trusty

What is vagrant?

Vagrant is a software for creating and configuring complete development environment. The environment is sandboxed in a virtual machine. The virtual machine is create by a virtualization software such as VirtualBox, VMware.
Vagrant works as a wrapper around a virtualization software and around configuration management software such as Ansible, Chef, Salt or Puppet.

Why and when use vagrant?

In software development process it's a common scenario that some software works on one developers machine and simply does not work in some other programmers machine for some dependency or some other issue. Vagrant is the answer here. It will create an identical development environment for everyone who intend to run/develop the software. Also running provisioning software like puppet, chef or ansible is a nice feature to have.

Installation

Vagrant needs virtualbox. Virtualbox is a virtualization software by oracle and its free. It can be downloaded from here. After downloading .deb file for specific ubuntu version, it can be installed by this command in the shell.

$ sudo dpkg -i <location/of/the/file>

Next step is installing vagrant. .deb file of it can be downloaded from here. Installation process is same as the virtualbox. Once vagrant get installed it will seem like noting has happened but we get some command line options for vagrant.


Up and running

I am going to create a virtual environment of a ubuntu trusty 32 bit system in my ubuntu trusty computer. Reason of choosing 32 bit system is, it takes less RAM than its 64 bit counterpart in the host machine. The host machine is my personal computer and the guest machine is the virtual machine I am going to create.

Create a directory trusty32 and cd to that directory from terminal. Directory name can be anything.

$ mkdir trusty32
$ cd trusty32

Now we add vagrant box. A box is a base image to quickly clone a virtual machine. The easiest way to download boxes is downloading it from HashiCorp's Atlas using this command,

$ vagrant box add ubuntu/trusty32

Another way is to add a box from vagrantbox.es site. In this case we follow the site's instruction and add the box like this,

$ vagrant box add trusty32 https://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-i386-vagrant-disk1.box

vagrant box add command will download the specified box. This will take some time. Once downloaded it can be reused in multiple projects.

The next command is,

$ vagrant init trusty32

The command above will place a Vagrantfile in trusty32 directory. Vagrantfile describes type of machine required for a project, and how to configure and provision these machines. For now it will be not changed.
To boot up our virtual machine,

$ vagrant up

Above command creates and configures guest machines according to our Vagrantfile. To check if a guest machine is running,

$ vagrant status

We can ssh to the machine and gain access to the guest machine shell,

$ vagrant ssh

To shut down a running machine we have to exit from the ssh session  and initiate the command,

$ vagrant halt

If we want to destroy the virtual machine and it's resource,

$ vagrant destroy

Sunday, 18 January 2015

Python map, filter, lambda explained with example

map(function, sequence[, sequence, ...]) -> list

map(function, iterable, ...) is a python builtin function.
  • It returns a list of results applying the function to the items of the argument sequence(iterable) or sequences.
  • If more than one iterable arguments are passed, the function must take that many arguments. The function is called with an argument list consisting of the corresponding item of each sequence.
  • If one iterable is shorter than another it is assumed to be extended with None items.
We can examine these rule with some codes.

def square(a):
    return a ** 2

Here  square is a function that makes a number square. We want the square of number 0 to 5. Using map(),

>>> map(square, range(6))
[0, 1, 4, 9, 16, 25]

So map() takes a function square and a list as arguments and returns a list of square(item).  Here item is elements in the list range(6).
If we pass two iterable in map(), the function must take two arguments.

def my_pow(a, n):
    return a ** n


Now my_pow function takes two arguments a, n and returns an.

>>> map(my_pow, [2, 3], [3, 2])
[8, 9]

But what if the two iterables are not of same length? In this particular case it will  generate a TypeError.



filter(function or None, sequence) -> list, tuple, or string

filter(function, iterable) is a python builtin function.
  • It returns those items of sequence for which function(item) is True.
  • If function is None, return the items that are True.
  • If sequence is a tuple or string, return the same type, else return a list.
We want to filter out the even integer from a list and we have written a function for this.

def is_odd(n):
    if n % 2 == 1:
        return True    
    return False

is_odd  function takes an integer and returns True if it's odd number otherwise returns False.

>>> filter(is_odd, range(10))
[1, 3, 5, 7, 9]

What happens here is, filter() applies the is_odd function on each item of the list range(10) and returns a list of those items for which is_odd returned True. It filters out the other items.


>>> filter(None, k = [True, False, True, False])
[True, True]

If the function is None it return the items that are True


lmabda function:

Now, fun begins with lambda functions. in Python,
  • lambda operator is a way to create small anonymous function. An anonymous function is a function without a name.
  • lambda function can be used wherever a normal function is used but it's restricted to a single expression.
  • function require multiple expression can not be expressed using lambda.
lambda functions are written as:
lambda arguments: expression (operation to perform on arguments)
lambda function returns the result of the operation.

What we have done in map() and filter() examples is, separately wrote a function and pass those in map(), filter()

We can do the same using lambda function.

>>>  map(lambda n: n**2, range(6))
[0, 1, 4, 9, 16, 25]

what happens here is same in the map() example but in this case a lambda function is used.

lambda n: n**2

On the left of : are the arguments. Here n is the argument and on right side is the expression that will be applied to the argument. The return value is the result.

>>> map(lambda a, n: a**n, [2, 3], [3, 2]) 
[8, 9]

This is equivalent to the my_pow() example.

Suppose we want to get all the digits from a string. According to our filter() and lambda knowledge,

>>> filter(lambda s: s.isdigit(), "adf9df215")
'9215'

We can write a little complex expression in lambda.

>>> filter(lambda s: False if s.isspace() else True, "This is SPARTAAAAAAA")
'ThisisSPARTAAAAAAA'

This filters out all the space from the string. Hope king  Leonidas don't mind.

Friday, 26 December 2014

Python built-in: setattr(), getattr()

setattr(object, name, value)


setattr() sets a named attribute on an object. The arguments are an object, a string and an arbitrary value.
setattr(x, 'foobar', 123) is equivalent to x.foobar = 123.

Example Usage:

Lets assume Peter's mother has to write a class for preparing tiffin box for her son Peter who does not want to eat very regularly. Everyday in a week she puts random sandwichs and fruits in the tiffin box and checks if peter has eaten that or not. Let help her write the class using setattr()

class TiffinBox(object):
    def __init__(self):
        setattr(self, random.choice(['bacon', 'egg', 'jam']),
             random.choice(range(1, 4)))
        setattr(self, random.choice(['bacon', 'egg', 'jam']), 
             random.choice(range(1, 4)))

Here we are setting a random food with a random number from 1 to 3. The number defines the amount of that food in Peter's tiffin box.
Now we also want Peter to drink something and check if he has eaten the food. Let's write some methods in this class to do those.

class TiffinBox(object):
    def __init__(self):
        setattr(self, random.choice(['apple', 'banana', 'oranges']), 

             random.choice(range(1, 4)))
        setattr(self, random.choice(['bacon', 'egg', 'jam']), 

             random.choice(range(1, 4)))

    def take_drink(self):
        drinks = random.choice(['coffee', 'tea'])
        setattr(self, 'drink', drinks)

    def eat_food(self):
        for food in self.__dict__:
            setattr(self, food, 0)
        setattr(self, 'eaten', True)


Peter can eat using eat_food method and if he eats that food we will add a new attribute to the object named eaten set it to Boolean True and make the all the food count 0 assuming Peter ate all the foods.
We can also add a weekday attribute here. Now the class looks like this.

# -*- coding: utf-8 -*-
import random
from datetime import datetime

class TiffinBox(object):
    def __init__(self):
        setattr(self, random.choice(['apple', 'banana', 'oranges']), random.choice(range(1, 4)))
        setattr(self, random.choice(['bacon', 'egg', 'jam']), random.choice(range(1, 4)))
        self.tiffin_day = self.get_weekday(datetime.now().weekday()) + '_tiffin'
    def __str__(self):
        return self.tiffin_day

    def take_drink(self):
        drinks = random.choice(['coffee', 'tea'])
        setattr(self, 'drink', drinks)

    def get_weekday(self, weekday):
        py_week = {
            0: 'monday', 1: 'tuesday', 2: 'wednesday', 3: 'thursday', 4: 'friday', 5: 'saturday',6: 'sunday' }
        return py_week.get(weekday)

    def eat_food(self):
        for food in self.__dict__:
            setattr(self, food, 0)
        setattr(self, 'eaten', True)

Now Peter's mother can prepare tiffin for him.

>>>tiffin =TiffinBox()
>>>tiffin.__dict__
>>>{'apple': 3, 'jam': 3, 'tiffin_day': 'friday_tiffin'}
>>>tiffin.eat_food() 
>>>tiffin.__dict__
>>>{'eaten': True, 'apple': 0, 'jam': 0, 'tiffin_day': 'friday_tiffin'}

Neat !!

getattr(object, name[, default]) -> value


getattr() is a python built-in that gets a named attribute from an object and return the value of that attribute.
The attribute name must be a string. getattr(x, 'y') is equivalent to x.y. default is an optional argument.
If the named attribute does not exist, default is returned if provided, otherwise AttributeError is raised.

>>>la = range(5)
>>>getattr(la, 'pop') 
<built-in method pop of list object at 0xb7470eac>
>>>la.pop
<built-in method pop of list object at 0xb7470eac>

so getattr(la, 'pop') is equivalent to la.pop. It can be used like a function also.

>>>getattr(la, 'pop')()
4
>>>getattr(la, 'abc')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'abc' 

A  list object has no attribute named 'abc'. So a AttributeError is raised here. But if we use the optional default argument, then the default value is returned.

>>> getattr(la, 'abc', 'no such attribute')
'no such attribute'

Example Usage:

Let's help Peter to find out what his mother has put in the box and in how many amount. He does not know if it's a apple or banana, bacon or jam! Time to use getattr()

mybox = TiffinBox()

for atr in mybox.__dict__:
    print atr, ': ', getattr(mybox, atr)


tiffin_day :  friday_tiffin
bacon :  2
oranges :  3


Hope Peter likes bacons and oranges!




Wednesday, 24 December 2014

Python built-in: any(), all()


any(iterable) -> bool


any(iterable)  is a built-in function in python's  __builtin__ module. It returns True if bool(x) is True for any x in the iterable. If the iterable is empty, return False.
Now let's take a list.

>>>la = [True, False, False]
>>>any(ls)
>>>True

This returns True because at least one of the value in la was True. Let's take another list,

>>>lb = [False, False, False, False]
>>>any(lb)
>>>False 

So any() will return True if at least one of the value in the iterable is True else it will return False.

Example Usage:


>>>str = "Wikipedia is the seventh-most popular website \
...and constitutes the internet's largest and most popular \
...general reference work"
>>>keywords = ['Google', 'Wikipedia', 'Facebook', 'Microsoft']

We want to check if there is keyword from keywords list is present in the string str. With the use of any() function it can be done in a one-liner!

>>>any(keyword in str for keyword in keywords)
>>>True

That's awesome!! isn't it? But what sorcery is this? Breaking it down:

>>>[keyword in str for keyword in keywords]
>>>[False, True, False, False]

So it's like the earlier la, lb type list. It's returning True for the keyword 'Wikipedia'. Now what is happening in the list
compression is:

for keyword in keywords:
    if keyword in str:
        # append True in a_list
    return <a_list>

 

all(iterable) -> bool


This function takes an iterable as argument and returns True if bool(x) is True for all values x in the iterable. If the iterable is empty, return True.

It will return if all the values in the iterable are True.

>>>all(la)
>>>False
>>>all(lb)
>>>False

>>>lc = [True]*3
>>>all(lc)
>>>True

 

Example Usage:


We are going to check if a list is a odd number list.

>>>all(map(lambda x:x%2==1, range(1, 10, 2)))
>>>True

Breaking it down:

>>> range(1, 10, 2)
>>>[1, 3, 5, 7, 9]

map() is a built-in which will return a list of result applying the lambda function to the item of the [1, 3, 5, 7, 9] list.

>>>map(lambda x:x%2==1, range(1, 10, 2))
>>>[True, True, True, True, True]
>>> all([True, True, True, True, True])
>>>True

Nice!!