On Feeling Needed

This article in new york times by Dalai Lama was a good one to read. I collected these important points from the article.

  • A small hint comes from interesting research about how people thrive. In one shocking experiment, researchers found that senior citizens who didn’t feel useful to others were nearly three times as likely to die prematurely as those who did feel useful. This speaks to a broader human truth: We all need to be needed.
  • Being “needed” does not entail selfish pride or unhealthy attachment to the worldly esteem of others. Rather, it consists of a natural human hunger to serve our fellow men and women. As the 13th-century Buddhist sages taught, “If one lights a fire for others, it will also brighten one’s own way.”
  • Feeling superfluous is a blow to the human spirit. It leads to social isolation and emotional pain, and creates the conditions for negative emotions to take root.
  • Indeed, what unites the two of us in friendship and collaboration is not shared politics or the same religion. It is something simpler: a shared belief in compassion, in human dignity, in the intrinsic usefulness of every person to contribute positively for a better and more meaningful world.

We are going to Mars!

A very exciting thing happened this week.

Elon Musk laid out a plan for human beings like us to travel to mars and start a civilization there.

This is a multi-year plan, and there will be multiple rockets carrying humans and cargo. It seems it will take 3 months to reach to the destination and it has be done with Earth and Mars are in sync, which happens every 28 months.

Elon Musk's SpaceX team is trying to reduce the cost to a affordable level of 200K USD per ticket. If the ticket happens to be more than 5x this, it will still be worth it.

The design of the rocket, the system architecture and the entire plan was really impressive. Here is the link to the presentation that Elon Musk gave.

If you are truly interested, this whole video for 1 hour gives a good overview.

There are short teaser video as well, like this one.

Things just got real!

Movie Review: Saving Private Ryan

We got the Netflix subscription recently and one of the movies I watched after getting Netflix was "Saving Private Ryan". Surprisingly, I had not watched this movie earlier. Every time I watched the opening scene, I had simply given up and that was enough of the movie for me.

Now, that I completely watched it, this movie just my regard for "Steven Speilberg" as the director. I can think of no other movie that is as brilliant, portraying human emotions, as this one. The war is shown in its brutality. Soldiers holding on religion as the moment of truth is shown dearly.

The characters in the movie include Captain Miller (Tom Hanks) who shows extraordinary leadership and Private Jackson (Barry Pepper), a highly reliable, accurate marksman, who quotes bible, gives himself unto god, as he fights his enemies.

Everything about this movie was great.

Python Programming Patterns

I can write idiomatic code both in Python and Java. Sometimes, when working with large teams, I prefer Java over Python. I am excited by the sturdiness, standardized and elaborate solutions written in Java. It gives me ample opportunity to learn and understand the world better.

Python, on the other hand, is a simple language. The rules of usage are very simple. It seems to me that, the driving force behind python, Guido Van Rossum, always prefers simplicity over anything else.

Here are his thoughts on writing python programs that can be fast (quoted verbatim)

  • Avoid overengineering datastructures. Tuples are better than objects (try namedtuple too though). Prefer simple fields over getter/setter functions.
  • Built-in datatypes are your friends. Use more numbers, strings, tuples, lists, sets, dicts. Also check out the collections library, esp. deque.
  • Be suspicious of function/method calls; creating a stack frame is expensive.
  • Don't write Java (or C++, or Javascript, ...) in Python.
  • Are you sure it's too slow? Profile before optimizing!
  • The universal speed-up is rewriting small bits of code in C. Do this only when all else fails.

Guido van Rossum

It is best to adopt all these without shame, that is, writing simple programs, using most common data structures. The above list creates a very low barrier for writing large programs. In addition to the above list, I think, if we ever write a python program which gets deployed to production, (meaning there is money / life at stake), having it readable, and having unit-tests and coverage will give confidence in the program. Making this a habit is a hard task, but if something can be adopted from the good parts of Java world, then the discipline for these hard tasks can be cultivated in the python world too.

Review of The Emerald Route by R.K.Narayan

The Emerald Route is a travelogue written by R. K. Narayan. He details the cultural and mythological history of various cities in Karnataka. Narayan presents the mythological history alongside with the facts about the place, and gives you a glimpse of his character. He presents them as true stories that have happened in those places.

For example, Narayan shares the story that Sankara was born with a definite time of 16 years to live, and thus he studied all the scholarly works by 10, became a monk, and started preaching. When he was 16, a debate between Vyasa and Sankara took place. Vyasa was the original author of the work that Sankara was discussing, and not knowing this, Sankara was still holding on to his stance. When the debate did not end, student of Sankara called for truce. Ved Vyasa impressed by Sankara's knowledge of his own work, granted him the boon to live for another 16 years.

There was another interesting story that at Srirangapatinum, in one of the battles that Tipu Sultan lost, he had give away two of his sons, aged 9 and 11, as hostages to British on conditions of surrender. It seems that he got his sons back after paying huge money to British.

Filled with stories like this, giving the account of history and culture of the various towns in Karnataka, this book was a pleasure to read.

Python Library: tabulate

I prefer to use Restructured Text for my posts, but drawing tables in Restructured text has always been tricky for me. Getting the columns right, drawing and aligning the bar characters is a frustrating exercise for me.

The best to draw tables is to then generate it. I've tried ascii art table maker and I have not been very impressed or successful with it.

I tried a python library called tabulate [1] which can be used to print the table in restructed text format. The table could easily be constructed using python data structures like list.

Here is an example table of Graph applications, which shows the concept graphs, their vertices and edges.

graph vertex edge
communication telephone, computer fiber optic cable
circuit gate, register, processor wire
mechanical joint rod, beam, spring
financial stock, currency transactions
transportation street intersection, airport highway, airway route
internet class C network connection
game board position legal move
social relationship person, actor friendship, movie cast
neural network neuron synapse
protein network protein protein-protein interaction
molecule atom bond

This table was generated using this python program.

python/tabulate_example.py (Source)

from tabulate import tabulate

table_header = ["graph", "vertex", "edge"]

items = [
    ["communication", "telephone, computer", "fiber optic cable"],
    ["circuit", "gate, register, processor", "wire"],
    ["mechanical", "joint", "rod, beam, spring"],
    ["financial", "stock, currency", "transactions"],
    ["transportation", "street intersection, airport", "highway, airway route"],
    ["internet", "class C network", "connection"],
    ["game", "board position", "legal move"],
    ["social relationship", "person, actor", "friendship, movie cast"],
    ["neural network", "neuron", "synapse"],
    ["protein network", "protein", "protein-protein interaction"],
    ["molecule", "atom", "bond"]]

print(tabulate(items, headers=table_header, tablefmt="rst"))

I landed upon tabulate when another python library by name PTable [2] did not provide a restructured text specific formatting. If you just prefer ASCII tables, then that's a convenient library as well.

[1] https://bitbucket.org/astanin/python-tabulate
[2] https://ptable.readthedocs.io/en/latest/tutorial.html

Not praising intelligence

There are many supporting studies on this, and I came another one which explicitly stated that

Praise for Intelligence Can Undermine Children's Motivation and Performance

The study suggests that parents should praise their children for the efforts they put in their tasks instead of praising the accomplishments or their intelligence for accomplishing the task.

Excerpt from the paper.

Praise for ability is commonly considered to have beneficial effects on motivation. Contrary to this popular belief, six studies demonstrated that praise for intelligence had more negative consequences for students' achievement motivation than praise for effort. Fifth graders praised for intelligence were found to care more about performance goals relative to learning goals than children praised for effort. After failure, they also displayed less task persistence, less task enjoyment, more lowability attributions, and worse task performance than children praised for effort. Finally, children praised for intelligence described it as a fixed trait more than children praised for hard work, who believed it to be subject to improvement. These findings have important implications for how achievement is best encouraged, as well as for more theoretical issues, such as the potential cost of performance goals and the socialization of contingent self-worth.

Abstract Factory - Design Pattern Explanation

Design patterns like "Decorator", "Singleton" are common and be easily recognized. The "Builder" pattern is also recognizable whenever you use Java's StringBuilder class.

Some patterns which are commonly used by frameworks are not easily recognizable unless you are a framework author. Recently, I spent time trying to recognize and understand Abstract Factory design pattern.

In this post, we will look at "Abstract Factory" and explain it with an example. I consulted multiple resources, and ultimately to gain confidence, I had to rely upon The Gang of Four [1] design patterns book.

This post is intended as refresher for a reader who has read the Gang of Four chapter on Abstract Factory. This post presents an example which the reader can relate to in the modern world.

Abstract Factory is a Factory for Factories. To understand this, you will first have to understand the Factory Design pattern, which encapsulates creation of objects. Factory pattern is recognized when instead of using new SomeClass() we call SomeClass.createObject() static method. The advantage is SomeClass is independent of your code, it could be supplied as a dependency from someone else and you simply use the factory. The person controlling the factory can modify the object creation process.

For example, SomeClass.createObject() in version1, can be return new SomeClass(arg1) and in version2, it can change to return new SomeClass(arg1, arg2) with you as the caller, invoking the object creation entirely as SomeClass.createObject() unaffected by the change made by the creator of SomeClass.

Factory pattern is easy to understand. The next step comes in dealing with Abstract Factory.

Intent

Abstract Factory provides an interface for creating families of related or dependent objects without specifying the concrete classes.

Canonical Design

https://dl.dropbox.com/s/3o1opat3zd7c569/Screenshot%202016-07-11%2023.04.02.png

Factory is a class that defers the instantiation of the object to the subclass.Factory encapsulates the decision-making that figures out which specific subclass to instantiate. There are three different kinds of Factory patterns observable with respect to object instantiation.

Simple Factory.
The client directly uses a static method of a subclass, to instantiate an object.
Factory Pattern
The client uses a Factory class to create an object. A Factory, is a class that defers the instantiation of an object to the subclasses.Factory method creates only one product
Abstract Factory
Abstract Factory is a constructional design pattern that is used to create a family of related products.

Abstract Factory is applicable when the

  • System should be configured with one of multiple families of Products.
  • The family of related product objects is designed to to used together and we need to enforce this constraint.

Design Problem

In this problem, we are trying to design a "Operating System Installer" for Unix family of Operating Systems. We know there are two popular variants of Unix, there popular operating system with Linux kernel and related application stack, and there is BSD systems.

Each Operating System will consists of components like

  1. Bootloader
  2. Kernel
  3. Shell
  4. DisplayManager
  5. WindowManager
  6. Applications

The installer will have to abstract those components and help the client create an Unix operating system choice.

Correspondence with Canonical Design

https://dl.dropbox.com/s/wmnawrv6h3rxx4u/Screenshot%202016-07-12%2002.15.48.png

Let's look at each of these in some detail.

Product Interfaces

Starting with products, these are:

  • Bootloader
  • Kernel
  • Shell
  • DisplayManager
  • WindowManager
  • BaseApplications

We will have Interfaces for the products.

java/abstractfactory/IBootLoader.java (Source)

public interface IBootLoader {
    /**
     * Boot up the System Image.
     */
    void bootUp();
}

java/abstractfactory/IKernel.java (Source)

public interface IKernel {

    /**
     * Load the kernel on top of the system image.
     */
    void loadKernel();
}

java/abstractfactory/IShell.java (Source)

public interface IShell {
    void loadShell();
}

java/abstractfactory/IDisplayManager.java (Source)

public interface IDisplayManager {
    void installDisplayManager();
}

java/abstractfactory/IWindowManager.java (Source)

public interface IWindowManager {
    void installWindowManager();
}

java/abstractfactory/IBaseApplications.java (Source)

public interface IBaseApplications {
    void installApplications();
}

Concrete Products

Each of these can create many difference concrete products. For the different concrete products like

  • Bootloader
    • BSDBootLoader
    • LinuxBootLoader
  • Kernel
    • BSDKernel
    • Linux
  • Shell
    • BASH
    • CShell
  • DisplayManager
    • X11
    • WayLand
  • WindowManager
    • Gnome
    • KDE
  • BaseApplications
    • SystemVUnix
    • GNUApplications
    • ProprietaryApps

Let's denote these concrete products in code that can be instantiated.

java/abstractfactory/BSDBootLoader.java (Source)

public class BSDBootLoader implements IBootLoader{
    /**
     * Boot up the System Image.
     */
    @Override
    public void bootUp() {
        System.out.println("Booting: " + this.getClass().getSimpleName());

    }
}

java/abstractfactory/BSDKernel.java (Source)

public class BSDKernel implements IKernel {
    /**
     * Load the kernel on top of the system image.
     */
    @Override
    public void loadKernel() {
        System.out.println("Loading: " + this.getClass().getSimpleName());

    }
}

java/abstractfactory/Bash.java (Source)

public class Bash implements IShell {

    @Override
    public void loadShell() {
        System.out.println("Loading: " + this.getClass().getSimpleName());

    }
}

java/abstractfactory/CShell.java (Source)

public class CShell implements IShell {
    @Override
    public void loadShell() {
        System.out.println("Loading: " + this.getClass().getSimpleName());

    }
}

java/abstractfactory/GNUApplications.java (Source)

public class GNUApplications implements IBaseApplications{
    @Override
    public void installApplications() {
        System.out.println("Installing: " + this.getClass().getSimpleName());
    }
}

java/abstractfactory/Gnome.java (Source)

public class Gnome implements IWindowManager {
    @Override
    public void installWindowManager() {
        System.out.println("Installing: " + this.getClass().getSimpleName());

    }
}

java/abstractfactory/KDE.java (Source)

public class KDE implements IWindowManager {
    @Override
    public void installWindowManager() {
        System.out.println("Installing: " + this.getClass().getSimpleName());
    }
}

java/abstractfactory/Linux.java (Source)

public class Linux implements IKernel{
    /**
     * Load the kernel on top of the system image.
     */
    @Override
    public void loadKernel() {
        System.out.println("Loading: " + this.getClass().getSimpleName());

    }
}

java/abstractfactory/LinuxBootLoader.java (Source)

public class LinuxBootLoader implements IBootLoader {
    @Override
    public void bootUp() {
        System.out.println("Booting: " + this.getClass().getSimpleName());
    }
}

java/abstractfactory/ProprietaryApps.java (Source)

public class ProprietaryApps implements IBaseApplications{
    @Override
    public void installApplications() {
        System.out.println("Installing: " + this.getClass().getSimpleName());
    }
}

java/abstractfactory/SystemVUnix.java (Source)

public class SystemVUnix implements IBaseApplications{
    @Override
    public void installApplications() {
        System.out.println("Installing: " + this.getClass().getSimpleName());
    }
}

java/abstractfactory/WayLand.java (Source)

public class WayLand implements IDisplayManager{

    @Override
    public void installDisplayManager() {
        System.out.println("Installing: " + this.getClass().getSimpleName());
    }

}

java/abstractfactory/X11.java (Source)

public class X11 implements IDisplayManager{
    @Override
    public void installDisplayManager() {
        System.out.println("Installing: " + this.getClass().getSimpleName());
    }
}

Factories

The products are created by Factories

  • BSDFactory
  • LinuxFactory
  • UbuntuFactory

java/abstractfactory/BSDFactory.java (Source)

public class BSDFactory implements IUnixFactory {
    @Override
    public IBootLoader installBootLoader() {
        return new BSDBootLoader();
    }

    @Override
    public IKernel installKernel() {
        return new BSDKernel();
    }

    @Override
    public IShell installShell() {
        return new CShell();
    }

    @Override
    public IDisplayManager installDisplayManager() {
        return new X11();
    }

    @Override
    public IWindowManager installWindowManager() {
        return new KDE();
    }

    @Override
    public IBaseApplications installApps() {
        return new SystemVUnix();
    }
}

java/abstractfactory/LinuxFactory.java (Source)

public class LinuxFactory implements IUnixFactory {
    @Override
    public IBootLoader installBootLoader() {
        return new LinuxBootLoader();
    }

    @Override
    public IKernel installKernel() {
        return new Linux();
    }

    @Override
    public IShell installShell() {
        return new Bash();
    }

    @Override
    public IDisplayManager installDisplayManager() {
        return new X11();
    }

    @Override
    public IWindowManager installWindowManager() {
        return new Gnome();
    }

    @Override
    public IBaseApplications installApps() {
        return new GNUApplications();
    }
}

java/abstractfactory/UbuntuFactory.java (Source)

public class UbuntuFactory extends LinuxFactory {
    @Override
    public IDisplayManager installDisplayManager() {
        return new X11();
    }

    @Override
    public IBaseApplications installApps() {
        return new ProprietaryApps();
    }
}

Abstract Factory

The factories will implement an abstraction provided by the Abstract Factory

java/abstractfactory/IUnixFactory.java (Source)

public interface IUnixFactory {
    IBootLoader installBootLoader();
    IKernel installKernel();
    IShell installShell();
    IDisplayManager  installDisplayManager();
    IWindowManager installWindowManager();
    IBaseApplications installApps();
}

Client

The design is best understood from the view of the client which uses the Abstract Factory to the create the products.

java/abstractfactory/OperatingSystem.java (Source)

public class OperatingSystem {
    IUnixFactory unixFactory;

    public OperatingSystem(IUnixFactory unixFactory) {
        this.unixFactory = unixFactory;
    }

    /**
     * installerClient uses only the interfaces declared by AbstractFactory (IUnixFactory) and AbstractProduct
     * (IBootLoader, IKernel, IShell, IDisplayManager, IWindowManager, IBaseApplications) classes.
     */
    public void installerClient() {


        IBootLoader bootLoader = unixFactory.installBootLoader();
        IKernel kernel = unixFactory.installKernel();
        IShell shell = unixFactory.installShell();
        IDisplayManager displayManager = unixFactory.installDisplayManager();
        IWindowManager windowManager = unixFactory.installWindowManager();
        IBaseApplications applications = unixFactory.installApps();

        bootLoader.bootUp();
        kernel.loadKernel();
        shell.loadShell();
        displayManager.installDisplayManager();
        windowManager.installWindowManager();
        applications.installApplications();

    }

    /**
     * client will not know the
     * products the type of bootloader, kernel, shell, display, window manager or applications.
     * That is encapsulated in factory used by the client.
     *
     */
    private static void factoryClient(IUnixFactory factory) {
        OperatingSystem operatingSystem = new OperatingSystem(factory);
        operatingSystem.installerClient();
    }

    public static void main(String[] args) {
        IUnixFactory factory;

        factory = new LinuxFactory();
        factoryClient(factory);

        factory = new BSDFactory();
        factoryClient(factory);

        factory = new UbuntuFactory();
        factoryClient(factory);
    }

}

The execution looks like this.

Booting: LinuxBootLoader
Loading: Linux
Loading: Bash
Installing: X11
Installing: Gnome
Installing: GNUApplications

Booting: BSDBootLoader
Loading: BSDKernel
Loading: CShell
Installing: X11
Installing: KDE
Installing: SystemVUnix

Booting: LinuxBootLoader
Loading: Linux
Loading: Bash
Installing: X11
Installing: Gnome
Installing: ProprietaryApps

Tabulated Correspondence

Mapping of the code with various elements in the design helps us to appreciate this pattern.

https://dl.dropbox.com/s/ahu9pj89qtt7pos/Screenshot%202016-07-27%2008.57.29.png

Hope this was useful. If you have any comments on this article, please add your thoughts in the comments section of this article.

Thank you for reading!

[1] https://en.wikipedia.org/wiki/Design_Patterns