Tuesday, 26 February 2013

Introduction to Simple DB

According to Wikipedia Amazon simple db is a distributed database written in Earlang used extensively as a web service. As the name suggests simple db has a very easy to understand and use architecture where a domain is a database, having a size of 10GB allowing 256 attribute per item. An item is like a table which holds attributes restricted to 1024 bytes in size.
     We can simply add, delete, query a sdb. First we need to connect to the sdb, we use the python interface to connect to amazon web services.

import boto

sdb = boto.connect_sdb(awsaccesskey, awssecretkey)

Now after getting connection to sdb, you can start either by creating a new domain or connecting to an existing domain.

domain = sdb.create_domain('my_domain_name') #create a new domain
domain = sdb.get_domain('my_domain_name') #connect to existing domain

If the domain doesn't exist, then get_domain returns error.

Now to create item inside domain we have,

item = domain.new_item('item_name')

Item is the key which is unique, associated with each key-value pair. To save the items to sdb use item.save(). Entries gets saved as unicode. Now simply running a for loop displays the items in the domain and you get to see the entries in the item. To update an existing value in the sdb. We use
 sdb.put_attribute(domain_name, item_name, values, replace=True)       

This writes to the domain, if the item already exists then it simply overwrites the existing value.

You can also query for a  particular value, using the select query. 

sdb.select('SELECT * from domain_name where value = value', None, True, None)

SDB is widely used in web services, for storing key- value pairs. It also gives you to save data any way you like.    


Thursday, 30 August 2012

Functional Programming in Javascript

The functional programming paradigm is very powerful feature captured in many programming languages and Javascript lies no where behind in it. We will see how functional programming finds its place in Javascript.

The idea of developing a program is to create the idea behind a functionality without increasing the complexity of the code. In programming languages there are built in functionalities which help a programmer to implement a code with less hassle. A simple example would be using the looping concept using 'while' or  'for' loop which saves us from performing same task n number of times.

We can define functions over a given data structure like display its contents, add an element etc. Each time we use the array we may need to perform different actions on it. We can in general define a function which takes arrays ans another function as arguments and define it in so that any defined function accepted as arguments can be applied to the array. For eg:-

function forEach(array, action) {
  for (var i = 0; i < array.length; i++)
    action(array[i]);
}

Here the function keyword is given to indicate it as a function, the arguments array and action are the passed arguments to this function as array and any defined function respectively.
The above defined function now accepts the array and applies the function passed to each element in the array. Such a function is called a higher order function.  The function which such a function accepts as arguments is called a first order function.
 
These higher order functions simplify our code by avoiding useless looping and conditions and can be applied to other functions which reduce its readability but lessen the work within a function. Taking the previous example itself with another function.Eg:-
 
function sum(numbers) {
  var total = 0;
  forEach(numbers, function (number) {
    total += number;
  });
  return total;
}

Like in Lisp and Python here too in Javascript we have reduce or fold which is a higher order function which accepts any function as argument and applies a function say sum, product, or square of sum to elements in a list and returns its result.Eg:-
function reduce(combine, base, array) {
  forEach(array, function (element) {
    base = combine(base, element);
  });
  return base;
}
 
function count_number(array) {
  function counter(total, element) {
    return total + (element === 0 ? 1 : 0);
  }
  return reduce(counter, 0, array);
} 


 Likewise we also have an implementation of map method like in Python and Lisp. Map function accepts a list of elements  and applies a function to each element in the list and returns the new list.Eg:-

function map(func, array) {
  var result = [];
  forEach(array, function (element) {
    result.push(func(element));
  });
  return result;
}
 
function AreaOfCircle(radius) {
    var area = Math.PI * (radius * radius);
    return area.toFixed(0);
}
var radii = [10, 20, 30];
var areas = radii.map(AreaOfCircle);
 

There are many built in functions like split, slice, charAt, which split the text based on tabs and spaces, "," etc, slice to extract a text or patterns from a given text , and charAt to return the text at any particular position respectively.
paragraph.split("\n\n"));
var character = paragraph.charAt(0);
var word = paragraph.slice(0, 4); 

There are many more functions available with Javascript only few basic ones have been discussed.

Saturday, 18 August 2012

Structure and Interpretation of computer programming- Introduction to scheme

SICP or structure and interpretation of computer programming is a fundamental book for understanding the simple logistics of programming in computers. According to the book SCIP A computational process, in a correctly working computer, executes programs precisely and accurately. novice programmers must learn to understand and to anticipate the consequences of their conjuring. Even small errors (usually called bugs or glitches) in programs can have complex and unanticipated consequences.Scheme a dialect of Lisp is a functional programming language taken to understand lexical scope and tail call optimization because of its minimalistic syntax notations which is very useful in translating to other languages as well.

Expressions and Naming 

We have prefix notation syntax in Lisp. 
=>(+ 2 3)                                            
      5 

The + operator can be applied to any number of elements.
=>(+ 2 3 5 6)
     16

We can assign value to variables and procedures ie, the function they are supposed to do by using the keyword define within parenthesis in mit-scheme.
In scheme it is as 
=>(define a 2)
=>a
     2
=>(define (s x) (x))
=>(s 2)
      2
For expression evaluations in lisp we have 
=>(/(+ 5 4 (- 2 (- 3 (+ 6 (/ 4 3)))))(* 3 (- 6 2)(- 2 7)))
 -23/90    

Compound procedures

Compound procedures are nothing but expressing the logic of a function in a given language. If we take the example of square of  number it is nothing but multiplying a number by itself. This is expressed in lisp as
=>(define (square x) (* x x))
=>(square 2)
     4 
Functions can also be passed as first order functions to other functions or rather procedures. These functions take in other functions as arguments and perform the required action on the variables. This is shown through a simple example:-

(define (sum term a next b)
        (if (> a b)
                0
                (+ (term a)
                        (sum term (next a) next b))))
(define (inc n) (+ n 1))
(define (cube x) (* x x x))
(define (sum-cube a b)
        (sum cube a inc b))

The above mentioned example takes in functions as arguments and the same function is called in another function to be applied.

Tail recursion and linear recursion

In simple recursion we have a function or a procedure calling it self many times. Each time a caller function calls a callee function the state of the caller function is saved in stack. In such cases the callee function has nothing to do but return the value to the caller function and in return it is returned back to its caller function. Some cases this proves to be very expensive in terms of space and/or time. In Scheme instead of iteration we have the same implemented as tail recursion optimization. 
In recursion if we see that if we call a procedure recursively, each recursive call has to be completed before the actual calculation to take place. This is illustrated using an example- 
(define (fibo n)
        (fibo-iter 1 0 n))
        (define (fibo-iter a b count)
                (if (= 0 count)
                        b
                        (fibo-iter (+ a b) a (- count 1) )))

The computation follows the pattern as-
(fibo-iter 0 1 5)

(fibo-iter 0 1 5)
(fibo-iter 0 1 4)
(fibo-iter 0 1 3)
(fibo-iter 0 1 2)
(fibo-iter 0 1 1)
(fibo-iter 0 1 0)
3
The actual value is computed after each recursive call then the final result is returned.

Now when we take the case of tail recursion, instead of saving the context of the caller procedure the callee procedure directly returns the result to the caller procedure's caller. Tail recursion is a feature of Scheme not just an implementation. In tail recursion at each time of the recursive call value of the running updates the value.In general a procedure may return to its caller (if it was non-tail called), or it's caller's caller (if it was tail-called but its caller wasn't) or it's caller's caller's caller (if it and it's caller were both tail-called), and so on. A procedure returns to the last caller that did a non-tail call.This can be shown by an example-

(define (fib n)
        (cond ((= n 0) 0)
                ((= n 1) 1)
                (else (+ (fib (- n 1))
                        (fib (- n 2))))))
(fib 5)
(fib 4) + (fib 3)
(fib 3) + (fib 2)
(fib 2) + (fib 1)
(fib 1) + (fib 0)
5
Each time the recursive call is made the value is returned to the caller procedure which returns to its caller and so on. Another example is pascal's triangle given as-

(define (pas row col)
        (cond ((or (= row col) (= 1 col)) 1)
                (else (+ (pas (- row 1) (- col 1))
                        (pas (- row 1) col)))))

returns the element of the triangle based on its position.

More examples based on the exercises from the book Structure and Interpretation of computer programming are experimented with in my git repository. Their Python translations are also available. https://github.com/anjalirmenon/scip.


Friday, 10 August 2012

Pymongo

In the previous blog about Mongodb which mainly dealt with javascript. But mongo version is also available for python users. It is almost similar and provides the same functionalities too. Pymongo is a python distribution to work with Mongodb.

Installation pymongo

 For Linux versions we will install sing pip.

$sudo pip install pymongo

Next to install dependencies in ubuntu/debian systems

 $ sudo apt-get install build-essential python-dev 

Getting started with pymongo

If pymongo is already installed you would be able to easily import it in the python prompt without raising an exception.

>>> import pymongo

Now the first step is to create a connection to the running mongod instance.
 
>>> from pymongo import Connection
>>> connection = Connection()

We can also connect to a specific network by specifying it in Connection('network',no.).

You can use the connection instance to access independent databases by applying any style either 

>>> db = connection.test_database
  
 or dictionary style

>>> db = connection['test-database']

A collection is a group of documents stored in a mongodb, similar to a table in RDBMS. The collection can be accessed similar to databasein both the above mentioned ways.

>>> collection = db.test_collection
>>> collection = db['test-collection'] 
  
Pymongo uses dictionary style for storing documents. Example of a document representing simple text is given below:-

 >>>import datetime
>>> post = {"author": "anjali", "text": "working with pymongo","date":datetime.datetime.utcnow()}

To insert document into a collection we can do the following-

>>> posts = db.post
>>> posts.insert(post)
ObjectId('5024d44b3e22462b281395f4')

This creates the first document in the server, which can be verified by-

>>> db.collection_names()
[u'test', u'system.indexes', u'things', u'post']

 
Similar to Sel in RDBMS SQL, we have find_one() which returns the  document matching the query.

>>> posts.find_one()
{u'date': datetime.datetime(2012, 8, 10, 9, 27, 24, 746000), u'text': u'working with pymongo', u'_id': ObjectId('5024d44b3e22462b281395f4'), u'author': u'anjali'}

You can also do specific querying by providing a particular condition in the find_one.Eg:-

>>> posts.find_one({"author":"anjali"})
{u'date': datetime.datetime(2012, 8, 10, 9, 27, 24, 746000), u'text': u'working with pymongo', u'_id': ObjectId('5024d44b3e22462b281395f4'), u'author': u'anjali'}
For a query which doesn't have a match we get like below:-

>>>posts.find_one({"author":"radha"})
>>>
Bulk inserts are also supported in pymongo by giving a list of dictionaries.Eg:-

>>> more_posts = [{"author":"Devi","text":"mongo is simple"},{"author":"resmy","text":"I like SQL more "}]
>>> posts.insert(more_posts)
[ObjectId('5024df513e22462b281395f5'), ObjectId('5024df513e22462b281395f6')]
>>>
 

Querying for more than one document is also supported using for loops-

>>> for post in posts.find():  
...   post
...
{u'date': datetime.datetime(2012, 8, 10, 9, 27, 24, 746000), u'text': u'working with pymongo', u'_id': ObjectId('5024d44b3e22462b281395f4'), u'author': u'anjali'}
{u'text': u'mongo is simple', u'_id': ObjectId('5024df513e22462b281395f5'), u'author': u'Devi'}
{u'text': u'I like SQL more ', u'_id': ObjectId('5024df513e22462b281395f6'),
u'author': u'resmy'}
>>>

Similarly search for a particular document can be done by providing a condition to be searched on in find().

Another function count returns the number of documents present in a collection either based on a condition or simply the total.

>>> posts.find({"author":"anjali"}).count()
1

We can also make our querying based on some index value. First we create an index and query the document.

>>> from pymongo import ASCENDING, DESCENDING
>>> posts.create_index([("date", DESCENDING), ("author", ASCENDING)])
u'date_-1_author_1'
>>> posts.find({"date": {"$lt": d}}).sort("author").explain()["cursor"]
u'BtreeCursor date_-1_author_1'
>>> posts.find({"date": {"$lt": d}}).sort("author").explain()["nscanned"]
2
 
Pymongo is a tool for experimenting with NoSQL databases. This shows again how python in helpful in bridging any variants.

Experimenting with Mongodb


  A NoSQL is a popular type of database system is Mongodb. The installation and working inn general shall be further.

 

 Installing Mongodb

I am using ubuntu 11.10, for which the steps are as follow:-
  • In a terminal type the following command to import the 10gen public GPG key:

       sudo apt-key adv --keyserver keyserver.ubuntu.com --recv   7F0CEB10
  • create a file /etc/apt/sources.list.d/10gen.list and write the following in the file and save.

For upstart users- deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen
For SysV users- deb http://downloads-distro.mongodb.org/repo/debian-sysvinit dist 10gen

For those who are confused what are upstart ans SysV it is nothing but the different in version of ubuntu/debian. Upstart is an event-based replacement for the /sbin/init daemon which handles starting of tasks and services during boot, stopping them during shutdown and supervising them while the system is running.
The "init" or "system initialisation" process on Unix and Linux systems has process ID (PID) "1". That is to say, it is the first process to start when the system boots (ignoring the initrd/initramfs). As the quote shows, Upstart is an "init" replacement for the traditional Unix "System V" "init" system.
  • Ok now to reload your repository type the following in the command prompt.
         sudo apt-get update

  • now to install the packages.

sudo apt-get install mongodb-10gen


Now to work with Mongo


For starting the mongodb type-

sudo services mongodb start

For stopping type-

sudo services mongodb stop

To restart-
 
sudo services mongodb restart

Now to use to use Mongodb like SQL we start by typing “mongo” in the terminal. The assigning and saving values is simple. The commands and their outputs are shown as below:-
mongo
MongoDB shell version: 1.8.2
Fri Aug 10 12:34:22 *** warning: spider monkey build without utf8 support.  consider rebuilding with utf8 support
connecting to: test
> db.things.save({name:"anjali"});
> db.things.find();
{ "_id" : ObjectId("5023e84c7c4542be6849ac20"), "name" : "mongo" }
{ "_id" : ObjectId("5024b2944bed5e5a6dba42e1"), "name" : "anjali" }
>
Data can be iteratively added to the db using the command below.

> for(var i=1;i<=20;i++) db.things.save({x:4,j:i});
> db.things.find();
{ "_id" : ObjectId("5023e84c7c4542be6849ac20"), "name" : "mongo" }
{ "_id" : ObjectId("5024b2944bed5e5a6dba42e1"), "name" : "anjali" }
{ "_id" : ObjectId("5024b93c4bed5e5a6dba42e2"), "x" : 4, "j" : 1 }
{ "_id" : ObjectId("5024b93c4bed5e5a6dba42e3"), "x" : 4, "j" : 2 }
{ "_id" : ObjectId("5024b93c4bed5e5a6dba42e4"), "x" : 4, "j" : 3 }
{ "_id" : ObjectId("5024b93c4bed5e5a6dba42e5"), "x" : 4, "j" : 4 }
{ "_id" : ObjectId("5024b93c4bed5e5a6dba42e6"), "x" : 4, "j" : 5 }
{ "_id" : ObjectId("5024b93c4bed5e5a6dba42e7"), "x" : 4, "j" : 6 }
{ "_id" : ObjectId("5024b93c4bed5e5a6dba42e8"), "x" : 4, "j" : 7 }
{ "_id" : ObjectId("5024b93c4bed5e5a6dba42e9"), "x" : 4, "j" : 8 }
{ "_id" : ObjectId("5024b93c4bed5e5a6dba42ea"), "x" : 4, "j" : 9 }
{ "_id" : ObjectId("5024b93c4bed5e5a6dba42eb"), "x" : 4, "j" : 10 }
{ "_id" : ObjectId("5024b93c4bed5e5a6dba42ec"), "x" : 4, "j" : 11 }
{ "_id" : ObjectId("5024b93c4bed5e5a6dba42ed"), "x" : 4, "j" : 12 }
{ "_id" : ObjectId("5024b93c4bed5e5a6dba42ee"), "x" : 4, "j" : 13 }
{ "_id" : ObjectId("5024b93c4bed5e5a6dba42ef"), "x" : 4, "j" : 14 }
{ "_id" : ObjectId("5024b93c4bed5e5a6dba42f0"), "x" : 4, "j" : 15 }
{ "_id" : ObjectId("5024b93c4bed5e5a6dba42f1"), "x" : 4, "j" : 16 }
{ "_id" : ObjectId("5024b93c4bed5e5a6dba42f2"), "x" : 4, "j" : 17 }
{ "_id" : ObjectId("5024b93c4bed5e5a6dba42f3"), "x" : 4, "j" : 18 }
has more
>
The has more means that the first 20 are displayed in the list. To view more we have a short command it which displays the rest of the elements in the list.

> it
{ "_id" : ObjectId("5024b93c4bed5e5a6dba42f4"), "x" : 4, "j" : 19 }
{ "_id" : ObjectId("5024b93c4bed5e5a6dba42f5"), "x" : 4, "j" : 20 }
>
To access data from a query, it is slightly different way than in sql. The find can be done using a while loop as well. Given below:-

> var icon = db.things.find();
> while(icon.hasNext()) printjson(icon.next());
{ "_id" : ObjectId("5023e84c7c4542be6849ac20"), "name" : "mongo" }
{ "_id" : ObjectId("5024b2944bed5e5a6dba42e1"), "name" : "anjali" }
{ "_id" : ObjectId("5024b93c4bed5e5a6dba42e2"), "x" : 4, "j" : 1 }
{ "_id" : ObjectId("5024b93c4bed5e5a6dba42e3"), "x" : 4, "j" : 2 }
{ "_id" : ObjectId("5024b93c4bed5e5a6dba42e4"), "x" : 4, "j" : 3 }
{ "_id" : ObjectId("5024b93c4bed5e5a6dba42e5"), "x" : 4, "j" : 4 }
{ "_id" : ObjectId("5024b93c4bed5e5a6dba42e6"), "x" : 4, "j" : 5 }
{ "_id" : ObjectId("5024b93c4bed5e5a6dba42e7"), "x" : 4, "j" : 6 }
{ "_id" : ObjectId("5024b93c4bed5e5a6dba42e8"), "x" : 4, "j" : 7 }
{ "_id" : ObjectId("5024b93c4bed5e5a6dba42e9"), "x" : 4, "j" : 8 }
{ "_id" : ObjectId("5024b93c4bed5e5a6dba42ea"), "x" : 4, "j" : 9 }
{ "_id" : ObjectId("5024b93c4bed5e5a6dba42eb"), "x" : 4, "j" : 10 }
{ "_id" : ObjectId("5024b93c4bed5e5a6dba42ec"), "x" : 4, "j" : 11 }
{ "_id" : ObjectId("5024b93c4bed5e5a6dba42ed"), "x" : 4, "j" : 12 }
{ "_id" : ObjectId("5024b93c4bed5e5a6dba42ee"), "x" : 4, "j" : 13 }
{ "_id" : ObjectId("5024b93c4bed5e5a6dba42ef"), "x" : 4, "j" : 14 }
{ "_id" : ObjectId("5024b93c4bed5e5a6dba42f0"), "x" : 4, "j" : 15 }
{ "_id" : ObjectId("5024b93c4bed5e5a6dba42f1"), "x" : 4, "j" : 16 }
{ "_id" : ObjectId("5024b93c4bed5e5a6dba42f2"), "x" : 4, "j" : 17 }
{ "_id" : ObjectId("5024b93c4bed5e5a6dba42f3"), "x" : 4, "j" : 18 }
{ "_id" : ObjectId("5024b93c4bed5e5a6dba42f4"), "x" : 4, "j" : 19 }
{ "_id" : ObjectId("5024b93c4bed5e5a6dba42f5"), "x" : 4, "j" : 20 }
>
 
We can use a forEach command just like while loop. In the following screenshot how it works.

> db.things.find().forEach(printjson);
{ "_id" : ObjectId("5023e84c7c4542be6849ac20"), "name" : "mongo" }
{ "_id" : ObjectId("5024b2944bed5e5a6dba42e1"), "name" : "anjali" }
{ "_id" : ObjectId("5024b93c4bed5e5a6dba42e2"), "x" : 4, "j" : 1 }
{ "_id" : ObjectId("5024b93c4bed5e5a6dba42e3"), "x" : 4, "j" : 2 }
{ "_id" : ObjectId("5024b93c4bed5e5a6dba42e4"), "x" : 4, "j" : 3 }
{ "_id" : ObjectId("5024b93c4bed5e5a6dba42e5"), "x" : 4, "j" : 4 }
{ "_id" : ObjectId("5024b93c4bed5e5a6dba42e6"), "x" : 4, "j" : 5 }
{ "_id" : ObjectId("5024b93c4bed5e5a6dba42e7"), "x" : 4, "j" : 6 }
{ "_id" : ObjectId("5024b93c4bed5e5a6dba42e8"), "x" : 4, "j" : 7 }
{ "_id" : ObjectId("5024b93c4bed5e5a6dba42e9"), "x" : 4, "j" : 8 }
{ "_id" : ObjectId("5024b93c4bed5e5a6dba42ea"), "x" : 4, "j" : 9 }
{ "_id" : ObjectId("5024b93c4bed5e5a6dba42eb"), "x" : 4, "j" : 10 }
{ "_id" : ObjectId("5024b93c4bed5e5a6dba42ec"), "x" : 4, "j" : 11 }
{ "_id" : ObjectId("5024b93c4bed5e5a6dba42ed"), "x" : 4, "j" : 12 }
{ "_id" : ObjectId("5024b93c4bed5e5a6dba42ee"), "x" : 4, "j" : 13 }
{ "_id" : ObjectId("5024b93c4bed5e5a6dba42ef"), "x" : 4, "j" : 14 }
{ "_id" : ObjectId("5024b93c4bed5e5a6dba42f0"), "x" : 4, "j" : 15 }
{ "_id" : ObjectId("5024b93c4bed5e5a6dba42f1"), "x" : 4, "j" : 16 }
{ "_id" : ObjectId("5024b93c4bed5e5a6dba42f2"), "x" : 4, "j" : 17 }
{ "_id" : ObjectId("5024b93c4bed5e5a6dba42f3"), "x" : 4, "j" : 18 }
{ "_id" : ObjectId("5024b93c4bed5e5a6dba42f4"), "x" : 4, "j" : 19 }
{ "_id" : ObjectId("5024b93c4bed5e5a6dba42f5"), "x" : 4, "j" : 20 }
>
You are also allowed to treat icon or cursors as arrays by the following command.

> var cursor = db.things.find();
> printjson(cursor[4]);
{ "_id" : ObjectId("4c220a42f3924d31102bd858"), "x" : 4, "j" : 3 }
 
You can also convert to an array by using:-
 
> var arr = db.things.find().toArray();
> arr[5];
{ "_id" : ObjectId("4c220a42f3924d31102bd859"), "x" : 4, "j" : 4 }
 

Using as Query


In sql queries we use sel * from table where condition. and for a particular case as sel element from table where condition.- The similar notation in mongo are -

db.things.find(condition).forEach(printjson);
 db.things.find(specific condition(s)).forEach(printjson);

findOne() 
 
  Lets you retrieve one document via findOne function.

> printjson(db.things.findOne({name:"mongo"}));
{ "_id" : ObjectId("4c2209f9f3924d31102bd84a"), "name" : "mongo" }
 

Limiting the result

 
 This is done using limit function as follows.

> db.things.find().limit(3);
{ "_id" : ObjectId("4c2209f9f3924d31102bd84a"), "name" : "mongo" }
{ "_id" : ObjectId("4c2209fef3924d31102bd84b"), "x" : 3 }
{ "_id" : ObjectId("4c220a42f3924d31102bd856"), "x" : 4, "j" : 1 }

NoSQL and Mongodb are not yet very popular substitutes to SQL but definitely strong scalable comtemporary to RDBMS.


Tuesday, 7 August 2012

Introspection -- Python code


While working on a code it is necessary to be aware of the use and purpose of functions, keywords used in python. Acoording to Wikipedia the act of introspection is ability to know and understand properties of an object at runtime.

An example code
def info(object, spacing=10, collapse=1): 
    """Print methods and doc strings.
    
    Takes module, class, list, dictionary, or string."""
    methodList = [method for method in dir(object) if callable(getattr(object, method))]
    processFunc = collapse and (lambda s: " ".join(s.split())) or (lambda s: s)
    print "\n".join(["%s %s" %
                      (method.ljust(spacing),
                       processFunc(str(getattr(object, method).__doc__)))
                     for method in methodList])
if __name__ == "__main__":                
    print info.__doc__
Here while initial properties of the above mentioned code are , it has a function info. The indented blocks indicate action to be performed inside a function. In if __name__ asks the python program to perform a action to show its working. In this case it returns the doc string of the function. It returns multi line doc string for all the properties of a given input object. Here spacing and collapse have set values hence the function works even without inputting them as arguments passed.

Type, str, dir and other built in functions

'type' function returns the type of the object it receives as input. It can also be used for comparing and returns a boolean value.It can be shown with examples-

>>>type(100) # returns the type of 100
<type 'int'>
>>>type('asdf') # returns the type of asdf
<type'str'>
>>> import fib
>>>type(fib) # returns the type of fib
<type 'module'>
>>>import types
>>>type(fib) == types.ModuleType #compares if both are same.
True

'str' function forces any object into a string type. It is possible to perform str on modules, integers, lists and strings. Given below are few examples-

>>>str('9') # returns the number as string
'9'
>>>str(fib) # returns the module as string
<module fib from fib.py>
>>>li=['hello','good','morning',1]
>>>str(li) # retuns list as string
'['hello','good','morning','1']'
>>>str(None) # returns None as string.
'None'

'dir' returns the list of methods that are possible in the object passed. It is possible to use dir on lists. Dictionary, modules etc. Eg:-

>>> fun =[]
>>>dir(fun)
['append', 'count', 'extend', 'index', 'insert','pop', 'remove', 'reverse', 'sort']
>>> d = {}
>>>dir(d)
['clear', 'copy', 'get', 'has_key', 'items', 'keys', 'setdefault', 'update', 'values']


'callable' functions return boolean values for objects that can be called as true else false. Eg:-

>>>import string
>>>a = 'anjali'
>>>callable(string.split)
True
>>>callable(a)
false

All the above mentioned functions are classified as builtin functions in python language and are found in a special module __builtin__. It also has error classes which is returned when ever an invalid action is performed. The most common error which is returned is AttributeError when we try to access a missing attribute.

>>>import __builtin__
getattr

It returns any attribute of the object passed. The object can be a list, module anything.

>>>getattr(lis,”append”)(“Moe”)
>>>getattr(li,”pop”)
<built-in method pop of list object at 0xb76d4b6c>
>>> getattr(odb,"buildConnectionString")
<function buildConnectionString at 0xb7785ae4>

import statsout

def output(data, format="text"):
output_function = getattr(statsout, "output_%s" % format, statsout.output_text)
return output_function(data)

As you can see, getattr is quite powerful. It is the heart of introspection. In the above example any data can be passed as object to output. The program can be extended to any formats like txt, pdf,odt etc.

List filteration

It is a part of list comprehension. Filteration is nothing but applying a method to the elements of the list and only return the values which satisfy the condition specified in the method. It allows us to avoid unwanted verbose in statements. Example given below.

>>> a = ["anj","menon","s","t","s","t"]
>>> [elem for elem in a if len(elem) > 1]
['anj', 'menon']
>>> [elem for elem in a if a.count(elem) > 1]
['s', 't', 's', 't']

The very example we used ie, methodlist is nothing but a list filteration implementation.


AND and OR

The “and” and “or” are boolean logic which can be used in implementation. They return one value.
In case of null or zero values are [],{},0,””. These are considered false.
AND returns the last true value of the expression and returns the first zero value in the expression.
OR returns the first true value of the expression and returns the last zero value in the expression. OR does not check if the second value is true or not if the first value is true. Further AND and OR can be expalined through these examples.

>>> 'yes' and 'no'
'no'
>>> '' and 'no'
''
>>> 'no' and ''
''
>>> 'yes' or 'no'
'yes'
>>> '' or 'no'
'no'
>>> 'yes' or ''
'yes'
>>> '1' and 'a' or 'b'
'a'
>>> '1' and 'a' or ''
'a'
>>> '1' and '' or 'a'
'a'
>>> '' and 'b' or 'a'
'a'


Lambda function

Lambda is a functionality borrowed from Lisp. They are miniature functions which can be used anywhere as functions. Lambda function can be used as normal functions. Lambda eliminates unnecessary verbose. It also takes any number of arguments.Lambda implementations are given below:-

>>>f = lambda x: return x*2
>>>f(2)
4
>>>f = lambda x:return x+1
>>>f(2)
3

In the first example of this blog we have a lambda function,
 processFunc = collapse and (lambda s: " ".join(s.split())) or (lambda s: s)

which joins the gaps in a given sentence. The previous “and” and “or” returns the first condition which falls true.


Summarize

The python language allows users to implement not just builtin functions but also user defined functions. The above mentioned functions help make python powerful to work with and help implement many functions.