Wednesday, November 14, 2018

TypeScript - Classes - Getters and Setters

In TypeScript there is a away to set a private variables using the getters and setters. You can refer the following link for its implementation:
https://www.typescriptlang.org/docs/handbook/classes.html#accessors

Below is a sample implemenation of accessing and modifying private variables in a class using the get and set keywords. Do notice that the private variable (_brand) is prefixed with a single underscore.

// Getters and Setters

class Motorbike {
private _brand : string = "TVS";

get brand(){
return this._brand;
}

set brand(value:string){
if (value.length > 3) {
this._brand = value;
} else {
this._brand = "TVS";
}
} // end set
}

let myBike = new Motorbike();
console.log(myBike.brand);


Let us now compile the file:

$ tsc
$

To check the console:

$ python -m SimpleHTTPServer 3000

Checking the console output:

TVS


Reference:

Tuesday, November 13, 2018

TypeScript - Classes - this keyword - constructors

Now, in the below example, elevation is a protected variable.

class Visakhapatnam extends City{
name = "Visakhapatnam";
protected elevation = 45;

constructor(){
super("Visakhapatnam");
}
}

We can also modify it inside the constructor by using this keyword. 

class Visakhapatnam extends City{
name = "Visakhapatnam";
constructor(){
super("Visakhapatnam");
this.elevation = 45;
}
}

const cityVisakha = new Visakhapatnam();
console.log(cityVisakha);
console.log(cityVisakha.name);
cityVisakha.setType("Town");
console.log(cityVisakha.printType());
console.log(cityVisakha.getElevation());
console.log(cityVisakha);

Now, if i compile:

$ tsc
$

and .. check in the console.  

Console Output:

Visakhapatnam elevation: 45name: "Visakhapatnam"type: "Town"__proto__: City
city-App.ts:138 Visakhapatnam
city-App.ts:109 new type = Town
city-App.ts:113 Town
city-App.ts:140 undefined
city-App.ts:104 45
city-App.ts:141 undefined
city-App.ts:142 Visakhapatnam

We can see, that using the this keyword - the elevation was updated. Note that we cannot update the type using the this keyword. This is because, private properties are not accessible using the this keyword in the child classes.


TypeScript - Classes - Inheritance and constructors

Now, in my Inherited class I can create its own constructor overriding the constructor in the parent class:

class City {
public name:string;
private type: string;
protected elevation: number;

constructor(name:string) {
this.name = name;
this.type = "city";
this.elevation = 505;
}

setElevation(elevation:number){
this.elevation = elevation;
console.log("new elevation = "+this.elevation);
}

getElevation(){
console.log(this.elevation);
}

setType(type:string){
this.type = type;
console.log("new type = "+this.type);
}

printType(){
console.log(this.type);
}

}

const city = new City("Hyderabad");
console.log(city);
city.getElevation();
city.setElevation(506);
city.printType();
city.setType("Metro");
city.printType();


class Visakhapatnam extends City{
name = "Visakhapatnam";
protected elevation = 45;

constructor(){
super("Visakhapatnam");
}
}

const cityVisakha = new Visakhapatnam();
console.log(cityVisakha);
console.log(cityVisakha.name);
cityVisakha.setType("Town");
console.log(cityVisakha.printType());
console.log(cityVisakha.getElevation());
console.log(cityVisakha);


As you observe, in the constructor of the inherited class - i call the super() function. The super function indicates that I am calling the constructor of the parent class. Since, the constructor of the parent class expects a input. Hence, we pass an input to the super("Visakhapatnam") function.

But, while instantiating the child class - we do not pass in any arguments. This is because, the constructor in the child class does not expect any input arguments. 


Ubuntu - Mount a ephemeral storage on AWS EC2 machine.

Consider a AWS EC2 machine which is accompanied by an ephemeral storage. Many times, this ephemeral storage is not visible to us. In such a situation, we have search for it and set it up manually. Following are the steps taken by me to identify and mount ephemeral storage on a EC2 machine.


Step 1: Identify the disk which needs mounting . For this we run the fdisk command:
ubuntu@ip-XXX-XXX-XXX-XXX:~$ sudo fdisk -l
Disk /dev/loop0: 87.9 MiB, 92164096 bytes, 180008 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/loop1: 12.7 MiB, 13324288 bytes, 26024 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/nvme0n1: 8 GiB, 8589934592 bytes, 16777216 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x9f3e4931

Device         Boot Start      End  Sectors Size Id Type
/dev/nvme0n1p1 *     2048 16777182 16775135   8G 83 Linux


Disk /dev/nvme1n1: 46.6 GiB, 50000000000 bytes, 97656250 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
ubuntu@ip-XXX-XXX-XXX-XXX:~$

Step 2: Once we have identfied our ephemeral storage which we want to mount. Next we identify the folder on which we want to mount it on. Since,we have decided to mount it on /opt . Now we will proceed with the following steps:

ubuntu@ip-XXX-XXX-XXX-XXX:/$ sudo mkdir opt

Step 3: Create filesystem on your volume (make sure you choose the correct volume because this creates a new file system on the volume):

ubuntu@ip-XXX-XXX-XXX-XXX:/$ sudo mkfs.ext4 /dev/nvme1n1
mke2fs 1.44.1 (24-Mar-2018)
Discarding device blocks: done                            
Creating filesystem with 12207031 4k blocks and 3055616 inodes
Filesystem UUID: 1e7f28c2-19b3-4a94-8689-4d9b75496eff
Superblock backups stored on blocks: 
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
4096000, 7962624, 11239424

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (65536 blocks): done
Writing superblocks and filesystem accounting information: done   

Step 4: Let us now, mount our volume:
ubuntu@ip-XXX-XXX-XXX-XXX:/$ sudo mount -t ext4 /dev/nvme1n1 /opt
ubuntu@ip-XXX-XXX-XXX-XXX:/$ mount -a
mount: only root can use "--all" option
ubuntu@ip-XXX-XXX-XXX-XXX:/$ df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            1.8G     0  1.8G   0% /dev
tmpfs           371M  716K  370M   1% /run
/dev/nvme0n1p1  7.7G  1.1G  6.7G  14% /
tmpfs           1.9G     0  1.9G   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/loop0       88M   88M     0 100% /snap/core/5328
/dev/loop1       13M   13M     0 100% /snap/amazon-ssm-agent/495
tmpfs           371M     0  371M   0% /run/user/1000
/dev/nvme1n1     46G   53M   44G   1% /opt
ubuntu@ip-XXX-XXX-XXX-XXX:/$ 


Step 5: If you want to preserve the mount after e.g. a restart, open /etc/fstab and add the mount to it

ubuntu@ip-XXX-XXX-XXX-XXX:/$ echo "/dev/nvme1n1 /opt auto noatime 0 0" | sudo tee -a /etc/fstab
/dev/nvme1n1 /opt auto noatime 0 0
ubuntu@ip-XXX-XXX-XXX-XXX:/$ 

Step 6: Make sure nothing is wrong with fstab by mounting all
ubuntu@ip-XXX-XXX-XXX-XXX:/$ sudo mount -a
ubuntu@ip-XXX-XXX-XXX-XXX:/$ 

Step 7: Check for the available disk size:
ubuntu@ip-XXX-XXX-XXX-XXX:/opt$ df -h .
Filesystem Size Used Avail Use% Mounted on
/dev/nvme1n1 46G 53M 44G 1% /opt
ubuntu@ip-XXX-XXX-XXX-XXX:/opt$ ls
lost+found
ubuntu@ip-XXX-XXX-XXX-XXX:/opt$ ls -l
total 16
drwx------ 2 root root 16384 Nov 13 08:45 lost+found
ubuntu@ip-XXX-XXX-XXX-XXX:/opt$ 





Reference:

Saturday, November 10, 2018

TypeScript - Classes - Inheritance

The main advantage of inheritance is to the developer. This is because, it enables code reuse. To inherit, we use the extends keyword. The following example the class Visakhapatnam extends the City class. Refer to it in the following link:
https://sashankexpresstech.blogspot.com/2018/11/typescript-classes-public-methods-and.html


class Visakhapatnam extends City{
name = "Visakhapatnam";
protected elevation = 45;

}

const cityVisakha = new Visakhapatnam("Visakhapatnam");
console.log(cityVisakha);
console.log(cityVisakha.name);
cityVisakha.setType("Town");
console.log(cityVisakha.printType());
console.log(cityVisakha.getElevation());
console.log(cityVisakha);


All the members of the City class got inherited by Visakhapatnam class. The constructor is also inherited by the child class. To modify the private and protected properties of the object. We need to use the set methods defined to modify them.


Reference:

  • https://www.typescriptlang.org/docs/handbook/classes.html


TypeScript - Classes - public methods and access modifiers

Now the question arises, If i have declared private and protected properties in a class. How can we modify them? The answer is, we modify them using public methods of the class.

In the below example, we have a class City in which, we have 2 private properties:
  • type
  • elevation
Okay, elevation here is protected. But, remember, protected property is a private property. It has an additional feature that it can get inherited by a child class.

class City {
public name:string;
private type: string;
protected elevation: number;

constructor(name:string) {
this.name = name;
this.type = "city";
this.elevation = 505;
}

setElevation(elevation:number){
this.elevation = elevation;
console.log("new elevation = "+this.elevation);
}

getElevation(){
console.log(this.elevation);
}

setType(type:string){
this.type = type;
console.log("new type = "+this.type);
}

printType(){
console.log(this.type);
}

}

const city = new City("Hyderabad");
console.log(city);
city.getElevation();
city.setElevation(506);
city.printType();
city.setType("Metro");
city.printType();


To access these two private properties, we have created 4 public methods.
  • 2 methods for setting value
  • 2 methods for getting the value of these properties
The public methods created here are:
  • getElevation
  • setElevation
  • printType
  • setType
The set methods accept one argument here - which modifies the corresponding private properties.
The get methods enable us to retrieve the values contained in the private properties.

Let us compile the class:

$ tsc
$

Console Output:

City {name: "Hyderabad", type: "city", elevation: 505}
city-App.ts:104 505
city-App.ts:100 new elevation = 506
city-App.ts:113 city
city-App.ts:109 new type = Metro
city-App.ts:113 Metro

We can observe here - how the properties are affected using the set methods. Also, the output from the get methods are also visible in the Console.

Friday, November 9, 2018

TypeScript - Classes - Introduction

To create a class, we need to use the class keyword. Consider the below example:

class City {
public cityName:string;
private type: string;
protected elevation: number;

constructor(name:string) {
this.cityName = name;
this.type = "city";
this.elevation = 505;
}

}

const currCity = new City("Hyderabad");
console.log(currCity);

Class helps us to create a blueprint for objects. This is a ES6 feature.

The public keyword makes the property available from outside the class. Here cityName is a public property.
The private keyword indicates private properties. That means, these properties are accessible from within the class.
We can create protected properties. Apart from being private, protected properties are also accessible from other classes which inherit from this class. That means, a child class has access to the parent properties. Basically, protected properties behave like private properties.

Note that we have setup these properties directly in the class body and not inside the constructor.

We can also provide a constructor function. We can also pass some arguments to it. In our example we have created a constructor function with a argument name.  It is assigned to the cityName private variable of the class City. To access the private variable inside the constructor, we use the keyword this. The this keyword is especially useful to resolve conflicts in names - especially when the name of the constructor argument and the name of the private property is the same.

To instantiate the class we use the new keyword. In our example currCity is the instance of the class City.



Reference:
https://www.typescriptlang.org/docs/handbook/classes.html