Tuesday, December 4, 2018

PowerMockito: Mocking static method

Mocking Static method and invoking private static method:

PowerMockito.mockStatic(ClassWithPrivateStaticMethods.class);
PowerMockito.when(ClassWithPrivateStaticMethods.class, "getParam", Mockito.anyString()).thenReturn("dummy");

but the best way is

PowerMockito.spy(ClassWithPrivateStaticMethods.class);
PowerMockito.doReturn("dummy").when(ClassWithPrivateStaticMethods.class, "getParam", Mockito.anyString())
String finalResult = Whitebox.invokeMethod(ClassWithPrivateStaticMethods.class, "getDetail", Mockito.anyString());


More details on:

https://initcodes.blogspot.com/2018/05/powermockito-mocking-one-static-method.html


We can Invoke the private static methods with whitebox from mockito which uses reflection api


public void testLoadLanguageCodeFile()
          throws WorldlingoException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {

    //testing correct file path    String path = "/webapps/worldlingo/conf/settings/";
    PowerMockito.mockStatic(SystemSettings.class);
    HashMap<String, String> hash_map = new HashMap<String, String>();
    SystemSettings ssmock = Mockito.mock(SystemSettings.class);
    Mockito.when(SystemSettings.GetInstance()).thenReturn(ssmock);
    Mockito.when(ssmock.getLangCodePath()).thenReturn(path);
    Method m = Whitebox.getMethod(MicrosoftEngine.class,"loadLanguageCodeFile", HashMap.class);
    m.invoke(null, hash_map);
    assertTrue((hash_map.containsKey("de") && hash_map.containsKey("ar")) || hash_map.isEmpty());

    //testing incorrect file path    path = "/webapps/worldlingo/conf";
    Mockito.when(ssmock.getLangCodePath()).thenReturn(path);
    hash_map = new HashMap<String, String>();
    try {
      m = Whitebox.getMethod(MicrosoftEngine.class, "loadLanguageCodeFile", HashMap.class);
      m.invoke(null, hash_map);
    }
    catch (InvocationTargetException e){
      String result =  e.getTargetException().toString();
      assertEquals("java.lang.NullPointerException",result);
    }
  }

} // MicrosoftEngineTest



Mockito important tips:

do answer:
      @Test
 public void test_answer() throws Exception {
    Dummy dummy = mock(Dummy.class);
    Answer<Integer> answer = new Answer<Integer>() {
        public Integer answer(InvocationOnMock invocation) throws Throwable {
            String string = invocation.getArgumentAt(0, String.class);
            return string.length() * 2;
        }
    };

    // choose your preferred way
    when(dummy.stringLength("dummy")).thenAnswer(answer);
    doAnswer(answer).when(dummy).stringLength("dummy");
 }



Mockito.doAnswer(new Answer() {
      @Override
      public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
        // this will be used to assert the status response
        key[0] =  (String)invocationOnMock.getArguments()[0];
        value[0] =  (String) invocationOnMock.getArguments()[1];
        return null;
      }
    }).when(robMock).addHeader(Mockito.anyString(),Mockito.anyString());


    PowerMokito:
       PowerMockito.whenNew(SFramework.class).withNoArguments().thenReturn(sfMock);

       https://github.com/powermock/powermock/wiki/Mockito#mocking-static-method


    https://initcodes.blogspot.com/2018/07/powermockito-javasecuritynosuchalgorith.html


Monday, July 30, 2018

searching of digit from string using regex and replace from Java String.

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class regex{

public static void main(String []args){
    System.out.println("Hello World");
    String s = "214投資者、43発行者";
    Pattern p = Pattern.compile("([0-9]+)");
    Matcher m = p.matcher(s);
    int index = 0;
    StringBuffer str = new StringBuffer();
    while (m.find()){

        String match = m.group();
        m.appendReplacement(str,"replace"+">");

    }
    m.appendTail(str);
    String result = str.toString();
    System.out.println(result);


   }
}

Monday, June 11, 2018

Breadth first search(BFS), Depth first Search(DFS) both iterative and recursive in Java

Graph can be represented in two ways:
 1)adjacency matrix
  2) adjancency list


Note here we create array of linkedlist. Array are of size length vertices.
Each array has all its childrens so each array is linkedlist  of size vertices V.






Saturday, June 9, 2018

Trees in java

Simple Trees in Java:

class node1
{
   int data;
    node1 left;
    node1 right;

    node1(int data){
        this.data = data;
        this .left = null;
        this.right = null;
    }

    void inorder(){
        if(left != null) {
            left.inorder();
        }
            System.out.println(this.data);
        if(right != null)
            right.inorder();

    }
}

public class Tree {

    void inorder(node1 root) {
        if (root!= null) {

            inorder(root.left);
            System.out.println(root.data);
            inorder(root.right);
        }
    }

    public static void main(String[] args)
    {
        node1 root1 = new node1(5);
        root1.left = new node1(3);
        root1.right = new node1(7);
        root1.left.left = new node1(2);
        root1.left.right = new node1(4);
        root1.right.left = new node1(6);
        root1.right.right = new node1(8);

        Tree tree = new Tree();
        tree.inorder(root1);

        root1.inorder();

    }
}


For addition of add_item function for creating binary tree:
Note to use in lower class if root has to be passed otherwise in the upper class of only item has 
to be passed as root should be referenced with object.



class node1
{
   int data;
    node1 left;
    node1 right;

    node1(int data){
        this.data = data;
        this .left = null;
        this.right = null;
    }

  void add_node(int item)
  {
      if(item < this.data )
      {
          if(this.left == null)
          {
              this.left = new node1(item);
          }

          else{
              this.left.add_node(item);
          }
      }
      else{
          if(this.right == null)
          {
              this.right = new node1(item);
          }
          else          {
              this.right.add_node(item);
          }
      }
  }


}

public class Tree {

    void inorder(node1 root) {
        if (root!= null) {

            inorder(root.left);
            System.out.println(root.data);
            inorder(root.right);
        }
    }

    void add_node(node1 root,int item)
    {
        if(root == null)
            root = new node1(item);
        else{
            if(item < root.data )
                if(root.left == null)
                    root.left = new node1(item);
                else                {
                    add_node(root.left,item);
                }
            else{
                if(root.right == null)
                    root.right = new node1(item);
                else {
                    add_node(root.right, item);
                }
            }
        }
    }

    public static void main(String[] args)
    {
        node1 root1 = new node1(5);
//        root1.add_node(3);//        root1.add_node(2);//        root1.add_node(1);//        root1.add_node(6);//        root1.add_node(8);//        root1.add_node(7);




        Tree tree = new Tree();


        tree.add_node(root1,3);
        tree.add_node(root1,2);
        tree.add_node(root1,1);
        tree.add_node(root1,6);
        tree.add_node(root1,8);
        tree.add_node(root1,7);
        tree.add_node(root1,9);
        tree.inorder(root1);


    }
}

Linked List creation in Java from scratch

1. Simple linked list creation statically:

class node{
    int data;
    node next;

    node(int item){
        data = item;
        next = null;
    }


}

class linked_list{

    node create_list()
    {
        node head = new node(2);
        head.next = new node(3);
        head.next.next = new node(4);
        head.next.next.next = new node(5);
        return head;

    }


    void get_list(node head)
    {
        node current = head;
        while(current != null)
        {
            System.out.println(current.data);
            current = current.next;
        }

    }

}

public class list {


    public static void main(String[] args)
    {

        linked_list start = new linked_list();
        node head = start.create_list();
        start.get_list(head);
    }


}

2. Simple within the same class:

class list
{
    int data;
    list next;
    list(int date)
    {
        data = date;
        next = null;
    }
}
public class graph {
    void add_data(list root,int item)
    {
        list current = root;
        while(current.next != null)
        {
            current = current.next;

        }
        current.next = new list(item);

    }

    void get_data(list root)
    {
        list current = root;
        while(current != null)
        {
            System.out.println(current.data);
            current = current.next;
        }
    }



    public static void main(String[] args)
    {
        list root = new list(5);

        graph obj = new graph();
        obj.add_data(root,3);
        obj.add_data(root,4);
        obj.add_data(root,7);

        obj.get_data(root);
    }
}





2.With recursive insert:

Note to always store head in an object so that each time it doesnot change. Otherwise we need to
keep track of head and end seperately.

class node{
    int data;
    node next;

    node(int item){
        data = item;
        next = null;
    }

    public int getData() {
        return data;
    }

    public node getNext() {
        return next;
    }

    public void setData(int data) {
        this.data = data;
    }

    public void setNext(node next) {
        this.next = next;
    }
    public void insert_number(int data)
    {
        if(next == null){
            next = new node(data);

        }

        else{
            next.insert_number(data);
        }
    }
}

class linked_list{
    node head;

    linked_list(){
        head = null;
    }
    

    void insert_node(int data){
        if(head == null)
        {
            head = new node(data);
        }
        else{
            head.insert_number(data);
        }
/////or even can be done without recursion
//   note the n.next should be used.
/// public void insert_number(int data)
//{
//       node end = new node(data);
//  
      node n = this;//      
  while(n.next!= null)
//       {
//            n = n.next;
//        }
//        n.next = end;
//    }


}
    void get_list()
    {
        node current = head;
        while(current != null)
        {
            System.out.println(current.data);
            current = current.next;
        }

    }

}

public class list {


    public static void main(String[] args)
    {

        linked_list start = new linked_list();


        start.insert_node(2);
        start.insert_node(3);
        start.insert_node(4);
        start.insert_node(5);
        start.insert_node(6);
        start.insert_node(7);

        start.get_list();
    }





}




3: To keep track of head and end seperately:

class node{
    int data;
    node next;

    node(int item){
        data = item;
        next = null;
    }

    public int getData() {
        return data;
    }

    public node getNext() {
        return next;
    }

    public void setData(int data) {
        this.data = data;
    }

    public void setNext(node next) {
        this.next = next;
    }

}

class linked_list{
    node head;
    node end;

    linked_list(){
        head = null;
        end = head;
    }


    void insert_node(int data){
        if(head == null)
        {
            head = new node(data);
            end = head;
        }
        else{
            node nptr = new node(data);
            end.setNext(nptr);
            end = nptr;

        }


    }
    void get_list()
    {
        node current = head;
        while(current != null)
        {
            System.out.println(current.data);
            current = current.next;
        }

    }

}

public class list {


    public static void main(String[] args)
    {

        linked_list start = new linked_list();


        start.insert_node(2);
        start.insert_node(3);
        start.insert_node(4);
        start.insert_node(5);
        start.insert_node(6);
        start.insert_node(7);

        start.get_list();
    }





}



So overall with just only one class node is 

class node{
    int data;
    node next;

    node(int item){
        data = item;
        next = null;
    }

   
    public void insert_number(int data)
    {
       node end = new node(data);
        node n = this;
        while(n.next!= null)
        {
            n = n.next;
        }
        n.next = end;
    }

    void get_list()
    {
        node current = this;
        while(current != null)
        {
            System.out.println(current.data);
            current = current.next;
        }

    }
}



public class list {


    public static void main(String[] args)
    {

       node start = new node(1);


        start.insert_number(2);
        start.insert_number(3);
        start.insert_number(4);
        start.insert_number(5);
        start.insert_number(6);
        start.insert_number(7);

        start.get_list();
    }





}

Saturday, June 2, 2018

Inserting of elements in Binary search tree with minimal tree creation in Python

class node:
    def __init__(self,val):
        self.value = val
        self.left = None        self.right = None
def insert_node(val,root):
    if(val<root.value):
        if(root.left is None):
            root.left = node(val)
        else:
            insert_node(val,root.left)

    else:
        if(root.right is None):
            root.right = node(val)
        else:
            insert_node(val,root.right)

def inorder(root):
    if(root):
        print(root.value)
        inorder(root.left)
        inorder(root.right)

def minimal_tree(arr,start,end):
    if(start>end):
        return None    mid = (start+end)//2    root = node(arr[mid])
    root.left = minimal_tree(arr,start,mid-1)
    root.right = minimal_tree(arr,mid+1,end)

    return root

root = node(3)

x= [5,4,1,2]
for i in x:
    insert_node(i,root)

inorder(root)
y = [1, 2, 3, 4, 5 ,6 ,7, 8,9]




test = minimal_tree(y,0,len(y)-1)
print(test.value)
inorder(test)

Tuesday, May 29, 2018

recursive and iterative BFS, DFS,inorder traversal of graph in python

from collections import defaultdict
import queue

#######DFS  RECURSIVE
class node:
    def __init__(self,value):
        self.data = value
        self.left = None        
        self.right  = None
class DFS:
    def __init__(self):
        self.graph =defaultdict(list)
    def dfs_call(self,visited,val):
        if val is None:
            return        
        visited[val] = True        
        print(val)

        for i in self.graph[val]:
            if i is not None:
                if(visited[i]==False):
                    self.dfs_call(visited,i)


    def dfs_recursive(self,val):
        visited = [False]*(len(self.graph))
        self.dfs_call(visited, val)

###### DFS RECURSIVE

#######DFS ITERATIVE for binary tree
def dfs_iter(root):
    visited = [False]*10    
    stack =[]
    stack.append(root)

    while(stack):
        val = stack.pop()
        print (val.data)
        if((val.right  is not None)):
            stack.append(val.right)
        if((val.left  is not None)):
            stack.append(val.left)

###DFS ITERATIVE FOR GRAPH
def dfs_iter_graph(g,len):
    visited = [False]*len
    stack = []
    stack.append(0)
    visited[0]=True
    while(stack):
        val = stack.pop()
        print(val)

        for i in g.graph[val]:
            if i is not None:
                if(visited[i] == False):
                    stack.append(i)
                    visited[i] = True


##INORDER RECURSIVE AND ITERATIVE###########
def inorder(root):
    if(root):
        inorder(root.left)
        print (root.data)
        inorder(root.right)



def inorder_iter(root):
    current =root
    stack = []

    done = 0
    while(not done):
        if(current is not None):

            stack.append(current)
            current =current.left



        else:
            if(stack):
                current = stack.pop()
                print(current.data)

                current = current.right
            else:
                done =1





######BFS WITH QUEUE

def BFS(root,length):
    L = queue.Queue(maxsize=10)
    visited = [False]*length
    L.put(0)
    visited[0] = True
    while( not L.empty()):
        val = L.get()
        print(val)

        for i in g.graph[val]:
            if i is not None:
                if(visited[i] == False):
                    L.put(i)
                    visited[i] == True











root = node(5)
root.left = node(4)
root.right = node(10)
root.left.left = node(3)
root.right.right = node(15)
# inorder_iter(root)
# dfs_iter(root)






g = DFS()
g.graph[0].append(1)
g.graph[0].append(2)
g.graph[1].append(3)
g.graph[1].append(4)
g.graph[2].append(5)
g.graph[2].append(6)
g.graph[3].append(None)
g.graph[3].append(None)
g.graph[4].append(None)
g.graph[4].append(None)
g.graph[5].append(None)
g.graph[5].append(None)
g.graph[6].append(None)
g.graph[6].append(None)
# g.dfs_recursive(0)len = len(g.graph)
print (len)

# dfs_iter_graph(g,len)BFS(g,len)

Saturday, May 19, 2018

Matlab Multilayer Perceptron (MLP) with back propagation algorithm to solve a 3-class classification problem.

Specification:

Number of layers: 2 (hidden + output) 
Number of input units: 13 (+ 1 for bias)
Number of hidden units: 8 (+1 for bias) 
Number of output units: 3 
Activation functions: sigmoid for hidden units; softmax for output units 
Initial weights: uniform random numbers between 0 and 1




Code:






Output:


folowing steps are followed
1: forward pass to compute an,zn,bn and yn
2: compute the error signal.
3: Pass error backward to compter error for hidden layer.
4: Compute gradient
5: Update weight
*********************************************
Total number of instance = 178 
learning rate = 0.500000 
Total number of iteration = 5000 
cost befor training 1.971846
Total number of correctly classified instance before traing = 71 
Start Training
Total number of correctly classified instance after training = 178 
Cost after Training = 0.020612 
Total time = 22.546547 sec
*********************************************
>> 

Matlab program to implement gradient descent algorithm to fit linear regression parameter

Data: fisherIrisSetosaData

implementation of  Linear Regression to predict sepal length given sepal width. 



This program finds the regression parameter w0 and w1. With this parameter, the test instance output can be obtained.
Code:



clc
clear all

fisherIrisSetosaData = importdata("fisherIrisSetosaData.txt");

Y = fisherIrisSetosaData(1:end,1);%sepal length
X = fisherIrisSetosaData(1:end,2);%sepal Width

w0_old = 0;
w1_old = 0;
w0diff =1;
w1diff = 1;
alpha = 0.1;
count =0;
tic;
while (w0diff >0.000001 || w1diff >0.000001)
        count = count +1;
        sum_w0 = 0;
        sum_w1 = 0;
        for i =1:50
            sum_w0 = sum_w0+ ((w0_old+w1_old.*X(i,1))-Y(i,1));
            sum_w1 = sum_w1+ ((w0_old+w1_old.*X(i,1))-Y(i,1)).*X(i,1);
        
        
            
            
            
        end 
       % sum_w0 = sum((w0_old+w1_old.*X)-Y);
       % sum_w1 =sum(((w0_old+w1_old.*X)-Y).*X);
        cost_w0 = alpha/50 *sum_w0;
        cost_w1 =alpha/50 *sum_w1;
        w0_new = w0_old-cost_w0;
        w1_new = w1_old -cost_w1;
        w0diff = abs(w0_old - w0_new);
        w1diff = abs(w1_old - w1_new);
        w0_old = w0_new;
        w1_old = w1_new;
        
        
     
    
end 

time_descent = toc;


fprintf('value of Parameter w0 and w1 is with alpha  = %f\n',alpha)
%disp("value of Parameter w0 and w1 is given as ");
fprintf('W0 = %f\n',w0_old);
fprintf('W0 = %f\n',w1_old);

plot(X,Y,"g*");
hold on
calculated_y = w0_old+w1_old.* X;
plot(X,calculated_y,'r')
legend("real data","calculated line",'Location','NorthWest');

xlabel("SepalWidth");
ylabel("Sepal Length");
title("linear regression with gradient descent");
fprintf("Solving linear function takes %f seconds.\n",time_descent);
fprintf("Total number of Iteration  = %d.\n",count);
disp(count)



Sunday, April 8, 2018

Merge sort and quick sort in python

Merge Sort:

def merge_sort(sor):
    mid = len(sor)//2    
    if(len(sor)<=1):
        return sor


    left =merge_sort(sor[0:mid])
    right = merge_sort(sor[mid:])
    i=j=0 
    tar =0   
    while(i<len(left) and j<len(right)):
        if(left[i]<right[j]):
            sor[tar]=left[i]
            i = i+1        
        else:
            sor[tar] =right[j]
            j=j+1 
        tar =tar+1    
    while(i<len(left)):
        sor[tar] =left[i]
        i =i +1        
        tar=tar+1    
    while(j<len(right)):
        sor[tar]= right[j]
        j=j+1        
        tar =tar+1
    return sor


if __name__ =='__main__':
    arr = [150,4,9,26,1,5,65,2,12,1,1]
    final =merge_sort(arr)
    print (final)





Quick Sort:

def partition(arr,l,h):
    # Note for finding the pivot element and placing the pivot element in middle    
    #  start from initial index and then increase one pointer j to teh last greatest element 
   #   if small element is found swap small element with bigger that is pointed by the j pointer 
   #   
    pivot = arr[h]
    j=l
    for i in range(l,h):
        if(arr[i]<pivot):
            arr[i],arr[j]=arr[j],arr[i]
            j=j+1    arr[j],arr[h]=arr[h],arr[j]
    return  j

def quick_sort(arr,l,h):
    if(l<h):
        pivot = partition(arr,l,h)
        quick_sort(arr,l,pivot-1)
        quick_sort(arr,pivot+1,h)


if __name__ =='__main__':
    arr = [150,4,9,26,8,100,80,12]
    n= len(arr)
    quick_sort(arr,0,n-1)
    print (arr)

Binary tree creation and inorder,preorder traversal both iterative and recurrence in python

class node:
    def __init__(self,key):
        self.data=key
        self.left= None
        self.right =None
def insert(root,nd):
    if(nd.data <= root.data):
        if(root.left is None):
            root.left = nd
        else:
            insert(root.left,nd)
    else:
        if(root.right is None):
            root.right = nd
        else:
            insert(root.right,nd)
def bin_search(root,key):
    if(root is None or root.data == key):
        return root
    else:
        if(key<root.data):
            return bin_search(root.left,key)
        else:
            return bin_search(root.right,key)


def tree_inorder_traverse(node):
    if(node == None):
        return    tree_inorder_traverse(node.left)
    print(node.data)
    tree_inorder_traverse(node.right)

def inorder_iterative(node):
    stack = []
    current = node
    while(1):
        if(current is not None):
            stack.append(current)
            current = current.left
        else:
            if(len(stack)>0):
                current = stack.pop()
                print(current.data)
                current = current.right
            else:
                break
def preorder_traversal(node):
    if(node == None):
        return    print(node.data)
    preorder_traversal(node.left)

    preorder_traversal(node.right)

def preorder_iterative(root):
    stack_pre=[]
    current = root
    while(1):
        if(current is not None):
            print(current.data)
            stack_pre.append(current)
            current =current.left
        else:
            if(len(stack_pre)>0):
                current = stack_pre.pop()
                current = current.right

            else:
                break

def preorder_iterative2(root):
    stack_pre=[]
    current = root
    stack_pre.append(current)
    while(len(stack_pre)>0):
        current = stack_pre.pop()
        print (current.data)
        if(current.right is not None):
            stack_pre.append(current.right)
        if(current.left is not None):
            stack_pre.append(current.left)


if __name__ == "__main__":
    root = node(50)
    insert(root,node(20))
    insert(root,node(70))
    insert(root,node(10))
    insert(root,node(30))
    insert(root,node(60))
    preorder_traversal(root)
    value = bin_search(root,20)
    print (value.data)

    print ("iterative")
    preorder_iterative(root)

Thursday, February 15, 2018

Compare and Swap(CAS) implementation for counter shared data in C++

 implementation in simple increase counter:

#include <iostream>
#include <thread>
#include <unistd.h>
#include <atomic>

std::atomic<int> count(0);
std::mutex n_mutux;

void increase_counter()
{
    int old_value = count.load(); ;
    while (!count.compare_exchange_weak(old_value, old_value +1))
    {
    }



}


int main() {
    unsigned int n = std::thread::hardware_concurrency();
    std::cout << n << " concurrent threads are supported.\n";
    int thread_num =10;
    std::thread t[thread_num];
    for(int i=0;i<thread_num;i++)
    {
        t[i]=std::thread((increase_counter));
    }

    for(int i=0;i<thread_num;i++)
    {
        t[i].join();
    }
   std::cout<<count;
}





implementation with vector and structure:



//// Created by Laxmi Kadariya on 2/14/18.//
#include <iostream>
#include <vector>
#include <thread>
#include <atomic>



std::atomic<int> count(0);
struct Counter {
    int value;

    Counter() : value(0){}

    void increment(){
        int old_value = count.load(); ;
        while (!count.compare_exchange_weak(old_value, old_value +1))
        {
        }
//        ++value;
    }
};

int main(){
    Counter counter;

    std::vector<std::thread> threads;
    for(int i = 0; i < 10; ++i){
        threads.push_back(std::thread([&counter](){
            for(int i = 0; i < 1000; ++i){
                counter.increment();
            }
        }));
    }

    for(auto& thread : threads){
        thread.join();
    }

    std::cout << count << std::endl;
//    std::cout << counter.value << std::endl;

    return 0;
}

Wednesday, February 14, 2018

Reading chunk of data from a file calling a function using C++

#include <iostream>
#include <fstream>
#include <string>
#include<vector>
using namespace std;
bool generate_chunks(ifstream &fp,int chunk_size,string &buffer)
{
    vector<char>data(chunk_size);
    fp.read(&data[0],chunk_size);
    std::streamsize datasize = fp.gcount();
    if(datasize == 0)
    {
        cout<<"here";
        return false;
    }
    buffer =string(data.begin(),data.end());
    return true;

}
int main() {
    std::cout << "Hello, World!" << std::endl;
    ifstream myfile;
    bool test;
    string data;

    myfile.open("../test.dat",ifstream::binary);

   if(myfile.is_open()) {


        if(generate_chunks(myfile, 1024, data))

        {
            cout<<data;
        }

   }


    myfile.close();

    return 0;
}

Tuesday, February 13, 2018

reading of chunk of data from the binary file and unpacking the data using struct construct in python

import struct

class parser():
    def __init__(self):
        self.data = ""        self.offset = 0
    def add_data(self,data):
        self.data = self.data[self.offset:]
        # print (self.data)        
        self.data += data
        self.offset = 0

    def parse_header(self):
        l,n = struct.unpack('>hh',self.data[self.offset: self.offset+4])
        if len(self.data) - self.offset >l:
            status = True        
        else:
            status = False
        return l,n,status



    def parse_QuoteORTrade_data(self):
        val,type = struct.unpack('>hs',self.data[self.offset:self.offset+3])
        # print(val,type)        
        self.update_offset(3)
        if(type == b'Q'):

            # sym,price,size = struct.unpack('>5shh',self.data[self.offset:self.offset+9])            self.update_offset(17)
            # print (sym,price,size)            
            self.update_offset(val-20)
        if(type == b'T'):

            sym,price,size = struct.unpack('>5shq',self.data[self.offset:self.offset+15])
            self.update_offset(15)
            print (sym,price,size)
            self.update_offset(val-18)




    def update_offset(self,offset):
        self.offset = self.offset+ offset

def ReadInChunk(fileobj,ChunkSize= 50):
    while True:
        data = fileobj.read(ChunkSize)
        if not data:
            break
        yield data



def read_file():
    f = open("input.dat","rb")
    return  f



if __name__ == "__main__" :
    offset_chunk = 0    obj=parser()
    fp = read_file()

    for chunk in ReadInChunk(fp):

        obj.add_data(chunk)
        # print (obj.data)       
        while True:
            try:
                length,num,status = obj.parse_header()


            except Exception,e:
                break            
            if(status == False):
                break
            obj.update_offset(4)
            for data in range(num):

                obj.parse_QuoteORTrade_data()





    fp.close()