Monday, 19 March 2018

Of a mocha test of a ES6 Fibonacci tree, the modulo operator, use case for getters and setters and undefined value of 0

ES6 has introduced some nice OO-like feel to javascript.

I somehow find it difficult to grasp the OO part of it in a language which says "Just use === when you wish to check and compare for both value and type"!

But with the ES6 specifications, the 'class' keyword and property getters and setters, javascript is looking to become more readable, intelligible and maintainable!

So, when I chanced upon a question in Stackoverflow dot com about a B Tree constructed on the Fibonacci series with 0 and 1 as root and subsequent values either to the left or to the right of the tree, I picked up the example to write a mocha test on it using ES6.

That is,
and so on.

So, the requirement says that we need a Node class with Left and Right node properties to contain the position of the node's value in the tree and a FibonacciTree class that would build the tree.

Remember, this is a test not the actual implementation of the tree so, the logic of decision-making to determine whether the value should be to the left or to the right of a node is left to the assertion of the test and is statically manipulated from the test ie., it is understood that since it is the test that knows its expectations as per "Arrange, Act, Assert" of the test, the conditional value (instanceOfB.X) is assumed.
Edit: You may need to use babel or target your config.json file to ES6 for the code to transpile.

The Fibonacci tree class is as below:

// fibonaccitree.js

var Node=require('./nodetree');
class fibonaccitree{
get X(){
    return this._X;
set X(newX){
    if (newX)

       var node = new Node();
            console.log("Value of X is " +this._X)
            if (this._X % 2==0){   
            else if (this._X % 2!=0){
       return node;

The node class that would contain the left and right values of a node is as below:

// nodetree.js

class nodetree {
    get right(){
        return this._right;
    set right(rightValue){
        if (rightValue>=0)
    get left(){
        return this._left;
    set left(leftValue){
        if (leftValue>=0)

and the mocha test is as below:

// fibonaccitreetest.js

var A=require('./nodetree'),B=require('./fibonaccitree')
var assert=require('assert');
describe("Fibonacci tree Test", () => {
    var InstanceOfB=new B(); // Arrange
    var InstanceOfA=new A(); // Arrange
    beforeEach(() => {
    if (InstanceOfB.x==1){ // can use or not since test mthod also initializes. You may use either 
        InstanceOfB.X=2; // Arrange (Re-arrange) - edit: this is to check / ensure if /and even if the order of execution of test  methods change it will execute the if switch in code correctly
        //InstanceOfB = new B(); // Arrange (Re-arrange)
        //InstanceOfA = new A(); // Arrange (Re-arrange)
    it('should check if the node is to the left of the binary tree', () => {
        InstanceOfB.X=2; // Arrange - can use or not since afterEach already initializes 
        var y=8;
        console.log("Value passed is " +y)
        var res = InstanceOfB.buildTree(y); // Act
        assert.equal(res.left,y); // Assert       
    it('should check if the node is to the right of the binary tree', () => {
        InstanceOfB.X=1; // can use or not since afterEach already initializes 
        var z=13
        console.log("Value passed is " +z)
        var res = InstanceOfB.buildTree(z);

And the test result on node (node.js) is as in the below screenshot:

What is interesting in this example is the way the modulus operator in Javascript behaves.

If  a value of 0 is passed to the property 'X' in the fibonaccitree class then if (this._X%2==0) fails to evaluate correctly because the value of '0' supposedly gets interpreted as 'undefined', which is the reason for InstanceOfB.X=1; and InstanceOfB.X=2;

Happy mocha-ing, ES6-ing, and modulo-ing javascript!

Friday, 27 October 2017

Sugar on Pi - headless Raspberry Pi 3 B

SoaS or Sugar on a Stick was a popular interface to boot a Windows or any non-XO machine into the Sugar GUI.

With Raspbian (Stretch), there are different variants that range from Pidora to Fedora but this approach using a Sugar based Linux image created by Sugar labs, thankfully worked although with a few glitches when working with the Journal. Not sure if it is less memory or connecting through XMing.

I guess it is early days to make any confident observation on the maturity of using Sugar Activities on the RasPi3 but I am thrilled with the prospect of offering a less than $100 educational laptop based on the Raspberry Pi 3 B or Raspberry Pi Zero W hardware.

The reason for the thrill is the My Neighborhood interface that works with the WiFi connectivity.

The interface I used to connect graphically with the Pi is the ssh+x11 using Xming as the display output. 

Extensively testing the Sugar interface on a 512 M or 1 G motherboard would be the first task that I must accomplish to enable the bundling of the fabulous Sugar activities on the Pi.

A video of the Sugar interface on the Raspberry Pi and the Sugar activities on Sugarizer server.

Friday, 13 October 2017

Headless boot of RasPi 3 B

First things first, the micro sd card can be removed/inserted without having to disassemble the Raspberry Pi official enclosure box unlike what is maintained in many online posts!

It is so important that I thought it best to mention it ahead of any technical steps because so winding are the many forum documentation on how to start up your Raspberry Pi, without a monitor/screen/keyboard via wifi/lan.

I used Windows 7 laptop so all instructions are based on Windows.

Other Assumptions - DHCP client service running, ethernet enabled.

Early footnote - There is a 'w' version (wireless and bluetooth) of Raspberry Pi Zero that is lesser to the Pi 3 B in RAM, some processing power and about Rs. 2k less in cost.

To get on with the Pi 3 B, first,

the Dont's

1. Don't use Noobs!

2. Don't buy the pre-installed SD Card from CrazyPI (Not being mean here (anyway, CrazyPI is getting free reach here!) but the sheer waste of a day because of the SD card is simply a no-no) that says you would get a Pidora, too. It does not even boot!

3. I bought the one, a 16 GB card, and after a day of tussle with online forums and the SD card, I finally went and bought a Micro SD Card adapter, deleted all the files (made a copy of the files, of course, just for good measure!), downloaded/installed Etcher, ran it and selected the Rasbian Stretch image that I had downloaded from the official RasPi website and flashed it onto the SD Card. 

4. It took 4 hours to burn the image (and an additional hour or so to validate the burned image)! Probably, a 8 GB micro SD card (Class 10) should burn faster. Size matters, in this context!

the Do's

1. Open Notepad and simply save the open (empty) file as is, naming the file as "ssh" to the root of the sd card that you just created. This is to enable ssh in the RasPi.

2. Safely remove the SD card and insert the SD Card into the RasPi slot, power it on and plug in the LAN cable.

3. Open your Wireless connection from the Network and sharing center 

4. Click Properties and switch to the Sharing tab. Check the two values as in the below screenshot:

5. A prompt to associate a private connection to share will appear. Select "Local area connection" from the Select Private connection drop-down menu. Click Ok.

6. Open the LAN connection properties. It should show something like this:

7. Download Putty and RealVNC and install them.

8. Open Putty and type "" in the hostname field under the Session menu item to the left pane in Putty. Note: Once you change the name, however, use the ip address of the pi to connect (applicable only with DHCP usage because upon reboot, the IP, to which the name points, may not remain the same).

Click Open.

9. Enter login as "pi" and password as "raspberry"

10. Type "sudo raspi-config". The below screen will help in starting the VNC server. Scroll to the "Interfacing options" with the help of the down arrow key as below:

 Press Enter.

11. Scroll down to "VNC" as below, press Enter and select Enable in the subsequent dialog box. Navigate to finish using the arrow keys and press Enter to return to the shell.

12. Start RealVNC from your Windows laptop and enter the hostname as before and you will have the VNC viewer displaying your RasPi desktop.

13. Most likely, a warning to change password will appear and the text may overflow beyond the screen.

14. You can set the Raspi configuration now using the GUI, as below!

15. Select Raspberry configuration and modify the screen resolution. A prompt to reboot the Pi may appear. Click Ok. The VNC server would shut down so close the VNC viewer in your Windows laptop and reopen it.

16. The new resolution may enable you to view the wifi information

17. Start the "node-red" server from the programs. Note the IP address with the port number. Type the address with the port number from your Windows browser and the speed with which node-red connects will surely make you straight away buy a led and fiddle with the GPIOs to set up your first IoT thing!

Happy IoT-ing!

Monday, 19 June 2017

Docker EE for AWS

Somehow, I struggled through Docker. I will outline my steps so that you don't have to follow the tedious path.

First I tried to install Docker on Windows.

Pitfalls: OS version. If you don't have the Windows 10 Pro edition, you can use the Docker toolbox only.

So, okay. Download the Docker toolbox.

And I checked the VirtualBox option for good measure and it did stand me in good stead because - believe this - the Windows Server image on the EC2 instance cannot have its Hyper-V/AMD something started because you can set the setting only in BIOS and on the EC2 instance, running on Remote Desktop (RDP), there is no way of accessing the BIOS, even if it made sense!

Below is the roadmap of the 1st attempt

Initially, I thought like this:                                                          Then, I thought like this:


2nd attempt

I went into the VM using the RDP and started the VirtualBox and gave Ubuntu as the image. Great! It started the VirtualBox but the EC2 instance had only 1 GB RAM and the VirtualBox script wanted 2 GB, by default!

Okay! Let me check if an EC2 instance with a Linux Ubuntu image (AMI) was available. It was but I did not want to get into the Linux OS shell.

So, what is the alternative?

Docker EE for AWS

A native AWS stack that could host a Docker container on a kernel all its own, with a set of Docker engines. No underlying OS but on top of the AWS IaaS services!

Sounds great but you really cannot see it working because when you try to access the public DNS via a browser, an Apache server message will greet you that the url you are looking for cannot be reached although the server is okay!

Docker EE for AWS is a one click template to quickly deploy Docker on Amazon EC2, says the Docker EE Store. And it is, if only the template stayed put in its AWS region. It seems that the tricky part has not been documented.
The KeyPair that the template requires must be an EC2 generated Keypair from the same region in which your EC2 instance is!

Finally, the aws CloudFormation Stack gets created.

You can use this in your Docker EE deployment to AWS