Tuesday, December 10, 2019

[PythonOOPTutorial] 04 Object Oriented Programming Using Python

This is the forth article of the Python OOP Tutorials.
If you are new to Python, this is how to install Python on your computer.

When it comes to Object Oriented Programming, it has four main concepts:
  • Abstraction
  • Encapsulation
  • Inheritance
  • Polymorphism
Lets discuss those one by one.



ABSTRACTION:


Abstraction is simply hiding the real complexity of the code from the user. It hides internal implementation of the code from user; only required parts will be exposed.

For ex:

Without having any idea how engine works or ignition works, a driver can start a car. He do not need to know how battery is supplying power, how engine works, how fuel is pumping, etc... Same thing is applied here.


Above is a simple example for abstraction. If we go into more details most suitable example for abstraction can be explained using Abstract classes. You can learn more about Abstract classes from here.

For Ex:

Consider we have an abstract class Vehicle and extended class Car. In vehicle we write an abstract which is common to all the vehicles - "start" method. So we can implement that method in extended class as we want.



ENCAPSULATION:


Main idea of encapsulation is wrapping the attributes. We can use this to restrict unintentional data modifications.

For Ex:

Consider there is a Student class and a private attribute called age. So age should be always a positive value. So we can implement that as follows using object oriented programming concepts.
Here when user enters 16 as age, system allows that to be saved because 16 is a valid age. But when user enters -3 as age, system do not allows that to be saved because -3 is not a valid age; so age will remain as 16. After that when user enter 21 as age, system allows that to be saved because 21 is a valid age. So like that we can do so many things using OOP concept encapsulation.



INHERITANCE:


Inheritance is process of deriving parent class (super class) attributes and methods to its child classes (sub classes). In real life examples, we will have our parents' qualities, behaviours and features; those are inherited from parent to child. Same thing happens in Python too. When we extend a class (child class) from another existing class (parent class).

Please note private attributes and members are not inherited to child classes; because they are private. Only public and protected attributes and methods are inherited to child classes. If you want more information about Python access modifiers please refer this.

When it comes to Python Inheritance, there are two major levels:
  • Single inheritance
  • Multiple inheritance

Single Inheritance:

Basic way to define Car class is like below.
class Car:
But when we want Car class to extend Vehicle, we have to change it like below.
class Car(Vehicle):

Here Vehicle is the parent class which has two public and protected attributes. Car class is extending Vehicle class. So as I mentioned in the screenshot, vehicle attributes are accessible even from car class objects. That is because Car class is extending Vehicle class. Same thing goes with methods too.

Multiple Inheritance:

What we have discussed above is Single inheritance (those child classes inherit only one parent class). So Multiple inheritance means there can be child classes which are extended by multiple parent classes. 

A child may inherit skills from both his mother and father.
Above example describes how basic multiple inheritance will work using attributes. Here "canCook" attribute is inherited from Mother class and "canSwim" attribute is inherited from Father class.
Same applies to the methods too.

Then let us consider if both classes have the same attributes how Python is working.
Here system prints "Baby can dance". When there is a conflict among attributes or methods while multiple inheritance, system will act according to the first parent class. At this case it it Mother class.
class Baby (Mother, Father)
If we change the order of inheritance like class Baby (Father, Mother) system will not print "Baby can dance".


In addition to those there is a concept called "Multi-level Inheritance".

For Ex:

Consider below HondaCivic class; it is inherited from Car class; it is inherited from Vehicle class. This is called Multi-level inheritance and the last class at the hierarchy can access all super class attributes and methods.


POLYMORPHISM:


Polymorphism is simply having multiple formats. When it comes to Object Oriented Programming polymorphism, it has two sub concepts;
  • Overriding
  • Overloading

Overriding:


Overriding is directly related to inheritance; user can override something which is defined in super class (parent class).
Here Vehicle class has a start method. When we create a Vehicle object and call start method it will print "Vehicle starting..!!". 
Then we create a Car object and call the start method; but Car class do not have a start method. One important thing, Car class extends Vehicle class. So it can access its parent class methods. So Car object start method will also print "Vehicle starting..!!".
This is called Inheritance.

Here Car class also has a start method. When we call start method of  Car object, system will print "Car starting..!!". This is called Overriding. Here Vehicle object start method is overridden by Car object start method.


Overloading:

In normal OOP concepts Overloading means changing the input parameters without changing the method name. In Python in a single class we cannot have two methods with the same name. So for overloading we use a different technique.
Here at the Calculator class we have a method called "getSum". We can call that method with two parameters and with three parameters. When we call it with two parameters third parameter "c" will be initialized as zero (as we defined). This is called an "Optional Parameter" in Python. So nothing will happen to the summation flow. If we pass three parameters to the method, then it will return the sum of three parameters.
Like that if we want to take the multiplication value we have to initialize value of "c" to one (c=1) to get the correct answer. So always you have to initialize the optional values carefully. Otherwise you will not get the expected outcome.


So these are the basic Object Oriented Programming concepts using Python. Hope this is useful for you.


Cheers...!!

Saturday, December 7, 2019

[PythonOOPTutorial] 03 Python Access Modifiers

This is the third article of the Python OOP Tutorials.
If you are new to Python, this is how to install Python on your computer.

Basically access modifiers are some sort of keywords used to control or limit the accessibility of attributes, methods, functions, classes, etc... Simply access modifiers define the scope of the components. When it comes to Python there are no specific keywords to define access modifiers; but the number of underscore parameters handles that.

In Python there are 3 access modifier levels.
  • public (no underscore parameters as prefix)
  • private (two underscore parameters as prefix)
  • protected (one underscore parameter as prefix)
Lets learn about those using Python attributes.


Public access modifier:

When an attribute is public, that can be accessed from each and every class. Simply it is visible for all the classes.
There are no underscore parameters as attribute name prefixes.


Private access modifier:

When an attribute is private, that can only be accessed within the same class. When we try to access a private attribute from outside the class it will throw an AttributeError error.
To make an attribute private, we use two underscore parameters as an attribute name prefix.
Since private attributes are not accessible from outside the class, we can write a public method inside the same class to access the private variable.


Protected access modifier:

When an attribute is protected, that can be accessed from the same class or from a sub-class of that class. When we try to access a protected attribute from outside the class which is also not a sub-class it will throw an AttributeError error.
To make an attribute protected, we use one underscore parameter as an attribute name prefix.
From this example you can also understand how to create a sub-class using a super-class. Here class Dog is created by extending the class Animal.


So now you are aware of how to handle access modifier of the Python.
Lets discuss about OOP with Python from the next article.


Cheers...!!

Sunday, November 24, 2019

[PythonOOPTutorial] 02 Python Classes, Attributes, Methods and Functions

This is the second article of the Python OOP Tutorials.
If you are new to Python, this is how to install Python on your computer.

In this tutorials we assume you have some basic experience in programming.


CLASSES

Below is how to define a class in Python.

Here "Animal" is the class name. Currently we do not have any attributes or methods in it. So we write "pass" inside it. You can find more about Python empty classes from here. At the end we have created an object using out empty class.
We use the keyword "class" before the class name and ":" symbol after the class name based on Python standards.

You can find more about Python naming conventions from here.


ATTRIBUTES

Classes can contain different attributes which describe the class.
In Python we do not need to define the type of the attribute; it is not mandatory.

Below is how to define attributes and assign values for attributes in Python.

To comment a line you can use "#" symbol in the beginning of the line

When it comes to attributes, there are two types of attributes.
  • Class Attributes - Value common for all the instances of the class
  • Instance Attributes - Values changes based on the instance
If you need more details about Class attributes and Instance attributes please refer this.


METHODS

Classes may contain different methods which serves an specific task.
Below is how to define methods in Python.
Here method name is "sound". We use keyword "def" before method name and ":" symbol after the method name. As the default input parameter we have to pass the object which invokes the method as an input parameter "(self)".

Python has methods and functions. We have to keep that in mind carefully. You can find out more about those differences on methods and functions from here.

When it comes to Python methods;
  1. Method is always associated to an object.
  2. The object which invokes the method, will be passes as a parameter to the method.
  3. Returning data from a method is optional.
There are two types of methods in Python.
  • Instance Methods
  • Static Methods
If you need more details about Instance methods and Static methods, please refer this.


FUNCTIONS

Functions are almost like Methods, but those are not associated with objects. You can define a function in the same way as a method. Functions do not need default input parameters; input parameters are optional for functions.

Here we have a function called printCountry which has an input parameter which is optional. Additionally we have assign that input parameter a value. It will be used when we call the function without passing a parameter.

First we call our function with the argument "India". So it will assign India to the input parameter and print Country name is India.
At third function call we do not pass any argument to the function. So it will assign Sri Lanka to the input parameter and print Country name is Sri Lanka


Those are the fundamentals of Python required for OOP (Object Oriented Programming).
Lets discuss about OOP with Python from the next article.


Cheers...!!




More about Class attributes and Instance attributes..

For example consider we have a class called Animal and it has a Class attribute called numberOfLegs.
So when we assign a value for a class attribute it is same across all its instances.


To change the value we have to access it with the class reference; not with the object reference. If we try to change the value of the class variable using object reference, it will create a Instance attribute with the same name.
Here initially there are no instance attributes called numberOfLegs. So animal1 and animal2 objects will display the value as 4. 

Then we change the value of numberOfLegs for animal1 object. So what will happen is instance attribute will be created for animal1 object and its value will be 3.

When we ask for the value of any attribute; system will check whether there is any instance attribute with the same name; if a matching instance attribute found, system will return that value.
If there is no matching instance attribute found, then system will check for class attributes.

At animal1 object, system will find the instance attribute and return its value 3.
At animal2 object, system will cannot find a instance attribute and then checks for a class attribute. Then system find the class attribute and return its value 4.


More about Instance methods and Static methods..

Usually we write methods inside classes where at least one input parameter is required are called Instance methods. Static methods are bit different from instance methods.
On static methods we do not need the default self parameter. To make a static method we have to use the @staticmethod annotation before the method definition. So that will make a static method.
We can call static methods by the class reference too.

Ex: here we have called the static method by creating an object reference. 
animal = Animal();
animal.staticMethod1();

Other than that, we can directly call the static method using the class reference too.
Animal.staticMethod1();
This will also provide the same output.

Saturday, November 23, 2019

[PythonOOPTutorial] 01 How to Setup Python

This is the first article of the Python OOP Tutorials.

First you can check whether you have already installed python on your computer by executing following command.

"python --version"


If python version is displayed, then it says you have already installed Python on your computer. Otherwise you can follow below steps to install python on your computer.

You can download Python setup from Python.org. From there it is always better to download the latest version.


If you want to download a previous version you can choose those from the "Downloads" section. (Python downloads page)

There are different download types and you can select what is most suitable based on your operating system and requirement. What I prefer is exe version.

Then run the downloaded exe file. Do not forget to check the "Add to path" checkbox. Otherwise you will have to add Python for the PATH manually. 

After successful installation you can run the previous command again and check for the current Python version check by the command "python --version". It should work now.To make sure Python is working fine, you can download below file and run it.Sample file: https://drive.google.com/open?id=1vHIAmN7cpfqjJNESmY-23ABSE9rm5THZ- Open command prompt.
- Navigate to the file saved location.
- Run "py TestPython.py"
Then you will get output like below.
Now Python is working fine in your computer. Lets continue the learning process.
Cheers...!!


Wednesday, August 7, 2019

Canary Tokens (Traps) – Expose Information Leaks

Canary Tokens are traps to identify information leaks. Using canary tokens you can identify who has accessed your secret files (timestamp, geographical location, etc...)

You can generate canary tokens in different formats. To generate canary token you can visit below websites.


>> Lets start with the first website

  • First you have to select the token type from the drop-down. For our easiness select "MS Word" option.
  • your email address (when file is accessed, notification will be sent to this email address)
  • some text to identify the token

Then download the generated canary token. When you open the file, you will get an email notification as below (you can edit the content of the downloaded MS Word file as you wish. You can include some false information and an attractive file name such as "my_passwords". So hackers will surely open that file and you will get their information)



When you open the email alert you will see some details of the file access.


By clicking on the "Manage this Canarytoken" you can manage the future email notifications for this canary token.
By clicking on the "More info on this token" you can see details access information.



>> If you visit the second website first you have to enter two fields.

  • your email address (when file is accessed, notification will be sent to this email address)
  • some text to identify the token

After clicking on "Generate Token" button you will see what are the available options to generate canary tokens. Below are some of the token types available.

  • Web bugs
  • DNS Tokens
  • SMTP Token
  • Remote Image
  • QR Code
  • SQL Server Alert on SELECT, UPDATE, INSERT, DELETE
  • MS Word
  • Acrobat Reader PDF
  • SVN Token
  • Signed EXE / DLL
  • SecretKeeper Token
  • Windows Directory Browsing

For this example you can select "MS Word" and download the canary token file. When you open the file, you will get an email notification as below (you can edit the content of the downloaded MS Word file as you wish. You can include some false information and an attractive file name such as "my_passwords". So hackers will surely open that file and you will get their information)


When you open the email alert you will see all details of the file access.


When you visit the provided url at the bottom of the email, you will be able to see the file access history too.

Using MS Word files is just a one way to use canary tokens. There are lots of options available and you can use those wisely.


Cheers...!!

Thursday, September 6, 2018

AWS SQS Standard Queue vs FIFO Queue

When it comes to cloud computing, AWS plays a major role as a IaaS (Infrastructure as a Service), PaaS (Platform as a Service) and SaaS (Software as a Service). Amazon Web Service (AWS) is a well known public cloud service provider. You can find out more about AWS from https://aws.amazon.com/

Simple Queue Service is one of the most useful service which is provided by AWS. If you are using AWS then no need to waste your time on implementing queues. AWS has done that part for you. You just have to use it. You can find more details from https://aws.amazon.com/sqs/

AWS SQS provides two types of queues.
  • Standard Queue
  • FIFO Queue (First In First Out queue)

Comparison of Standard and FIFO queue types are as below.

Standard Queue FIFO Queue
Available on all AWS regions Available in the US West (Oregon), US East (Ohio), US East (N. Virginia), and EU (Ireland) regions
Unlimited Throughput Support up to 3,000 messages per second
Message delivered at least once, but occasionally more than one Delivered once
Not execute in order they sent First in first out
Good for high throughput scenarios Recommended when order of the event is important


Hope this will help you too.


Cheers...!!

Wednesday, August 16, 2017

Solve unrelated svn blames while building projects with maven

Previously I have used Subversion as the version control system. Then moved the projects to Git. Converted all the Jenkins build jobs accordingly. But while building some sonar jobs got below error.

[ERROR] Failed to execute goal org.codehaus.mojo:sonar-maven-plugin:2.6:sonar (default-cli) on project YourTestProject: The svn blame command [svn blame --xml --non-interactive -x -w --username ******** --password ******** src/your/test/project/code/path/Main.java] failed: svn: 'src/your/test/project/code/path/' is not a working copy -> [Help 1]

Since we are using Git and so no need of any svn blames. Using below parameters disabled svn activities.

-Dsonar.scm.disabled=True

Hope this will help you too.


Cheers...!!