Tuesday, September 24, 2013

About novalidate clause in oracle 11g.

Suppose we create 2 tables and insert data in parent table as follow:
query:  
sql>>
--start of query
create table parent
(
  a number(2),
  b number(2)
);
create table child
(
  c number(2),
  d number(2)
);

insert into parent values(1,1);
insert into parent values(2,2);
insert into parent values(3,3);
insert into parent values(1,1);
--end of query

output>> 
Table PARENT created.
Table CHILD created.
1 row inserted.
1 row inserted.
1 row inserted.
1 row inserted.

Here we have inserted duplicates into parent table and if we want to apply primary key on column 'a' of parent table it won't accept because it has duplicate keys so we need to remove duplicates first .

sql>>
--start of query
Alter table parent add constraint pk_parent primary key (a); 
--end of query

output>> Error Cause:attempted to validate a primary key with duplicate values or
 null values.

So what to do??
We have 2 alternatives 1) Use novalidate clause
                                   2) Remove duplicates and then apply primary key

Alternative1)We can use novalidate clause if we want that from now onwards whatever we insert into table is not same as content of table itself. i.e if table is currently having duplicate, we are ok with it but we want that incoming data doesn't have any duplicates ,that means we want primary key constraint to be evaluated for data which we are going to insert from now onwards and hence here we can use  novalidate clause .


sql>>
--start of query
Alter table parent add constraint pk_parent primary key (a) enable novalidate  ;
--end of query

output>> Error:attempted to validate a primary key with duplicate values or null
           values.

Here also we are getting error because table contains duplicate.But we are using novalidate clause ,which means: check constraint for existing data not for persisting data. Reason is that oracle is trying to create a unique index for primary key we are applying (in this case for column 'a') by default and hence we are getting error. But we can ask oracle to use non-unique index by explicitly mentioning that in our query like this. 

sql>>
--start of query
Alter table parent add constraint pk_parent primary key (a) using index (create index pk_parent on parent(a)) enable novalidate  ;
--end of query

output>> table PARENT altered.

Success!! it works. Lets insert some keys in parent table. 
sql>>
--start of query
insert into parent values(4,2);
--end of query

output>> 1 rows inserted. 

Now if we try to insert some duplicate or null key in column 'a' of parent, it won't accept.
Lets try inserting. 

sql>>
--start of query
insert into parent values(1,2);
--end of query
output>> Error unique constraint violated

Here we get error because column 'a' of parent table already has key 1 and primary constraint is in effect.
Content of table parent is:

sql>>
--start of query
Select * from parent;
--end of query
output>> 

        A          B
---------- ----------
         1          1 
         2          2 
         3          3 
         1          1 
         4          2 


So by using novalidate clause me have made sure that no duplicates get inserted after applying primary key but table can have previous duplicate keys. Notice that for that we need to create non-unique index explicitly because oracle creates unique index by default.

Now lets talk about alternative 2)Removing duplicates: For this we can  ask oracle to help us know which duplicates are violating constraints. In effect we need to provide oracle with table in which oracle will insert rowid of rows violating constraint whose columns are something like below:

First drop primary constraint to explore alternative  2.

sql>>
--start of query
Alter table parent drop constraint pk_table;
--end of query
output>> Table PARENT altered.

sql>>
--start of query

create table excp_table
(
row_id rowid,
table_owner varchar2(30),
table_name varchar2(30),
violated_constraint_name varchar2(30)
);
--end of query

output>> 
table EXCP_TABLE created.

Now we can use this table to apply primary key.

sql>>
--start of query

Alter table parent enable constraint pk_parent exceptions into excp_table;
--end of query
output>> 
Error:primary key violated.

This is ok as we can see now our excp_table contain duplicates which were causing trouble.

sql>>
--start of query
select * from excp_table;
--end of query
output>> 
ROW_ID      TABLE_OWNER                    TABLE_NAME     VIOLATED_CONSTRAINT_NAME     
------               ------------------------------ ------------------------------ ------------------------------
AAASfC       SYSTEM                          PARENT                         PK_PARENT                      
AABAAA                                                                                              
VVxAAD                                                                                              

AAASfC       SYSTEM                          PARENT                         PK_PARENT                      
AABAAA                                                                                              
VVxAAA                                                                                              


Here by this we get all duplicates i.e if key 1 is present 2 times , we get 2 rows in excp_table.
Here is the same case as we are getting 2 rows in excp_table because key 1 is present 2 times.
Now we can delete duplicates using excp_table as:
sql>>
--start of query
Delete parent where rowid in (select row_id from excp_table);  
--end of query

output>> 
2 rows deleted.

Now lets apply primary key on column 'a' of parent table.

sql>>
--start of query
alter table parent add constraint pk_parent primary key(a);
--end of query
output>> 
table PARENT altered.

Success!! we are able to apply primary key because it doesn't contains any duplicates.
Now lets disable this contraint and add some duplicates.

sql>>
--start of query

alter table parent disable constraint pk_parent ;
insert into parent values(1,1);
insert into parent values(1,2);
--end of query

output>> 
table PARENT altered.
1 row inserted.
1 row inserted.

Here we are able to insert duplicates because we disabled primary key constraint. Now when we try to enable this constraint  we get error saying unique constraint violated. Constraint can be enabled as follows:
sql>>
--start of query
alter table parent enable constraint pk_parent ;
--end of query

output>> 
Error cause: attempted to validate a primary key with duplicate values or null
           values.

So here also we can use alternative 2 mentioned above.So it was all about novalidate clause from myside, thank you. I will talk about deferrable clause later.
If any doubt you can ask freely in comments.


Wednesday, July 24, 2013

Different types of scheduler in operating system

  1.   Long Term Scheduler: It is also called job scheduler. Long term scheduler determines which programs are admitted to the system for processing. Job scheduler selects processes from the queue and loads them into memory for execution. Process loads into the memory for CPU scheduler. The primary objective of the job scheduler is to provide a balanced mix of jobs, such as I/O bound and processor bound. It also controls the degree of multiprogramming. If the degree of multiprogramming is stable, then the average rate of process creation must be equal to the average departure rate of processes leaving the system. On some systems, the long term scheduler may be absent or minimal. Time-sharing operating systems have no long term scheduler. When process changes the state from new to ready, then there is a long term scheduler.
  2.  Short Term Scheduler: It is also called CPU scheduler. Main objective is increasing system performance in accordance with the chosen set of criteria. It is the change of ready state to running state of the process. CPU scheduler selects from among the processes that are ready to execute and allocates the CPU to one of them. Short term scheduler also known as dispatcher, execute most frequently and makes the fine grained decision of which process to execute next. Short term scheduler is faster than long term scheduler.
  3.   Medium Term Scheduler: Medium term scheduling is part of the swapping function. It removes the processes from the memory. It reduces the degree of multiprogramming. The medium term scheduler is in charge of handling the swapped out-processes.
Running process may become suspended by making an I/O request. Suspended processes cannot make any progress towards completion. In this condition, to remove the process from memory and make space for other process. Suspended process is move to the secondary storage device. Saving the image of a suspended process in secondary storage is called swapping, and the process is said to be swapped out or rolled out. Swapping may be necessary to improve the process mix.

Saturday, July 20, 2013

Max HeapSort Algorithm and its Application in implementing Max Prioritiy Queues

Operations supported on Max-Priority Queues are:
1)  Max : Returns maximum element currently in queue in O(1) time
2)  ExtractMax : Returns and removes maximum element currently
                         in queue in O(lg(n)) time
3) Insert: Inserts given element in queue in O(lg(n)) time
4) IncreasePriority : Increases priority of element at given index in
                             O(lg(n)) time
5) IncreasePriority : Decrease priority of element at given index in
                             O(lg(n)) time

All order statistics mentioned above is in worst case

#include <stdio.h>
#include <stdlib.h>

#define LEFT(i) ((i<<1)+1)
#define RIGHT(i) ((i<<1)+2)
#define PARENT(i) (i>>1)

void Swap(int *i,int *j);
void MaintainMaxHeap(int a[],int index,int size);
void BuildMaxHeap(int a[],int size);
void HeapSort(int a[],int size);

int Max(int a[],int numValidElements,int size)
{
    if(numValidElements>0)
        return a[0];
    else
            fprintf(stderr,"Error\n");
}


/*
Number of numValidElements count should be decremented 
by caller itself.
*/
int ExtractMax(int a[],int numValidElements,int size)
{
    if(numValidElements>0)
    {
        Swap(&a[0],&a[--numValidElements]);
        MaintainMaxHeap(a,0,numValidElements);
        return a[numValidElements];
    }
    else
        fprintf(stderr,"Error\n");  
}


void IncreasePriority(int a[],int index,int newPriority,int numValidElements)
{
    if(a[index]<newPriority)
    {
        a[index]=newPriority;
        while(index>0 && a[index]>a[PARENT(index)])
        {
            Swap(&a[index],&a[PARENT(index)]);
            index=PARENT(index);
        }
    }
    else
    {
        fprintf(stderr,"Error in IncreasePriority\n");
    }
}


void DecreasePriority(int a[],int index,int newPriority,int numValidElements)
{
    if(a[index]>newPriority)
    {
        a[index]=newPriority;
        MaintainMaxHeap(a,index,numValidElements);
    }
    else
    {
        fprintf(stderr,"Error in DecreasePriority\n");
    }
}


/*
Its upto caller to increase numValidElements after 
*/
void Insert(int a[],int priority,int numValidElements,int size)
{
    if(numValidElements<size)
    {
        a[numValidElements]=INFINITE;
        IncreasePriority(a,numValidElements,priority,size);
    }
    else
        fprintf(stderr,"Queue Overflow\n");

}


/*
Pre: Subtrees of node at position index are already both max_heaps 
       and size is number of elements in an array 
Post: Element at position index is inserted properly so that tree rooted 
        at index is max_heap 
*/
void MaintainMaxHeap(int a[],int index,int size)
{
        int left=LEFT(index);
        int right=RIGHT(index);
        int max=index;
        if(left < size && a[left]>a[max])
                max=left;
        if(right < size && a[right]>a[max])
                max=right;
        if(max!=index)
       {
                Swap(&a[max],&a[index]);
                MaintainMaxHeap(a,max,size);
       }
}


/*
Post: Elements of array a from index [0,size-1] are 
         arranged so as to form max heap
*/
void BuildMaxHeap(int a[],int size)
{
        int i;
        // Elements from (size/2) to (size-1) are all leaves 
        for(i=size/2-1;i>=0;i--)
        MaintainMaxHeap(a,i,size);
}


void HeapSort(int a[],int size)
{
        int i;
        BuildMaxHeap(a,size);
        for(i=size-1;i>0;i--)
       {
                // Max element in array is at index=0
               Swap(&a[0],&a[i]);
               MaintainMaxHeap(a,0,--size);
       }
}


void Swap(int *i,int *j)
{
        int temp=*i;
        *i=*j;
        *j=temp;
}


NOTES: 
1)Above implementation was for implementing max priority queues, similarly minimum priority queues could also be implemented using min heaps. 
2)Error Handling is done simply by printing message.In any practical application it should be thoroughly done. Thank you.

Friday, July 19, 2013

1) Reversing k elements of a singly linked list at a time (Amazon Test Question) and 2) Finding last kth elements of singly linked list Efficiently

Suppose our linked list contains integer values and is described as:
1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8
and if we want to reverse 2 elements at a time then modified linked list should be:
2 -> 1 -> 4 -> 3 -> 6 -> 5 -> 8 -> 7
and if we want to reverse 3 elements at a time then modified linked list should be:
3 -> 2 -> 1 -> 6 -> 5 -> 4 -> 7 -> 8
Here in case where k=3 last 2 elements are not reversed because 2<3.

We will use recursive algorithm to implement above reversal. Assume List Node's defination as:

//Node Data
typedef struct _DATA
{
int key;

}DATA,*PDATA;

//Node Struct
typedef struct _NODE
{
DATA item;
struct _node *pnext;

}NODE,*PNODE;

//
//Helper functions
//

PNODE NewNode(DATA item);
PNODE InsertAtStart(PNODE phead,DATA item);
PNODE DeleteAtStart(PNODE phead,PDATA pdata);
void Traverse(PNODE phead);
void DeleteList(PNODE phead);

//Reverses linked list pointed by phead
PNODE Reverse(PNODE phead);

//
// First reverse first k nodes then reverse N-k nodes  
// recursively where N=total size of  list
//
PNODE ReverseKNodesAtATime(PNODE phead,int k)
{
PNODE temp=phead;
int count=k;
     
        // Move to kth node from starting
while(temp && --count)
{
temp=temp->pnext;
}
if(temp)
{
                /*remaining list containing N-k  nodes head*/
PNODE nextstart=temp->pnext;
                 
                /* set end of list of first k nodes to NULL*/
temp->pnext=NULL;                                    

                 /* newHead is head of list of first k nodes after reversal*/
                PNODE newHead=Reverse(phead);                
                                                                                            
               /*Now phead points to end of first k nodes list after  reversal*/
               phead->pnext=ReverseKNodesAtATime(nextstart,k);

return newHead;
}
else
{
return phead;
}
}

//
// Finds and returns kth element from end of list if exist else returns NULL
//
PNODE LastKthNode(PNODE phead,int k)
{
PNODE back=phead,
  front=phead;

if(k<1)
return NULL;
else
{
// move front forward till number of nodes between 
                // front and back becomes not equal to k-1
while(front && k--)
{
front=front->pnext;
}

if(k!=-1)
{
if(!front)
printf("Error\n");
else
return NULL;
}
else
{
// when front becomes null, our back points to last kth  
                        // nodes because initial difference of k-1 nodes
while(front)
{
front=front->pnext;
back=back->pnext;
}
return back;
}
}
}

int main(void)
{
PNODE phead=NULL;
DATA d;
int i;
        //insert 10 elements 
for(i=1;i<=10;i++)
{
d.key=i;
phead=InsertAtStart(phead,d);
}
phead=Reverse(phead);
printf("List Reversed\n");
Traverse(phead);
printf("Reversed 2 at a time\n");
phead=ReverseKNodesAtATime(phead,2);
Traverse(phead);
PNODE pLastkthNode=LastKthNode(phead,6);
if(pLastkthNode)
{
printf("Last 6th node: %d\n",pLastkthNode->item.key);
}
else
{
printf("Last 6th node doesn't exist\n");
}

        DeleteList(phead);

        return 0;
}

PNODE Reverse(PNODE phead)
{
PNODE ptail,pmiddle;
ptail=pmiddle=NULL;

while(phead)
{
ptail=pmiddle;
pmiddle=phead;
phead=phead->pnext;
pmiddle->pnext=ptail;
}
return pmiddle;
}

PNODE NewNode(DATA item)
{
PNODE pnode=(PNODE)malloc(sizeof(NODE));
if(pnode)
{
pnode->item=item;
pnode->pnext=NULL;
}
return pnode;
}

PNODE InsertAtStart(PNODE phead,DATA item)
{
if(!phead)
{
PNODE pnode=NewNode(item);
if(!pnode)
printf("Failed to allocate memory\n");
return pnode;
}
else
{
PNODE pnewNode=NewNode(item);
if(pnewNode)
pnewNode->pnext=phead;
else
printf("Failed to allocate memory\n");
}
}

PNODE DeleteAtStart(PNODE phead,PDATA pdata)
{
if(!phead)
return phead;
else
{
PNODE pnext=phead->pnext;
*pdata=phead->item;
free(phead);
return pnext;
}
}

void Traverse(PNODE phead)
{
while(phead)
{
printf("%d\n",phead->item.key);
phead=phead->pnext;
}
}

void DeleteList(PNODE phead)
{
while(phead)
{
PNODE ptemp=phead;
phead=phead->pnext;
free(ptemp);
}
}

Please ask for doubt if any. Thank you.

Saturday, June 15, 2013

Traversal (inorder,preorder,postorder) of Binary Tree using recursion and also iteratively in c.

Hi friends,
      I will focus on how to traverse binary tree using iterative method which is tricky as compared to traversal using recursion. Consider following binary tree for example:


Inorder traversal means (left,visit,right) for every node starting from root .Inorder traversal for above tree gives:               1 3 4 6 7 8 10 13 14

Preorder traversal means (visit,left,right) for every node starting from root .Preorder traversal for above tree gives:               8 3 1 6 4 7 10 14 13

Postorder traversal means (left,right,visit) for every node starting from root .Postorder traversal for above tree gives:               1 4 7 6 3 13 14 10 8

Assume Node defination as:


typedef struct BinaryTreeNode
{
    int key;
    struct BinaryTreeNode *left,*right,*parent;
}TreeNode;

/*Recursive Inorder traversal: This traversal print keys in non-decreasing sorted order */

void traverse_inorder_rec(TreeNode *root)
{
    if(root!=NULL)
    {
        traverse_inorder_rec(root->left);
        printf("%d ",root->key);
        traverse_inorder_rec(root->right);
    }
}

/*Recursive Preorder traversal*/

void traverse_preorder_rec(TreeNode *root)
{
    if(root!=NULL)
    {
        printf("%d ",root->key);
        traverse_preorder_rec(root->left);
        traverse_preorder_rec(root->right);
    }
}

/*Recursive Postorder traversal: This traversal is helpful for deleting tree*/

void traverse_postorder_rec(TreeNode *root)
{
    if(root!=NULL)
    {
        traverse_postorder_rec(root->left);
        traverse_postorder_rec(root->right);
        printf("%d ",root->key);
    }
}

/*In all 3 iterative traversals we use stack as auxiliary data structure*/

/*Iterative Preorder: This is the most easiest among all 3 iterative traversals*/

void traverse_preorder_iterative(TreeNode *root)
{
    /*
    *Node's currently on stack are nodes whose keys are to be printed
    and left and right both child to be processed later.

    *All TreeNode * in stack are non NULL
    */
    stack<TreeNode *> s;
    if(root!=NULL)
    {
        s.push(root);
        while(!s.empty())
        {
            TreeNode *ptr=s.top();
            s.pop();
            printf("%d ",ptr->key);
            if(ptr->right!=NULL)
                s.push(ptr->right);
            if(ptr->left!=NULL)
                s.push(ptr->left);
        }
    }
}

/*Iterative Inorder traversal*/

void traverse_inorder_iterative(TreeNode *root)
{
        stack<TreeNode *> s;
        TreeNode *current=root;
        bool done =false;
        while(!done)
        {
            if(current)
            {
                s.push(current);
                current=current->left;
            }
            else
            {
                if(s.empty())
                {
                    done=true;
                }
                else
                {
                    current=s.top();
                    s.pop();
                    cout<<current->key<<" ";
                    current=current->right;
                }
            }
        }
}

/*Iterative post order traversal: This is the most trickiest among all this 3 iterative versions.Here we use prevNode pointer for keeping track in which direction we are moving.For eg. Either (upward from left or right ) or (downward from left or right )*/

void traverse_postorder_iterative(TreeNode *root)
{
    if(root!=NULL)
    {
        stack<TreeNode *> s;
        TreeNode *prevNode,*currentNode;
        prevNode=NULL;
        s.push(root);

        while(!s.empty())
        {
            currentNode=s.top();
            if(prevNode==NULL || prevNode->left==currentNode ||                            
                prevNode->right==currentNode)
            {
                if(currentNode->left!=NULL)
                    s.push(currentNode->left);
                else if(currentNode->right!=NULL)
                    s.push(currentNode->right);
                else
                {
                    printf("%d ",currentNode->key);
                    s.pop();
                }
            }
            else if(prevNode==currentNode->left)
            {
                if(currentNode->right!=NULL)
                    s.push(currentNode->right);
                else
                {
                    printf("%d ",currentNode->key);
                    s.pop();
                }
            }
            else if(prevNode==currentNode->right)
            {
                printf("%d ",currentNode->key);
                s.pop();
            }
            prevNode=currentNode;
        }
    }
}
Please comment if any doubt and like if helpful.
For detail explanation of postorder iterative version ,refer to link:http://leetcode.com/2010/10/binary-tree-post-order-traversal.html

Creating pascal triangle using 1-Dimensional array for finding coefficients of (1+x)^n efficiently.

Hi friends,
     As you all know pascal triangle looks like
                                                                     1                                      n=0
                                                                 1      1                                  n=1
                                                             1      2      1                              n=2
                                                          1     3       3      1                         n=3
                                                       1     4     6      4      1                      n=4
                                                    1     5    10   10     5     1                   n=5
                                                 ............................................                so on
Observe some facts:
1)Number of element in ith row = (i+1)

2) ith element in nth row = sum of (i-1)th & (i)th element in (n-1)th row  for 1<i<(n+1)

3)Also ,
For n=0:     (1+x)^0=1
For n=1:     (1+x)^1=1*(x)
For n=2:     (1+x)^2=2*(x) + 1*(x*x)
For n=3:     (1+x)^3=3*(x) + 3*(x*x) + 1*(x*x*x)

Observe the coefficients underlined, they belongs to each row of above triangle. For eg. when n=2,
coefficients are (1,2,1) which is row number 2 of pascal triangle. Similar situation occurs for n=3,here
coefficients are (1,3,3,1) which is row number 3 of pascal triangle.

4)Coefficients of (1+x)^n form sequence :      (nC0,nC1,nC2,.....nCn)    
where nCr=(n!)/(r! * (n-r)!)

So,by finding nth row of pascal triangle we are finding coefficients of (1+x)^n and hence indirectly above sequence of combinations.

Now we will see how to generate above pascal triangle recursively for specified value of 'n' using 1-D array in c++.


Size of 1-D array for above triangle for some n = 1+2+3+....+(n+1) =
 ((n+1)*(n+2))/2

Starting index in this 1-D array for nth row of  pascal triangle= 1+2+...n =
(n*(n+1))/2

//Actual c++ routine starts here:

void Form_Pascal_Triangle(unsigned long int a[],unsigned long int n)
{
    if(n==0)
    {
        a[0]=1;
    }
    else
    {
        Form_Pascal_Triangle(a,n-1);

        unsigned long int PREVTO_NTH_ROW_START_INDEX=(n*(n-1))/2;

        unsigned long int CURRENT_NTH_ROW_START_INDEX=(n*(n+1))/2;

        //initialise first and last element of nth row of pascal triangle
        a[CURRENT_NTH_ROW_START_INDEX]=1;
        a[CURRENT_NTH_ROW_START_INDEX+n]=1;

        for(unsigned long int i=1;i<n;i++)
        {
             a[CURRENT_NTH_ROW_START_INDEX+i] =
                                       a[PREVTO_NTH_ROW_START_INDEX+i-1] +
                                       a[PREVTO_NTH_ROW_START_INDEX+i];
        }
    }
}


int main()
{
    unsigned long int n;
    cout<<"Enter n:";
    cin>>n;
    unsigned long int size=((n*(n+1))/2)+(n+1);
    unsigned long int a[size];
 
    Form_Pascal_Triangle(a,n);

    return 0;
}

Please comment if any doubt and like if helpful.Thanks


Monday, May 20, 2013

Connecting to gmail and downloading all mails in .mbox file format on local hard drive using python is so easy.Try it



Note that this script marks all mails in your mailboxes as read so be careful. Also I am not responsible for any data loses or corruption.Save this with some filename with .py extension and run it from command line as filename.py. Also this is written for 3.x python version. 

 

import imaplib,re,mailbox
from email.parser import BytesParser
class mbox:
def __init__(self,filename):

filename=filename.split('/')[-1]+".mbox"

#print(filename)
self.file_handle=mailbox.mbox((filename))

def dump_mails(self,msg_list):
try:
self.file_handle.lock()

for each_msg in msg_list:
mbox_msg=mailbox.mboxMessage(each_msg)
self.file_handle.add(mbox_msg)

self.file_handle.flush()
finally:
self.file_handle.unlock()

def close(self):
self.file_handle.close()



class gmail:
def __init__(self):
self.IMAP_SERVER='imap.gmail.com'
self.IMAP_PORT=993
self.M = None
self.response=None
self.mailboxes=[]

def connect(self,username,password):
self.M=imaplib.IMAP4_SSL(self.IMAP_SERVER,self.IMAP_PORT)
status,self.response=self.M.login(username,password)
return status

def get_mailboxes(self):
rc,self.response=self.M.list()
#pattern=r'"([[a-zA-Z0-9]+]/)*[a-zA-Z0-9 ]+"'
pattern=r'".*?"'
pattern=re.compile(pattern)

for item in self.response:
item=item.decode("ascii")
folder_subpath_list=pattern.findall(item)
self.mailboxes.append(folder_subpath_list[-1][1:-1])
#print("self.mailbox",self.mailboxes)
return self.mailboxes

def get_mailcount(self,mailbox='inbox'):
rc,self.response=self.M.select(mailbox)
self.mailcount=int(self.response[0])
return self.mailcount

def get_unread_mailcount(self,mailbox='inbox'):
rc,message=self.M.status(mailbox,"(UNSEEN)")
for item in message:
print("message item= ",item)
unreadCount = re.search("UNSEEN (\d+)", str(message[0])).group(1)
return unreadCount

def rename_mailbox(self, oldmailbox, newmailbox):
rc, self.response = self.M.rename(oldmailbox, newmailbox)
return rc

def create_mailbox(self, mailbox):
rc,self.response= self.M.create(mailbox)
return rc

def delete_mailbox(self, mailbox):
rc, self.response = self.M.delete(mailbox)
return rc

#retrieves all mails and probably marks them as read so be carefull
def get_all_mails(self,mailfolder):

#select specified mailfolder at server
rc,self.response=self.M.select(mailbox=mailfolder)

#get mail numbers list
status,data=self.M.search(None,'ALL')

mail_obj_list=[]
for mail_no in data[0].split():
status,mail=self.M.fetch(mail_no,'(RFC822)')
mail_obj=BytesParser().parsebytes((mail[0][1]))
mail_obj_list.append(mail_obj)
return mail_obj_list

def disconnect(self):
self.M.logout()


#main script starts


gmail_link=gmail()
username=input('Enter username:').strip()
password=getpass.getpass()
print ("connecting...")
print (gmail_link.connect(username,password),gmail_link.response)

print ("Retrieving mailboxes...")
mailboxes=gmail_link.get_mailboxes()
print ("Done...")


for mailbox in mailboxes:
try:
if mailbox.find(' ')!=-1:
continue
print ("Processing mailbox:",mailbox)

#get all mails_list present in mailbox
mails=gmail_link.get_all_mails(mailfolder=mailbox)

#create new mailbox(type =mbox) for each folder(mailbox)
mailbox_handle=mbox(filename=mailbox)

#add mails to mailbox created
mailbox_handle.dump_mails(mails)
print ("Processing mailbox:",mailbox,"done")
except:
print("Could not retrieve from mailbox:",mailbox)
finally:
mailbox_handle.close()
print("Disconnecting...")
gmail_link.disconnect()