Since the last 5 days, I seffered a high fever. First two days I thought that I will recover within some moments. But it was totally wrong. The fever went too high and I felt pain in many parts of my body. After two days, I went to doctor and had a injuction. Even though the fever still remained. But today I felt like, “Okay, I am fine”.
Category: Uncategorized
Simple Test in Python
Testing is the process of knowing whether the code is successfully executed or not. In Python there is a inbuilt module unittest, which give us whether the Test condition is True or false.
Unit test
The test condition should be written as a separate program. The name of the program should be either test_filename.py or filename_test.py. Let’s write a program to find square of a number.
Save the file name as Square.py
def sq(n):
return n*n
Test code
To write the test code, first we want to import unittest module which is a inbuilt module.
import unittest
from square import sq
Class Testing (unittest.TestCase):
def test_sq(self):
r = sq(5)
self.assertEqual(r, 25)
Thus when we execute the text file, the output will be.
————————————————–
Ran test 1 in 0.00s
OK
Assert functions
Python has many inbuilt functions to test the functions. One of it is assert function. There are many functions like assertTrue, assertFalse, assertEqual, assertNotEqual and so on.
Testing Exception
Exception can be handled in Python using assertRaises function. In case of division by zero, we can raise the exception by.
self.assertRaises(ZeroDivisionError, Divnum, 0)
Test Coverage
Test Coverage method is used to detect untested part of the code. We can install it by simply typing
pip install coverage
Virtualenv
Virtual Environment or Virtualenv is a software which is used to install different versions of module, in which we can test our program.
Venv is the keywords which is used to create the virtualenv.
Installation and Usage
To create it, First change to the Virtual directory. Then type the following :-
$ cd virtual
$ python3 -m venv virtual1
To activate the created virtual1 environment type :-
$ source virtual1/bin/activate
Then the terminal will look like :-
(virtual1)[user@host]$
To deactivate this, simply type :-
(virtual1)$ deactivate
Pipenv
Pipenv tools are created by Kenneth Reitz in which these tools are used to manage the multiple virtual environments
To install this tool, type:-
$ python3 -m pip install –user pipenv
To exit from the Virtualenv simply type exit.
After using pipenv in your project, you can see that two files Pipfile and Pipfile.lock will have been created. You have to checkin so that other can create the same exact environment of yours.
Barbeque
Today, I went to Absolute Barbeques which is located in T Nagar along with my friends. I thought Absolute Barbeques will be more better than Coal Barbeques but it was totally wrong. The dishes in Absolute Barbeques is very less compared to the Coal Barbeques. But taste wise both are same.
After finishing our brunch, My stomach felt worst bad so that it started to pain. I can’t able to want. So we booked a car and reached Koyembedu. After that I reached my home through bus. Then also I felt the pain as I ate a lot. But overall, I really enjoyed the day.
Windows To Linux
First when I thought that linux will be hard to use but after switching to it, I almost forgotten about the Windows. I installed linux with dual boot in my system. Even though i have both the operating system, I often use Linux.
In linux most of the software all already pre installed. Each semester I had different programming languages. If I used windows, i need to install seperate software for different programming language. But in case of linux, it’s already pre-installed and no need to install seperately.
Switching to linux will not cost even a penny as it is a free OS. But in case of Windows, we want to buy the key.
Modules
The main purpose of the module is to split up the code for efficient use. It makes debugging simple, so that the errors can be easily identified. In case, if we write the program in the python Interpreter, the program gets lost as soon as we exit the terminal. But in case of the modules, all the programs are stored in the main memory.
Let create a program to add two numbers. For this first create a python file add.py and type the following program in it.
def sum(int a, int b):
int x = a+b
print(“The sum is %s” %x)
Thus after saving it, if we want to execute this program, first we want to import it.
import add
add.sum(5,7)
Thus when we execute it, we will get the output as 12.
A module can be declared inside another module in which it is said to be sub-module. Thus all the sub-module will be under the module which has a common name. The program which is importing the module can also access its variable too.
Python has provided with some of the module in which it deals with the Operating system. Some of the modules are os, platform, request.
>>> import os
>>> os.getuid()
200
>>> os.getpid()
2772
where getuid returns the id of the currently executing process and in which the getpid returns the parent executing process.
Class in Python
Class is a user defined datatype in which it consists of functions and data variables. Classes can be accessed by creating a object which is the instance of the class.
class className(parentClass):
This is the syntax for the class. By default, object is the parent class for all the classes.
Functions Inside Class
Functions can be declared inside the class by using a keyword self which is similar to this pointer. Function which is declared inside the class should have a arguement as a self.
class myFirstClass(object):
def set(self, name):
self.name = name
Creating Objects
For the above class, the object can be created by simply assigning the object name with the class name.
p = myFirstClass()
p.set(“Chandru”)
Constructor
Constructor is used to initialize a variable of the class. __init__() function is used to initialize the variable. Constructor is automatically invoked when a object is created.
class constructor(object):
def __init__(self):
print(“Object is created”)
Inheritance
Inheritance is the property of acquiring the base class property. In simple it is also defined as deriving the property of one class into another class. For deriving the properties of base class, the name of the class which has to be derived in the derived class has to be passed as the arguement.
class base(object):
def __init__(self):
print(“Base class”)
class derived(base):
def __init__(self):
print(“Derived class”)
d = derived()
Output:-
Base class
Derived class
Multiple Inheritance
This can be achieved by passing the multiple classes in which the properties of these classes has to be derived to the class as arguement.
class multiple(baseClass1, baseClass2):
Setters and Getters
class setAndGet(object):
def __init__(self, name):
self.name = name
p = setAndGet(“Chandru”)
print(p.name)
p,name = “Python”
print(p.name)
Output:-
Chandru
Python
Sem 3 Done
That’s it, I have successfully completed my 3rd semester. In this semester, I didn’t perform some of the exams well. This vacation, I have planned to Enjoy as well to complete the python which I had left in between.
Data structure Lab
Today, I written a exam on Data structures. I picked up a paper which has two questions. First question, it was easy and I done it soon. But the second is very hard. I thought I won’t receive any output. But working after 2 hours, I got the output.
Bigil
Today we went to Bigil movie. Initially we didn’t get tickets, but when we checked again and again, the tickets which are cancelled by someone has got. Flim was good with a nice story. Apart from this, day was as usual.