Python

Python Data Persistence – Package

Python Data Persistence – Package

The concept of the package takes Python’s modular approach to next level. A package is a collection of modules. All modules having the functionality of certain common features are stored in one folder. Python’s standard library contains some packages that are collections of modules. For example, the Tkinter package, which is Python’s implementation of the TCL/Tk UI toolkit, consists of several modules such as Tkinter. ttk, Tkinter.messagebox, tkinter.commondialog, and so on. The importlib is another Python package in the standard library that has multiple modules (importlib. ABC, importlib. util, importlib. machinery, and so on.) You can build your own package having multiple but related modules under one folder. Let us see how it can be done.

In addition to Python modules, the folder should have a special file named as__init__.py for it to be recognized as a package by the Python interpreter. This file is also useful to build a packaging list of functions from various modules under the folder.
So, let us build a package named ‘MyPackage’. First of all, create a new folder at some suitable location in a filesystem (for example c:\testpackage) and a subfolder under it. Name the subfolder as ‘MyPackage’. Go on to save the following module scripts in it.

Example

#addfunctions.py
def add ( num1 , num2 ) :
result=num1 + num2
return result
def adda11(*nums) :
tt1 = 0
for num in nums:
tt1=tt1+num
return tt1
#mathfunctions.py
def sum(x,y):
return x+y
def average(x,y):
return (x+y)/2
def power(x,y):
return x**y
#myfunctions.py
def isprime(num):
x=2
for x in range(2,num):
if num%x==0:
return False
else:
return True
def'iseven(num):
if num%2==0:
return True
else:
return False
def isleap(num):
if num%4==0:
return True
else:
return False

In addition to above, create an empty__init__. py in My package folder. The folder structure should appear as shown below (figure 3.17) :

C:\TestPackage 
| example.py 
| 
|_ __ _MyPackage 
addtunctions.py 
mathfunctions.py 
myfunctions.py 
__init___.py

Next, open the command prompt window while in the TestPackage directory and start Python. The child directory in it, MyPackage is recognized as a legitimate Python package because it does contain__init__.py, so you can import any module available in it. The usage is shown below:

C:\TestPackage>
python >>>

Example

>>> from MyPackage import add functions
>>> add functions.adda11(1,2,3,4)
10
>>> from MyPackage import functions
>>> functions . is prime (67)
True
>>> from MyPackage import math functions
>>> math functions.power(10,2)
100

What is more, it is possible to require a function by name from any module.

Example

>>> from MyPackage.math functions import power,
average
>>> power ( 5 , 4 )
625
>>> average ( 20 , 30 )
25.0

This is where__init__. py file plays an important role. It can provide package-level access to certain functions chosen from various modules. Let us modify the __init__. py script as shown below:

Example

#__init__.py
from. mathfunctions import average
from. addfunctions import adda11
from. myfunctions import isprime, iseven

Now, what does this__init__.py script do? It allows a function to be accessed by name of a package and not by name of a module which originally defined. To verify this behavior, save the following script as ‘example.py’ in the parent folder (‘testpackage’)

Example

#example.py
import MyPackage
ffcalling isprime function
num=int ( input ( 1 enter a number . . ' ) )
retval=MyPackage . isprime ( num )
if retval==True:
print ( " { } is a prime number " . format ( num ) )
else:
print ( " { } is not a prime number " . format ( num) )
#adda11 function
result=MyPackage.adda11( 10 , 20 )
print ( " Result of addition : " , result )

Output

C:\TestPackage>python 
example.py enter a number..23 
23 is a prime number 
Result of addition: 30

In the above discussion, we learned how to create a package and use it from inside its parent folder. It is also possible to install the package for system-wide use. However, this book’s scope doesn’t cover its explanation. At this juncture, it is sufficient to state that it can be achieved by using a setup tools module from Python’s standard library. You can also upload your package to the Python Package Index repository (https://pypi.org//). The procedure is explained in detail on the website itself.
However, it is important to know how to install a new package in your computer’s Python library. The latest versions of Python come with pip utility.

Python Data Persistence – Package Read More »

Python Data Persistence – Math Module

Python Data Persistence – Math Module

As you would expect, the math built-in module is one of the most frequently used. Various functions which are often required are defined in this module. Functions for computing all trigonometric ratios are present in the math module. All these functions calculate the respective ratio of an angle given in radians. Knowing that 30 degrees are roughly equal to 0.5236, various ratios are calculated as follows:

Example

>>> import math
>>> math.sin(0.5236)
0.5000010603626028
>>> math.cos(0.5236)
0.866024791582939
>>> math.tan(0.5236)
0.5773519017263813
>>>

The math module also has two functions to convert the measure of an angle in degrees to radians and vice versa.

Example

>>> math.radians(30)
0.5235987755982988
>>> math.degrees(0.52398)
30.021842549264875
>>>

Two important mathematical constants are defined in this module. They are Pie (π) and Euler’s number (e)

Example

>>> math.e
2.718281828459045
>>> math.pi
3.141592653589793
>>>

We have used sqrt ( ) function from math module. Likewise the module has pow ( ) , log ( ) , and exp ( ) functions.

Example

>>> math . sqrt ( 25 )
5.0
>>> math . pow ( 5 , 2 )
25.0
>>> math.log(10) #natural logarithm using math.e as base
2.302585092994046
>>> math.loglO(100) ttstandard logarithm using 10 as base
2.0
>>>

The mod operator (%) and floor operator (//) were introduced earlier. This module contains similar functions ( remainder ( ) and floor ( ) ). Another ceil( ) function in this module returns the nearest integer of a division operation.

Example

>>> math.remainder(10,6) #Difference between numerator and closest integer multiple of denominator
-2.0
>>> math.floor(10/6) #Returns the largest integer < = given float
1
>>> math.ceil(10/6) #Returns the smallest integer >= float
2
>>>

 

Python Data Persistence – Math Module Read More »

Python Data Persistence – User Defined Modules

Python Data Persistence – User Defined Modules

A module is a collection of Python objects such as functions, classes, and so on. Python interpreter is bundled with a standard library consisting of a large number of built-in modules, some of which we got acquainted with. Just as built-in modules, you can create a collection of your own functions and import them in an interactive Python session or in another script.

Any Python script (having .py extension) can be used as a module. The idea behind a user-defined module is the same as that of a built-in module. If the entire programming solution involves quite a large number of functions and classes, in that case putting all definitions in a single script is likely to be troublesome. A better way is to organize them in separate modules. Functions and classes of similar relevance are kept in one module. Such an approach makes the code easier to maintain. First of all, let us put some functions in a script called ‘mymodule.py

Example

‘docstring of mymodule’
def isprime(num):
x=2
for x in range(2,num):
if num%x==0:
return False
else:
return True
def iseven(num):
if num%2==0:
return True
else:
return False
def isleap(num):
if num%4==0:
return True
else:
return False

We can now import any function from this module in an interactive interpreter session, just as we imported the math module.

Example

>>> import mymodule
>>> mymodule . isprime ( 43 )
True
>>> mymodule . isprime ( 72 )
False
>>> mymodule . iseven ( 28 )
True
>>> mymodule . iseven ( 93 )
False
>>> mymodule . isleap (2019)
False
>>> mymodule . isleap ( 1996 )
True
>>>

It is also possible to import this module in another Python script. Here is an example script ‘moduledemo.py’

Example

#moduledemo.py
import mymodule
print (‘calling isprime function from mymodule1)
n=int(input(‘enter a number..’))
retval=mymodule.isprime(n)
if retval==True:
print ( ‘ { } is a prime number ‘ . format ( n ) )
else:
print ( ‘ { } is not a prime number ‘ . format ( n ) )

Output

E:\python37>python moduledemo.py
calling isprime function from mymodule
enter a number . . 39
39 is not a prime number
E:\python37>python moduledemo.py
calling isprime function from mymodule
enter a number . . 97
97 is a prime number

_name_ attribute
As is often said, everything in Python is an object. Likewise, a module – whether built-in or user-defined – is also an object of module class.

Example

>>> #module object – built-in module
. . .
>>> import math
>>> type(math)
<class ‘module’>
>>> #module object – user defined module
. . .
>>> import mymodule
>>> type(mymodule)
<class ‘module’>

Module object is characterized by various attributes. One of the important attributes of a module object is__name___and it has a peculiar behavior. Inside Python’s interactive shell, the__name__attribute returns ‘__main__’. It is the name of the top-level namespace in which the Python interpreter is running. However, the value of an imported module’s__name__ attribute is the name of the module itself (excluding the .py part from the script’s name)

Example

>>> #__name__ attribute of interactive shell
. . .
>>>__name__
‘__main__’
>>> #__name__ attrbute of imported module
. . .
>>> import math
>>> math.__name__
‘ math’
>>> import mymodule
>>> ‘mymodule.__name__
‘mymodule’

This is also the same in the case of a Python script. When a certain script is run from the command line, Python is running in scripting mode. Hence value of__name__in the script is ‘__main__’. So also,__name__attribute of a module imported in the script is its name itself. Run the following code from the command line.

Example

#moduledemo-1.py
import mymodule
print (‘_name_ of top level module: ‘ ,__name__)
print (‘__name__ of imported mymodule : ‘ , mymodule.__name__)

Output

E:\python37>python moduledemo-1.py
__name__of top level module:__main__
__name__ of imported mymodule: mymodule

A script having function definitions may also have certain executable code also in it. What happens if it is imported into another script? Let us see. Open mymodule.py and add statements that call is even( ) function after definitions.

Example

‘docstring of mymodule’
def isprime(num):
x=2
for x in range(2,num):
if num%x==0:
return False
else:
return True
def iseven(num):
if num%2==0:
return True
else:
return False
def isleap(num):
if num%4==0:
return True
else:
return False
##add following statements
n=int(input(‘enter a number..’))
retval=iseven(n)
if retval==True:
print ( ‘ { } is even ‘ . format ( n ) )
else:
print ( ‘ { } is odd ‘ . format ( n ) )

Now if the moduledemo.py is run (it imports my module in it). Look at the result, (figure 3.15)

E:\python37>python moduledemo.py
enter a number..23
23 is odd
calling is prime function from my module
enter a number23
23 is a prime number

You find that the executable part in the imported module also runs. Obviously, we don’t want this to happen. We do want the executable code to run when the module script is invoked but not when it is imported. The peculiar behavior of__name__attribute comes in handy here. As we saw, its value happens to be ‘__main__’ when a module script is run, but__name__attribute takes up its name when imported. So we have to test this attribute and run the executable code only if it is ‘__main__’. Modify the my module code as follows:

Example

‘docstring of mymodule’
def isprime(num):
x=2
for x in range(2,num):
if num%x==0:
return False
else:
return True
def iseven(num):
if num%2==0:
return True
else:
return False
def isleap(num):
if num%4==0:
return True
else:
return False
#modify as follows:
if __name__==’__main__’ :
n=int ( input ( ‘ enter a number . . ‘ ) )
retval=iseven(n)
if retval==True:
print ( ‘ { } is even ‘ . format ( n ) )
else:
print ( ‘ { } is odd ‘ . format ( n ) )

You can run mymodule.py independently but wouldn’t affect the execution of moduledemo.py. (figure 3.16)

E:\python37>python moduledemo.py
calling is a prime function from my module
enter a number..11
11 is a prime number

 

Python Data Persistence – User Defined Modules Read More »

Python Data Persistence – Structured Python

Python Data Persistence – Structured Python

The previous two chapters explained some basic features of Python, which will empower you to develop a programming solution for just about any algorithm. Now it’s time to learn how to make the solution more tidy, efficient, and easy to maintain.
In the previous chapter, the factorial.py code calculates the factorial value of a number. Based on the same algorithm, let us calculate a combination of two numbers. In mathematics, a combination is a selection of items from a collection, such that (unlike permutations) the order of selection does not matter. Its mathematical notation is nCr. Following script uses for loop in its solution.

Example

#combinations.py
n=int ( input ( " enter n . . " ) )
r=int ( input ( " enter r . . " ) )
t=n-r
#calculating factorial of n - fn
fn=1
for i in range ( 1 ,n+1 ) :
fn=fn*i
#calculating factorial of r - fr
fr=1
for i in range(1,r+1):
fr=fr*i
#calculating factorial of t - tr
ft = 1
for i in range ( 1 , t+1 ) :
ft=ft*i
combinations=fn/(fr*ft)
print ( " C ( { } , { } )={ }". format ( n , r , combinations ) )

Output

E:\python37>python combinations.py
enter n . . 5 
enter r . . 2 
C ( 5 , 2 ) = 10 . 0 
E:\python37 >python combinations.py 
enter n . . 10 
enter r . . 5 
C ( 10 , 5) = 252 . 0

Looks alright, isn’t it? However, looking at the above code, the thing that strikes immediately is that it uses for loop thrice to calculate the factorial value of three numbers. Following code is the alternative solution to the same problem but appears to be cleaner and more concise.

Example

#combinations-2.py
#calculate factorial
def factorial ( x ) :
f = 1
for i in range ( 1 , x+1 ) :
f = f*i
return f
n=int ( input ( " enter n . . " ) )
r=int ( input ( " enter r . . " ) )
combinations=factorial(n)/
(factorial(r)*factorial(n-r))
print ( " C ( { } / { } )={ } " - format ( n , r , combinations ) )

sys Module

This module defines some important system-specific parameters and functions. In this section, you will learn one such parameter which is useful in the scripting mode of Python.
sys. argv stores a list of all command-line arguments provided to Python script. We know that the input ( ) function in a Python script is used to collect user inputs. However, inputs can also be given to the script from the command line. They are made available to the program in the form of this list object. For example:

E:\python37>python example.py aa bb

This is the command line to execute ‘example.py’ providing ‘aa’ and ‘bb’ as arguments. Three strings are stored in sys.argv list and can be accessed in the script. Name of script – sysexample.py in this case is stored at sys.argv[0]. Other arguments are stored at successive indexes. Let sysexample.py script be as follows:

Example

#example.py
import sys
args=sys.argv
print ("Hello { }".format(sys.argv [ 1 ] ) )

It is now executed from the command line, (figure 3.3)

E:\python37>python sysexample.py Python
Hello Python
E:\python37>python sysexample.py Java
Hello Java

We are going to learn few more built-in modules and their functions in the following chapters:

  • Serialization: CSV module,pickle module,dbmmodule,JSON module.
  • DB-API: sqlite3 module, pymysql module, pyodbc module.

Function with Parameters

A parameter (also called an argument) is certain data passed on to the function while calling it. The previous section (sys module) showed how command-line arguments passed to a Python script can be processed by using sys. argv parameter. Similarly, one or more parameters can be passed to function from inside a script. Values of received parameters are then processed inside the function.

Two things must be kept in mind here. First, the function should be defined taking into consideration the number of parameters it is going to receive. Secondly, the function call should ensure it passes an adequate number of parameters as anticipated in the function’s definition. Variables used in function’s definition to receive passed values are often called formal arguments. Actual arguments are the values passed while calling.
In the following script, the LoveUO function is defined with one argument with a formal name as Hang’. Its value is used in the function to print relevant messages. User input is passed to this function as an actual argument.

Example

#function-2.py
def LoveU(lang):
'function with one parameter'
print ('I love { }'.format(lang))
return
#call above function
lang=input('enter name of a language..')
LoveU(lang)

Output:

E:\python37>python function-2.py 
enter name of a language..Python 
I love Python 
E:\python3 7 >python function-2.py 
enter name of a language..Hindi 
I love Hindi

return Keyword

The return keyword at the end of the function block is merely to indicate that program flow would be returned to the calling position after the block is over. Use of return is optional in the sense program flow anyway goes back to calling position even if it is not used. However, if you intend to send some data back to the calling environment, then the return keyword must be used along with the expression to be returned.
Following script is a simple example of a function returning its result. It defines add () function to receive two numbers, perform addition, and return the result.

Example

#func-with-return.py
def add ( num1 , num2 ) :
'function assumes a two numbers to be passed'
result=num1+num2
return result
#call above function
x=int(input('enter a number..'))
y=int(input(1 enter another number.. '))
z=add(x,y)
print ( ' addition of { } and { } is { } ' . format ( x , y , z ) )

Output

E:\python37>python func-with-return.py 
enter a number..100 
enter another number..200 
addition of 100 and 200 is 300

Required Arguments

The statement that calls a function should pass exactly the same number of values as a number of actual arguments in its definition, otherwise, Python raises TypeError as in the following example, where the function is defined with two parameters but there is an attempt to call it with three values.

Example

#function-3.py
def LoveU( lang1 , lang2 ) :
'function with two parameters'
print ( ' I love { } and { } ' . format ( lang1 , lang2 ) )
return
#call above function
LoveU( 'Hindi ', ' Python ' , ' Java ' )

Output

E:\python37>python function-3.py 
Tracebaok (most recent call last): 
File "function-3.py", line 7, in <module> 
LoveU('Hindi' , 'Python' , 'Java') 
TypeError: LoveU( ) takes 2 positional arguments but 
3 were given 
E:\python37>

Values passed to a function are picked up by formal arguments in positional order. Since Python doesn’t enforce static typing, any formal argument may hold any type of data. However, there still may be errors raised inside a function if the treatment of the variable is not as per its value.

Look at the following script. The function in it receives two values. First, one is expected to be a string, and second a number. The function calculates the length of the string – the first parameter and determines if the number – second argument – is odd/even. Therefore, the function receives two values but with mismatching types, TypeError is raised when trying to compute len ( ) on a number data type.

Example

#function-4.py
def function(string,num):
'function assumes a string and number to be passed'
print ('parameter received', string, num)
print ('length of string:', len(string))
if num%2==0:
print (num,' is 'even')
else:
print (num, ' is ','odd')
return
#call above function
function ('Hello', 10)
function (10, ’Hello')

Output

E:\python37>python function-4.pyOutput
parameter received Hello 10
length of string: 5
10 is even
parameter received 10 Hello
Traceback (most recent call last) :
File "function-4.py", line 13, in <module>
function (10, 'Hello')
File "function-4.py", line 5, in function
print ('length of string:', lend string))
TypeError: object of type 'int' has no len( )

Parameter with Default Value

You can have a function that defines one or more parameters with default values. If the function call doesn’t explicitly give any value to the said parameter, the default will be used inside the function. If however, the call does pass a value, it will override the default settings in the function’s definition.

Example

#func-with-default.py 
def dress(trouser, shirt='White'): 
            print ( 'Trouser is { }. Shirt is { }'. 
format ( trouser , shirt ) ) 
dress( ' Black ' ) 
dress( ' Blue ' , ' Green ' )

You can see that the second argument is having a default value. The first call doesn’t give any value to it, so default is used. The second call overrides the default value. The output demonstrates this behavior (figure 3.9):

Trouser is Black. Shirt is White
Trouser is Blue. Shirt is Green

There is one precaution to be taken. Arguments with default values must appear after the required arguments in the parameter list of the function definition.

Function with Variable Arguments

We have already seen how arguments can be passed to Python script from the command line. All arguments are stored in a list object (sys. argv). On similar lines, if a function is called by passing the variable number of arguments from within the script, they are stored in a parameter prefixed with ‘*\ It is a list object of all values passed. Treat it as a normal list object and process it accordingly within the function. The following script is a very appropriate example of a function with variable parameters.

Example

#func-with-var-args.py
def addall(*nums):
tt1 = 0
for num in nums:
tt1=tt1+num
return tt1
total=adda11 ( 10 , 20 , 50 , 70 )
print ( ' Total of 4 numbers: ' , total )
total=adda11 ( 11 , 34 , 43 )
print ( ' Total of 3 numbers : ' , total )

Output

E:\python37>python func-with-var-args.py 
Total of 4 numbers: 150 
Total of 3 numbers: 88

Python Data Persistence – Structured Python Read More »

Python Data Persistence – Function

Python Data Persistence – Function

The function is called a structured approach to program development. Idea is to break the algorithm into one or smaller, independent, and reusable blocks of instructions. Each such block, designed to perform a single task is known as a function. It is called wherever it is required in the main routine of the program. When called, each function performs the process as defined in it and returns the control back to the main routine, (figure 3.2)

IMG

Thus the function is an important building block of any software. Python interpreter itself has quite a few in-built functions. You have already come across some of them. In addition, standard Python installation a library of many built-in modules (nearly 500 modules are bundled in standard Python distribution), each one having some functions defined in it. While built-in functions are always available, functions in other built-in modules have to be explicitly loaded. Remember we used a randint () function to generate a random integer as a secret number in the previous chapter (secret-number.py). Let us look at another function that calculates the square root of a number. The sqrt () function is defined in the math module. (The math module itself contains nearly 50 functions.) You need to import this module for that purpose.

Example

>>> import math
>>> math.sqrt(100)
10.0
>>>

As you can see, the Python interpreter has access to a large number of predefined functions, either built-in or built-in modules. You can employ these functions in your program as per requirement. You can of course develop your own function if you don’t find a suitable function in any of the built-in modules. These built-in modules are appropriately named to indicate the type of functions in them. Examples are math module (having mathematical functions) and random module (having random number generation functions).

Built-in modules are generally written in C and bundled with Python interpreter in precompiled form. A built-in module may be a Python script (with .py extension) containing useful utilities. A module may contain one or more functions, classes, variables, constants, or any other Python resources.

To display a list of all available modules, use the following command in the Python console:

>>> help(‘modules’)

Python has an import keyword to load all contents of a module in the current namespace of the Python interpreter. Functions can then be called using dot operator as follows:

Example

import module
module.function ( [ arguments if any ] )

Python also provides from keyword that allows only specified functions from a module rather than all functions. The sqrt () function from the math module can also be imported as follows:

Example

>>> from math import sqrt
>>> sqrt(100)
10.0
>>>

We shall first get ourselves acquainted with some of the frequently required functions in built-in modules before we learn how to define and use our own functions.

Python Data Persistence – Function Read More »

Python Data Persistence – Reading a File

Python Data Persistence – Reading a File

Let us now read ‘top-quotes.txt programmatically by opening it with r mode. When in R mode, the file object can call read (), readline ( ) , and readlines () methods.

Out of these, the read ( ) method can read a specified number of bytes from the file, the size defaults to file size. However, if the file is very big, the available memory restrictions may not allow the entire file to be read, so you may have to provide size parameters to read bytes at once.

Example

>>> file=open ( ' top-quotes . txt' , ' r')
>>> text=file . read ()
>>> text
"'The best way to predict the future is to invent it.' - Alan Kay'\nThere are only two kinds of programming languages: 
those people always bitch about and those nobody uses.' - 
Bjarne Stroustrup\ n'The only way to learn a new programming language is by writing programs in it.' 
-Dennis Ritchie\n'A computer would deserve to be called intelligent if it could deceive a human into believing that 
it was human.' - Alan Turing\nprogramming languages have a devious influence. They shape our thinking habits 
- Edsger W. Dijkstra\nprogrammers do programming not because they expect to get paid or get adulation by the public, 
but because it is fun to program - Linus Torvalds\nA computer would deserve to be called intelligent if it could deceive 
a human into believing that it was human - Alan Turing"
>>>file . close ( )

To read specified number of bytes from the beginning of file

Example

>>> file=open (' top-quotes . txt' , ' r' )
>>> text =file . read (65)
>>> text
" ' The best way to predict the future is to invent it.' - Alan Kay\n"
>>> file . close ()

Reading a File Line-by-Line

The readline () method reads all bytes till a newline character ‘\n’ is encountered. It returns an empty string when no more lines are left to be read. Hence, we can call readline ( ) method in a loop till empty string is returned, (figure 5.2)

>>> file=open ( ' top-quotes . txt1 , ' r ' )
>>> while True:
line=file . readline () 
if line=='':break 
print (line, end='')

'The best way to predict the future is to invent it.1 - Alan Kay 1 There are only two kinds of programming languages: 
those people always bitch about and those nobody uses.' - Bjarne Stroustrup 'The only way to learn a new programming 
language is by writing programs in it.1 -Dennis Ritchie
'A computer would deserve to be called intelligent if it could deceive a human into believing that it was human.' 
- Alan Turing programming languages haile a devious influence. They shape our thinking habits - Edsger W. Dijkstra
programmers do programming not because they expect to get paid or get adulation by the public, but because 
it is fun to program - Linus Torvalds
A computer would deserve to be called intelligent if it could deceive a human into believing that it was human - Alan Turing

The file object is a data stream that acts as an iterator. An iterator serves subsequent object every time next() function 
is called till stream is exhausted and Stoplteration exception is encountered. 
We can use the next () method on the file object to read a file line by line.

Example

f=open("top-quotes.txt", "r")
while True:
           try:
               line=next(f)
               print (line, end=" ")
      except StopIteration:
                  break
f . close ( )

Or simply use a for loop over each line in the file iterator:

Example

file=open ("top-quotes . txt", " r") 
for line in file:
print (line, end="") 
file. close ( )

The readlines ( ) method returns list of lines in the file.

>>> file=open( top-quotes.txt ' 'r')
>>> lines=file.readlines( )

Python Data Persistence – Reading a File Read More »

Python Data Persistence – Opening File

Python Data Persistence – Opening File

The open ( ) function takes a string corresponding to the disk file’s name along with its path as an argument. The second argument indicates the mode in which the file is intended to be opened. Default file opening is ‘r’ which stands for ‘read’ mode which means data in the file is read into program variables. In order to use the file as the output destination, use ‘w’ as the value of the mode parameter. The function returns a file object.

Example

>>> obj=open( ' test.txt' , 'r')
Traceback (most recent call last):
File "<pyshell#16>", line 1, in <module>
obj =open('test.txt' , ' r ')
FileNotFoundError: [Errno 2] No such file or
directory: 'test.txt'
>>> obj=open('test.txt' , 'w')
>>> obj.close()
>>>

Noth that when in ‘r’ mode open( ) function can open existing file otherwise raises FileNotFoundError. Always ensure that the opened file object is closed to flush data if any in the buffer.

Python Data Persistence – Opening File Read More »

Python Data Persistence – File I0

Python Data Persistence – File I0

‘File’ is an indispensable word in the vocabulary of even an ordinary computer (even mobile) user. Every day, he is required to deal with files which may be documents, spreadsheets, presentations, images, and so on. Slightly advanced users, to whom we may call developer, prepare scripts, build executables which are also files.

When a user starts an application, he enters certain data, either through a keyboard or any other device such as a mouse, camera, scanner, and so on. The data goes into the computer’s main memory and is further processed as per the process defined in the application. If this data – input or resulting from the process – is needed for subsequent use, it is saved in a computer file, because if left in the computer memory, it will be erased when a computer is turned off. In this chapter, we shall discuss how data from Python program is stored in persistent disk files.

A Python console application interacts with peripheral devices through its built-in input ( ) and print ( ) functions. Channels of interaction between processor and peripheral devices are called streams. A stream is an object that sends/receives a continuous flow of data. Python’s input ( ) function reads data from standard input streaming device i.e. keyboard that is recognized as sys. stdin object defined in sys built-in the module. Similarly, the print ( ) function sends data to a standard output device which is a computer display screen (monitor), defined as sys. stdout object.

The stdin object has read ( ) and readline ( ) methods to accept user input through the keyboard. The read ( ) method accepts data till the stream is terminated by Ctrl+D character. On the other hand readline ( ) method accepts all keystrokes till the ‘Enter’ key is pressed. Both methods leave ‘\n’ at the end of the input.

Example

>>> data=sys.stdin.read( )
Hello
How are you?
>>> data
'Hello\nHow are you?\n'
>>> data=sys.stdin.readline()
Hello How are you?
>>> data
'Hello How are you?\n'
>>>

In fact, the input ( ) function performs stdin. readline ( ) and returns by stripping the trailing ‘\n! character. The write ( ) method available to the stdout object does exactly what the print ( ) function does. It sends the argument data to the default output device – the computer monitor. However, when using interactive mode, it also displays the size of the object in bytes.

Example

>>> sys.stdout.write(data)
Hello, How are you?
19
>>>

Any object that can send/receive the stream of bytes is called ‘File like object in Python. A file (like) object can invoke read ( ) or write ( ) methods depending upon the stream to which it is connected. Hence, stdin and stdout objects are file-like objects too. Python can perform 10 operations with objects representing disk files, network sockets, memory arrays, and so on. In this chapter, we shall deal with computer disk files. These files store data in a persistent manner, which is often the need as the same collection of data may be needed repeatedly. Instead of laboriously keying in the same data again and again from the keyboard, reading it from a file becomes more efficient and less error-prone. Similarly, screen output being temporary and limited, can instead be stored in files, (figure 5.1)

Python Data Presistence - File 10 chapter 5 img 1

Standard 10 streams communicating with stdin and stdout objects are always available. To use a disk file for reading/writing purposes file object needs to be declared first, by using the built-in open () function.

Python Data Persistence – File I0 Read More »

Python Data Persistence – Magic Methods

Python Data Persistence – Magic Methods

Each Python class inherits methods from its ultimate parent class – object class. Methods in objects are peculiarly named – having double underscores on either side. One such method is well known to you by now. The __init__( ) method. To display a list of methods, we have to use the built-in dir ( ) function.

Example

>>> dir(object)
['__class__' , '__delattr__' , '___dir___' , ___doc___' , '__eq__' , 
'__format__' , __ge__' , '__getattribute__' , '__get__' , '__hash___' , '__init__' , 
'___init_subclass__' , '__le__' , '___lt___' , '__ne__' , '__new___' , '__reduce__' , 
'__reduce_ex__' , '__repr__' , '___setattr__' , '___sizeof___' , '___str__' , '__subclasshook__']

These double underscored methods are known as ‘magic’ methods. They have a very important role in the Pythonic brand of object-oriented programming. What is so ‘magical’ about them? You’ll soon come to know.

These methods (sometimes referred to as ‘special’ or ‘dunder’ – short for double underscore) in object class are really abstract methods in the sense they don’t have any particular implementation. Its subclasses override them as per necessity. Take int class for example. It overrides___str___( ) method to return a printable string version of integer object.

Example

>>> a=123
>>> a.__str___( )

Incidentally, many of these ‘dunder’ methods are rarely called directly. A corresponding built-in function internally calls them. The str( ) function implements the __str__( ) method.

Example

>>> a=123 
>>> str(a)
'123'

Another example is setattr( ) function we used earlier in this chapter. It dynamically adds attribute to an object. It in fact performs operation of__setattr__( ) method. Have a look at following code:

Example

>>> class MyClass:
pass
>>> obj1=MyClass( )
>>> setattr(obj1,'myname','Madhav')
>>> #using__setattr__( ) method
>>> obj1.__setattr__(’myage',21)

Even the dir ( ) method used at the beginning of this section actually calls__dir__( ) magic method.

Example

>>> a. dir _( ) 
['__repr__' , '__hash__' , '__str__' , '___getattribute___' , '__lt__' , '___le__' , '__eq__' , '___ne___' , '__gt__' , '___ge__' ,
 '__add__' , '__radd__' ,'___sub__' , '__rsub__' , '__mul__' , ' ___rmul___', '___mod___' , '___rmod___' , '___divmod__' ,
'___rdivmod__' ,'__pow___' , ' ___rpow___' , ' __neg__' , '___pos___ ' , '___ abs__' ,'___bool___ ' , '__invert___' , '___shift__' , 
'___rlshift__ ' , '__rshift___' , '___rrshift___' , '___and___', '___rand___', '___xor__' , '__rxor__' , '__or___' , '___ror__' , '__int__' ,
 ' __float__' , '___floordiv__' , '__rfloordiv__' ,'___truediv___' , '__rtruediv__' , '__index___' , '___new__' , '___conjugate___ ' , 
'__bit length__' , '___to_bytes___' , '___from bytes__' , '___t rune__' , '__floor__' , '___ceil__' , ' ___round___', '__getnewargs___' , 
'__format __', ' __sizeof___' , '___real___', '___imag___ ' , '__numerator__ ' , '____denominator____ ' , '____doc__' , '___setattr__' 
,'___delattr____' , '__init___' , '___reduce_ex__' , '___reduce__' , '__subclasshook___ ' , '__init_subclass___ ' , '__dir__' , '__class__']
>>> #is equivalent to 
>>> dir(int)

It may be noted that dir (int) shows a lot more attributes than that of the object class. These are actually inherited from abstract Number class and overridden in int class. Of particular interest are methods with names indicating arithmetic operations (such as__add__, ___sub___ , ___ mul__ , and so on.) and logical operations (like ___ge__ , __ gt___ , ___eq___ , and so on.) They are mapped to respective operators so that a conventional arithmetic/logical operator invokes the respective method. In other words, a+b actually performs a . __add__(b) call.

Example

>>> a=20
>>> b=10
>>> a+b
30
>>> a.__add__(b)
30
>>> a.__mul__(b)
200
>>> a*b
200
>>> a>b
True
>>> a.__le__(b)
False

The real magic lies ahead. You can override these methods to customize the behavior of operators to achieve operator overloading.

Example

#timerclass.py
class timer:
         def__init__(self, hr=None, min=None):
               self.hrs=hr
               self.mins=min
         def__add__(self, arg):
               temp=timer( )
               temp.hrs=self.hrs+arg.hrs
               temp.mins=self.mins+arg.mins
               if temp.mins>=60:
                        temp.mins=temp.mins-60
                        temp.hrs=temp.hrs+1
                  return temp
         def__str__(self):
                  timestring='{ } Hrs. { }
mins.'.format(self.hrs,self.mins)
                  return timestring

In the above script, the timer class has two instance attributes hrs and mins. The __add__( ) method performs addition of two timer objects. This method is invoked in response to the use of the ‘+’ operator along with two timer object operands. By the way timer class also has overridden the implementation of __str___( ) method to produce string representation of its object.

Example

>>> from timerclass import timer
>>> t1=timer(2,45)
>>> t2=timer(3,30)
>>> t3=t1+t2
>>> print (t3)
6 Hrs. 15 mins.
>>> t3=t1.__add__(t2)
>>> print (t3)
6 Hrs. 15 mins.

Go ahead and overload any operator to suit the needs of your application. A list of all magic methods is provided in Appendix E.
This chapter is a very brief discussion on object-oriented programming as implemented in Python. Only those facets that are required in subsequent chapters of this book have been explained here.

Python Data Persistence – Magic Methods Read More »

Python Data Persistence – Overriding

Python Data Persistence – Overriding

With two similar radii for the circle, the equation of the area of the ellipse (Ï€*r1 *r2) turns out to be 7rr2, and the equation of perimeter of the ellipse (2Ï€ effectively becomes 27Ï€r. However, we would like to redefine area( ) and perimeter( ) methods, which are inherited by circle class, to implement these specific formulae. Such redefinition of inherited methods is called method overriding.
Modify circle class as per following code:

Example

class circle(ellipse):
        def___init___(self, r1, r2=None):
                super().__init___(r1,r2)
                self.radius2=self.radiusl
        def area(self) :
                 area=math.pi*pow(self.radius1,2)
                  return area
       def perimeter (self) :
           perimeter=2*math.pi*self.radius1
           return perimeter

The result will be unaffected though. Python class can be inherited from multiple classes by putting names of more than one class in parentheses of the class definition.

Protected?

As mentioned earlier, Python doesn’t believe in restricting access to attributes. To emulate the behavior of Java-like protected access specifier, a single underscore character is prefixed to the instance attribute. However, for all practical purposes, it behaves like a public attribute, not even requiring name mangling syntax as in the case of the ‘private’ attribute (prefixed by double underscores). The _ character serves merely as a deterrent expecting responsible programmer to refrain from using it outside inherited class.

Python Data Persistence – Overriding Read More »