Friday, January 31, 2020

scala : Factory design / Create objects using string

Factory design / Create objects using string



object testing extends App{
val jmap:Map[String,dime] = Map("x" -> new curr)
println(jmap("x").dimensionName)
}

class dime{
val dimensionName: Option[String] = None
}

class curr extends dime{
override val dimensionName: Option[String] = Some("curr")
val y=3432
}
class currex extends dime{
override val dimensionName: Option[String] = Some("currex")
}

Thursday, January 30, 2020

Scala : Provide column names as Seq to select statement in Scala

Scala : Provide column names as Seq to select statement in Scala 



Sample Code :

val df1: DataFrame = Seq((1,1,"kjj"), (2,2,"987987")).toDF("col1", "col2","col3")
val columnName = Seq("col1", "col3")
val DFFiltered = df1.select(columnName.map(name => col(name)): _*)
DFFiltered.show()

Full Code :
project sbt version : 2.1
------------------------build.sbt------------------------------

name := "scala_sample"
version := "0.1"
scalaVersion := "2.11.12"
libraryDependencies ++= Seq(
  "org.apache.spark" %% "spark-core" % "2.1.0",
  "org.apache.spark" %% "spark-sql" % "2.1.0")
------------------------testing.scala------------------------------

import org.apache.spark.sql.{DataFrame,SparkSession}
import org.apache.spark.sql.functions._

object testing extends App{
  val spark = SparkSession.builder().master("local").appName("spark-shell").getOrCreate()
  import spark.implicits._
  val df1: DataFrame = Seq((1,1,"kjj"), (2,2,"987987")).toDF("col1", "col2","col3")
  val seq:Seq[String]=Seq("col1","col3")
  val obj:DF=new DF()
  obj.selectCols(df1,seq).show()
}


class DF{
  def selectCols(dataFrame: DataFrame,seq: Seq[String]): DataFrame ={
    val DFFiltered = dataFrame.select(seq.map(name => col(name)): _*)
    DFFiltered
  }
}

Result:
+----+------+
|col1|  col3|
+----+------+
|   1|   kjj|
|   2|987987|
+----+------+

Anti Pattern / Code Smells


Anti-patterns/Design smell

"Design smells are certain structures in the design that indicate violation of fundamental design principles and negatively impact design quality”. 

Tips :
  • Functions should be as small as possible.
  • Avoid if else / Case statements instead used Class and inheritance (Open Closed principle).
  • The number arguments sent to function should be not more than 2 . If exceeds either create a class and send as an object or send as a map.
  • Never send boolean into a function ( as usually it contain if else to validate the same).


There are many design smells classified based on violating design principles:

Abstraction smells

  1. Missing Abstraction: When clumps of data or encoded strings are used instead of creating a class or an interface.
  2. Imperative Abstraction:  When an operation is turned into a class.
  3. Incomplete Abstraction:When an abstraction does not support complementary or interrelated methods completely.
  4. Multifaceted Abstraction: When an abstraction has more than one responsibility assigned to it.
  5. Unnecessary Abstraction: When an abstraction that is actually not needed .
  6. Unutilized Abstraction: When an abstraction is left unused (either not directly used or not reachable).
  7. Duplicate Abstraction: When two or more abstractions have identical names or identical implementation or both.

Encapsulation smells

  1. Deficient Encapsulation: When the declared accessibility of one or more members of an abstraction is more permissive than actually required.
  2. Leaky Encapsulation: When an abstraction “exposes” or “leaks” implementation details through its public interface.
  3. Missing Encapsulation: When implementation variations are not encapsulated within an abstraction or hierarchy.
  4. Unexploited Encapsulation: When client code uses explicit type checks (using chained if-else or switch statements that check for the type of the object) instead of exploiting the variation in types already encapsulated within a hierarchy.

Modularisation smells

  1. Broken Modularisation: When data and/or methods that ideally should have been localized into a single abstraction are separated and spread across multiple abstractions.
  2. Insufficient Modularisation: When an abstraction exists that has not been completely decomposed, and a further decomposition could reduce its size, implementation complexity, or both.
  3. Cyclically-Dependent Modularisation: When two or more abstractions depend on each other directly or indirectly (creating a tight coupling between the abstractions).
  4. Hub-Like Modularisation: When an abstraction has dependencies (both incoming and outgoing) with a large number of other abstractions.

Hierarchy smells

  1. Missing Hierarchy: When a code segment uses conditional logic (typically in conjunction with “tagged types”) to explicitly manage variation in behavior where a hierarchy could have been created and used to encapsulate those variations.
  2. Unnecessary Hierarchy: When the whole inheritance hierarchy is unnecessary, indicating that inheritance has been applied needlessly for the particular design context.
  3. Unfactored Hierarchy: This smell arises when there is unnecessary duplication among types in a hierarchy.
  4. Wide Hierarchy: When an inheritance hierarchy is “too” wide indicating that intermediate types may be missing.
  5. Speculative Hierarchy: When one or more types in a hierarchy are provided speculatively (i.e., based on imagined needs rather than real requirements).
  6. Deep Hierarchy: When an inheritance hierarchy is “excessively” deep.
  7. Rebellious Hierarchy: When a subtype rejects the methods provided by its supertype(s).
  8. Broken Hierarchy:When a supertype and its subtype conceptually do not share an “IS- A” relationship resulting in broken substitutability.
  9. Multipath Hierarchy: When a subtype inherits both directly as well as indirectly from a supertype leading to unnecessary inheritance paths in the hierarchy.
  10. Cyclic Hierarchy: When a supertype in a hierarchy depends on any of its subtypes.

Other Code Smells:

  1. Duplicated Code and Logic Code Smell:refractor part into a separate method 
  2. Long Methods and Classes Code Smell:The perfect method should be between 4 to 20 lines.
  3. Duplicated Methods in the Same or Different Class Code Smell:when you have two methods that do the same functionality. 
  4. Class Divergent Change Code Smell: Not following single responsibility principle
  5. Shotgun Surgery Code Smell: For example, you need to create a new user rule such as ‘Supper-Admin’ then you found yourself must edit some methods in Profile, Products and Employees classes. In that case, consider grouping these methods in one single class so this new class will have a single reason to change.
  6. Feature Envy Code Smell:A method in your class that extensively makes use of another class , then refractor that method into a different class.
  7. Data Clumps Code Smell:A list of functions accepts same group of large parameters.In that case its better to create a class which accepts parameters and send an object instead of parameter group.
  8. Switch Statement Code Smell: Case statements should not have lot of statements instead delegate call other methods/functions or return factory method
  9. Temporary Fields Code Smell:When you have a class instance variables that have been used only sometimes. 
  10. Message Chains Code Smell:When you have a class that uses another class which uses another class and so on.
  11. (in scala : Make your code cleaner by shortening the chain to Employee->Config
  12. Middle Man Code Smell:Sometimes you find many methods in one class do nothing but delegating to another method in a different class.t would be better to get rid of it.
  13. Alternative Classes with Different Interfaces Code Smell:two different classes are created which do the same thing 
  14. Incomplete Library Class Code Smell:Sometimes when you need to retrieve all documents of a particular user? Remember that it is horrible if you tried to edit third-party classes on your own. In this case, you need to extend the functionality of the Document class without editing the original class. Well, the decorator design pattern can be helpful here
  15. Comments Code Smell : Comments is a code smell
  16. Speculative Generality Code Smell : 
    1. * Don’t over plan your code.
    2. * Don’t try to cover a case that likely has 1% chance to happen in the future.
    3. * Sacrifice some speed in order to make your algorithm simpler, especially if you don’t need a real-time result from your application.
    4. * Optimise for speed when your application is actually slow not when you only have 100 users.

Ref :Refactoring for Software Design Smells: Managing Technical Debt  by Girish Suryanarayana

Sunday, January 19, 2020

Scala vs Python

Scala vs Python

----------------VAR / CONST ------------
val a:String="123" // Scala
var a:String="123" // Scala

a="123"# Python
--------------COMMENTS------------------
// this is a commented line in Scala
/* and this is a multiline comment,
...just choose! */

#Hi # Python
""" # Python
multi
"""
or
''' # Python
multi Line
'''
---------------PRINT-----------------
print("whatever you want") #  Python
println("whatever you want") // Scala

a="hi"
print(f"{a}")

val a="hi"
println(s"$a")

---------------LIST/ARRAY-----------------
Declaration :
var myArray = Array(2, 5, "apple", 78) // Scala
my_list = [2, 5, 'apple', 78] # Python

Indexing
myArray(1) // Scala 
my_list[1] # Python 

Slicing
myArray.slice(0,3) // Scala
my_list[0:3] # Python 


myArray.last    // Scala last element
myArray.max     // Scala maximum element
myArray.min     // Scala minimum element

my_list[-1]    # Python last element
max(my_list)   # Python maximum element
min(my_list)   # Python minimum element

sum(my_list) # Python- sum

myArray.sum    // scala sum
myArray.product  // multiplying elements in array

my_list.append('last words') # Python
myArray :+= "last words" 

---------------LOOP-----------------
for i in my_list:print(i)# Python
for (i <- myArray){ println(i)}// Scala
val list = List(1, 2, 3)
list.foreach(x => println(x)) 


--------------MAP------------------
list(map(lambda x:x*3,my_list)) # map in Python
myArray.map(i => i*3) // map in Scala

---------------FILTER-----------------
list(filter(lambda x:x%3==0,my_list)# filter in Python
myArray.filter(i => i%3 == 0) // filter in Scala

---------------REDUCE-----------------
from functools import reduce # Python
print(reduce(lambdax,y:x+y,(1,2,3))) #Python

val lst = List(1, 2, 3) // Scala
list.reduce((x, y) => x + y) // => 6 // Scala

---------------LIST COMPREHENSION-----------------
[i*3 for i in my_list if i%2 == 0] # list comprehension
myArray.filter(i => i%2 == 0).map(i => i*3) // Scala

---------------SORT-----------------------
sortBy(f: T => K): Seq[T]

---------------DICTIONARY-----------------
Declaration:
my_dict = {'first_name': 'Emma','age': 18}# Python
var myMap = (("firstName", "Emma"),("age", 18)) // Scala

New key:
my_dict['country_of_origin'] = 'Italy' # Python
myMap += ("countryOfOrigin" -> "Italy") // Scala

Access:
my_dict['first_name'] # Python
myMap("firstName") // Scala

Loop:
for key, value in my_dict.items(): # Python
    print(key)
    print(value)

for ((key, value) <- myMap){ // Scala
    println(key)
    println(value)
}
--------------TUPLE------------------
my_tup = (1, 2, 3) # Python
my_tup[0] # Python


myTup = (1, 2, 3) // Scala
myTup._1 // Scala
// the indexing is way different than arrays!

-------------SET-------------------
my_set = {1, 3, 5, 1} # Python
mySet = Set(1, 3, 5, 1) // Scala

-------------FUNCTION-------------------
def chop_string(input_string): # Python
    return input_string[0:5]

def chopString(inputString: String): String = { // Scala
    inputString.slice(0, 5)
}
--------------NAMED TUPLE/CASE CLASS------
from collections import namedtuple
n=namedtuple("n",["a","b"])
n2=n(10,20)
print(n2.a)

case class n(a:String,b:Int)
val n2: n = n("name", 1)
println(n2.a)
----------------SORTED 1---------------------
List(10, 5, 8, 1, 7).sortWith(_ < _) // Scala asc
sorted([10, 5, 8, 1, 7] , reverse =false) # Python

----------------SORTED 2---------------------
val list = List(('b',30),('c',10),('a',20))// Scala
println(list.sortBy(_._2))

l=[[1,2],[2,1]] # Python
sorted(l,key=lambdax:x[1])

----------------SORTED 3---------------------
List("banana", "pear", "apple", "orange").sortWith(_.length < _.length) // Scala
sorted(["banana", "pear", "apple", "orange"],key=lambda x:len(x)) # Python

output:(pear, apple, banana, orange)

-----------------Switch / Case ----------------
val result =1
result match {
case -1 =>{println("-1")}
case 0 => { println("asd")}
case x:Int if(x>0) =>
{ println(x)}
}

------------------OPTION = Some/getorelse/match---------------------
To eliminate NullPointerExceptions.

val age:Option[Integer] = None //Some(28)
val x=age.getOrElse(throw new Exception("error"))

Friday, December 27, 2019

Python : SOLID Principles


SOLID principles:


  • S Single Responsibility - Should have single reason to change and should be related to its primary objective.
  • O Open Closed - add features by extension, not by modification
  • L Liskov Substitution
  • I Interface Segregation
  • D Dependecny Inversion

AntiPattern:
    God Object : Reverse of Single Responsibility ie., 1 class doing everything

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Single Responsibility : A class should have only one job
Example :
class User:
def __init__(self, name: str): self.name = name
def get_name(self) :pass
def save(self, user: User): pass

Problem: User class is taking responsibility of Saving as well .
Solution :
class User:
def __init__(self, name: str): self.name = name
def get_name(self): pass

class UserDB:
def save(self, user: User):pass
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Open Closed : Classes should be open for extension, not for modification.
Example :
class bottle:
def __init__(self, season, price):
self.season = season
self.price = price

def give_discount(self):
if self.season == 'normal': return self.price
if self.season == 'festival': return self.price * 0.1

Problem : Let’s imagine you have a store, and you give a discount of 20% to your favorite customers using this class:
When you decide to offer double the 20% discount to VIP customers. You may modify the class

Solution:
class bottle:
def __init__(self, season, price):
self.season = season
self.price = price
def give_discount(self):return self.price

class Festival_bottle_price(bottle):
def give_discount(self): return self.price * 0.1
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Liskov Substitution Principle:  A sub-class can assume the place of its super-class without errors.
Example :
class Bird():
def fly(self):pass

class Duck(Bird):
def fly(self):pass

class penguin(Bird):
def fly(self):pass

Problem : This breaks LSP.As penguin cannot fly hence this causes error
Solution :
class Bird():pass
class FlyingBirds(Bird) :
def fly(self):pass

class Duck ( FlyingBirds):pass
class Ostrich ( Bird): pass
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Interface Segregation:Make interfaces that are client specific Clients should not be forced to depend upon interfaces that they do not use
Example :
class Machine:
def print(self):raise NotImplementedError()
def call(self):raise NotImplementedError()

class Printer(Machine):
def print(self, document): pass

Problem : Printer object throws exception when Printer().call() is performed as it should not have been exposed in the first place.
Solution:
class Machine:
def perform_operation(self):raise NotImplementedError()

class Printer(Machine):
def perform_operation(self, document): pass
class call_phone(Machine):
def perform_operation(self, document): pass
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Dependency Inversion  :A: High-level modules should not depend on low-level level modules. It should depend upon its abstraction.
Example :
class Http:
def __init__(self, xml_http_service: XMLHttpService):self.xml_http_service = xml_http_service

def get(self, url: str, options: dict):self.xml_http_service.request(url, 'GET')

def post(self, url, options: dict):self.xml_http_service.request(url, 'POST')

class XMLHttpService(XMLHttpRequestService):pass

Problem:Http =high-level component , XMLHttpService= low-level component.
The Http class should care less the type of Http service you are using

Solution:
class Connection:
def request(self, url: str, options: dict):
raise NotImplementedError

class Http:
def __init__(self, http_connection: Connection):
self.http_connection = http_connection

def get(self, url: str, options: dict):self.http_connection.request(url, 'GET')

def post(self, url, options: dict):self.http_connection.request(url, 'POST')

class NodeHttpService(Connection):def request(self, url: str, options:dict):pass

class MockHttpService(Connection):def request(self, url: str, options:dict):pass






Python : Interface Segregation

Python : Interface Segregation


class Printer:
    @abstractmethod    def print(self, document): pass
class Scanner:
    @abstractmethod    def scan(self, document): pass


class MyPrinter(Printer):
    def print(self, document):
        print(document)

class Photocopier(Printer, Scanner):
    def print(self, document):
        print(document)

    def scan(self, document):
        pass  # something meaningful

Python :open Closed principle



"""OCP - Open Closed PrincipleOpen for expansion but closed for modification 
Goal : Instead of modifing existing class , your are encouraged to create a new class to solve the problem
Summary : To select object from a list of objects based on the properties
Description :Assume you have a box containing objects .You job is to select an object based on the properties like size and color . Here number of objects , size and color can be of any number .You have to printTrue for object which satisfies all the properties mentioned using Open Closed Principle.
Solution:1. you can select an object by size or color .Eg: small2. You can select object by both size and color . Eg: small and red
Steps:1. Start by creating classes which selects an object based on 1 property2. If u have 2 property create 2 classes3. For combination create last class which takes above 2 class objects as input  
"""
from enum import Enum
class COLOR(Enum):
    black="BLACK"    white="WHITE"
class SIZE(Enum):
    big="BIG"    small="small"
class Product():
    def __init__(self,color,size):
        self.color=color
        self.size=size

class match():
    def test(self,obj1,obj2):
        pass
class color_match(match):
    def __init__(self,color):
        self.color=color

    def test(self,target):
        return self.color==target.color

class size_match(match):
    def __init__(self,size):
        self.size=size

    def test(self,target):
        return self.size==target.size

class And_operation():
    def __init__(self,match1,match2):
        self.match1=match1
        self.match2=match2

    def test(self,target):
        return self.match1.test(target) and self.match2.test(target)

pen=Product(COLOR.black.value, SIZE.small.value)
paper=Product(COLOR.white.value,SIZE.small.value)
book=Product(COLOR.white.value,SIZE.big.value)

Products=[pen,paper,book]
Color_match=color_match(COLOR.white.value)
Size_match=size_match(SIZE.small.value)

combination=And_operation(Color_match,Size_match)
for product in Products:
    print(combination.test(product))
Output :
False
True
False

Enum vs Named tuple in Python

Enum vs Named tuple

  1. enums are a way of aliasing values, 
  2. namedtuple is a way of encapsulating data by name. 
  3. The two are not really interchangeable, 
 Example :

from collections import namedtuple
from enum import Enum

class HairColor(Enum):
    blonde = 1
    brown = 2
    black = 3
    red = 4

Person = namedtuple('Person', ['name','age','hair_color'])
bert = Person('Bert', 5, HairColor.black)

 you can use enums as values in  namedtuple.


Python Setter and getter :

Python Setter and Getter :

Without Setter and getter , in the below example when the users first name or last name is changed individually his full name is not getting changed : 

Method 1 : Without Decorator

class  a():
   _value=0
    def __init__(self,value):
         self._value = value
    
    def get_value(self):
        return self._value
     
    def set_value(self, x):
        self._value = x


---------------------------------------------------------------------------------
Method 2  : With Decorator 
class a():
    _value=0
    def __init__(self,value):
        self._value = value

    @property
    def value(self):
        return self._value

    @value.setter       # should match the method name of @property
    def value(self, a):
        self._value = a


A=a("hi")
A.value=10
print(A.value)




Thursday, December 26, 2019

Python Refactor

Python Refactor :


Rules:

    General :
  1.         Follow PEP8 Standard
  2.         DRY - Dont Repeat Yourself ie., not more than 2 times
  3.         1 Function / method should perform only 1 thing
  4.         Use helper functions (small functions)
  5.         Simplicity (logic , naming etc )
    Functions/Method:
  1.         big functions are bad
  2.         group related methods / variables in a file
  3.         split or merge methods / functions using refactor feature in IDE
    Classes/Objects:
  1.         Hide/Expose logic using middeleman classes
  2.         Separate general and specific code in a class
  3.         objects should not talk to strangers
  4.         Use parent-child class whenever method/variables is repeated across different classes
    Others:
  1.         breaking 1 big expression in conditional statements into simple statments
  2.         Use named tuple to mini classes for data segregation
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Tools:

  1.     Linters (Quality Checker):             Pylint , PEP8 and McCabe
  2.     Formatters (Formatter):                 Autopep8
  3.     Style Checkers(recommendations):    pycodestyle , pydocstyle Eg: pycodestyle file.py
  4.     Pycharm(Refactor):                    In Editor > Rt ck >refactor

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Anti Patterns :    

    1. Too much code in a function/method
    2. Implementation of OOP / Not correct application of OOP /Too much OOP
    3. Duplicate Code
    4. Avoiding intertwine code
    5. Rename variables ie., do not use v1 ,v2 etc.,
    6. Avoid useless /non-operational code
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Design Patterns in OOP:

    1. If subclasses need not be modifiedby others , than instantion of child object inside parent class and make parent method static.
     2. Instead of having checks to returm certain value , you can create a null class which returns null /empty/relavent value and instantiate that instead.Instead of actual classs.
    3.Replace conditional with subclasses
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Law of Demeter

 the Law of Demeter for functions requires that a method m of an object O may only invoke the methods of the following kinds of objects:

    O itself
        public class ClassOne {
            public void method1() {method2();}
            public void method2() {}
        }


    m's parameters
        public class ClassOne {
            public void method1(ClassTwo classTwo) {
                classTwo.method2();
            }
        }
        class ClassTwo {
            public void method2() {   }
        }


    Any objects created/instantiated within m
        public class ClassOne {       
            public void method1() {
                ClassTwo classTwo = new ClassTwo();
                classTwo.method2();
            }
        }
        class ClassTwo {       
            public void method2() {  }
        }


    O's direct component objects
        public class ClassOne {
            private ClassTwo classTwo;   
            public void method1() {
                classTwo = new ClassTwo();
                classTwo.method2();
            }
        }
        class ClassTwo {
            public void method2() { }
        }

    A global variable, accessible by O, in the scope of m
        public class ClassOne {
            public void method1() {
                classTwo.STATIC_INSTANCE.method2();
            }
        }
        class ClassTwo {
            public static final ClassTwo STATIC_INSTANCE = ...;
            public void method2() { }
        }

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  1. OOP is mainly used to organize your business logic while  
  2. AOP helps to organize your non-functional things like Auditing, Logging, Transaction Management , Security etc. This way you can decouple your business logic with non-fictional logic that makes code cleaner.







Monday, December 23, 2019

Installing lubuntu OS

Installing lubuntu


Pre-Req:
  • 4GB pen Drive
  • Linux OS 
Steps :
  1. Download lubuntu iso- https://lubuntu.net/
  2. Insert USB 
  3. if you are using Linux  than use - Gparted to format the device.
    1. If you dont than run - sudo apt-get install gparted # to install
  4. $df -hT #to show the filesystem and size
  5. notedown the device name eg:/dev/sdb1
  6. unmount the device $sudo umount /dev/sdb1
  7.  sudo dd bs=4M if=/path/lubuntu.iso of=/dev/sdb1 status=progress oflag=sync
  8. Start > System Tools >Software updater 
  9. Restart after update
  10.  Open Terminal 
  11. $sudo apt install python3-pip


Tuesday, October 1, 2019

GIT : Commands and Operation

 Commands and Operation



Remote Master-------------------------------------Remote branch
|                                                                         |
|                                                                         |
|                                                                         |
|                                                                         |
Local Master ---------------------------------------Local branch


Usual Operations :

  1. Usually project will be made available in Git
  2. User clones the repository into local (once you clone master you get all the branches in the repository)
  3. creates a story branch locally if not available / if available checkout that branch
  4. Works on development work  
  5. commits the Changes to local branch
  6. Pushes the changes to remote branch
  7. Then the code pushed code is merged to master in remote.

Scenario 1: Developer wants to use the new code his colleague merged recently to master.
  1. Saves current changes in his current branch
  2. Developer switches to local master
  3. Perform PULL request to update his local master with latest changes.
  4. checkout the story branch
  5. Merges the updated the master to local branch
Scenario 2: Developer completed his coding while merging his code to remote master , he faces merge conflicts.
  1. checkout master
  2. pull all the changes from remote master to local master
  3. checkout story branch
  4. perform merge 
  5. Solve all the conflicts 
    1. Git shows "AUTO CONFLICT merge xxxx"
    2. Files turn red (Eg :intelliJ)
    3. rt ck > Git > resolve conflicts
    4. click Merge button
    5. Window with 3 screens open : middle screen - Current code , left and right screen shows the code in local staging and code in master
    6. Make required selection
    7. Save
  6. Commit changes
  7. Push to story branch
  8. Raise PR (merge to master) 
Sample Code :
git checkout master
git pull
git checkout my_branch
git merge master
(now use above conflict resolution to solve the issue )

Clone-Remote Master into local:
Usually any user will either clone the remote repository into his computer or create a local master repository.
git clone git_ssh_link #clone from git
git init #new repository without cloning

Check status: 
git branch

Result:
*master

Create / Use already existing branch 
once user has cloned / create a local repository
git checkout - b branch_new #Create a new branch in local and switch
git  branch branch_new #Just create a new branch in local

Message display / status
git branch #display all branches
git branch -vv #display all branches with their upstream branch
git status #display status of project wrt git
git log

Change branch 
git checkout branch_new #switch branch

Commit changes to staging whenever developer has reached a checkpoint
git add file #add file to local staging
git commit -m "description" #commit added file in staging

Push changes to story branch in remote (cloud)
git add file #add file to local staging
git commit -m "description" #commit added file in staging
git push -u origin branch_new #Create new story branch in remote and put changes there

Update the current branch if new changes are available in remote

git pull origin branch_name #1st time only
git pull #get latest updates from remote branch whichever is made upstream else same branch which we are currenlty

Remove the file from local git
git rm -rf file_name #delete file from local git

Reset
git checkout file #reset changes of the current file
git reset #save and reset changes of all the files to last commit
git reset --hard #reset to last commit without commiting

Delete branch
git branch -D branch-name

To Create a upstream
git branch -u masterbranchname # from where u want to pull latest code


Example to create a new project locally and push it to remote :
  • git init
  • git add README.md
  • git commit -m "first commit"
  • git remote add origin git@github.xxx.com:username/scala_protobuf.git
  • git push -u origin master

…or push an existing repository from the command line

  • git remote add origin git@github.xxx.com:username/scala_protobuf.git
  • git push -u origin master

Git Remove File from Face of the earth (all history included)
git filter-branch --index-filter "git rm -rf --cached --ignore-unmatch path_to_file" HEAD
git push --all

Git delete remote branch
git push origin --delete branch_name


Wednesday, September 18, 2019

Linux / Mac : Get latest of chromeDriver

Linux / Mac : Get latest of chromeDriver



LINUX
currdir=$(pwd)/misc
latest_version=$(wget https://chromedriver.storage.googleapis.com/LATEST_RELEASE -O -) && wget https://chromedriver.storage.googleapis.com/${latest_version}/chromedriver_linux64.zip -P $currdir
unzip -q $(pwd)/misc/chromedriver_mac64.zip && /bin/rm $(pwd)/misc/chromedriver_linux64.zip


MAC
#mac make sure "wget" is installed
#brew install wget
currdir=$(pwd)/misc
latest_version=$(wget https://chromedriver.storage.googleapis.com/LATEST_RELEASE -O -) && wget https://chromedriver.storage.googleapis.com/${latest_version}/chromedriver_mac64.zip -P $currdir
unzip -q $(pwd)/misc/chromedriver_mac64.zip && /bin/rm $(pwd)/misc/chromedriver_mac64.zip

Tuesday, September 17, 2019

scala : Perform basic test using MOCK


Scala : Perform basic test using MOCK and scalatest


Note :
  1. If you already have scala installed in your system do not add in your scalaVersion := "2.13.0" build file.
  2. Always add code in the test folder to use mock and scalatest.

build.sbt (I already have scala in my PC hence did not add scalaversion)

name := "sbt_test" version := "0.1"
//scalaVersion := "2.13.1"

libraryDependencies += "org.scalactic" %% "scalactic" % "3.0.0" libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.4" % "test" libraryDependencies += "org.mockito" % "mockito-all" % "1.8.4" libraryDependencies += "org.scalamock" %% "scalamock" % "4.3.0" % "test" libraryDependencies += "org.testng" % "testng" % "6.10" 

 Under Project > src > test > scala > test1 > test_class.scala
package test1
import org.scalatest.{BeforeAndAfter, FunSpec, FunSuite}
import org.scalamock.scalatest.MockFactory

object test_class extends App with MockFactory{
 val x=mock[Point]
}

class Point(xc: Int, yc: Int) {
  var x: Int = xc
  var y: Int = yc

  def start(x:Int,y:Int): Unit ={
    println("start")
  }

  def move(dx: Int, dy: Int) {
    println("move")
  }

  def end(dx: Int) {
    println("end")
  }
}

Bazel : Debugging / JVM debugging using JDB for Scala - Bazel


Debugging / JVM debugging using JDB


Follow steps to perform debugging in Bazel / Any JVMs :

Steps :
1.cd path
2. Open new Terminal Window
3. bazel run build_name --java_debug
4. Open another Terminal Window
5. jdb -attach localhost:5005 -sourcepath ~/dev/remote/src/main/java/

Note :Breakpoint can only be set in Dev Code not on scalatest .

Eg:
#stop in Dev code
stop in dev_package.class_name.method

#stop at Dev Code
stop at dev_package.class_name:130

#Clear BreakPoint
clear dev_package.class_name:line_number
clear dev_package.class_name.method

Commands :
run
cont
step over
next
locals
print resultdf.show()


Ref : http://pages.cs.wisc.edu/~horwitz/jdb/jdb.html

Friday, September 13, 2019

Git : Setting up a clone project


Git Pre- Req

Computer Authentication :


Pre- Req:

Windows:

                         i.         Open https://git-scm.com/download/win
                       ii.         Install (Next > next >…)
                      iii.         Right click on select git bash option

Mac :

·       Open Terminal

Steps :


1.     Enter ssh-keygen -t rsa in your git bash prompt / terminal (type “yes” when asked and press enter key otherwise)
2.     Note down location of the file created or usually file gets created in below locations
3.     Open id_rsa.pub file
a.     For Windows :   C:\User_name\.ssh\id_rsa.pub or  C:\xxx_USER \.ssh\id_rsa.pub
b.     For Mac :          /Users/User_name/.ssh/id_rsa.pub

4.     Copy entire Contents
Eg :
ssh-rsa sadfasfldfllflamflamsflWWQEW;as
--------
--------
---------------------------------@in.xxx.com
5.    Create a new git hub account
6.     Goto Settings
7.     Click on SSH and GPG keys
8.     Click on New SSH Keys
9.     Paste the contents inside
10.  Click on Add button
11.  Create a new folder in your system with name git_test (any location)
12.  Navigate inside the folder
13.   
a.     For windows : Open git bash by right click
b.     For mac : use terminal to navigate inside the new folder using “cd command”

14.  In your terminal / git bash run command (copy link below from the git project online)
15.  Run command “cd” into the cloned folder