Aug 18, 2018

JSON Tree Model handling with Jackson: Transform JSON string to JSON Tree and access fields

JSON (Employee details):
{
    "payload": {
        "audit": {
            "enteredBy": "NIKHIL",
            "entryType": "ORDER"
        },
        "employees": {
            "employee": [
                {
                    "lineNo": 1,
                    "name": "NIKHIL RANJAN",
                    "email": "NIKHILRANJAN@DEVINLINE.COM",
                    "address": {
                        "firstLine":"WINDSOR APT",
                        "SecondLine":"#204, BLOCK 2",
                        "state":"KARTA",
                        "postalCode":"50081",
                        "phone": {
                            "home": "4747521304",
                            "work": "9748767304"
                        }
                    },
                    "org": "IT",
                    "department": "DPC"
                }
            ],
            "requestType": "EMPLOYEE_DETAILS"
        }
    }
}

Java Sample code:
import java.io.IOException;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;

public class JacksonJsonNodeParser {
 private static com.fasterxml.jackson.databind.ObjectMapper getJacksonMapper() {
  com.fasterxml.jackson.databind.ObjectMapper mapper = 
    new com.fasterxml.jackson.databind.ObjectMapper();
  mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
  return mapper;
 }

 private static void doJsonTreeParse(String employeeJSON) throws IOException {
  JsonNode responseJsonNode = getJacksonMapper().readTree(employeeJSON);
  JsonNode employeesDetails = responseJsonNode.get("payload").get("employeesDetails");
  JsonNode employees = employeesDetails.get("employees");
  String status = employeesDetails.get("requestType").asText();
  System.out.println(status);
  if (employees.isArray()) {
   for (final JsonNode employee : employees) {
    String name = employee.get("name").asText();
    String email = employee.get("email").asText();
    JsonNode address = employee.get("address");
    JsonNode phone = address.get("phone");
    String homePhone = phone.get("home").asText();
    System.out.println("*** Employee Detail ***");
    System.out.println("Name: " + name);
    System.out.println("Email: " + email);
    System.out.println("HomePhone: " + homePhone);

   }
  }
 }

 public static void main(String[] args) throws JsonProcessingException, IOException {
  String employeeJSON = "{\n" + 
    "    \"payload\": {\n" + 
    "        \"audit\": {\n" + 
    "            \"enteredBy\": \"NIKHIL\",\n" + 
    "            \"entryType\": \"ORDER\"\n" + 
    "        },\n" + 
    "        \"employeesDetails\": {\n" + 
    "            \"employees\": [\n" + 
    "                {\n" + 
    "                    \"lineNo\": 1,\n" + 
    "                    \"name\": \"NIKHIL RANJAN\",\n" + 
    "                    \"email\": \"NIKHILRANJAN@DEVINLINE.COM\",\n" + 
    "                    \"address\": {\n" + 
    "                        \"firstLine\":\"WINDSOR APT\",\n" + 
    "                        \"SecondLine\":\"#204, BLOCK 2\",\n" + 
    "                        \"state\":\"KARTA\",\n" + 
    "                        \"postalCode\":\"50081\",\n" + 
    "                        \"phone\": {\n" + 
    "                            \"home\": \"4747521304\",\n" + 
    "                            \"work\": \"9748767304\"\n" + 
    "                        }\n" + 
    "                    },\n" + 
    "                    \"org\": \"IT\",\n" + 
    "                    \"department\": \"DPC\"\n" + 
    "                }\n" + 
    "            ],\n" + 
    "            \"requestType\": \"EMPLOYEE_DETAILS\"\n" + 
    "        }\n" + 
    "    }\n" + 
    "}";
  doJsonTreeParse(employeeJSON);
 }
}

Sample output:

EMPLOYEE_DETAILS
*** Employee Detail ***
Name: NIKHIL RANJAN
Email: NIKHILRANJAN@DEVINLINE.COM
HomePhone: 4747521304

Aug 17, 2018

Textual description of firstImageUrl

Python and MariaDB: How to access Maria Database with Python

Database table: Customer table with following columns.

database.py
:  Create database connection and close connection.
# -*- coding: utf-8 -*-
"""
Created on Tue Jul 24 09:40:55 2018

@author: n0r0082
"""

from mysql.connector import MySQLConnection, Error
import six
import os
#To sypport pyhton 2 and 3 
try:
    from configparser import ConfigParser
except:
    import ConfigParser

def read_db_config(filename='db_config.ini', section='maria_local'):
   #Read database configuration file and return a dictionary object
    # create parser and read ini configuration file 
    if six.PY2: #pyhton 2
        parser = ConfigParser.ConfigParser()
        curr_dir_path = os.path.dirname(os.path.realpath(__file__))
        config_full_path = os.path.join(curr_dir_path, filename)
        parser.read(config_full_path)
    else: #pyhton 3
        parser = ConfigParser()
        parser.read(filename)
    
    # get section, default to mysql
    db = {}
    if parser.has_section(section):
        items = parser.items(section)
        for item in items:
            db[item[0]] = item[1]
    else:
        raise Exception('{0} not found in the {1} file'.format(section, filename))
 
    return db

def get_connetion(db_config):
    try:
        #conn = MySQLdb.connect(host=migration_const.DB_HOST_NAME,user=migration_const.DB_USER_NAME,passwd=migration_const.DB_USER_PASSWORD)
        conn = MySQLConnection(**db_config)
        if conn.is_connected():
            print('Connected to MySQL database ' + db_config['host'])
        return conn
    except Error as e:
        print("Exception occured while creating database connection with host" + db_config['host'] +" and port#" + db_config['port'] )
        print(e)
        #log.exception('Error from throws():')
        raise
    finally:
        pass
        #conn.close()

def close_connetion(connection):
    try:
        connection.close()
    except Exception:
        print("Error occured while closing connection!!")

db_config.ini
: database details 
[maria_local]
host = localhost
database = DIPC
user = root
password =root
port=3306

[maria_qa]


[maria_stg]

db_operation.py : All database operation - Insert, Update, Select
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Aug 11 13:29:17 2018

@author: n0r0082
"""
import migration_const
import database 
from contextlib import closing

def get_connection(db = 'maria_local'):
    #Get database config 
    db_config = database.read_db_config(migration_const.DB_CONFIG_FILE_NAME,db)
    #Get Cconnection object 
    conn = database.get_connetion(db_config)
    return conn

def update_entity():
    phone = '041-237645' 
    email = 'rao@gmail.com'
    conn = get_connection()
    #SQl Query prep 
    sql_update_customer = "UPDATE Customer SET phone= {} where email = {}"
    email = "'{}'".format(email)
    phone = "'{}'".format(phone)
    sql_update_customer = sql_update_customer.format(phone, email)
    print(sql_update_customer)
    try:
        with closing(conn.cursor()) as cur:
           # Execute the SQL command
           cur.execute(sql_update_customer)
           conn.commit()
    except Exception as e:
            print(e)
    database.close_connetion(conn)
    
def select_entity():
    conn = get_connection()
    email = 'nikhilranjan235@gmail.com'
    #SQl Query prep 
    sql_select_employee = "select * from Customer where email = {}"
    email = "'{}'".format(email)
    sql_select_employee = sql_select_employee.format(email)
    print(sql_select_employee)
    try:
        with closing(conn.cursor()) as cur:
           # Execute the SQL command
           cur.execute(sql_select_employee)
           # Fetch all the rows in a list of lists.
           results = cur.fetchall()
           if len(results) > 0:
              row = results[0]
              name = row[1]
              phone = row[2]
              print("Name: " + str(name) + " and Phone: "+ str(phone))
    except Exception as e:
            print(e)
    database.close_connetion(conn)
    
def insert_entity():
    conn = get_connection()
    name = 'Kumkum'
    email= 'kumkum23@devinline.com'
    phone = '8769814567'
    sql_insert_customer = "INSERT INTO Customer (NAME , EMAIL, PHONE) values ({},{},{})"
    email = "'{}'".format(email)
    name = "'{}'".format(name)
    phone = "'{}'".format(phone)
    sql_insert_customer = sql_insert_customer.format(name,email,phone)
    print(sql_insert_customer)
    try:
        with closing(conn.cursor()) as cur:
           # Execute the SQL command
           cur.execute(sql_insert_customer)
           conn.commit()
    except Exception as e:
            print(e)
    database.close_connetion(conn)

def na():
    print("Invalid choice")
    
switcher = {
        1: select_entity,
        2: insert_entity,
        3: update_entity
    }

def execute_db_operation(argument):
    # Get the function from switcher dictionary
    func = switcher.get(argument, "na")
    # Execute the function
    return func()

def db_operations():
    x = 1
    while True:
        print( "Operation (1: select, 2: insert, 3: uupdate)")
        data = input("Enter a number 1-3 : ")
        execute_db_operation(data)
        x += 1
        
#starts here.....    
if __name__=="__main__":
    print ("************** Starting DB Operation ****************")
    db_operations()
    print ("************** Finished DB Operation ****************")

Sample output:
➜  MariaDBPython git:(master) ✗ python db_operation.py
************** Starting DB Operation ****************
Operation (1: select, 2: insert, 3: uupdate)
Enter a number 1-3 : 1
Connected to MySQL database localhost
select * from Customer where email = 'nikhilranjan235@gmail.com'
Name: Nikhil and Phone: nikhilranjan235@gmail.com
Operation (1: select, 2: insert, 3: uupdate)
Enter a number 1-3 : 2
Connected to MySQL database localhost
INSERT INTO Customer (NAME , EMAIL, PHONE) values ('Kumkum','kumkum23@devinline.com','8769814567')
Operation (1: select, 2: insert, 3: uupdate)
Enter a number 1-3 : 3
Connected to MySQL database localhost
UPDATE Customer SET phone= '041-237645' where email = 'rao@gmail.com'
Operation (1: select, 2: insert, 3: uupdate)
Enter a number 1-3 :

Jul 13, 2018

Textual description of firstImageUrl

Weekend learning: An ad-hoc way to add new arsenal - IOS Application development with iPhone App Bootcamp Udemy

Needless to say sometimes people are so much stressed that they want to come out of this so called "Work-Learn-zone" to "Fun-Learn-zone". Different people have different methodology of transition.
We work and learn but its inevitable to work with same enthusiasm on trite things. Yes, I get bored and believe me I have validated this with my friends - they have same traits.

Why not build something new, learn and have fun !! 

I decided to spent my weekends with something new learning, build and have fun with tech semantics. After browsing udemy I found a small and crisp tutorial for ios application development in swift programming language - The 10 Day iPhone App Bootcamp - NEW iOS 12 and Xcode 10.

https://udemy-certificate.s3.amazonaws.com/pdf/UC-KRHH9TOL.pdf
The very best part of this video tutorial is, it does not have sample code - You come, learn and do yourself. Although it is 10 days bootcamp, after spending 2 Days of exhaustive session with Nick Walter I end-up with learning of Swift programming basic and developed 4 Small IOS applications 
Building application using swift programming language

Swift programming language is simple in terms of syntax and semantics. This IOS application development tutorial explained very well required Swift programming language and fundamentals of Xcode(Integrated development environment (IDE) for macOS) - prerequisite for IOS application development.

JokeBank App:

This application explained fundamental of Swift array and objects. It contains a list view to choose joke type and on click of this we end up with detailed view of joke. Below screenshot shows UI involved.

Learning highlights :
1. Array in Swift programming and how to create array of objets to store Joke title and details
2. Navigation controller and tabular view of UI
3. How to use ViewController(or TableViewController) along the side of UI.
4. Display text in multi-lines in UI using (jokeQ.lineBreakMode = .byWordWrapping and jokeQ.numberOfLines = 0)
5. How to navigate from One UI to another using Segue(Storyboard segue)
JokeBank App Source Code

TODOList App:

This application provides UI to add task with provision to mark task important or non-important, it also allows to mark it completed and delete it from Home page of application.


Learning highlights :
1. Introduction of Core data concept and Save, fetch and delete from core data
(UIApplication.shared.delegate as? AppDelegate)?.saveContext()
2. Navigation controller, tabular view and navigation buttons.
3. Navigation from UI using multiple segues.
segue.destination as? CompleteViewController or CreateToDoViewController
4. Database entities in core data and associated API's for data interaction.
5. Redirecting from one UI to home screen using navigationController?.popViewController(animated: true)
TodoList App Source Code


Collector App:

This app allows to select images from directory & camera and display images in tabular form with title. It also allows to delete images from home screen using swipe.

Learning highlights :
1. Saving Image in binary form in Core data entity - store image locally.
2. Interaction with camera and local directory to capture image.
cameraImageCollect(_ sender: Any) and directoryImageCollect(_ sender: Any)
3. How to use array of binary data of image and saving in core data. Collectable is entity
var allCollected = [Collectable]()
4. Convert a UIImage into a Data using UIImagePNGRepresentation and UIImageJPEGRepresentation.
collectable.image = UIImageJPEGRepresentation(data, 0.8)
5. Delete image and title from tabular UI using swipe.
if editingStyle == .delete context.delete(collectable)
Collector App Source code

Bitcoin Tracker App:

This is bitcoin price app tracker shows latest price in different currency. Its UI page application with refresh button which brings updated value of bitcoin by making HTTP GET call.

Learning highlights:
1. How to execute GET HTTP request and get price updated in three different currency.
URLSession.shared.dataTask(with: url){(data, response, error)
2. How to write currency formatter using NumberFormatter()
Collector App Source code
Ahhh !! after developing these small app I really had fun with Swift & Xcode and finally became a novice IOS developer

May 4, 2018

Textual description of firstImageUrl

Solve Linear regression problem mathematically with least square method : Find slope and intercept of linear classifier

Linear regression is one of basic supervised learning which is used to predict outcome. In linear regression problem, we find best fit line using sample points with one independent variable and one dependent variable. Basic idea is to finds a linear function which predicts the dependent variable values as a function of the independent variables.
In order to best fit line which is represented by find linear function - we can use ordinary least squares method(minimize the residuals) or least absolute deviations (minimizing the sum of absolute values of residuals). Residuals means vertical distances between the points of the data set and the fitted line (wiki).
Linear best fit line(blue) for data points(Red) and green line indicates error/residues (Source:wiki

Least square method :-  Using this approach vertical distances between the data set points and the fitted line is computed such that sum of all distances for each point from best fit line is minimum. 

Dataset:- (x, y) = (2,10) (4,9) (3,6) (6,6) (8,6)  (8,3) (10,2)

Algorithm : For finding best fit line (y= . mX+ c) , we have to find value of m(slope) and c(intercept). Follow below steps to find slope and intercept.

1. Compute mean of x and y values. Here x̅  and ȳ are mean of x and y data points.


2.  Calculate slope of line(linear classifier)

3. Calculate intercept of line .
---------------------------------------------------------

Use python terminal to find mean of x and y data points.
Python 2.7.10 (default, Jul 15 2017, 17:16:57) 
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 
>>> import numpy
>>> a = [2,4,3,6,8,8,10]
>>> numpy.mean(a)
5.8571428571428568
>>> 
>>> 
>>> 
>>> b = [10,9,6,6,6,3,2]
>>> numpy.mean(b)
6.0

x̅ = 5.86
ȳ = 6.0

Find slope(m):  Pre-process sample data in tabular form below and compute slope of line.

iteration# xi yi xi - x̅ yi - ȳ (xi - x̅)(yi -ȳ)= (P) (xi - x̅)2 =(Q)
1 2 10 -3.86 4 -15.44 14.9
2 4 9 -1.86 3 -5.58 3.46
3 3 6 -2.86 0 0 8.18
4 6 6 0.14 0 0 0.02
5 8 6 2.14 0 0 4.58
6 8 3 2.14 -3 -6.42 4.58
7 10 2 4.14 -4 -16.56 17.14
                                                             
Slope of line:
 
Compute y-intercept :

Now equation of line  :


Best fit line separates data points below and this line can be used to predict outcome for other test(new) data points.