Friday, 22 March 2019

Tensor Flow from novice to expert - Part1

https://github.com/tensorflow/nmt

# -*- coding: utf-8 -*-
"""
Created on Tue Dec 26 21:21:01 2017

@author: krishgu
"""

import tensorflow as tf
node1 = tf.constant(3.0,tf.float32)
node2 = tf.constant(4.0)
# Printing it
print("Printing node1 node2 - %s" % node1,node2)

# Performing the operations in graph using session
sess=tf.Session()
print("Printing node1 node2 values - %s" % sess.run([node1,node2]))


#sample computation with constant value
a = tf.constant(5)
b = tf.constant(2)
c = tf.constant(3)

d=tf.multiply(a,b)
e=tf.add(c,b)

f=tf.subtract(d,e)

sess=tf.Session()
outs=sess.run(f)
sess.close()

print("outs = {}".format(outs))


#performing it with dynamic value
a= tf.placeholder(tf.float32)
b= tf.placeholder(tf.float32)

adder_node = a+b
sess= tf.Session()
print(sess.run(adder_node,{a:[1,2,3,4,5],b:[1,2,3,4,5]}))


#Perfroming it with variable values
w = tf.Variable([.3],tf.float32)
b = tf.Variable([-.3],tf.float32)
x = tf.placeholder(tf.float32)

linear_model=w*x+b;
init = tf.global_variables_initializer()
print(w)
print(b)
sess = tf.Session()
sess.run(init)
print(sess.run(w))
print(sess.run(b))
print(sess.run(linear_model,{x:[1,2,3,4]}))

linkedin

package com.linkedin.messaging;

User contacts and direct messaging are obtained through the features “r_network” and “w_messages,” respectively, both of which will be retired with this change. With this retirement, any direct sharing or contact related features that utilize LinkedIn contacts will no longer function after May 12th, 2015.


import java.util.Scanner;
import com.github.scribejava.core.builder.ServiceBuilder;
import com.github.scribejava.apis.LinkedInApi;
import com.github.scribejava.core.model.OAuth1AccessToken;
import com.github.scribejava.core.model.OAuth1RequestToken;
import com.github.scribejava.core.model.OAuthRequest;
import com.github.scribejava.core.model.Response;
import com.github.scribejava.core.model.Verb;
import com.github.scribejava.core.oauth.OAuth10aService;
import java.io.IOException;
import java.util.concurrent.ExecutionException;

public class LinkedInTest2 {
private static final String PROTECTED_RESOURCE_URL = "https://api.linkedin.com/v1/people/~";
private static String API_KEY = "81tqxy39b";

private static String API_SECRET = "bQg1l1hQzcKtL4";
private LinkedInTest2() {
}

public static void main(String... args) throws IOException, InterruptedException, ExecutionException {
final OAuth10aService service = new ServiceBuilder(API_KEY).apiSecret(API_SECRET).callback("https://www.google.co.in")
.build(LinkedInApi.instance());
final Scanner in = new Scanner(System.in);

System.out.println("=== LinkedIn's OAuth Workflow ===");
System.out.println();

// Obtain the Request Token
System.out.println("Fetching the Request Token...");
final OAuth1RequestToken requestToken = service.getRequestToken();
System.out.println("Got the Request Token!");
System.out.println();

System.out.println("Now go and authorize ScribeJava here:");
System.out.println(service.getAuthorizationUrl(requestToken));
System.out.println("And paste the verifier here");
System.out.print(">>");
final String oauthVerifier = in.nextLine();
System.out.println();

// Trade the Request Token and Verfier for the Access Token
System.out.println("Trading the Request Token for an Access Token...");
final OAuth1AccessToken accessToken = service.getAccessToken(requestToken, oauthVerifier);
System.out.println("Got the Access Token!");
System.out.println("(The raw response looks like this: " + accessToken.getRawResponse() + "')");
System.out.println();

// Now let's go and ask for a protected resource!
System.out.println("Now we're going to access a protected resource...");
final OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL);
service.signRequest(accessToken, request);
final Response response = service.execute(request);
System.out.println("Got it! Lets see what we found...");
System.out.println();
System.out.println(response.getBody());

System.out.println();
System.out.println("Thats it man! Go and build something awesome with ScribeJava! :)");
}
}

JSPDF with Angular (Acro forms are not included)

JSPDF :- If you want to print a pdf from web page

1. Run below commands to get the code base.
npm install jspdf --save

typings install dt~jspdf --global --save

npm install @types/jspdf --save

2. Create html and wire to Angular component :- Invoke printAdapter from there

3. Create a class in typescript : - Copy paste below code

const jsPDF = require('jspdf'); // or import * as jsPDF from 'jspdf' or import jsPDF from 'jspdf'
printValues(img) {
const doc = new jsPDF();
doc.addImage(img, 'PNG', 2, 5, 60, 30);
doc.text(70, 20, 'Web');
doc.setTextColor(255, 0, 0);
doc.text(140, 20, 'Confidential');
const reportTitle = 'This is a confidential report';
const splitTitle = doc.splitTextToSize(reportTitle, 180);
doc.text(2, 44, splitTitle);
doc.setTextColor(0, 0, 0);
doc.setLineWidth(0.5);
doc.text(60, 64, 'list section').setFillColor(0, 0, 0);
doc.text(2, 69, 'name :');
doc.text(50, 69, 'India');
doc.line(50, 69.5, 68, 69.5);
doc.text(2, 79, 'Code :');
doc.text(50, 79, 'INH');

doc.setDrawColor(0);
doc.setFillColor(211, 211, 211);
doc.rect(1, 99, 200, 10, 'F');


doc.setFontSize(12);
doc.setFontType('bold');
doc.text('ComboBox:', 10, 105);

doc.rect(20, 120, 5, 5); // empty square
doc.line(20, 120, 18, 118);
doc.text(20, 120, '✓');

doc.rect(40, 120, 10, 10, 'F'); // filled square

doc.setDrawColor(255, 0, 0);
doc.rect(60, 120, 10, 10); // empty red square

doc.setDrawColor(255, 0, 0);
doc.rect(80, 120, 10, 10, 'FD'); // filled square with red borders

doc.setDrawColor(0);
doc.setFillColor(255, 0, 0);
doc.rect(100, 120, 10, 10, 'F'); // filled red square

doc.setDrawColor(0);
doc.setFillColor(255, 0, 0);
doc.rect(120, 120, 5, 5, 'FD'); // filled red square with black borders

doc.setDrawColor(0);
doc.setFillColor(255, 255, 255);
doc.roundedRect(140, 120, 5, 5, 3, 3, 'FD'); // Black square with rounded corners

doc.save('FirstSample.pdf');
}

printAdapter() {
this.getImageFromUrl('http://localhost:4200/assets/images/logo.png'); // Path to image
}

getImageFromUrl(url) {
const current = this;
const img = new Image();
img.onload = function () {
current.printValues(img);
};
img.src = url;
}

Custom single threaded java server

 package com.diffengine.csv; import java.io.*; import java.net.*; import java.util.Date; public class Server { public static void main(Str...