Monday, March 02, 2015

Autocrlf setting in Git

How autocrlf works:
core.autocrlf=true:    core.autocrlf=input:      core.autocrlf=false:

       repo                     repo                    repo
    /        \               /        \              /        \
crlf->lf    lf->crlf     crlf->lf       \          /            \      
 /              \        /                \      /                \
Reminder: crlf = win-style end-of-line marker, lf = unix-style.
Note that cr (mac-style) in not affected for any of three options above.
When does this warning show up (under Windows)
    - autocrlf = true if you have unix-style lf in one of your files (= RARELY),
    - autocrlf = input if you have win-style crlf in one of your files (= almost ALWAYS),
    - autocrlf = false - NEVER!
What does this warning mean
The warning "LF will be replaced by CRLF" says that you (having autocrlf=true) will lose your unix-style LF after commit-checkout cycle (it will be replaced by windows-style CRLF). Git doesn't expect you to use unix-style LF under windows.
The warning "CRLF will be replaced by LF" says that you (having autocrlf=input) will lose your windows-style CRLF after a commit-checkout cycle (it will be replaced by unix-style LF). Don't use input under windows.
Yet another way to show how autocrlf works
1) true:             x -> LF -> CRLF
2) input:            x -> LF -> LF
3) false:            x -> x -> x
where x is either CRLF (windows-style) or LF (unix-style) and arrows stand for
file to commit -> repository -> checked out file
How to fix
Default value for core.autocrlf is selected during git installation and stored in system-wide gitconfig (%ProgramFiles(x86)%\git\etc\gitconfig). Also there're (cascading in the following order):
   - "global" (per-user) gitconfig located at ~/.gitconfig, yet another
   - "global" (per-user) gitconfig at $XDG_CONFIG_HOME/git/config or $HOME/.config/git/config and
   - "local" (per-repo) gitconfig at .git/config in the working dir.
So, write git config core.autocrlf in the working dir to check the currently used value and
   - add autocrlf=false to system-wide gitconfig             # per-system solution
   - git config --global core.autocrlf false        # per-user solution
   - git config --local core.autocrlf false          # per-project solution
Moral (for Windows):
    - use core.autocrlf = true if you plan to use this project under Unix as well (and unwilling to configure your editor/IDE to use unix line endings),
    - use core.autocrlf = false if you plan to use this project under Windows only,
    - never use core.autocrlf = input unless you have a good reason to (eg if you're using unix utilities under windows or if you run into makefiles issues),
P.S. What to choose when installing git for Windows?
If you're not going to use any of your projects under Unix, don't agree with the default first option. Choose the third one (Checkout as-is, commit as-is). You won't see this message. Ever.
P.P.S. My personal preference is configuring the editor/IDE to use Unix-style endings, and setting core.autocrlf to false.

Thursday, February 12, 2015

REST service with Payara

Based on - http://www.payara.co.uk/building_restful_javaee_microservices_with_paya

Payara is embedded glassfish server (4.1 as of this writing) and the above blog post describes how to write your REST (micro) service easily and quickly with Payara embedded server.

Sunday, February 08, 2015

Book Review: A Study in Scarlet

This is the first of the 4 novels of Sherlock Holmes in which Dr Watson has returned from his service in the military in Afghanistan and is looking for a cheaper accommodation in London city when he meets his acquaintance to whom he mentions of his need of a cheaper accommodation and who happens to know of another fellow who was looking for someone to share accommodation with at 221B Baker St London ... and this is how Dr Watson gets to meet his lodge partner Sherlock Holmes... and the rest as we know is history. In this book, Sherlock Holmes was asked for help by Mr Gregson and Mr Lestrade of Scotland Yard to help solve a mysterious death of a person who was found in an uninhabited house in the city... and the dead man had no injury on him except that his face was contorted in anguish such that it deeply shocked Dr Watson who had never before met with such a ghastly sight before even compared to his grim experiences in the army. In the following days there was another murder in a hotel and the common aspect of both these cases was that the murderer wrote RACHE with his own blood.. which Sherlock Holmes explained is german for revenge. Both Gregson and Lestrade took to wrong links to solve the case which ended up in failures and at last Sherlock Holmes solves the mystery. The other part of the book tells the story of the reason why these horrific murders took place. So that is in short about this book.. now on to the Sign of four, the next adventure of Sherlock Holmes and Dr Watson.

Thursday, August 28, 2014

Elements of PaaS


PaaS (Platform as a Service) is a layer over IaaS (Infrastructure as a Service) that provides the following:

  1. Some kind of deployement interface
  2. coupled with a load balancer - HAProxy
  3. a service provisioning system 
  4. Scaling  in/out support
  5. Optional add-on services: 
    1. Backup and restore service 
    2. Patching
    3. Caching or data grid service
    4. Different DB types - relational, in-memory, no-sql 


The above is generally controlled by a service management layer that exposes all of the above operations of PaaS as REST endpoints. The implementation of the endpoints will use IaaS service endpoints to perform infrastructure related operations (like say adding/removing a VM etc).

Generally some devops toolkit like chef (solo)/puppet combined with some bash scripts are used to provision the platform (like say application server or database server) in the VM after the VM has been created.

The tenant user can either use the REST endpoints directly or through some SDK or CLI or through a web UI to request say, creation of platform instance(s).

Once the platform instance is created, user can deploy their applications on the platform through the platform specific administration tools (like tomcat admin console or a DB client).

Examples of PaaS:
Heroku (see my other posts summarizing the salient features of Heroku platform) 

Understanding Heroku


Deploy, run and manage Java application on Heroku:
  1. Application = source code for application + web or application server framework + dependencies on third party libraries + dependency description (pom.xml) that instructs a build system (maven) to download the needed dependencies from a repository in order to build the application + Procfile.
  2. Procfile - lists named commands that you may want executed.
     
web: java -jar lib/foobar.jar $PORT
queuty: java -jar lib/queue-processor.jar

Defines a 'web' process type and provides a command that needs to be executed in order to run it.
  1. Git - used as primary means for deploying applications on Heroku platform. When application is created on Heroku, a new git remote is associated typically named "heroku" with the local git repository for your application. To deploy, simply do a git push heroku master. Thus git acts as a transport mechanism to push local application to heroku platform (remote).
  2. When Heroku platfrom receives a git push, it builds the application - mvn clean install.
  3. Slug - a bundle of application source code + dependencies + JRE + binary o/p from mvn install. A slug is the application ready for execution.
  4. Dynos - Heroku executes the applications by running a command you defined in Procfile on a dyno (unix container like Docker) that has been preloaded with slug and config-vars and add-ons (together called a release).
Each dyno gets its own ephemeral filesystem - with a fresh copy of the most recent release. It can be used as temporary scratchpad, but changes to the filesystem are not reflected to other dynos.
  1. To retain state information across dyno restarts use DB or message queues. The filesystem on a dyno are temporary.
  2. User can control the number of dynos: heroku ps:scale web=3 queuty=2 for example will run 5 dynos (3 for web and 2 for queuty process types).
  3. When a new version of application is pushed to heroku platform, currently executing dynos are killed and new ones are spawned to replace them with the new release.
  4. Application config-vars are set as:
heroku config:set ENCRYPTION_KEY= my_secret_launch_codes

These are exposed to application as environment vars.
  1. Release = version controlled ledger of (slug + config-vars + add-ons)
heroku releases - lists all changes deployed with their version id (internally they might be mapping version ids to git SHA ids)
Everytime you deploy, new slug is created and a release is generated.
Its thus very easy to rollback to a previous release.
Heroku releases:rollback v102
  1. Dyno manager - manages dynos across all applications deployed on heroku platform.
  2. Add-ons - are pluggable services like email service, Database service, queuing or caching services etc.

For example, a REDISTOGO_URL will be automatically added to your application when you provision the add-on. You can write code that connects to the service through the URL, for example:
uri = URI.parse(ENV["REDISTOGO_URL"])
REDIS = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)

  1. Logplex automatically collates log entries from all the running dynos of your app, as well as other components such as the routers, providing a single source of activity.
  2. HTTP Routing:

Heroku’s HTTP routers distributes incoming requests for your application across your running web dynos.

Tuesday, August 19, 2014

Book Review: The Giver by Lois Lowry

** spoiler alert ** The Giver is a science fiction about a special community which has attained a very peaceful and strictly disciplined lifestyle for its citizens by enforcing rules and cutting them off from any and all randomness of life. Everything is controlled - population, feelings, freedom to choose their own job. It makes one think about what living in such a time will be like where people just don't feel anything wrong in killing (called releasing in the book) because they have never thought about releasing being bad - as it is what they have learned to expect by living in their community. We can compare this kind of feeling with our times and come up with a lot of aspects in our lives that we believe are bad today but were being practiced just a few years ago by some communities. Overall a thought provoking story. I am looking forward to read the other books in this series.

Thursday, July 31, 2014

HTML/CSS Basics

Styles within HTML tags:
<p style="font-family: Arial; font-size: 20px; color: blue">A truly spectacular paragraph!</p>

<ol style="background-color: yellow;">
<li style="text-align:left">The Hawthorn Football Club</li>
</ol>

<strong> -- bold
<em> -- to italicize

<table style="border-collapse:collapse;">
<thead>
<tr>
<th colspan="2">test</th>
</tr>
</thead>
<tbody>
<tr>
<td style="padding:5px;border-left:1px solid black;">1</td>
<td style="padding:5px;border-left:1px solid black;">2</td>
</tr>
</tbody>
</table>

Following div will create a 50px X 50px rectangular block of red color:
<div style="width:50px; height:50px; background-color:red"></div>

A very long <span style="color:red;">text</span>!

CSS:
<head>
<link type="text/css" rel="stylesheet" href="stylesheet.css"/>
</head>

Stylesheet.css:
/* A comment */
body {
    background-color: #C6E2FF;
}

p {
    color: green;
}

td {
    height: 50px;
    border: 1px dashed blue;
}

table {
    border:1px solid black;
}

a {
    color: #cc0000;
    text-decoration: none;
}

img {
   display: block;
    height: 100px;
    width: 300px;
    border: 1px solid #4682b4;
    margin: auto; -- puts the image in the center
}

div {
    height: 50px;
    width:120px;
    border-color:#6495ED;
    background-color:#BCD2EE;
    border-style:dashed;
    border-width:2px;
    border-radius: 5px; -- gives a rounded corner
    margin: auto; -- puts the entire block in the center (equal margin on all sides)
    text-align: center; -- center the text within the div block
}
Selectors:
Cascading selection:
To select p nested within 2 divs:
<div>
    <div>
        <p>I like tacos!</p>

div div p {
    /*CSS stuff!*/
}

The font-size unit em is a relative measure: one em is equal to the default font size on whatever screen the user is using.
<p style="font-size: 0.5em">Half an em!</p>

Universal Selector: applies to all HTML elements on the page
* {
    border: 1px dashed #3A5FCD;
}

Classes and IDs based selection:
ID: good for identifying single selector/element on a page.
#summary {
font-size: 20px;
color: #000000;
}
<p id="summary">summary</p>

Class: good for identify a class/group of elements on a page.
.list_item {
font-family: Vivaldi, cursive;
}
<p class="list_item">

Psuedo class selectors: - way of accessing state of HTML elements like mouse hover on element <a>:
a:link {
    text-decoration: none;
    color:#008B45;
}

a:hover {
    color:#00FF00;
}

a:visited {
    color:#EE9A00;
}

First child selector: applies to first child <p> element only
p:first-child {
    font-family: cursive;
}
p:nth-child(2) { -- 2nd child p selected
    font-family:Tahoma;
}

p:nth-child(3) { -- 3rd child p selected
    color: #CC0000;
}

Draw circle with div:
div {
display: inline-block;
margin-left: 5px;
height:100px;
width:100px;
border-radius:100%;
border:2px solid black;
}

Positioning:
Each element gets its own box. The outermost box of each element go all the way across the page.
display property -
• Block - element becomes a block box and takes up the whole width of page (no other element can sit with it on same line) -- this is default behavior in the absence of display property.
• Inline-block: element becomes a block box (no overlapping elements), but allows other elements on same line.
• Inline: allows overlapping and other elements on same line and does not honour dimension settings (height and width etc)
• None: hides the element

div {
height: 50px;
width: 100px;
border: 2px solid black;
border-radius: 5px;
display:inline-block;
}
Following universal selector makes all boxes shown in the image above visible.
* {
border: 1px dashed blue;
}

Margins, border and padding: - Refer box model diagram above.
• Margin - outermost box - space around element.
• Border - edge of element
• Padding - space between content and border.
• Content - content within the element like text within <p>.

Margin: auto - sets equal margin on all sides (top, left, bottom and right)
div {
height: 50px;
width: 100px;
border: 2px solid black;
border-radius: 5px;
background-color: #308014;
margin-top: 20px;
margin-right: 50px;
margin-bottom: 10px;
margin-left: 5px;
}
Same goes with padding (padding-top, etc).

We can even set margin and padding on one line as:
padding: [top] [right] [bottom] [left];

Border takes - line size in px, solid/dotted/dashed/none, color.

Float property: no overlapping elements, used to place elements.
div {
height: 300px;
width: 100px;
border: 2px solid black;
border-radius: 5px;
background-color: #308014;
float: right;
}

p {
font-family: Verdana, sans-serif;
font-size: 20px;
width: 280px;
/*Add your CSS here!*/
float: right;
}

In the above snippet, both <p> and <div> contents will be floated to right however first element in the document will occupy the rightmost position followed by the next element in the doc floated to the right on the same line of page.

Clear:
 If you tell it to clear: both, it will get out of the way of elements floating on the left and right!

#header { <!DOCTYPE html>
        height: 50px; <html>
        background-color: #F38630;        <head>
        margin-bottom: 10px;                <link type="text/css" rel="stylesheet" href="stylesheet.css"/>
}                <title>Result</title>
       </head>
.left {        <body>
        height: 300px;                <div id="header"></div>
        width: 150px;                <div class="left"></div>
        background-color: #A7DBD8;                <div class="right"></div>
        float: left;                <div id="footer"></div>
        margin-bottom: 10px;        </body>
} </html>

.right {
        height: 300px;
        width: 450px;
        background-color: #E0E4CC;
        float: right;
        margin-bottom: 10px;
}

#footer {
        height: 50px;
        background-color: #69D2E7;
           clear:both;
}

Positioning:
1. Default is "static" position - so element is position relative to entire page.
Position: static
2. Absolute - position relative to its parent element that does not have position: static. In absence of such a parent the element will position relative to <html> element.
3. Relative - position relative to where it would have landed if it had default static positioning.
4. Fixed - anchors element to the browser window. Element won't change its position even if you scroll up or down. So this element seems to be glued to the screen and when one scrolls its always visible.

z-index property:
You can think of the z-index as a measure of importance: the higher an element's z-index, the higher it will be "stacked" on the page. Giving your header a z-index of 1 while not giving any of the other elements a z-index ensures that your header will sit "on top of" your other elements and won't get stuck behind them.

#header {
    height: 50px;
    width: 100%;
    background-color: violet;
    position:fixed;
    z-index:1;
}

Saturday, July 26, 2014

Python 2 by example

Python 2.7 syntax examples:
Code samples at : https://github.com/rwatsh/python 

1.       Math:
2**3 = 8 , where, ** is exponent operator
+, -, *, / = math operators
% = modulo operator
2.       Comments:
# - single line comment

“”” my multi line
comment “””
3.       Print:
print(“%.2f”, total) -> will print total rounded to 2 decimal places.
4.       Escape char:
'There\'s a snake in my boot!'
Not required when using double quotes for string.
5.       String:
fifth_letter = "MONTY"[4]
print fifth_letter => prints Y
parrot = "Norwegian Blue"
print len(parrot)
print parrot.lower()
print parrot.upper()
pi = 3.14
print str(pi) => str() converts non-strings to string
Methods that use dot notation only work with strings.
On the other hand, len() andstr() can work on other data types.
print "Spam " + "and " + "eggs"
print "The value of pi is around " + str(3.14)
string_1 = "Camelot"
string_2 = "place"

print "Let's not go to %s. 'Tis a silly %s." % (string_1, string_2)
ð  Let's not go to Camelot. 'Tis a silly place.
String slicing:
s = "Charlie"

print s[0]
# will print "C"

print s[1:4]
# will print "har"
Getting i/p and printing o/p:
name = raw_input("What is your name?")
quest = raw_input("What is your quest?")
color = raw_input("What is your favorite color?")

print "Ah, so your name is %s, your quest is %s, " \
"and your favorite color is %s." % (name, quest, color)
6.       Datetime library:
from datetime import datetime
print datetime.now() => 2014-07-25 17:42:13.304805
print now.month => 7
print now.day => 25
print now.year => 2014
print '%s/%s/%s' % (now.month, now.day, now.year) => 7/25/2014
print '%s:%s:%s' % (now.hour, now.minute, now.second) =>17:48:28

7.       Comparison:
==, !=, <, >, <=, >=
8.       Logical: and, or, not
9.       Conditional: if, elif, else
def clinic():
    print ("You\'ve just entered the clinic!")
    print ("Do you take the door on the left or the right?")
    answer = input("Type left or right and hit 'Enter'.").lower()
    if answer == "left" or answer == "l":
        print ("This is the Verbal Abuse Room, you heap of parrot droppings!")
    elif answer == "right" or answer == "r":
        print ("Of course this is the Argument Room, I've told you that already!")
    else:
        print ("You didn't pick left or right! Try again.")
        clinic()

clinic()
10.   Functions:
See above example –
def [function_name(param1, param2)]:
                x = 2
                return x
11.   Import:
a.       Generic import: Import a module by name. Ex:
import math

print math.sqrt(25)
b.      Function import:
from [module] import [function]

from math import sqrt
print sqrt(25)
c.       Universal import:
from [module] import *

from math import *
print sqrt(25)
d.      Check everything a module has:
Import math
Print dir(math)
12.   Built-in functions: max, min, abs, type
Max(1,2,3) => 3
Min(1,2,3) => 1
Abs(-10) => 10
Print type(1) => or type(num) == int
13.   List:
zoo_animals = ["pangolin", "cassowary", "cat", "dog"]
print "The first animal at the zoo is the " + zoo_animals[0]
print len(zooanimals)
print zooanimals[0:2] => 1st and 2nd elements in the list
pets = zooanimals[2:4] or zooanimals[2:] => cat and dog => slicing the list.
Cat_index = zooanimals.index(“cat”)
Zoonimals.insert(cat_index, “cobra”) => ["pangolin", "cassowary", “cobra”, "cat", "dog"]
Zooanimals.sort() => sort the list.
zooanimals.remove(“cat”) => remove element from list
                pets[] => empty list
                print “----“.join(zooanimals) => pangolin ---- cassowary ---- cobra ---- cat ----- dog
               
                List Comprehension syntax:      

                my_list = range(51)
evens_to_50 = [i for i in range(51) if i % 2 == 0]

List Slicing:
List slicing allows us to access elements of a list in a concise manner. The syntax looks like this:

[start:end:stride]

                my_list = range(1, 11) # List of numbers 1 - 10

# print all odds from start to finish so no need to specify the start and end only need stride.
print my_list[::2]
# reverse a list
backwards = my_list[::-1]


to_21 = range(1,22)
odds = to_21[::2]
middle_third = to_21[7:14:1]
14.   For loop:
my_list = [1,9,3,8,5,7]
for number in my_list:
    print 2*number
else:
    print “Well done”
The else block only executes if the for loop executes normally (ie there is no break causing the loop to terminate mid-way).
for loop with range():
n = [3, 5, 7]
def print_list(x):
    for i in range(0, len(x)):
        print x[i]
print_list(n)

for I in range(20) => 0 to 20
for I in range(1, 10) => 1 to 9
Need index with for each loop – use enumerate() built-in function:
choices = ['pizza', 'pasta', 'salad', 'nachos']
print 'Your choices are:'
for index, item in enumerate(choices):
    print index+1, item
Iterate over 2 or more lists at once:
Zip() will create pairs of elements when passed two lists, and will stop at the end of the shorter list.
list_a = [3, 9, 17, 15, 19]
list_b = [2, 4, 8, 10, 30, 40, 50, 60, 70, 80, 90]
for a, b in zip(list_a, list_b):
    # Add your code here!
    print max(a,b)
Populate a list:
evens_to_50 = [i for i in range(51) if i % 2 == 0]
print evens_to_50

15.   Dictionary:
residents = {'Puffin' : 104, 'Sloth' : 105, 'Burmese Python' : 106}
menu = {}
menu[‘Samosa’] = 1.60
del residents[‘Puffin’] => remove item from dictionary
menu[‘Samosa’] = 1.50

for key in residents
                print residents[key]

print residents.items() => returns dictionary as list of key/value pairs.
Print residents.keys() => list of all keys
Print residents.values() => list of all values
16.   While loop:
while count < 10:
                count += 1
else:
                print “Game over!”
17.   Print:
The , character after our printstatement means that our next printstatement keeps printing on the same line.
d = {'a': 'apple', 'b': 'berry', 'c': 'cherry'}

for key in d:
    # Your code here!
    print key, " ", d[key]
18.   Lambda: Anonymous function:
Python’s support for functional programming => meaning you are allowed to pass functions around just as if they were variables or values.

lambda x: x % 3 == 0

is same as:

def by_three(x):
    return x % 3 == 0

Usage: filter uses lamba expression below to print only those elements of the list that satisfy that return true for the lambda expression (or satisfy the condition divisible by 3).

my_list = range(16)
print filter(lambda x: x % 3 == 0, my_list) => [0, 3, 6, 9, 12, 15]
19.   Bitwise:

print 5 >> 4  # Right Shift => 0
print 5 << 1  # Left Shift => 10
print 8 & 5   # Bitwise AND => 0
print 9 | 4   # Bitwise OR => 13
print 12 ^ 42 # Bitwise XOR => 38
print ~88     # Bitwise NOT => -89 (equivalent to adding 1 to the number and putting a – sign).

XOR => in-equality is true (0b1100 ^ 0b101010 => 0b100110)

In Python, you can write numbers in binary format by starting the number with 0b.

print 0b1 + 0b11 => 4

bin() – to binary (as string)
hex() – to hex
oct() – to octal

print bin(5) => 0b101
print hex(5) => 0x5
print oct(5) => 05

int(str, radix)  - any string (includes, binary/hex/octal with appropriate radix (2,16,8)) to int

int("110", 2) => 6
print int("0b100",2) => 4
print int(bin(5),2) => 5

20.   Classes:
An empty class:
class Animal(object):
    pass

pass – special python keyword for placeholder – in areas of code where python expects an expression.

class Triangle(object):
    number_of_sides = 3
    def __init__(self, angle1, angle2, angle3):
        self.angle1 = angle1
        self.angle2 = angle2
        self.angle3 = angle3

    def check_angles(self):
        if self.angle1 + self.angle2 + self.angle3 == 180:
            return True
        else:
            return False

my_triangle = Triangle(90, 30, 60)
print my_triangle.number_of_sides
print my_triangle.check_angles()

class Equilateral(Triangle):
    angle = 60
    def __init__(self):
        self.angle1 = self.angle
        self.angle2 = self.angle
        self.angle3 = self.angle

class MyTriangle(Equilateral):
    def __init__(self, beauty):
        self.beauty = beauty
        self.angle1 = 70

my_triangle = MyTriangle(True)
print my_triangle.angle1 # angle1/2/3 not inherited from base class Triangle
print my_triangle.number_of_sides # member inherited from Triangle
print my_triangle.angle # member inherited from Equilateral

1.       object is base class from which all classes inherit (same as Java).
2.       Constructors are - __init__(self…)
3.       Self needs to be the first param for all member methods and constructors.
4.       Derived class constructor needs to initialize all base members that don’t have a default value.
5.       Derived class inherits all base class members that have default values (defined outside of base class’s constructor).

21.   File I/O:
my_list = [i**2 for i in range(1,11)]
# Generates a list of squares of the numbers 1 - 10

f = open("output.txt", "w")

for item in my_list:
    f.write(str(item) + "\n")

f.close()

or

with open("text.txt", "w") as my_file:
                my_file.write("Success!")


with … as syntax is like try-with-resources in Java and it auto closes the file.