Operator Overloading in Python

In this article, we’ll learn about operator overloading in Python with examples.

We all know what are operators (+, -, <=). In python, operators work for built in classes, but some operator behaves differently with different types. For example ‘+’ operator can add two numbers and also can concatenate two strings.

Program:

Output:

30

Program:

 

Output:

hello programmer”

 

So, using any operator to perform different operation that are not usually performed, is known as operator overloading. We can change the behavior of operators using operator overloading.

Or we can also say that “assigning a different work to an operator is known as operator overloading”.

To perform operator overloading, there are some magic methods provided by Python. Using these methods we can perform any operation we want on a operator.

The operators that can be overloaded are as follows:

OperatorsMethods
+__add__(self, other)
__sub__(self, other)
*__mul__(self, other)
//__floordiv__(self, other)
/__div__(self, other)
%__mod__(self, other)
**__pow__(self, other[ , modulo])
<__lt__(self, other)
<=__le__(self, other)
==__eq__(self, other)
!=__ne__(self , other)
>=__ge__(self, other)

The basic idea to perform the operator overloading in python is to define any of these methods in the class then call them using operators.

Let’s see an example. Suppose we want to overload  ‘+’ operator.

As mentioned above that we can concatenate two strings and add two numbers with the help of ‘+’ operator but here we’ll perform the addition on two objects of a class named as Test.

 

Program:

Output:

TypeError: unsupported operand type(s) for +: ‘Test’ and ‘Test’

So there is an error that says that we can’t perform addition to add values of the both the Test class’s objects. So we will define the __add___(self, other)  method  here to add the values of both the objects.

Program:

 

Output:

11

So what we did in above program is just added a __add__(self, other) method that will be called when we use ‘+’ operator on the objects of the Test class.

It is not necessary to always perform addition on ‘+’ operator we can do anything we want like we can also subtract object’s values by using operator ‘+’.

In this way you can achieve operator overloading in python. Comment below if you have any doubts.

StringBuffer To byte Array Java Example

StringBuffer To byte Array Java Example

This example shows how to convert StringBuffer to byte Array in Java.

 

 

Git : A beginners Guide

Git:

I am back after a long time. :). In this article i am going to talk about Git. I faced some problem when i started using git hence i am writing this article to provide you some basic stuff about Git to start with.

 

In this article i will cover below aspect of Git

  1. Get code from remote repository(In this case GitHub)
  2. Create a new local branch
  3. Ignore unnecessary files
  4. Add & Commit new/changed files to newly created local branch
  5. Push changes to remote repository
  6. Pull/Merge code from Remote repository

Git Setup

In order to work with Git. First you need to setup Git on your local system(PC/MAC). Please download and install appropriate Git based on your OS. Or follow the guide as mentioned(Linux).

Once done with installation, check if everything works properly by checking git version on terminal.

You should get an output like below.

Git Clone

Once git setup is done. Next step would be to get code from remote repository. In this situation from Github. You can execute clone command for this purpose.

Without authentication

With Authentication

Example

Output

Git Branch

Unless specified explicitly default branch will be master. In normal situation you wont make changes directly to master branch. So first step would be to create a branch and commit all the changes into that branch and push the changes to remote repository.

To check what is the current branch for the code you cloned, execute “git branch”. It will show below output.

To create a new branch use “git branch branch_name”.

Check status again by executing “git branch”.

Here * means current branch is master. To switch to newly created branch execute

Check status : “git branch”

Now * is pointing to jbt_branch and not master. So now what ever you commit in the code will go into this new branch and not master.

Git Ignore

Now your code is pointing to new branch and you are ready to commit the changes. But there are unwanted file which you don’t want to commit. And you want git to ignore all these files while committing. You need to create .gitignore file containing list of files that needs to be ignored.

You can create this file globally or locally. When you know that certain type of files needs to be ignored all the time irrespective of repository create this file in home folder of user, else create in current workspace.

I have done some changes in the code i checkedout. See all the files that are modified by executing below code.

In my case output is like this

I have done changes only in one file(ApplicationStart.java), but git displayed one more file spring-boot-sample.iml(modified coz of IDE), which i don’t want to be checked in. So i will created .gitignore file with this file name.

Once created, check status again

As you can see git is ignoring other file now. And if you commit file now only one file will be committed.

Git Commit

Next step would be commit the changes using command “git commit“. But if you execute this command git will complain.

So first you need to add the file to stage by running “git add .“. It will add all the file in stage. Now check status.

It is now added stage and color is changed to green. Now commit the code.

Git Push

Use “git push” to push the changes to remote repository. But if you do that you will get below exception.

Reason is branch is only created locally and it is not available in remote repository. Execute below command to push everything to remote.

Git Pull/Merge

In case you want to get latest code from remote repository use “git pull“.  And if there is any conflict between remote and local repository you can use “git merge” to merge the changes and then commit the changes to remote.

[ad_2]

Java StringBuffer setLength Example

 

Google Pixel 2 Portrait Mode working on Android devices with Camera2 API

Hello everyone, today I got one good news, that right now on my lenovo Zuk Z2 I can use Portrait Mode on a modded Google Camera, just like we have Portrait Mode in iPhones and top range android phones.

 

I clicked this pic with my Zuk Z2 Plus

 

How to install:

  1. Download the modified Gcam app .
  2. Install the apk file.
  3. Set the following in app settings:

Model: Nexus 6

Portrait mode on all models: on

camera.af.debug.show: off

camera.faceboxes: on

Currently it’s working just for Rear Camera, your Phone needs to Camera 2 API, which is in most of the Oreo+ devices.

Once you’ve done that, portrait mode and HDR+ should be a lot less prone to crashing on your Exynos-based smartphone. (Note that your handset has to support the Camera2 API)

Portrait mode’s just scratching the surface of the Google Camera port’s capabilities, of course. You’ll also enjoy the ability to capture RAW photos and compressed photos simultaneously, zero shutter lag (ZSL), and 4K video recording (if your phone’s hardware supports it).

 

Source: XDA

 

Normalization in DBMS – 1NF, 2NF, 3NF and BCNF

Here you will learn about normalization in dbms with examples.

What is Normalization in Database?

Most of the projects and websites contain lot of information. They store the data in tables. Since lot of data therefire database contains so many tables. In order to retrieve some information we must go through number of tables which leads to complex quires.

We may think that the solution to avoid these complex quires is instead of number of tables store all information in one big table and retrieve required information from it. It works fine but in this case table will contain redundant information. And also arises other anomalies. They cause problems when inserting new data, updating and deleting. i.e if we try to delete some field in database due to mixing all data along with this data, some other data may lost. Same problem when updating, other fields also may update since all are in same table. And inserting data may give, so much of redundant data.

To avoid these problems of inserting, deleting and updating data, divide the tables as small as possible. The ideal size of the table is two attributes. To make this way we should follow some rules for dividing tables. First test each table whether it’s design is good or not. If not good go for division (split the table). This process of splitting tables is called Normalization.

So, Normalization means split the tables into small tables which will contain less number of attributes in such a way that table design must not contain any problem of inserting, deleting, updating anomalies and guarantees no redundancy.

 

To do normalization we use concept of “Functional dependency” and “Candidate keys”. Using these concepts we check whether table design is good or not, if not good we go from Normalization (splitting the table).

Prerequisite

For better understanding of this concept you should know about:

  • What is functional dependency
  • How to find candidate keys
  • How to find closure
  • Basic knowledge about all keys

Without these also you can understand what is 1NF, 2NF, 3NF, BCNF. But to work with more complex problems you should know those concepts.

Our final aim is that, after normalization, whatever the functional dependency (FD) applicable on table (let FD is x -> y), the left hand side is always must be a key (here x). This final form is called BCNF

BCNF guarantees zero (0) % redundancy.

To go to BCNF first we have to check whether the table is in 1NF, 2NF, 3NF and then BCNF.

Note: Any Normal form aim is that to reduce redundancy and to avoid anomalies.

Normalization in DBMS

First Normal Form (1NF)

First Normal Form says that table is flat i.e in table there must no multi-valued and no composite attributes.

Example:

IDNameCourse
1NeerajC1
2PankajC1, C2
3PawanC3

In above case Pankaj has two courses C1 and C2, so Course is multi valued. Hence it is not in 1NF.

Below table in is in 1NF.

IDNameCourse
1NeerajC1
2PankajC1
2PankajC2
3PawanC3

We need not worry about this, because while converting ER model (diagram) to relational model (table), we follow rules that they guarantees all attributes free from multi-valued and composite.

So any relational table by default is in 1NF.

Second Normal Form (2NF)

Second Normal Form says that, if candidate key containing more than one attribute then any part of that key (called partial key) should not determine anything.

Example:

RollNumStuNameCorNumCorNameResult
1Neeraj3DBMSA
2Venkat2OSC
3Phani1FLATB

Functional dependencies are:

  1. The attribute student name functionally depends on roll number. So RollNum -> StuName
  2. The attribute course name functionally depends on course number. So CorNum –> CorName
  3. The attribute Result depends on Roll number and Course number. So RollNum, CorNum -> Result

We can find that RollNum, CorNum  combine form as candidate key.

Here we can see that a part of candidate keys are deriving other things which we called partial dependency. FD1 and FD2 are partial dependencies.

So this table is not in 2NF. To convert into 2NF find the closure where problem occurred and split the table with that result.

RollNum+ = { StuName } ;;;  CorNum+ = { CorName } ;;;

These two should be separate tables and generally with candidate key other table formed.

Resultant tables which are in 2NF:

Table 1:

RollNumStuName
1Neeraj
2Venkat
3Phani

Table 2:

CorNumCorName
3DBMS
2OS
1FLAT

Table 3:

RoNumCorNumResult
13A
22C
31B

For table 1 candidate key is RollNum, for table 2 candidate key is CorNum since these tables has a single prime attribute we can say these two are in 2NF. Coming to the third table candidate key is RollNum and CotNum combine. But on this table there is only one functional dependency is existing. So this is also in 2NF.

i.e. 2NF is based on Full Functional Dependency. No partial keys are allowed. So in 2NF we checked for partial dependency and eliminated.

Important Note: Whenever you find a part of key on left hand side of FD, don’t confirm that it is partial dependency. Check right side also if right hand side is non-prime attribute then only it is partial dependency. If right hand side also prime attribute it is not a partial dependency.

Third Normal Form (3NF)

Third normal form says that there is no “Transitive Dependency”.

We know the rule of transitivity that, If A -> B and B -> C then A -> C. We can find the transitive dependency FD’s in such a way that, “Non-prime attribute derive something”. If any FD is like this we can say that it has Transitive dependency and we need to eliminate it to make it into 3NF.

We can check 3NF in other way also, formal definition of 3NF is:

Definition: A relational schema (table) is in 3NF if and only if every non trivial FD X -> Y

Either X is a super key or Y is a prime attribute (it is part of some candidate key). If this definition follows there is no chance of transitive dependency.

Example:

Student Table

StuIDStuNameRollNumClassCodeClassName
1Mishra12CS1Lect.Hall
2Amit14CS2Lab
3Jack16CS3Theorey

Functional dependencies:

  1. StuID -> StuName, StuName, RollNum, ClassCode
  2. ClassCode -> ClassName

Here StudID is candidate key which can able to derive everything. So one and only prime attribute is StuID only. But we can see that 2nd FD i.e ClassCode -> ClassName in this ClassCode is a non-prime attribute which is deriving something. So this is not in 3NF.

To convert into 3NF find the closure where problem occurred to split the table.

ClassCode+ = { ClassName };

Resultant tables after splitting are

Student Table

StuIDStuNameRollNumClassCode
1Mishra12CS1
2Amit14CS2
3Jack16CS3

In this table StuID is candidate key and only one Functional dependency existing which is StuID -> StuName, RollNum, ClassCode. So there is no problem this is in 3NF.

Class Table

ClassCodeClassName
CS1Lect.Hall
CS2Lab
CS3Theorey

In this table ClassCode is candidate key and only one functional dependency existing which is ClassCode -> ClassName. So this table is also in 3NF

Boyce Codd Normal Form (BCNF)

To make sure zero % redundancy two scientists Boyce and Codd invented this BCNF. In BCNF result each FD determinants (left hand side attribute) must be a key.

Definition: A relational schema R is in BCNF if whenever a non-trivial FD X -> Y , X should be a super key.

Example:

IpAddPortNumProcessReq
10.4.9.3480Register Application form
10.11.4.99110Gmail message request
10.1.11.11125Remote User request

Functional dependencies exist on this table are:

  1. IpAdd, PortNum -> ProcessReq
  2. ProcReq -> PortNum

Applying normalization means converting into BCNF. For that we first check 1NF, 2NF, 3NF.

By default every relational schema is in 1NF.

Before proceeding to next normal forms, we should find candidate keys. If we find candidate keys we get { IpAdd, PortNum } and { IpAdd, ProcessReq } are candidate keys. So prime attributes (part of candidate keys) are IpAdd, PortNum, ProcessReq. As per formal definition of 3NF, if right hand side has prime attribute, it is enough to say that it is in 3NF. Since all attributes are prime attributes we can say that table is in 3NF also. If already in 3NF, no need to check 2NF. So up to 1NF, 2NF, 3NF all are fine.

Now check for BCNF. According to the definition of BCNF left hand side should be key. So FD IpAdd, PortNum -> PorcessReq . Therefore AB is a key there is no problem.

Other FD PorcessReq -> PortNum, here this FD not deriving all attributes, since it’s not deriving everything ProcessReq is not a key. We can say that it is not in BCNF.  To make it into BCNF,

ProcessReq+ = { ProcessReq, PortNum } is a separate table.

PortNumProcessReq
80Register Application form
110Gmail message request
25Remote User request

And { IpAdd, ProcReq} is other table.

IpAddProcessReq
10.4.9.34Register Application form
10.11.4.99Gmail message request
10.1.11.111Remote User request

On table PortNum, ProcessReq, Functional Dependency is ProcReq -> PortNum, here ProcessReq is key, so satisfies BCNF. And on table IpAdd, ProcessReq, { IpAdd, ProcessReq } itself a key, so it also is in BCNF. But here we lost FD, { IpAddr, PortNum } -> ProcReq so called this is a not functional dependency preserving result even it is in BCNF.

Finally this BCNF guarantees that there is no redundancy and no problem of anomalies of inserting, updating and deleting.

Comment below if you have queries or found any information incorrect in above tutorial for normalization in dbms.