Kung Fu “Pandas”


I have planned to write an article per week on Data Engineering from the perspective of a beginner – the first one was Python for Frontend Engineers. Do not expect a proven pathway to become a Data Engineer someday because I do not have any strategy at the moment. I am just following my gut feeling to become proficient sooner than later. So consider these blog posts as my personal notes which may or may not be helpful to others.

So, Pandas is a data processing tool which helps in data analysis – meaning it provides various functions/methods to manipulate large datasets efficiently.

I am still learning pandas and will continue to explore its new features. The Pandas documentation is pretty self-explanatory so I will just give a glimpse of its powers in this article just like the trailer.

This is how you can import pandas and start using it right away.

Importing Pandas

import pandas as pd

pd.* # where * denotes all the supported methods

Pandas supports two data structures at the moment.

Series

The Series data structure represents a one-dimensional array with labels i.e. Python Dictionary. However, the data in the dictionary can be of any primitive types supported by Python.

Creating Series

sons_of_pandu = {
  'son1': 'Yudhishthira',
  'son2': 'Bhima',
  'son3': 'Arjuna',
  'son4': 'Nakula',
  'son5': 'Sahadeva'
}
pandavas_series = pd.Series(sons_of_pandu)
print(pandavas_series)
# Prints following in Jupyter Notebook
# son1   Yudhishthira
# son2   Bhima
# son3   Arjuna
# son4   Nakula
# son5   Sahadeva
# dtype: object

Changing Indices of Series

Sometimes we prefer to change the indexing for brevity. So here we can change the index of the series to Pandavas’ progenitors.

pandavas_series.index = ["Yama", "Vayu", "Indra", "Ashwini Kumara Nasatya", "Ashwini Kumara Darsa"] # Prints following in Jupyter Notebook
# Yama                   Yudhishthira
# Vayu                   Bhima
# Indra                  Arjuna
# Ashwini Kumara Nasatya Nakula
# Ashwini Kumara Darsa   Sahadeva
# dtype: object

Slicing Series

Slicing is really handy when glancing at a large dataset. We can also slice the series for an exploratory view as follows.

pandavas_series[0:2] # Prints first and second rows excluding the third
pandavas_series[1:]  # Prints all rows except the first one
pandavas_series[-2:] # Prints the last two rows only

Appending Series

It is very common to deal with different data sets in Pandas and the append method is just a compliment you can not ignore.

kauravas = ["Duryodhan", "Dushasana", "Vikarna", "Yuyutsu", "Jalsandh", "Sam", "Sudushil", "Bheembal", "Subahu", "Sahishnu", "Yekkundi", "Durdhar", "Durmukh", "Bindoo", "Krup", "Chitra", "Durmad", "Dushchar", "Sattva", "Chitraksha", "Urnanabhi", "Chitrabahoo", "Sulochan", "Sushabh", "Chitravarma", "Asasen", "Mahabahu", "Samdukkha", "Mochan", "Sumami", "Vibasu", "Vikar", "Chitrasharasan", "Pramah", "Somvar", "Man", "Satyasandh", "Vivas", "Upchitra", "Chitrakuntal", "Bheembahu", "Sund", "Valaki", "Upyoddha", "Balavardha", "Durvighna", "Bheemkarmi", "Upanand", "Anasindhu", "Somkirti", "Kudpad", "Ashtabahu", "Ghor", "Roudrakarma", "Veerbahoo", "Kananaa", "Kudasi", "Deerghbahu", "Adityaketoo", "Pratham", "Prayaami", "Veeryanad", "Deerghtaal", "Vikatbahoo", "Drudhrath", "Durmashan", "Ugrashrava", "Ugra", "Amay", "Kudbheree", "Bheemrathee", "Avataap", "Nandak", "Upanandak", "Chalsandhi", "Broohak", "Suvaat", "Nagdit", "Vind", "Anuvind", "Arajeev", "Budhkshetra", "Droodhhasta", "Ugraheet", "Kavachee", "Kathkoond", "Aniket", "Kundi", "Durodhar", "Shathasta", "Shubhkarma", "Saprapta", "Dupranit", "Bahudhami", "Yuyutsoo", "Dhanurdhar", "Senanee", "Veer", "Pramathee", "Droodhsandhee", "Dushala"]
kauravas_series = pd.Series(kauravas)
pandavas_series.append(kauravas_series) # Prints following in Jupyter Notebook
# Yama                   Yudhishthira
# Vayu                   Bhima
# Indra                  Arjuna
# Ashwini Kumara Nasatya Nakula
# Ashwini Kumara Darsa   Sahadeva
# 0                      Duryodhan
# 1                      Dushasana
.
.
.
# Length: 106, dtype: object

Dropping from Series

Pass the index to drop any row from the series.

pandavas_series.drop('Yama') # Prints following in Jupyter Notebook
# Vayu                   Bhima
# Indra                  Arjuna
# Ashwini Kumara Nasatya Nakula
# Ashwini Kumara Darsa   Sahadeva
# 0                      Duryodhan
# 1                      Dushasana
.
.
.
# Length: 105, dtype: object

Dataframes

The Dataframe data structure represents a two-dimensional list with labels i.e. Python List.

Creating Dataframe

sons_of_pandu = [{
  'name': 'Yudhishthira',
  'progenitor': "Yama"
}, {
  'name': 'Bhima',
  'progenitor': "Vayu"
}, {
  'name': 'Arjuna',
  'progenitor': "Indra"
}, {
  'name': 'Nakula',
  'progenitor': "Ashwini Kumara Nasatya"
}, {
  'name': 'Sahadeva',
  'progenitor': "Ashwini Kumara Darsa"
}]
df_pandavas = pd.DataFrame(sons_of_pandu)

Head’ing DataFrame

df_pandavas.head()  # returns first 5 rows
df_pandavas.head(3) # returns first 3 rows

Tail’ing DataFrame

df_pandavas.tail()  # returns last 5 rows
df_pandavas.tail(3) # returns last 3 rows

Sorting DataFrame

df_pandavas.sort_values(by="name")

Slicing DataFrame

df_pandavas[0:2] # Prints first and second rows excluding the third
df_pandavas[1:]  # Prints all rows except the first one
df_pandavas[-2:] # Prints the last two rows only
df_pandavas[["name"]] # Prints all rows with "name" column only

Copying DataFrame

df_pandavas_in_alternate_dimension = df_pandavas.copy()

Wrap up

That’s it. There are more to Pandas than mere slicing/merging/copying/sorting. You can easily read/write CSV/XL files in Pandas like never before. Head over to Pandas Documentation for more information.

If you found this article useful in anyway, feel free to donate me and receive my dilettante painting as a token of appreciation for your donation.

Python for Frontend Engineers


Py thee on.

— William Shakespeare (meaning Python code turns you on) 😅

I tried to teach Python to myself for a week and now I’m able to read and understand Python code comfortably. This blog summarises my thought process while learning Python and comparing its syntax with Javascript at every step. I’ll keep updating it as and when I encounter new Pythonic stuff.

First and foremost, wear a gauntlet and snap a finger for semicolons, curly braces, and camelCase names to bite the dust.

console.log vs print

JavascriptPython
console.log(“Hello World!”);print(“Hello World!”)
console.log(“Hello ” + “World!”);print(“Hello ” + “World!”)
var greet = “Hello”;
var who = “World!”;
console.log(`${greet} ${who}`);
greet = “Hello”
who = “World!”
print(“{0} {1}”.format(greet, who))

Primitive Types

JavascriptPython
var name = “John Doe”;name = “John Doe”
var age = 100;age = 100
var isMale = true;is_male = True

Utility Methods

JavascriptPython
“amit”.toUppercase(); “amit”.upper() # “amit”.upper().isupper()
“amit”.length;len(“amit”)
“amit”.indexOf(“a”);“amit”.index(“a”) // throws error if not found 😦
String(100);str(100)
parseInt(100.300, 10);int(100.300)
parseFloat(100.300, 10);float(100.300)
Math.abs(-100);abs(-100)
Math.pow(3, 2);pow(3, 2)
Math.round(3.2);round(3.2)
Math.floor(3.2);floor(3.2)
Math.ceil(3.2);ceil(3.2)
Math.sqrt(25);sqrt(25)
NA (use Lodash)max(4, 2)
NA (use Lodash)min(4, 2)

User Input

JavascriptPython
var ans = prompt(“What’s up?”);ans = input(“What’s up?”)

Arrays vs Lists

JavascriptPython
var arrStr = [“a”, “b”, “c”];lst_str = [“a”, “b”, “c”]
arrStr[0]lst_str[0]
arrStr[-1] // returns undefinedlst_str[-1] # returns “c”
NAlst_str[1:] # returns [“b”, “c”]
NAlst_str[1:2] # returns [“b”] ignores 3rd
NAlst_str[:2] # returns [“a”, “b”] ignores 3rd
arrStr[1] = “d”;lst_str[1] = “d”
arrStr.push(“d”); lst_str.append(“d”)
arrStr.splice(1, 0, “f”);lst_str.insert(1, “f”)
arrStr.indexOf(“b”);lst_str.index(“b”)
NAlst_str.count(“d”) # returns 2 occurrences
arrStr = arrStr.sort();lst_str.sort()
arrStr.reverse();lst_str.reverse()
var arrNum = [1, 2, 3];lst_num = [1, 2, 3]
arrStr = arrStr.concat(arrNum);lst_str.extend(lst_num)
var arrStr2 = arrStr.slice();lst_str2 = lst_str.copy()

Tuple in Typescript vs Tuple in Python

JavascriptPython
var point = [“x”, “y”];point = (“x”, “y”)
point[1];point[1]
var arrTuple = [[“x”, “y”], [“w”, “z”]];list_tuple = [(“x”, “y”), (“w”, “z”)]

function vs def

JavascriptPython
function greetMe(name){
    console.log(
       "Hi! " + name
    );
}
def greet_me(name):
    print("Hi! " + name)
 
 
 


function greetMe(name){
    return "Hi! " + name;
}
def greet_me(name):
    return "Hi! " + name

 

If Statements

JavascriptPython
if ((1&&1)||(2 === 2)) {
  console.log("124");
} else if (1 !== 2) {
  console.log("421");
} else {
  console.log("Lakhan!");
}
if (1 and 1) or (2 == 2):
   print("124")
elif (1 != 2):
   print("421")
else:
  print("Lakhan!")
 

Objects vs Dictionaries

JavascriptPython
var objRomans = {
  "five": "V",
  "ten":"X"
}
dict_romans = {
  "five": "V",
  "ten":"X"
}
// Returns undefined
// if key not exists
objRomans["five"]
# throws an error
# if key not exists
dict_romans["five"]

Loops

JavascriptPython
var i = 0;
var crossedLimit = false;
while (i !== 10) {
  i += 1;
  crossedLimit = i==10;
  if (!crossedLimit) {
    console.log(i);
  }
}
i = 0
crossed_limit=False
while i != 10:
  i += 1
  crossed_limit = i == 10
  if not(crossed_limit):
    print(i)
 
 
var name = "Amit";
var l = name.length;
for (var i = 0;i<l;i++) {
  console.log(
    name.charAt(i)
  );
}
name = "Amit"
 
for char in name:
  print(char)
 
 
 
var names = ["A", "M"];
for (let name in names) {
  console.log(
    names[name]
  );
}
names = ["A", "M"]
for name in names:
  print(name)
 
 

Comments

JavascriptPython
// single line comment
# single line comment
/*
  multi-line comment
  can be very long
*/
"""
  multi-line comment
  can be very long
"""

Imports

JavascriptPython
// imports everything
// from cookies.js
import "./cookies";

// imports `a`
// from cookies.js
import a from "./cookies";
# imports everything
# from cookies.py
import cookies

# imports `a`
# from cookies.py
from cookies import a

Try Catch vs Try Except

JavascriptPython
try {
  let obj = JSON.parse("")
} catch(e) {
  console.log(e.message);
}
import json
try:
  dict = json.loads("")
except Exception as e:
  print(str(e))

Classes

JavascriptPython
class Bike {
  isBikeToo() {
    return true;
  }
}

class Car
extends Bike {
  constructor(
    tire
  ) {
    super();
    this.tire=tire;
  }

  whoAmI() {
    if(this.tire==3){
      return "Auto";
    } else {
      return "Bike";
    }
  }
}

var c=new Car(3);
console.log(
  c.tire,
  c.whoAmI(),
  c.isBikeToo()
);
class Bike:
  def is_bike_too(self):
    return True
 
 

class Car(Bike):
   
  def __init__(self, tire):
     
    
    
    self.tire = tire


  def who_am_i(self):
    if self.tire == 3:
      return "Auto"
    else:
      return "Bike"
 
 
 
 
c = Car(3)
print("{0} {1} {2}".format(
  str(c.tire),
  c.who_am_i(),
  str(c.is_bike_too())
))
If you found this article useful in anyway, feel free to donate me and receive my dilettante painting as a token of appreciation for your donation.

Frontend Frontier – sci-fi short story


This is my 2nd science-fiction short story (the 1st one is here). It is a tribute to the Frontend Engineers and how their life and job would change in the future due to the advent of ML/AI.

Special thanks to Aamna, Roshan, and Abhishek for grammatical/punctuation improvements and the overall constructive feedback.

Please star or comment to provide your valuable feedback, whatever it may be. I’m immune to anything hurls at me 😅.

IN A VACANT BUT CHAOTIC CABIN, a soul, deep in reverie entered. The door plate had a caption that read, “Yesterday’s gadgets were in front of you, Today’s gadgets are in your hands, and Tomorrow’s gadgets will be right within you”. That was the most influential gift his grandpa had given him at the age when he knew the only use of a tongue in his mouth was to tease others. Now, he was in his late twenties and worked part-time as an A.I specialist for many years. Dare not to gaze in awe since everybody was an A.I. specialist these days, mostly due to high demand and high wages in return compared to other jobs or sarcastically no jobs. He recalled the “There is an APP for that!” story often shared by his grandpa to justify the current situation. Because of the rampant use of Machine Learning tools and techniques, making something smarter was a piece of cake now. Off-course, you’ve to dig deeper to improve the existing Machine Learning models or invent your own for an unprecedented result.
Dave was an autodidact who inhabited his working space. The room was furnished with the aesthetically minimalistic design which gave the impression of everything fairy. One side of the room, the door facing wall was painted pinkish blue, only to warp it nicely or rather gel with other drabbed walls. Silvery photo frame, containing meaningless photos in baker’s dozen, was planted on one of the drabbed walls in a shape of Kalpataru tree. It was connected to the Line (was called Internet before) as pictures that were framed-in changed randomly or perhaps as per the mood and emotions emanated by the people around. It turned out that even the walls changed colors and patterns by injunction.
There was no source of open vents, but still, it was quite frigid in there for anybody to retreat on chaise longue—cemented far away at the corner to rest for eternity.
Another wall, facing east had big nestled sliding windows for sunlight and thereby for warmth to slip through. As usual, the Sun had shone on the sliding windows from outside like a stage lighting. Looking down with half-shut eyes through the glass confirmed that the cabin was a part of 100-story construction erected from quite above the ground. Might be lodged on top of some flat structure below but could not be seen from up above. Even the surroundings were occupied with similar but sufficiently spaced out constructions.
Adjacent to the sliding windows, the stacked circular knobs controlled the artificial light—sunlight to be precise supposing the sun was not around—emerging from the glass to populate the room since no working hours were pre-defined.

Most of the things were wall mounted but the only exception was Dave’s workstation. Long gone were days when people in the past profoundly designated themselves as software engineers. In those days, their work and identity extended beyond mere programming software or so-called coding: analyzing project requirements, brainstorming with a team, creating prototypes, beta testing with handful of early adopters, fixing bugs, churning out new features, and many more such activities had made engineering the nail-biter job in a good way. But things had changed drastically and with unexpected swiftness since then.
Now they were called experts.
When people stopped doing traditional searches on various search engines back then, the obvious progression for many of those search engines was to become Digital Personal Assistants. Many could not fathom the idea and some died due to competition and so to speak complexity in giving accurate answers. Today, they collaborate or ironically fraternize themselves with Intelligent Digital Personal Assistant (IDPA) for any software related work as if humans and machines had switched roles. Even the schism between frontend and backend had become illusory.
Nowadays these experts had been trusting IDPAs more than their instincts when it came to problem-solving. By the same token, this probably might have made the software engineer term obsolete in this era.

When Dave entered the room, IDPA wished him welcome, “Good Morning, Dave!” and reminded in a soothing female voice, “I’m stoked to work alongside you on a new project today.”, followed by a giggle. Then it auto played his favorite song when Dave said nothing in return, deadpan. Dave was mulling over a problem he had been carrying in his head since last night. Yeah, that was one of the things he was allowed to do until A.I. figured out to do of its own; speculating the exponential growth in technology, it will happen soon.
While the song reached its peak, Dave slingshotted himself off to a fridge, fetched various flavors of staple meal as much as he could clasp, and hurriedly trotted near his desk before he unintentionally dribbled everything on the matted floor. In the meantime, his workstation lit up automatically when it sensed him near. IDPA had recognized Dave, booted his monitor instantly or it just reappeared magically as if someone tossed it in the realm of the 3D from the unknown 4D world, while he was busy tearing off one of the staple meals he just lugged.
After some time when his mouth was full enough to chew some of it, he transferred the bowl in one hand and threw the remaining meal bags on the other side of the desk. Then buried himself in the ergonomic chair, chewing vigorously raised one hand with the palm facing the drabbed wall, like a rocket flies off the surface, only to stop the music.
He turned his face squarely towards the computer screen, affixed to the table. Then stared at it for long enough, making the surroundings blurry after a while, almost nonexistent as if he was floating on a cloud.
He was alone. Working From Home Forever.

 

AS USUAL, HE STUFFED the remaining meal into his mouth and set the bowl away, literally slid it far across the desk to never grab it again. Dave began his day by skimming over unread messages first, in a nick of time, as he had already linked IDPA to a universal messaging gateway.
Back in the old days, when the poorest of the poor were given the affordable internet, it became obvious that accumulating such a huge data by just one company would have been hazardous. Thence the market leaders from various countries back then were the first to realize that something needs to be done to avoid the draconian future. So these early liberators decided to come to terms with it and had discerned of creating the universal platform to amalgamate the disintegrated tribes. Eventually, their efforts gave birth to the universal messaging gateway. It had been developing as the open project since then, mainly consisted of a mixture of open APIs and a distributed file system at its helm. This meant, no one company would be held accountable for owning the public data, not even the government by any means.
With such an architecture at its core, even the smallest entrepreneur delivering weed at home could use the messaging gateway to notify its customers, for anything. The weed delivery Page in ARG, however, has to provide hooks for the universal messaging gateway to pull in real-time updates and notify those customers in a scalable manner. Later similar strategy was used with IDPA so that any kind of requests could be made without even installing the Page. Just give a command to your IDPA and boom!

The Universal Messaging Gateway now happened to be the common but much powerful interface to access all sorts of messages coming from family members, friends, colleagues, ITS, IoT home appliances, and all sorts of fairy things mounted around the room of Dave. And from strangers too—spam detection was an unsolved problem. It would worsen though, some said.
IDPA moved few important incoming messages on top automatically while the overwhelming majority of such messages were grouped separately that needed Dave’s consideration. Although, some experts prefer to check out each message manually by disabling IDPA (the icon resembles a bot head placed atop the human abdomen) in order to vindicate their amour-propre, but for Dave that just saved his time.
Predominantly, the incoming messages would be resolved instantly, in a moment as soon as they arrive, by IDPA itself only if they are project specific. But those replies were unerringly apt as if Dave himself were involved. Although, Dave could see such messages on the right side of the screen. He prodded on a flat sheet of a keyboard which was paired with ARG to close the list of auto-replied messages in order to shift his focus to the important ones in the center. Now went full screen with a gesture.
After a while, IDPA voiced sympathetically, “The injunction to be nice is used to deflect criticism and stifle the legitimate anger of dissent”.
It was one of the famous quotes fetched furtively over the wire when Dave was busy smashing keys. In this case, IDPA dictated him not be rude.

 

SPLAYED ACROSS THE BOTH EARDRUMS in a stream of steep hum was the reminder of an upcoming live code conference, happening at the luxurious resort. Dave supposed to be attending it but woefully caught up with urgency. Earlier he had watched such events remotely without any privation despite the fact that one had to be physically present at the venue to grab various sponsored tech goodies for free.
He welted on the notification to start a live stream that swiftly covered the preoccupied screen in ARG as if a black hole had swallowed a glittering star in oblivion.
Having himself competitively gazing at the live stream was not rendered on the real computer screen. The projection of the virtual computer screen, made via ARG (he was wearing), was of a shape of a glowing 3D rectangle. Having glowy, it was not fairy though. It looked almost real as if it was materialized there, and moreover, others could see it too if they were on the same Line. Further, he would stretch the screen to suit his needs. Sometimes he would transmogrify it into multiple screens for more arduous tasks. Inevitably, he would start the new project today, so a single screen. But wider.
Augmented Reality Goggle (ARG) was a small device of the shape of a cigarette, mounted behind both ears, which made this possible. It zip-tied to the top of his left and right auricles and connected by a thin wire from behind, someday to talk to the amygdala to send data via neural signals to enable brain-to-brain communications. High definition cameras and mics were attached to both aft and rear ends of the device so that a 360 video feed could be viewed or captured.
By the same token, ARGs were of equal stature to human eyes.

Ruffling his hair with left hand, Dave reposed himself in the chair when chattering noise coming from the remote conference advanced. Still, plenty of time remained to start a keynote though.
ARG had been upgraded beyond what it was during its nascent stage. Now you would beam your reality around you for others to experience in real-time. Similarly, the conference sponsors had broadcasted the whole conference hall in a 360-degree video feed that anybody—and most importantly from anywhere would tap into. That way, Dave could see everyone who was attending the conference physically as if he was with them. Even the conference attendees use ARGs during the live conference instead of watching directly with their naked eyes; mostly for fancy bits that you would not see otherwise.
Strips of imagery scrolled in the line of sight when Dave observed the conference hall in a coltish manner for some familiar faces. With a mere gesture, you could hop person to person to face them as if you were trying to make an eye contact to begin a conversation with. The only difference here was that the person on the other side would not know of until you sent a hi-five request.
Dave stiffened for a moment and looked for his best friend with the keen observation who was physically attending the conference this time. “There he is!”, Dave shouted aimlessly. Before sending him a hi-fi request, Dave flipped his camera feed by tapping on the computer screen to face him, and drew a blooming rectangle on it, starting with a pinch in the middle of the screen and then both fingers going away from each other, that captured his face down to his torso like a newspaper cut out. That was, as a matter of fact, the only subset from his untidy reality that would be broadcasted when conversing. He flipped back the screen to encounter his friend and sent him the hi-five. They retreated and discussed the technology in the midst of laughter and jokes until the conference began.
Subsequently, Dave shifted gears and colonized the chair and the escritoire in the room with his feet to watch the keynote for a few productive hours.

 

HOPPED OVER A CODE EDITOR when Dave reclined in the chair with the satisfied feeling after watching his favorite conference. Dave’s job was to find various ways to make IDPA and related AI machinery astute. On this day, however, he would spend his time building a Page a.k.a. ARG application.
The code editor opened all those files he left unclosed last night. Then it was overlapped with a small notification dialog about pending reviews. Dave resisted the urge to cancel the notification, for he knew that he would need complete focus for the new project starting today, to not leave his colleagues hanging in the air.
In the past, some people were worried about Software eating the world but lo and behold, software ate the Hardware too. Software as a Service was the norm back then but after few years some smart folks thought of Hardware as a Service and it was a game changer. The result of that, today, if you need a new machine to run any software, you just have to launch AHS Page (Augmented Hardware Services) in ARG and choose the likes of configurations you prefer in terms of RAM, Graphics card, HD display, Storage, and whatnot—Up and Running workstation in no time and far cheaper. After this setup, all you need was a high-speed Fiber Line which was pretty commonplace nowadays. ARG lets you connect to the Line which in turn allows you to interact with the workstation (and many things) in Augmented Reality. It is, in other words, the entire operating system and the likes of Pages you need at the moment, all run on the cloud and then projected in your field of view.
That way the Dave’s code editor was rendered too.

Dave engaged and finished with the review without further ado except at one place wherein according to Dave it needed a personal touch. So he wrote a polite explanation to prove his point with a mundane graph drawn using arrows (–>), dashes (—), dots (…), and attached the video recording along with it—meaning he just had an idea of making the program extensible and immune to future requirement (Still a dream). Then moved on to face the new instance of the code editor. Before he began thinking of the new project, IDPA prompted unconventionally in his ears, “Dave, I would like to inform you that you had forgotten to submit the review”. Dave quickly submitted it with embarrassment. With revengeful sense, Dave teased patiently to IDPA,

“What is life?”

“I know you are not interested in scientifically accurate answer”, rebutted IDPA in disguise after sensing Dave’s intention, “but to me (and you too), it’s a ToDo list!”.

Dave ought to take revenge, but instead defeated by the machine, he conceded his defeat and decided to divert his attention to the job at hand.

 

WHEN THE USE OF MACHINE LEARNING techniques soared, many programmers gave up on traditional UI/UX development and in fact focused on training the machines to do so. They long ago predicted, if it was achieved to the level of human intelligence, it would save time and money for many. Today, IDPA was not that profound when it comes to creatively lay out a design on an empty canvas on its own. Instead it relies a lot on existing designs and trends only to come up with a somehow similar but little bit different designs compared to the rest. Although, it’s not imaginatively creative at all but still, Dave was optimistic for having IDPA on his side today.
IDPA built into the code editor was given a simple command to fetch the designs for the new project from the cloud repository. The new project was about a newly launched Flying Commercial Vehicle which was as compact as a 4-seater electric car but flies up too without the need of big rotor systems. Dave was given the task of creating an ARG Page that must include various shots of the Vehicle from all angles, using which people around the world can make bookings from anywhere. When he fed those details to IDPA from within the code editor, it quickly brainstormed in nanoseconds and churned out a design which made Dave sad—not because it was bad but because it looked the same as hundreds of other Pages he had seen before. Most importantly, he did not like the placement of the snapshots and the details provided.
There was one more way although, especially, for those who still had some artistry left in them when it came to design Pages. He briskly drew boxes on a blank canvas in a circular form making up the circle of life (as if that’s the last thing people needed to complete them), filled some of them with random texts, and marked certain areas where the snapshots must be. Then fed his magnum opus to IDPA which produced the Page out of it instantly. On the same line, he drew few more things to capture the booking details and then have it Page‘d too. In the meanwhile, IDPA slapped some statistics on the screen, apart from some not-so-important maths, it showed file size of the compiled Pages vs the original design files. That made Dave sighed in satisfaction as if humans had contacted an Alien Race they could talk to.
Dave went through the created Pages just to read information about the vehicle, mentioned next to each angled shot. Looks like he was on fire today since ideas kept coming to him in order to make the current Page design even better. Now he could either go back to the drawing board or make edits in the existing Pages himself. He thought for a moment and decided to go with the latter option, that is, to open an Interface Builder. With a flick of a button it literally transformed the code editor UI into the interface builder UI, snapping a bunch of pallets on each side, only to assist him.
Dave focused on the current design to change it the way he intended and also fixed few design errors that the intelligent interface builder suggested, given the best practices and performance incentives. It was intelligent for a reason since it had added appropriate validations on the data fields automatically that supposed to capture the registration details of people who wanted to buy the vehicle. The only thing Dave had to do was connect the same to the cloud data storage which was the kid’s play. So he picked one of the few available cloud storage engines to save the data and pelted the finish button that in turn compiled the final ARG application that he then pushed live on the ARGStore which was the one-stop destination to host all sorts of ARG applications now.
This had become the reality now since browsers are long dead and the Web, if you may know, had been transfigured beyond recognition.

Dave saw IDPA holding up on some news since a long time, not to disturb him. He unmuted it only to know that some researchers cracked the way how Brain creatively thinks and he determined to let go of the thoughts he was mulling over since the previous night.
If you liked this story in anyway, feel free to donate me and receive my dilettante painting as a token of appreciation for your donation.

Rendezvous – sci-fi short story


This is my first attempt to write a science-fiction short story (the couple of them are still in progress 😙). I must acknowledge that I’m no Douglas Adams to hit a home run with the first story 😄. Nonetheless, it was exciting and tough as f***. My grand salute to all those science-fiction authors who have been writing countless pages with the integrity and the soul of the real science in their writing.

Please star or comment to provide your valuable feedback, whatever it may be. I’m immune to anything hurls at me 😅.

WE ARE ALONE IN THE UNIVERSE was the faith deeply rooted in as the planet earth became a utopian world. The world without poverty, corruption, wars, and maladies. The world now run by Gods called Humans.

He woke up to the silence for the final countdown. His body was numb and furthermore, the ticking wall clock seemed deliberately obtuse. Tick, Tock, Tick, Tock, Tick, Tock, Tick, Tock, Tick, Tock – Ten seconds have passed. The clock on his right glowing blurred and continuously blinking as if someone was around him that could not be seen with naked eyes. But he can sense it, feel it, believe it. Few minutes have passed but all he could hear was a ticking clock. Suddenly, lights splashed on him from all angles. He was lying on a bed in the hospital. Yuvon is dying!

Not a similar death by old age or illness like ancient humans. Going through euthanasia by his own will. He had lived prosperous life for straight 3 centuries. Today is his last day!

His best friend and a renowned scientist, Fa, who had worked with him earlier on many revolutionary projects sat next to hear his final words before Yuvon rests himself in peace. Yuvon’s last invention was a disgrace (according to him) that forced him to end his life. But he is still believed to be influential and his opinions are still taken seriously. He was gathering thoughts to search for the answers within, confusingly blabbered. “Fa, Have you ever thought of having some kind of life around us…?”

“…unseen to our blindfolded eyes?”, questioned to himself.

According to Fa, he was literally exaggerating things at hand, but that’s how he has been since eons – no one believed in him until he achieved the impossible and became the greatest of all. Yuvon is one of the brightest minds on the planet, was born as a child prodigy. He has been the proponent of genetically improving human mortality and cognition to make them real gods, rather than making humanoid robots. He believed imitating body functions is far easier than mimicking conscience. And consciousness to be exact. Later he led the massive massacre event in the late 21st century where all poor, ailing, corrupt, and incompetent people were genetically massacred. The only motive behind such gruesome act, unlike none, had been seen in the ancient history, was to reboot the humanity with a controlled population of the smartest and brightest humans only. With such appalling rules, within few centuries, the danger of existential crisis became the trouble of the past and humans were throned as Interplanetary species.

Since childhood, his inquisitive mind was occupied with mysteries of our existence in the universe and restive to find answers. The theory of evolution levied upon the humanity since ages were the little white lie to him so he always opined to find extraterrestrial civilization to prove himself wrong for a long while. For that matter, it turned out to be his life’s only mission or rather a fixation. With Yuvon’s sheer will, his team worked on the clandestine project for many decades. But consequently, his latest obsession to contact alien life in the vast and somewhat mysterious sea of stars had emerged as a complete fiasco.

While generations of efforts exhausted to make humans immortals, Yuvon’s move to meet the death – considered to be a disease nowadays – is unsettling for Fa’s befuddled brain. Before Fa reacts, Yuvon continued, “…We may not be aware of their presence.”

After a long silence hoping Yuvon has to say more. “What made you come to that conclusion?”, interrogated Fa by refuting the possibility, phlegmatically.

Yuvon knew that he was insane but not enough to let go, at least not when he has mere 10 minutes left. “We sent high-frequency radio signals, made mammoth machines to tap on to cosmic changes, detected hundreds of Earth-like planets within reach, propelled human ships there to look for some life forms. Indeed, we left no stones unturned to find alien life in outer space. What have we found so far…” He choked out of breathing. Then breathed deeply to confess further but decided to restrain himself.

Fa tried to console Yuvon’s franticness, “Nothing. But Isn’t that commendable what you have achieved so far? I have not seen you so desperate before. Where is your perseverance? If you choose to stay alive, who knows, we may find success in our research.”

Yuvon regressed, “It’s not about persistence. It’s about perception”. A spooky looking flying robot with chimpanzee-like long hands followed by sprawling fingers entered the cubicle. It had no legs or a torso. It literally flew over Yuvon’s lying body, a digital prompt popped up after scanning his face. While checking background details of Yuvon on the screen, Silicon-based eyes of the bot widen when he acknowledged Yuvon as its creator. With its spidery fingers, it initiated euthanizing Yuvon. His breathing returned to normal as the doctor prepared the preliminary procedures.

He continued impatiently, “As you know we humans can not live in water like Marine animals do. Would we ever have found marine life at all on earth if we had forgone searching oceans for life just because our own characteristics falling flat there? The whole problem with our research is we reckon that other life-forms may not exist in an environment where we can not survive. We have always looked for an alien planet that exhibits somewhat similar characteristics of our own planet, and that is the problem!”

Fa smiled and said evenly, “Certainly! But that’s the fastest way to find extraterrestrial life on billions of habitable planets around us?”

Yuvon stopped to ponder but swivel the debate to bring it on the track, then said firmly, “What sounds right is not always right. Ever thought that why there are so many different animals, insects, what not on this planet? It’s amusing not to think of having no conspiracy here.”

Fa bestirred for the first time, “What do you mean?”

“Maybe. All the life-forms including us that we see, touch, smell, and feel are artificially created by some sort of higher beings.”

“Why would they do that?”

By pointing his finger towards the doctor, he said thoughtfully, “Why have we created these bots? To help us right? I highly doubt that they left us here alone because no creator builds something that’s not useful to him. We must be useful to them somehow and presumably, they must be here around us in a different form. Watching us, Controlling us.”

“You mean, demigod??”, Fa questioned reluctantly to accept what Yuvon was trying to sell him.

“They may be ancient civilization, advanced themselves to be non-human, for the better or worse.”, rebutted Yuvon.

Fa desperately wanted to speed up the meaningful consternation knowing the less time at hand for Yuvon, “Why would any civilization do that? What does it have to do with your death?”.

Yuvon sensed his urgency and said confidently, “Once you invent a technology, there is no fun being a caveman!” Yuvon questioned rapidly to the perplexed Fa as he sensed his brain and heart about to shut down within a minute, “When you do a certain thing the same way, over and over again, you run by your muscle memory which makes you boring, monotonous, and essentially the owner of whatever you are doing because muscle memory was built on experiences you had in the past. But when you do the same thing differently each time, you run by your intuition which makes you interesting, creative, and essentially the puppet. Because suddenly some higher energy comes into play that feeds you that intuition you are no longer in control. Our senses are too restrictive for us to see them. Or we are not meant to see them. For a Shark to comprehend the vastness of the sea, it has to leave the box it is trapped in and come out only to perceive the grandeur of the seas.”

While Yuvon began to enter into the eternal slumber, the elfin grin emerged on his face as if he had solved the mystery, “Yes, Fa. What we can not see, does not deny its existence!”.

Before Fa reacts, Yuvon’s brain is about to halt forever. The tongue slipped, shuddered, faltering his words. He could not continue further as his heart stopped pumping permanently, but his brain was surprisingly spry. “Will Fa pursue what I devised? Will humanity heed my advice after my death? Will humans ever learn to see things differently? Or Will my words be forgotten forever?” He thought to himself while his brain went black.

His eyes shimmered for the last time and cool breeze took over his entire body, making it cold rapidly as if someone touched him.

If you liked this story in anyway, feel free to donate me and receive my dilettante painting as a token of appreciation for your donation.

NativeScript: Edge of Tomorrow


This article is all about my experiences of building a Native Android application using NativeScript. NativeScript a framework for building cross-platform Android & iOS apps using Javascript and XML. For the reference, I am in the middle of migrating the same application to NativeScript + Angular combo (just for fun) as it leverages existing Angular skillset such as data-binding, event handling, routing, AuthGuard, etc, and makes it easy to reason about as well.

In a nutshell, NativeScript is not just a set of Javascript libraries but more of a runtime that enables to call Native Android and iOS apis using Javascript. It uses a Javascript virtual machine – V8 for Android and Webkit Core for iOS – that interprets and executes Javascript code at runtime to their respective native code. Hence, there is no limit in terms of which native API can be called in NativeScript, makes it a super powerful tool – worth adding in your arsenal 🙂

This article does not discuss about building Native apps in NativeScript because their documentation is pretty neat and covers it all.

Android ecosystem

Even though you were convinced to write native apps in pure Javascript, that fact is partially true. Meaning you still need to setup android SDK and tools in order to run your application on Emulator or Device. Oh boy, it was PITA in the beginning, especially when you are used to write for Web only – just Write. Refresh. Repeat!

One of such issues faced even before I could run their Sample Groceries app was the ANDROID_HOME environment variable is not set error, while running tns platform add android command. Although, there were tons of stack-overflow answers about that weirdo behavior but unfortunately all were suggesting to add ANDROID_HOME to the PATH environment variable – been there, done that already. At one point, I had decided to give up on NativeScript as I was this frustrated!

But soon the NativeScript community slack channel came to the rescue which I found on their troubleshooting documentation. That culprit is sudo – was running sudo tns platform add android like a rookie 👊. Nonetheless, I quickly got over it and set up the project with tns create FreshCall command.

NativeScript Plugins

The android application named FreshCall that I was building is a simple phone call management app to keep a tap on number of users being called on a daily/weekly basis, extract call charges for reimbursement purposes, and possibly record a phone conversation (with speakers on) for quality purposes. As you have already guessed that the application supposed to use android native APIs for most of the requirements such as ask permissions, choose or dial contact numbers, record conversation and upload it to Firebase, fetch call charges from USSD dialogs, etc. Luckily, the NativeScript community is flourished enough to have many plugins ready for most of the things I needed 😊

I realized that building traditional apps in NativeScript is far more easier than ones using native APIs. That’s why problems and worries did not stop for me at the rookie mistake made earlier, after all it was just the beginning.

Migrate Android code to NativeScript

Obvious thing for any phone management application is to detect Incoming calls and allow to make Outgoing calls using TelephonyManager. Truck load of articles and stack-overflow questions can be found on the topic, however, using them in NativeScript was a big deal. But Extending native classes section from the NativeScript documentation was quite useful that opened up ways for further exploration.

Below is the incoming call tracking code in Java (android):

private class CallStateListener extends PhoneStateListener {
  @Override
  public void onCallStateChanged(int state, String incomingNumber) {

  }
}
tm = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);
tm.listen(callStateListener, PhoneStateListener.LISTEN_CALL_STATE);

So the above Java code can be ported to NativeScript as follows:

var callStateListener = android.telephony.PhoneStateListener.extend({
onCallStateChanged: function (state, incomingNumber) {

}
var application = require("application");
var tm = application.android.context.getSystemService(android.content.Context.TELEPHONY_SERVICE);
tm.listen(new callStateListener(), android.telephony.PhoneStateListener.LISTEN_CALL_STATE);

How did I manage to port it so flawlessly? Here is a simple trick:

  1. Learn to extend android native classes in NativeScript.
  2. Search android documentation for a native property such as PhoneStateListener and refer the java.lang.Object path for the same i.e. android.telephony.PhoneStateListener.

The similar trick is applicable for application ctx, android Context, and tm.listen event.

Background Services

NativeScript community is so kind to give you all the tools to build a bridge, but the only condition is that you’ve to learn to build it yourself, 😄. I hope one day it will be a piece of cake as NativeScript grows and will be widely adopted as a standard to build native apps.

To track the USSD dialog’s response – the call charge window appears after the call ends – I needed a background service to keep an eye on it and extract the the call charge as soon as it shows up. Somehow I could write a background service myself with the help of the sample provided by NativeScript community and port the USSD parsing using the trick we just learned. However, the onServiceConnected() method – enables accessibility service to read USSD responses – was not getting registered even after enabling it for the application from the android settings.

During the salvation of this problem, I learned two things:

  1. When in doubt, always rebuild using tns build android (remove the existing application from the device before live-syncing again).
  2. Reinstall android packages if updates are available.
    Android SDK and tools
    Android SDK and tools

Wrap up

Experience of building the android application (without having any knowledge of Native Application Development before) was terribly empowering 😉. My advice to fellow developers is to keep Android Documentation open all the time!

Peter Kanev from NativeScript core team had been a great help. Also, special thanks to Brad Martin (who built many plugins I’ve been using) and Marek Maszay for their constant support and cheer on Slack.

Live on the Edge Of Tomorrow today with NativeScript!

If you found this article useful in anyway, feel free to donate me and receive my dilettante painting as a token of appreciation for your donation.

Cautionary tale for Experts


Imagine a beginner (who has come from a decent front-end background) asking not-so-dumb questions to an expert in the field of Native application development. I hate the term “expert” because no one is purely expert in this field – every person is learning something new from others on a daily basis. But I’m quoting the term for the sake of the article. Here is how the conversation went:

Beginner: Is there a way to find an SDK version like we do with JS library?

Expert: What a dumb question! You do not understand native development that’s why you are asking it.

Beginner: Actually documentation does not have that details, hence asked.

Expert: Have you ever coded in Android/Java??

Beginner: Nope. Will I automatically get the new version of the SDK or have to update the manifest file each time?

Expert: Yes, automatic update!

Beginner: So I do not even have to run some sort of build command to pull in the new version like npm update does.

Expert: Please don’t over analyze the things that you don’t understand or don’t know. It wastes your as well as my time 🙂

Beginner: I’m not over analyzing, just trying to understand by comparing it with frontend development, but you can be little gentle solving such queries raised by the beginner like me.


As you can see the expert was trying to be arrogant by his knowledge and skills. To me it were acceptable if and only if he/she would have resolved my queries. Unfortunately, he/she had not answered any query in a sane way though. Una Kravets‘s recent tweet speaks about the same issue:

Through this post I just want to advice all the experts that,

Whatever high-skilled wits you’ve acquired were based on documentation, manuals, books, and articles written by someone else. You would not be the expert today if those experts had disparaged you like that. All the experts owe it to those they have learned from and the only thing to pay back the favor is by sharing your knowledge to the next generation without being cocky.

Remember, Sharing your knowledge with others does not make you less important.

Angular2: Data Binding


In the previous article, we had explored how dependency injection works in Angular2 compared to Angular1. In this article, I will walk you through the one-way vs two way data binding and how its different but quite similar in Angular2.

Angular1

As usual, we’ll quickly go through the Angular1 example. Let’s take a simple example (HTML) from my free ebook on Angular1 Directives that explores how data binding works in Angular1. Notice we have not written a single line of Javascript code to enable the data binding in the following example:

<br>
&lt;html ng-app="App"&gt;<br>
&lt;head&gt;<br>
  &lt;title&gt;Two way Data Binding with Form Controls&lt;/title&gt;<br>
  &lt;script src="../bower_components/angular/angular.js"&gt;&lt;/script&gt;<br>
&lt;/head&gt;<br>
&lt;body&gt;<br>
  &lt;input type='checkbox' ng-model='choose' /&gt;<br>
  &lt;div&gt;{{choose}}&lt;/div&gt;</p>
<p>  &lt;button ng-click="choose=!choose"&gt;Toggle Checkbox&lt;/button&gt;</p>
<p>  &lt;script type="text/javascript"&gt;<br>
    angular.module('App', []);<br>
  &lt;/script&gt;<br>
&lt;/body&gt;<br>
&lt;/html&gt;<br>

Now that we’ve a working demo, let us migrate it to Angular2.

Angular2

Let us create a typescript file twoway-binding.ts first to lay our main component.

<br>
import { NgModule, Component } from '@angular/core';<br>
import { FormsModule } from '@angular/forms';<br>
import { BrowserModule } from '@angular/platform-browser';<br>
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';</p>
<p>@Component({<br>
  selector: 'ng-app',<br>
  template: `<br>
    &lt;input type='checkbox' [(ngModel)]='choose' /&gt;<br>
    &lt;div&gt;{{choose}}&lt;/div&gt;<br>
    &lt;button (click)="choose=!choose"&gt;Toggle Checkbox&lt;/button&gt;<br>
  `<br>
})<br>
export class DataBindingComponent {<br>
  choose: boolean = true;<br>
}</p>
<p>@NgModule({<br>
  declarations: [DataBindingComponent],<br>
  imports:      [BrowserModule, FormsModule],<br>
  bootstrap:    [DataBindingComponent]<br>
})<br>
export default class MyAppModule {}</p>
<p>platformBrowserDynamic().bootstrapModule(MyAppModule);<br>

We are already familiar with Component, ES6 Class, bootstrap, etc. jargons from the first article as those are the backbone of Angular2. The only difference here is the template with data-binding expressions.

In Angular1, the two-way data binding was enabled by default, for one-way data binding the expression should be wrapped in {{choose}}. Also the attribute name supposed to be hyphenated. However, in Angular2 the attribute should be in camel-case with subtle differences that affect the way data flows:

  1. [Component -> Template]
    To show a model in a template which was defined in the component, use square-bracket or double-curly syntax a.k.a. One Way Binding.
  2. (Template -> Component)
    To broadcast an event from a template in order to update the model in the component, use round-bracket syntax a.k.a. One Way Binding.
  3. [(Component -> Template -> Component)]
    To have both behaviors simultaneously, use round-bracket inside square-bracket (or banana-in-the-box) syntax a.k.a. Two Way Binding.

This clearly means that the two-way binding in Angular2 is opt-in and can be enabled by importing BrowserModule to plug it in the NgModule.

In the above example, we want to toggle the expression choose from the template to the component using (click) when the button is clicked and update the checkbox in the template immediately using [ngModel]="choose". We also want to update the model choose in the component if the checkbox is toggled manually using [(ngModel)]="choose". Do not get confused with one-time binding in Angular1 i.e. {{::choose}} where the model used to be freezed once evaluated, there is no such thing in Angular2.

Now let us update HTML markup as usual.

<br>
&lt;html&gt;<br>
&lt;head&gt;<br>
  &lt;title&gt;Angular2: Two way Data Binding with Form Controls&lt;/title&gt;<br>
  &lt;meta charset="UTF-8"&gt;<br>
  &lt;meta name="viewport" content="width=device-width, initial-scale=1"&gt;</p>
<p>  &lt;!-- 1. Load libraries --&gt;<br>
  &lt;script src="../node_modules/core-js/client/shim.min.js"&gt;&lt;/script&gt;<br>
  &lt;script src="../node_modules/zone.js/dist/zone.js"&gt;&lt;/script&gt;<br>
  &lt;script src="../node_modules/reflect-metadata/Reflect.js"&gt;&lt;/script&gt;<br>
  &lt;script src="../node_modules/systemjs/dist/system.src.js"&gt;&lt;/script&gt;</p>
<p>  &lt;!-- 2. Configure SystemJS --&gt;<br>
  &lt;script src="../systemjs.config.js"&gt;&lt;/script&gt;<br>
  &lt;script&gt;<br>
    System.import('ch01/twoway-binding').catch(function(err){ console.error(err); });<br>
  &lt;/script&gt;<br>
&lt;/head&gt;<br>
&lt;body&gt;<br>
  &lt;!-- 3. Display the application --&gt;<br>
  &lt;ng-app&gt;Loading...&lt;/ng-app&gt;<br>
&lt;/body&gt;<br>
&lt;/html&gt;<br>

Wrap up

Remember, because of the simplified template syntax in Angular2, you can do lot of things easily such as,

  • {{textContent}} becomes [textContent]="textContent"
  • ng-bind-html="html" becomes [innerHTML]="html"
  • ng-mouseup="onMouseUp()" becomes (mouseup)="mouseUp()"
  • my-custom-event="fire()" becomes (customEvent)="fire()"

Fewer templating syntax means Faster learning! Head over to a live data binding in action.

If you found this article useful in anyway, feel free to donate me and receive my dilettante painting as a token of appreciation for your donation.

Angular2: Dependency Injection


In the last article, we’ve explored basic differences between Angular 1 and 2 with respect to Component over Directive, TypeScript over ES5, ES6 Class over Controller, and Decorators/Annotations. We’ve pretty much got acquainted with setting up Angular2 in TypeScript with SystemJS to begin our journey with Angular2 in coming weeks. In this article, we’ll investigate how dependency injection that we always loved is different in Angular2 than Angular1. We are not going to cover what dependency injection is in this article though, so if you are not familiar with it yet, follow the documentation for more information.

Angular1

As usual, we’ll quickly go through the Angular1 example. Let’s take a simple example (HTML and JS) from my free ebook on Angular1 Directives that explores how DI works in Angular1. Notice we’ve used ngController directive in which we are going to inject a custom service. Also stare at ngApp directive which was a recommended way to bootstrap an application automatically in Angular1 rather than manually using angular.bootstrap method.

<br>
&lt;html ng-app="DI"&gt;<br>
&lt;head&gt;<br>
  &lt;title&gt;AngularJS: DI&lt;/title&gt;<br>
  &lt;script src="../bower_components/angular/angular.js"&gt;&lt;/script&gt;<br>
  &lt;script src="../js/ch01/di.js"&gt;&lt;/script&gt;<br>
&lt;/head&gt;<br>
&lt;body ng-controller="SayHi"&gt;</p>
<p>&lt;h1&gt;Check the console&lt;/h1&gt;</p>
<p>&lt;/body&gt;<br>
&lt;/html&gt;<br>

Here is the JavaScript code for the above example. We have defined the custom service named Hello and later injected the same into the controller called SayHi.

<br>
var App = angular.module('DI', []);<br>
App.service('Hello', function() {<br>
  var self = this;<br>
  self.greet = function(who) {<br>
    self.name = 'Hello ' + who;<br>
  }<br>
});</p>
<p>App.controller('SayHi', function(Hello) {<br>
  Hello.greet('AngularJS');<br>
  console.log(Hello.name);<br>
});<br>

Now that we’ve our demo up and running, it’s time to migrate it to Angular2.

Angular2

Let us first update the JS code first and walk through the changes.

<br>
import { NgModule, Component, Injectable } from '@angular/core';<br>
import { BrowserModule } from '@angular/platform-browser';<br>
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';</p>
<p>@Injectable()<br>
class HelloService {<br>
  name: string;</p>
<p>  greet(who) {<br>
    this.name = 'Hello ' + who;<br>
  }<br>
}</p>
<p>@Component({<br>
  selector: 'ng-app',<br>
  template: '&lt;h1&gt;Check the console&lt;/h1&gt;'<br>
})<br>
export class DIComponent {<br>
  constructor(public Hello: HelloService) {<br>
    this.Hello.greet('Angular2');<br>
    console.log(this.Hello.name);<br>
  }<br>
}</p>
<p>@NgModule({<br>
  declarations: [DIComponent],<br>
  imports:      [BrowserModule],<br>
  providers:    [HelloService],<br>
  bootstrap:    [DIComponent]<br>
})<br>
export default class MyAppModule {}</p>
<p>platformBrowserDynamic().bootstrapModule(MyAppModule);<br>

We are already familiar with Component, ES6 Class, bootstrap, etc. jargons from the previous article as these are the backbone of Angular2. The only difference here is the service, HelloService. However, if you notice, the service is itself a ES6 Class similar to our application component, DI. Just like @Component annotation is there to tell Angular2 to treat a ES6 class as a component, the @Injectable annotation annotated the ES6 Class as an Angular2 Service that is injected elsewhere.

In the above example, we’ve injected the service via providers options in the @NgModule. This way the instance of the service will be available for the entire component only and its child components, if any. However, services in Angular2 are not singleton by nature unlike Angular1 which means if the same service is injected as a provider in a child component, a new instance of the service will be created. This is a confusing thing for beginners so watch out.

The public Hello: HelloService parameter in the constructor is equivalent to Hello: HelloService = new HelloService();. It means Hello is an instance of HelloService and of a type HelloService (optional). One benefit of annotations/decorators in Angular2 in my opinion is that there is no confusion over Factory vs Service vs Provider, instead it’s just a ES6 class.

Slight changes compared to the previous example is that we need to give more configurations in SystemJS for typescript compilers to emit decorator MetaData so that generated ES5 code will work fine in a browser.

<br>
{<br>
  "compilerOptions": {<br>
    "target": "ES5",<br>
    "module": "commonjs",<br>
    "moduleResolution": "node",<br>
    "sourceMap": true,<br>
    "emitDecoratorMetadata": true,<br>
    "experimentalDecorators": true,<br>
    "removeComments": false,<br>
    "noImplicitAny": false<br>
  }<br>
}<br>

Remember experimentalDecorators flag enables experimental support for ES7 decorators in TypeScript and emitDecoratorMetadata flag emits design-type metadata for decorated declaration in ES5 output, especially, for public Hello: HelloService code in the constructor as we’d seen before. Using the alternative mentioned above will not need these flags though.

Now let us update HTML markup.

<br>
&lt;html&gt;<br>
&lt;head&gt;<br>
  &lt;title&gt;Angular2: DI&lt;/title&gt;<br>
  &lt;meta charset="UTF-8"&gt;<br>
  &lt;meta name="viewport" content="width=device-width, initial-scale=1"&gt;</p>
<p>  &lt;!-- 1. Load libraries --&gt;<br>
  &lt;script src="../node_modules/core-js/client/shim.min.js"&gt;&lt;/script&gt;<br>
  &lt;script src="../node_modules/zone.js/dist/zone.js"&gt;&lt;/script&gt;<br>
  &lt;script src="../node_modules/reflect-metadata/Reflect.js"&gt;&lt;/script&gt;<br>
  &lt;script src="../node_modules/systemjs/dist/system.src.js"&gt;&lt;/script&gt;</p>
<p>  &lt;!-- 2. Configure SystemJS --&gt;<br>
  &lt;script src="../systemjs.config.js"&gt;&lt;/script&gt;<br>
  &lt;script&gt;<br>
    System.import('ch01/di').catch(function(err){ console.error(err); });<br>
  &lt;/script&gt;<br>
&lt;/head&gt;<br>
&lt;body&gt;<br>
  &lt;!-- 3. Display the application --&gt;<br>
  &lt;ng-app&gt;Loading...&lt;/ng-app&gt;<br>
&lt;/body&gt;<br>
&lt;/html&gt;<br>

Wrap up

That’s it guys..! Wolksoftware‘s blog has covered a great intro to reflection in Typescript, if interested. In the next post, we’ll explore two-way databinding in Angular2, but meanwhile Checkout Dependency Injection in action.

If you found this article useful in anyway, feel free to donate me and receive my dilettante painting as a token of appreciation for your donation.

Angular2: The First Time


Angular 2.0 is lot like Angular1 but still different in its own terms and you’ll see why as we go along. Purpose of this article is to migrate one of the simplest Angular1 examples to Angular2 and understand benefits as well as pain-points. For the record, Angular2 aimed to build web applications in three different languages or flavours i.e. JavaScript (ES5/ES6), Typescript(ES6+), and Dart. If you interested in other two then the 5-minute-session will give you a head-start to understand how it could be done. However, I’m going to use Typescript over JavaScript here because in my opinion it feels more natural. This article is for the ones who worked with Angular1 before.

Angular1

Let’s take a simple example (HTML and JS) from my free e-book on Angular1 Directives that explores how data binding works in Angular1. Let us quickly take a look at our good old Angular1 HTML template and then we’ll move to new shiny Angular2 version of it. But for now, notice we’ve used ngBind directive over double-curly notation for data-binding, obviously to avoid FOUC as we all know. Also checkout ngApp directive which was a recommended way to bootstrap an application automatically in Angular1 without using angular.bootstrap method.

<br>
&lt;html ng-app="App"&gt;<br>
&lt;head&gt;<br>
  &lt;title&gt;HTML/DOM Template in AngularJS&lt;/title&gt;<br>
  &lt;script type="text/javascript" src="../bower_components/angular/angular.js"&gt;&lt;/script&gt;<br>
  &lt;script type="text/javascript" src="../js/ch01/angular-template.js"&gt;&lt;/script&gt;<br>
&lt;/head&gt;<br>
&lt;body&gt;<br>
  &lt;span&gt;Hello, &lt;span ng-bind="name"&gt;&lt;/span&gt;&lt;/span&gt;<br>
&lt;/body&gt;<br>
&lt;/html&gt;<br>

Here is the JavaScript code for the above example which simply binds a value of name property on the $rootScope.

<br>
var App = angular.module('App', []);<br>
App.run(function($rootScope) {<br>
  $rootScope.name = 'AngularJS';<br>
});<br>

Now that we’ve our demo up and running, its time to migrate it to Angular2.

Angular2

Let us first update the JS code and walk-through the changes. Give it a very long, hard stare to get familiar with the following snippet, I’ll wait.

<br>
import { NgModule, Component } from '@angular/core';<br>
import { BrowserModule } from '@angular/platform-browser';<br>
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';</p>
<p>@Component({<br>
  selector: 'ng-app',<br>
  template: '&lt;span&gt;Hello, &lt;span [textContent]="name"&gt;&lt;/span&gt;&lt;/span&gt;'<br>
})<br>
class MyAppComponent {<br>
  name: string = 'Angular2';<br>
}</p>
<p>@NgModule({<br>
  declarations: [MyAppComponent],<br>
  imports:      [BrowserModule],<br>
  bootstrap:    [MyAppComponent]<br>
})<br>
export default class MyAppModule {}</p>
<p>platformBrowserDynamic().bootstrapModule(MyAppModule);<br>

Your first reaction would be, “Alright, that looks familiar..!”. Well it is except few things, let us go over one by one. But before that we must know that Angular2 moved from a concept of directives to components to leverage web-components specs so that developers can use Polymer components with Angular2 templates. Basic idea is to break down monolithic application into small pieces i.e. components and plug them together at the end under main component like we have here. Each component should have one or more Classes or Functions to store data (name in this case) and necessary methods (not defined in the example above). Please note we no longer use $scope in Angular2 as this is somewhat like CtrlAs syntax from Angular1. When Angular2 instantiates MyApp class, all properties or methods defined will be accessible via keyword this.

Update: As per RC5 release, angular2 introduced a concept of NgModule metadata which plays an important role in the compilation process, especially offline compilation. Because of NgModule, Angular2 will able to lazy load components on demand by resolving component dependencies efficiently. So every component should expose a NgModule for other components to import and reuse. Ideally, NgModule should be extracted into it’s own file importing various components, services, pipes, etc. but for the sake of readability, we’ll have it in the same file along with the component.

export statement

Angular2 has adopted modularity at its core unlike Angular1 wherein you could only define modules without any built-in support for lazy loading. Using ES6 export module syntax, you can export functions, objects, or primitives that other classes can use in Angular2. Having one export class per module recommended though. We’ve defined a property, name with a type string which is optional in TypeScript.

Import Statement

With the same export class syntax, Angular2 has organized its code base so that developers can import what they need in a module. In this example, we are only importing Component and NgModule annotations from angular2/core library and platformBrowserDynamic method from angular/platform-browser-dynamic modules. Note that the extension is optional in the definition.

Using platformBrowserDynamic().bootstrapModule method from angular2 browser module (similar to angular.bootstrap in Angular1), we can bootstrap the NgModule named MyAppModule. Angular2 is a lot different from Angular1 on the architectural level. Angular1 only targeted Desktop and Mobile web browsers, but Angular2 is platform agnostic, that means it can be used to write Native mobile apps using NativeScript or Cordova, run angular2 application inside web workers, and to enable server-side rendering with NodeJS or so. For now, we’ll just run it in a web browser by pulling down browser specific bootstrap method as above.

Annotation

The strange-looking syntax above the class called as a Class Decorator that annotates and modifies classes and properties at design time. Wolksoftware’s engineering blog has great articles on decorators and metadata in Typescript. In a nutshell, Angular2 uses decorators/annotations to make dependency injection possible. To simplify it, let us take a small requirement.

Imagine you want to log a function name and passed arguments to browser console every time a method invoked without modifying the function body. In the following example, @debug is a method decorator that attached a special behavior to the method body. overtime a method called, the decorator will find a method name being called and it’s parameter that we can log. Here is a working demo if interested.

<br>
export class AnnotationExample {<br>
  @debug<br>
  life(n: number) {<br>
    return n;<br>
  }<br>
}</p>
<p>function debug(target: Function, fnName: string, fn: any) {<br>
  return {<br>
    value: function (argument: number) {<br>
      document.getElementsByTagName('body')[0].textContent = `function "${fnName}" was called with argument "${argument}"`;<br>
    }<br>
  };<br>
}</p>
<p>var whatIs = new AnnotationExample();<br>
whatIs.life(42);<br>

On the same note, @component annotation/decorator tells Angular2 that the defined class, MyApp is an Angular2 Component not just a ES6 class. And once it’s defined as the component, Angular2 needs to know how the component supposed to be consumed in HTML or which template to render once it’s registered. That’s why we need to pass the meta data as above such as selector, template, etc.

Notice selector name need not be the same as the class name unlike Angular1. There is no restrict option anymore, instead use a tag name (ng-app) or a property name (with square brackets [ng-app]) or a class name (.ng-app) directly as a selector (I think a comment selector is dropped..!). Also note that we have used a double curly notation for data binding here as there is no ngBind directive available, but you can use property binding (which we’ll explore in future posts) to get the same feeling with <span>Hello, <span [textContent]="name"></span></span>.

Now let us update HTML markup.

<br>
&lt;html&gt;<br>
&lt;head&gt;<br>
  &lt;title&gt;HTML/DOM Template in Angular2&lt;/title&gt;<br>
  &lt;meta charset="UTF-8"&gt;<br>
  &lt;meta name="viewport" content="width=device-width, initial-scale=1"&gt;</p>
<p>  &lt;!-- 1. Load libraries --&gt;<br>
  &lt;script src="../node_modules/core-js/client/shim.min.js"&gt;&lt;/script&gt;<br>
  &lt;script src="../node_modules/zone.js/dist/zone.js"&gt;&lt;/script&gt;<br>
  &lt;script src="../node_modules/reflect-metadata/Reflect.js"&gt;&lt;/script&gt;<br>
  &lt;script src="../node_modules/systemjs/dist/system.src.js"&gt;&lt;/script&gt;</p>
<p>  &lt;!-- 2. Configure SystemJS --&gt;<br>
  &lt;script src="../systemjs.config.js"&gt;&lt;/script&gt;<br>
  &lt;script&gt;<br>
    System.import('ch01/angular-template').catch(function(err){ console.error(err); });<br>
  &lt;/script&gt;<br>
&lt;/head&gt;<br>
&lt;body&gt;<br>
  &lt;!-- 3. Display the application --&gt;<br>
  &lt;ng-app&gt;Loading...&lt;/ng-app&gt;<br>
&lt;/body&gt;<br>
&lt;/html&gt;<br>

If you remember, we’d included just angular.js in Angular1 template before, however, Angular2 relies on couple of other JavaScript libraries that you need to inject along with it. But things may change eventually, as it’s just a RC release (at the time of writing this article, and who knows we have to just include one file as before).

Refer SystemJS Wiki for more details to suite your needs. And as you guessed already, we have created a custom element in the body at the end to kick it off.

Wrap up

Peek at a demo..!. That’s it guys..! This is the exact flow you’ll always be using to write new components in Angular2. I liked the progress with Angular2 and Typescript, I would recommend to use Visual Studio Code Editor over SublimeText to feel at home. Be hopeful that many of the things we explored would be simplified by the time it reaches 1.0 (stable), I mean 2.0 😉

If you found this article useful in anyway, feel free to donate me and receive my dilettante painting as a token of appreciation for your donation.

Protractor: The Secret Service


This post is not about how to install and use Protractor. I believe setting up Protractor is a lot easy, so heading over to the documentation is an ideal choice to make. For those who have not heard of Protractor yet, it is nothing but an end-to-end test framework for AngularJS applications. However, it works seamlessly for non-angular applications as well, obviously, with a small tweak which is what this post is all about.

In a nutshell, Protractor runs tests against your application running in a real browser, interacting with it as a user would. That means you have to give a set of instructions for each test you write. But before that, let us create a small demo in jQuery (no AngularJS). Below demo is fairly simple but useful to put my point across about robustness of writing tests in Protractor. In this demo, we are having a dummy Google Sign In button loaded via a fake REST call to Google Developers APIs.

<br>
&lt;!DOCTYPE html&gt;<br>
&lt;html&gt;<br>
&lt;head&gt;<br>
	&lt;title&gt;Protractor: The Secret Service&lt;/title&gt;<br>
	&lt;script src="http://code.jquery.com/jquery-1.11.3.min.js"&gt;&lt;/script&gt;<br>
	&lt;script&gt;<br>
	$(window).load(function() {<br>
		// Simulating a REST call with Google APIs<br>
		window.setTimeout(function() {<br>
			$('#signin-placeholder').replaceWith(<br>
				'&lt;button id="signin"&gt;Google Sign In&lt;/button&gt;'<br>
			);</p>
<p>			$('#signin').click(function() {<br>
				$(this).text('Sign Out');<br>
			});<br>
		}, 2000);<br>
	});<br>
	&lt;/script&gt;<br>
&lt;/head&gt;<br>
&lt;body&gt;<br>
	&lt;div id="signin-placeholder"&gt;&lt;/div&gt;<br>
&lt;/body&gt;<br>
&lt;/html&gt;<br>

Sleep

Now that we understood how our application works, it’s time to write E2E tests for it. Following test block might look sound and easy to grasp to you, but there are couple of hidden tricks have been used in order to pass it.

<br>
describe('Non EC Tests', function() {<br>
	beforeEach(function() {<br>
		browser.ignoreSynchronization = true;<br>
		browser.get('demo.html');<br>
	});</p>
<p>	it('Should', function() {<br>
		browser.sleep(2 * 1000);<br>
		element(by.id('signin')).click();<br>
		expect(element(by.id('signin')).getText()).toBe('Sign Out');<br>
	});<br>
});<br>

First line in the beforeEach block i.e. browser.ignoreSynchronization is to tell Protractor that, Hey, this is not an Angular application, so do not wait for Angular to load. Start running tests as soon as the page loads in a browser. This is the one and only trick to consider while using Protractor for non-angular applications which means if in case a webdriver control flow hangs while running tests, that’s the thing to look for in the Spec. Moving on to the it block which is our actual test. In the beginning, we make sure to wait for 2 seconds as per our business logic before we click the login button. Later we confirm the button text is changed to Sign Out.

The important thing to note here is that the browser.sleep method is a last resort most people use to make tests pass to prevent random failures, however, in our case, it is tightly coupled with the business logic. If we increase/decrease the timeout in the fake REST call, we’ll have to adjust the same to fix the broken test. In addition to it, the real REST call may or may not complete within the expected timeout making your test unstable. Let’s fix that…

Wait

Selenium webdriver exports a method called browser.driver.wait that schedules a command to wait for a condition to hold true. This means wait will repeatedly evaluate the condition until it returns a truthy value. You can pass a condition or a custom function that returns a promise to it. So we will simply replace the browser.sleep statement with,

<br>
browser.wait(element(by.id('signin')).isDisplayed, 2 * 1000);<br>

In here browser will wait until isDisplayed method returns a truthy value. If it did not, it will time out after 2 seconds. Note in protractor version 2.0.0, due to changes in WebDriverJS, wait without a timeout will now default to waiting for 0 ms – which off-course forbids our business logic. So we chose to provide ideal explicit timeout. To your surprise the above statement will throw, Failed: No element found using locator: By.id("signin") error because it’s not available in the DOM yet. We could have used browser.waitForAngular method for Angular to render the button if we were using it in the demo. Protractor deals with such scenarios nicely, especially in Angular applications wherein it forces the webdriver to wait until Angular has finished rendering and has no outstanding $http or $timeout calls before continuing.

ExpectedConditions

No worries..! Selenium offers a different mechanism to handle the same for Protractor called as ExpectedConditions a.k.a. EC. It’s similar to protractor isDisplayed API that we saw earlier but especially for non-angular applications. Again the webdriver waits until the expected condition returns a truthy value. Moreover, you can mix it with multiple and, or, and not operators. So to fix the error, we’ll use,

<br>
browser.wait(protractor.ExpectedConditions.visibilityOf(<br>
  element(by.id('signin'))<br>
));<br>

This will schedule a command for the webdriver to wait and repeatedly evaluate the condition to check if the element is available/visible in the DOM. There are various functions EC supports that you should take a look at http://www.protractortest.org/#/api?view=ExpectedConditions.

Wrap up

So that’s it. In a real-world project, I happened to encountered similar issues on staging environment where pages load slighly slow than local webserver and some of my tests started failing randomly because browser.sleep hack for HTTP or AJAX calls did not pan out :-). EC statements have helped me a lot while writing robust Protractor tests for non-angular apps and made tests more stable too, mostly, on Firefox and Safari.

Thanks for reading and keep Rocking \m/.

If you found this article useful in anyway, feel free to donate me and receive my dilettante painting as a token of appreciation for your donation.