0% found this document useful (0 votes)
110 views159 pages

Python Advanced Programming Guide

The document provides a comprehensive guide to advanced Python programming, covering topics such as multithreading, socket programming, GUI programming, email sending, XML and JSON processing, and database programming with MySQL, Oracle, MongoDB, and Firebase. Each chapter includes theoretical explanations, practical examples, and programming exercises to illustrate the concepts. The content is structured to facilitate learning and application of advanced Python techniques across various domains.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
110 views159 pages

Python Advanced Programming Guide

The document provides a comprehensive guide to advanced Python programming, covering topics such as multithreading, socket programming, GUI programming, email sending, XML and JSON processing, and database programming with MySQL, Oracle, MongoDB, and Firebase. Each chapter includes theoretical explanations, practical examples, and programming exercises to illustrate the concepts. The content is structured to facilitate learning and application of advanced Python techniques across various domains.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Advanced Programming

Chapter-1 (Multithreading)
1. Implementing Multithreading in Python
2. Different ways of Creating user-defined Threads in Python
3. Program: Creating a Thread without extending any class – target function not taking any
arguments
4. Program: Creating a Thread without extending any class – target function taking some arguments
5. Program: Demo on creating our own Thread by extending Thread class
6. Program: Demo on creating multiple Threads
7. Synchronization
8. Program: Without Synchronization
9. Program: With Synchronization – using Locks
10. Program: Producer Consumer Threads – Problem (Buggy Program)
11. Program: Producer Consumer Threads – Problem – Solution – using Condition class
12. Program: Producer Consumer Threads – Problem – Solution – using Queue class

Chapter-2 (Socket/Network Programming)


1. What is a Computer Network
2. Different Types of Networks
3. Clients and Servers
4. Sockets
5. Socket Programming in Python
6. Program: Socket Programming in Python – Client-Server Demo
7. Program: Socket Programming in Python- creating a chat application – demo

Chapter-3 (GUI Programming in Python)


1. How to install PyCharm IDE and use it
2. Program: GUI Programming in Python using Tkinter Module
3. Program: GUI Programming using PyQt5 Module
4. Program: GUI Programming using PyQt5 Designer
5. Program: Creating a GUI Calculator using PyQt5 Designer
6. Converting .py to .exe (Steps)

Chapter-4 (Sending Email)


1. Sending Email from Python code – Ex1
2. Sending Email from Python code – Ex2 (Sending to Multiple Receivers)
3. Sending Email from Python code – Ex3 (Sending message with a subject)
4. Sending Email from Python code – Ex4 (Sending message with File Attachment)

Chapter-5 (Python XML and JSON Processing)


1. Program: Python XML Processing Demo1
2. Program: Python XML Processing Demo2 - findall()
3. Program: Python XML Processing Demo3 - iter()
4. Program: Python JSON Processing Demo1
5. Program: Python JSON Processing Demo2 – traversing the details of all Employees
6. Program: Python JSON Processing Demo3 - getting a particular key’s value
7. Program: Python JSON Processing Demo4 - getting nth key’s value in an array
8. Program: Python JSON Processing Demo5 - len()

DATAPRO COMPUTERS 1
Chapter-6 (Python Database Programming – accessing MySQL Database)
1. Program: Establishing a connection with the MySQL Database and fetching all records in a table
2. Program: Get a particular Record(s) in a table
3. Program: Add/Insert Record
4. Program: Update a Record
5. Program: Delete a Record
6. Program: Establishing a connection with a public MySQL Database Engine Server and fetching all
records in a table

Chapter-7 (Python Database Programming – accessing Oracle Database)


1. Establishing a connection with the Oracle Database and fetching all records in a table
2. Program: Add/Insert Record
3. Program: Update a Record
4. Program: Invoking a stored procedure in Oracle DB
5. Program: Invoking a stored procedure in Oracle DB with “out” parameters
6. Program: Invoking a stored function in Oracle DB

Chapter-8 (Python Database Programming – accessing MongoDB Database)


1. Program: Establishing a connection with the MongoDB Database and fetching all documents in a
collection
2. Program: To exclude the _id field in a find query in pymongo
3. Program: Get a particular Document(s) in a Collection – condition with logical operator
4. Program: Get a particular Document(s) in a Collection – condition with equality
5. Program: Add a Document to a MongoDB Database Collection
6. Program: Update a Document
7. Program: Delete a Document
8. Setting up a Remote live public MongoDB Database
9. Installing Mongo DB Compass Tool and connecting to the MongoDB Atlas Server
10. Program: Python Program accessing the MongoDB Live Server

Chapter-9 (Python and Firebase Database Integration)


Round-1
1. What is Firebase
2. Setting up the Firebase Database
3. Choose Firebase database type
4. Program: To add a new Record/Object in Firebase database (Connecting to Firebase using
Python)
5. Program: Get all records from Firebase
6. Program: Get a particular record from firebase
7. Program: Update a record in Firebase
8. Program: Delete a record in Firebase
Round-2
1. Firebase – Authentication Steps Demo
2. Program1: Establishing authenticated connection to the Firebase database
3. Program: Adding data to the Firebase database – using Authentication
4. Program: Add records with Custom ids
5. Program: Getting data from the Firebase database – using Authentication

DATAPRO COMPUTERS 2
Chapter-1: Multithreading in Python

Implementing Multithreading in Python

Python is a multi-threaded programming language which means we can develop multi-threaded


program using Python. A multi-threaded program contains two or more parts that can run concurrently
and each part can handle a different task at the same time making optimal use of the available
resources especially when your computer has multiple CPUs.

It has the following benefits:

• Multiple threads within a process share the same data space with the main thread and can
therefore share information or communicate with each other more easily than if they were
separate processes.
• Threads sometimes called light-weight processes and they do not require much memory
overhead; they are cheaper than processes.

Examples of Multithreading:
• Copy Paste Application
• Media Player Application
• Flight shooting with Machine gun - Game Application (Eg: Sky Battle Game)
• Google Chrome Desktop Web Browser Application
• Web Server implementation – for each user – one thread caters his/her request. (there would be
a readymade pool of threads to cater the requests.)
• Search facility in Google Suggest Box (actual search is done in different Google Servers all over
the world)
• Search facility in the Operating System (to search for files and folders in all the drives – Eg: each
thread searches for the search keyword(s) in a particular Drive of the OS)
• In a Mobile App, where the user uploads a photo to the Server (via one thread), uploads a
document (via another thread) and simultaneously he/she can access the UI.

Note: Multithreaded application would be more affective on a multi-core processor where each core
can process a thread simultaneously.

Life Cycle of a Thread

Different ways of Creating user-defined Threads in Python

DATAPRO COMPUTERS 3
Python provides “Thread” class of “threading” module that is useful to create threads. There are
different ways of creating our own thread:

• Without extending the Thread class


• By extending Thread class

Program: Creating a Thread without extending any class – target function not taking any arguments

Note: We can create a thread by creating an object of Thread class and pass the target function to be
executed parallelly.

Output:

Program: Creating a Thread without extending any class – target function taking some arguments

DATAPRO COMPUTERS 4
Note: Observe the comma (, ) after the argument mentioned in the tuple. Recollect that when a single
element is specified in a tuple, a comma is needed after that element.

Output:

Program: Demo on creating our own Thread by extending Thread class

Note:
• Define a new subclass of the Thread class.
• Then, override the run(self [,args]) method to implement what the thread should do when
started.

Once you have created the new Thread subclass, you can create an instance of it and then start a new
thread by invoking the start(), which in turn calls run() method.

Notice that it is similar to implementing Multithreading in Java

Step1. Write the following program

DATAPRO COMPUTERS 5
Step2. Run the program

Output:

Sample Execution:

Another Sample Execution:

DATAPRO COMPUTERS 6
Program: Demo on creating Multiple Threads

Step1. Write the following program

Step2. Run the program

Output:

DATAPRO COMPUTERS 7
Synchronization

At times when more than one thread try to access a shared resource, we need to ensure that resource
will be used by only one thread at a time. The process by which this is achieved is called synchronization.

Concurrent accesses to shared resource can lead to race condition.

A race condition occurs when two or more threads can access shared data and they try to change it at
the same time. As a result, the values of variables may be unpredictable/data corruption.

Locks

Locks are typically used to synchronize access to a shared resource. A Lock has only two states —
locked and unlocked. It is created in the unlocked state and has two principal methods — acquire() and
release().

The acquire() method locks the Lock and blocks execution until the release() method in some other
function sets it to unlocked. Then it locks the Lock again and returns True. The release() method should
only be called in the locked state, it sets the state to unlocked and returns immediately. If release() is
called in the unlocked state, a RunTimeError is raised.

lock = Lock()

[Link]() # will block if lock is already held


... access shared resource
[Link]()

Program: Without Synchronization

Note: Allocating Railway reservation berths from different counters

DATAPRO COMPUTERS 8
Assume that in Reservation counter1, the clerk has sent a request to the server to allot that berth to
his/her passenger. In Reservation counter2, the second clerk has also sent a request to the server to
allot that berth to his passenger. It means two passengers are competing for the same berth (race
condition).

Output:

Program: With Synchronization – using Locks

DATAPRO COMPUTERS 9
Output:

Program: Producer Consumer Threads – Problem (Buggy Program)

Note: The producer's job is to generate a piece of data, put it into the buffer and start again.
At the same time, the consumer is consuming the data (i.e., removing it from the buffer) one piece at a
time

So, producer and consumer need to run concurrently. Hence we need separate threads for Producer
and Consumer.

We keep one variable which will be global and will be modified by both Producer and Consumer
threads. Producer produces data and adds it to the queue. Consumer consumes data from the queue
i.e. removes it from the queue.

DATAPRO COMPUTERS 10
Output:

Execution 1:

Execution 2:

DATAPRO COMPUTERS 11
Note:
• Producer keeps on adding to the queue and consumer keeps on removing from the queue.
• Since queue is a shared variable, we keep it inside lock to avoid race condition.
• At some point, consumer has consumed everything and producer is still sleeping. Consumer
tries to consume more but since queue is empty, an IndexError is raised.
• But on every execution, before IndexError is raised you will see the print statement telling
“Nothing in queue, but consumer will try to consume”, which explains why you are getting the
error.

Note: We found this implementation as the wrong behavior.

What is the correct behavior?

When there was nothing in the queue, consumer should have stopped running and waited instead of
trying to consume from the queue. And once producer adds something to the queue, there should be a
way for it to notify the consumer telling that it has added something to queue. So, consumer can again
consume from the queue. And thus IndexError will never be raised.

Program: Producer Consumer Threads – Problem – Solution – using Condition class

You will learn about the following thread topics:

• Condition in threads.
• wait() method available on Condition instances.
• notify() method available on Condition instances.

Note: Condition object allows one or more threads to wait until notified by another thread.

Condition is always associated with a lock.


A condition has acquire() and release() methods that call the corresponding methods of the associated
lock.

Condition provides acquire() and release() which calls lock’s acquire() and release() internally, and so we
can replace lock instances with condition instances and our lock behavior will keep working properly.

DATAPRO COMPUTERS 12
Output:

Execution 1:

DATAPRO COMPUTERS 13
Execution 2:

Program: Producer Consumer Threads – Problem – Solution – using Queue class

Output:

DATAPRO COMPUTERS 14
Note:
• In place of list, we are using a Queue instance.
• queue has a Condition and that condition has its lock. You don’t need to bother about Condition
and Lock if you use Queue.
• Producer uses put available on queue to insert data in the queue.
• put() has the logic to acquire the lock before inserting data in queue.
• Also put() checks whether the queue is full. If yes, then it calls wait() internally and so producer
starts waiting.
• Consumer uses get.
• get() acquires the lock before removing data from queue.
• get() checks if the queue is empty. If yes, it puts consumer in waiting state.
• get() and put() has proper logic for notify() too.

DATAPRO COMPUTERS 15
Chapter-2: Network/Socket Programming in Python

Note: Before digging into the programming part of networking, 1st let’s understand some of the basic
concepts of Computer Networks.

What is a Computer Network

Interconnection of computers is called a Computer Network.

The main advantage of a network is sharing of resources – data, files and hardware.

The data (of employees/customers) and files (documents/images) in a computer can be shared with
other computers in the network.

It is also possible to share the hardware in the network, Eg: there is no need of having individual printer
for each computer in an office, all the computers can share the single printer – any user can give a print
command sitting at his computer.

Internet is the interconnection between different Computer Networks globally.

Different Types of Networks

These are of four types

DATAPRO COMPUTERS 16
• LAN
• CAN
• MAN
• WAN

Client and Server

Note: Let’s see some client and Server concepts

Server Machine: The computer which serves the requests of the other computer(s) is known as Server
Machine. Ex.s: File Server, Web Server, Email Server

Client Machine: The computer which sends the requests to the server machine is known as Client
Machine. Ex.s: File Client, Web Client, Email Client

Client and Server software: Both the client and server machines need software to send/receive the
request/response. It means we need a program at client side and another program at server side.

Server program should be up and running first, so that it can handle any client’s request.

Client should first make a connection request, if accepted by the server, only then communication can
take place.

Host ID/IP Address: A unique ID (4 byte number: around 429 crores) for each computer in the network.

Port ID: A unique ID (2 byte number: 0 to 65535) for each server program running on any computer.
Ex1: MySQL server: 3306
Ex2: Oracle server: 1521
Ex3: Apache Tomcat Web Server: 8080

Socket: It is the communication end point for any computer in the network.
We can say, Socket = Host ID + Port ID

Sockets

It is a logical connection point between a Server and client machine so that communication can be done
through that point. Establishing communication between them using sockets is called as “socket
programming”.

We should use a new Port ID for each new socket. The port numbers/IDs from 0 to 1023 are already
allotted for various services and hence we should avoid using these port numbers in our
networking/socket programs

Eg:

DATAPRO COMPUTERS 17
Socket Programming in Python

Sockets are the end-point of a two-way communication link. An endpoint is a combination of IP address
and the port number.

DATAPRO COMPUTERS 18
Python Socket Programming Work Flow

Program: Socket Programming in Python – Demo

Step1. Write the following Server Program

DATAPRO COMPUTERS 19
Note: [Link]() is equivalent to:

[Link](socket.AF_INET, socket.SOCK_STREAM);

AF_INET: represents IP Address version 4 and SOCK_STREAM: indicates that we are using TCP/IP
protocol for communication.

For IP Address version 6, we have to pass: socket.AF_INET6

Step2. Run the program

Output:

Note: Encoding and Decoding is not required in Python 2 but required in Python 3

Step3. Open another instance of Python Console/IDLE

Step4. Write the following Client Program

DATAPRO COMPUTERS 20
Note: Instead of “[Link]” we could have even said: “localhost”

Step5. Run the client program

Output on the client console:

Output on the Server console:

Note: The client socket’s port id may way from execution to execution.

Program: Socket Programming in Python- creating a chat application - demo

Step1. Write the following Server program

DATAPRO COMPUTERS 21
Step2. Write the following Client program

[Link] the Server program first

DATAPRO COMPUTERS 22
Step4. Then run the Client program

Step5. Now take a look at the Server console

Step8. Now start chatting – beginning from the client:

After some chat:

Client Side:

Server Side:

DATAPRO COMPUTERS 23
Chapter-3: GUI Programming in Python

How to install PyCharm IDE and use it

PyCharm is an IDE used specifically for the Python language. It is developed by the Czech company
JetBrains.
d
Note: First make sure that python is already installed on the desktop
Recollect the location: C:\Users\HP\AppData\Local\Programs\Python\Python35-32

Step1. In Google search box

Step2.

Step3.

DATAPRO COMPUTERS 24
Note:

Step4. After download, click on the installer file

Click “Run” and leave the default values

DATAPRO COMPUTERS 25
DATAPRO COMPUTERS 26
Leave the defaults and click “OK”

Step5.

Click “Create New Project”

Choose python interpreter location if it doesn’t detect automatically

Leave default project name - untitled

Click “Create”

Step6. Now Right Click on our project node:

DATAPRO COMPUTERS 27
Step7. Now edit [Link] as follows:

Note: Keyboard Shortcut to run a python program: Shift + F10

Note: To increase font size, press Ctrl + Shift + A and type increase font size, click on the option
multiple times.

DATAPRO COMPUTERS 28
Program: GUI Programming in Python – demo
(Tkinter Programming)

Note: Tkinter (Tk-inter) is the standard GUI library for Python. Python when combined with Tkinter
provides a fast and easy way to create GUI applications. Tkinter provides a powerful object-oriented
interface to the Tk GUI toolkit.

Output:

Keep clicking on the “wish” button (say for 5 times):

Note: We could have done this program on PyCham IDE as well. So that we can take advantage of
Intellisense of the PyCharm IDE

Some explanation:

We are using three widgets:


DATAPRO COMPUTERS 29
Tk is the class which we use to create the root window/container – the main window of our
application. Our application should only have one root, but it is possible for us to create other
windows which are separate from the main window.

Button and Label are the other two classes/widgets/components. Each of them has a parent
widget, which we pass in as the first parameter to the constructor – we have put the label and
button inside the main window, so they are the main window’s children in the tree. We use the
pack() on each widget to position it inside its parent

[Link]() is a method on the main window which we execute when we want to run our
application. This method will loop forever, waiting for events from the user, until the user exits the
program – either by closing the window, or by terminating the program with a keyboard interrupt in the
console.

Program: GUI Programming using PyQt5 Module

Note: PyQt is a GUI widgets toolkit. It is a Python interface for Qt, one of the most powerful, and
popular cross-platform GUI library. PyQt is a blend of Python programming language and the Qt library.

PyQt API is a set of modules containing a large number of classes and functions. While QtCore module
contains non-GUI functionality for working with file and directory etc., QtGui module contains all the
graphical controls.

Step1. Installing PyQt5

Step2. Write the following program:

Note: Better to use PyCharm IDE for Intellisense.

Note: In PyCharm IDE if we get reference warnings at the imports even though we have installed the
modules in the correct locations, then File → Invalidate Caches/Restart

DATAPRO COMPUTERS 30
Note: If we get PyQt5 module not found error then check where it got installed:

Copy the PyQt5 folders and paste them into the appropriate path for the Python IDLE

DATAPRO COMPUTERS 31
Note:

Every PyQt5 application must create an application object. The [Link] parameter is a list of arguments
from a command line. Python scripts can be run from the shell. It is a way how we can control the
startup of our scripts.

Note:
The mainloop receives events from the window system and dispatches them to the application widgets.
The mainloop ends if we call the exit() method or the main widget is destroyed.

[Link](app.exec_()): Tells that if a termination signal is captured, exit the program

Step3. Run the program

Output:

Click the button:

Program: GUI Programming using PyQt5 Designer

DATAPRO COMPUTERS 32
Note: Using its simple drag and drop interface, a GUI interface can be quickly built without having to
write the code. It is however, not an IDE such as Visual Studio. Hence, Qt Designer does not have the
facility to debug and build the application.

Step1. Install PyQt5 Designer

Step2. Open PyQt5 Designer Application

Click Windows Start Button → In the Search box just type “Designer”

One should see the following window:

(OR)

In the command prompt itself, just type “[Link]”

Step3. Design the Interface via the QT Designer Tool

(a)

DATAPRO COMPUTERS 33
(b) Change the Window Dimensions

(c) Save the File - [Link]

Update the UI further:

(d) Drag and Drop a Button:

DATAPRO COMPUTERS 34
(e) Change it’s label:

(f) Change the Window Title:

DATAPRO COMPUTERS 35
Note: Signals and Slots:

Unlike a console mode application, which is executed in a sequential manner, a GUI based application is
event driven. Functions or methods are executed in response to user’s actions like clicking on a button,
selecting an item from a collection or a mouse click etc., called events.

Widgets used to build the GUI interface act as the source of such events. Each PyQt widget, which is
derived from QObject class, is designed to emit ‘signal’ in response to one or more events. The signal on
its own does not perform any action. Instead, it is ‘connected’ to a ‘slot’. The slot can be any callable
Python function.

Note: This .ui file contains XML representation of widgets and their properties in the design.

If we open it in editor like EditPlus, we can see the XML script:

<?xml version="1.0" encoding="UTF-8"?>


<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>200</height>
</rect>
</property>
<property name="windowTitle">
<string>Visakhapatnam Message</string>
</property>
DATAPRO COMPUTERS 36
<widget class="QWidget" name="centralwidget">
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>144</x>
<y>50</y>
<width>91</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>Welcome</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>

Note: So, it uses Android App Development style for designing the UI

Step4. Create a python file which uses the above Python GUI file

DATAPRO COMPUTERS 37
Step5. Run the Application

Output:

Click the button:

Program: Creating a GUI Calculator using PyQt5 Designer

Note: In this example, we shall be using the widgets: QMainWindow, QPushButton, QLineEdit,
QComboBox, QLabel, QStatusBar and QMessageBox

Step1. Open the PyQt5 Designer

The final design of the UI should be as follows:

DATAPRO COMPUTERS 38
Here we have used the following Widgets:
Line Edit (under “Input Widgets” Category)
Combo Box (under “Input Widgets” Category)
Label (under “Display Widgets” Category)
Push Button (under “Buttons” Category)

Step2. Drag and Drop the required Widgets on to the Designer window

To Increase Font size for the label (=) → Select it → in its Property Editor window → Go to Font
Property and increase the font size

Step3. To Remove the MenuBar, go to ObjectInspector Window → select MenuBar , Right Click →
Remove

Step4. For the 1st TextBox(Line Edit Widget) → Set the ToolTip property value to: Enter First Number

This tooltip message would be displayed when the user hovers the mouse pointer over it.

Repeat the same for 2nd and 3rd TextBoxes as well.

Note: For the 3rd TextBox (Line Edit Widget) → Set the ReadOnly property to True

Step5. To add items to the ComboBox, just double click on it and add the items

After adding all the items, click on “OK”

Step6. For “Clear” Button change the object name

DATAPRO COMPUTERS 39
Then change its “text” property to “Clear”

Similarly, for the “Calculate” button, change the object name to: calculateButton and its text

Step7. Adding functionality to the “Clear” Button

(a) Select the “Clear” Button and go to its “Signal/Slot” Editor Window:

Click it’s “+” Button

Initially it shows:

DATAPRO COMPUTERS 40
(b) Double on each of the “Sender”, “Signal”, “Receiver” and “Slot” and select appropriate items

Similarly adding two more signals:

Step8. To Check:

DATAPRO COMPUTERS 41
Enter two numbers and click “Clear” button, we can notice that the two “Line Edit” widgets gets cleared.

Step9. If we click on “Form” Menu → View Code

We can notice the C++ Code behind it:

Note: If we get an error like:

Solution:

DATAPRO COMPUTERS 42
Note: Just make a Copy Paste don’t do Cut and Paste

Step10. We can convert this C++ Code into Python Code using the following tool:

Note: We can notice that a new python file gets created.

Step11. Open the [Link] in the Python Editor:

We can see the following:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file '[Link]'


#
DATAPRO COMPUTERS 43
# Created by: PyQt5 UI code generator 5.10.1
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
def setupUi(self, MainWindow):
[Link]("MainWindow")
[Link](400, 200)
[Link] = [Link](MainWindow)
[Link]("centralwidget")
self.lineEdit1 = [Link]([Link])
[Link]([Link](10, 40, 61, 31))
[Link]("lineEdit1")
[Link] = [Link]([Link])
[Link]([Link](90, 40, 51, 31))
font = [Link]()
[Link](14)
[Link](True)
[Link](75)
[Link](font)
[Link]("comboBox")
[Link]("")
[Link]("")
[Link]("")
[Link]("")
self.lineEdit2 = [Link]([Link])
[Link]([Link](160, 40, 61, 31))
[Link]("lineEdit2")
[Link] = [Link]([Link])
[Link]([Link](240, 50, 21, 21))
font = [Link]()
[Link](14)
[Link](True)
[Link](75)
[Link](font)
[Link]("label")
self.lineEdit3 = [Link]([Link])
[Link]([Link](290, 40, 81, 31))
[Link](True)
[Link]("lineEdit3")
[Link] = [Link]([Link])
[Link]([Link](90, 90, 81, 31))
[Link]("clearButton")
[Link] = [Link]([Link])
[Link]([Link](220, 90, 81, 31))
[Link]("calculateButton")
self.label_2 = [Link]([Link])
self.label_2.setGeometry([Link](106, 10, 171, 20))

DATAPRO COMPUTERS 44
font = [Link]()
[Link](14)
[Link](True)
[Link](75)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
[Link]([Link])
[Link] = [Link](MainWindow)
[Link]("statusbar")
[Link]([Link])

[Link](MainWindow)
[Link]([Link])
[Link]([Link])
[Link]([Link])
[Link](MainWindow)

def retranslateUi(self, MainWindow):


_translate = [Link]
[Link](_translate("MainWindow", "My Simple Calculator"))
[Link](_translate("MainWindow", "First Number"))
[Link](0, _translate("MainWindow", "+"))
[Link](1, _translate("MainWindow", "-"))
[Link](2, _translate("MainWindow", "*"))
[Link](3, _translate("MainWindow", "/"))
[Link](_translate("MainWindow", "Second Number"))
[Link](_translate("MainWindow", "="))
[Link](_translate("MainWindow", "Result"))
[Link](_translate("MainWindow", "Clear"))
[Link](_translate("MainWindow", "Calculate"))
self.label_2.setText(_translate("MainWindow", "Simple Calculator"))

if __name__ == "__main__":
import sys
app = [Link]([Link])
MainWindow = [Link]()
ui = Ui_MainWindow()
[Link](MainWindow)
[Link]()
[Link](app.exec_())

Step12. Create a new python file – [Link] which contains the implementation of “Calculate” Button
click functionality

DATAPRO COMPUTERS 45
Step13. Run the Program

Output:

Note: If we give invalid input like a non-numerical value or dividing integer with zero:

Converting .py to .exe (Steps)

Note: The advantage is we need not open the Python IDLE to run the program. And this file can be run
on any Windows Computer by just double clicking on it.

Step1.

DATAPRO COMPUTERS 46
Step2. Now go to the folder where python code file is located

Step3. Give the following command to generate the .exe file

DATAPRO COMPUTERS 47


Note: Notice that the following files and directories get generated:

Note: the flag –w stands for “windowed/GUI application”

Note: this flag prevents a console window from being displayed when the application is run.

DATAPRO COMPUTERS 48
Step4. Open [Link] (Application)

Double Click on it, we can find our application up and running:

Step5. If we don’t want so many files to be generated:

DATAPRO COMPUTERS 49


Note: The flag “F” indicates only one file should be generated (package everything into a single
executable)

Step6. To set a custom icon to our GUI App

DATAPRO COMPUTERS 50
Place an icon file in the current working directory:

Now give the following command:



Note: Notice that the icon got changed:

DATAPRO COMPUTERS 51
Note: It may not show the updated icon. Just place it in some other location, we can see the change.

Place the file on the desktop, double click and run the application

DATAPRO COMPUTERS 52
Chapter-4: Sending Email

Program: Sending Email from Python code – Ex1

Note: The smtplib modules is useful for communicating with mail servers to send mail.
sendmail() arguments: sender’s email id, receiver’s email id and body

Output (just check the destination email):

Program: Sending Email from Python code – Ex2 (Sending to Multiple Receivers)

Program: Sending Email from Python code – Ex3 (Sending message with a subject)

DATAPRO COMPUTERS 53
Output (check the receiver’s email account):

Program: Sending Email from Python code – Ex4 (Sending message with File Attachment)

Note: Try attaching different types of files as well like jpg, pdf

Note: If any of the email ids doesn’t exist then that would just ignored.

Note: If any of the files doesn’t exist then the program won’t work.

DATAPRO COMPUTERS 54
Output:

DATAPRO COMPUTERS 55
Chapter-5: Python - XML and JSON Processing

Program: XML Data processing Demo1

Note: We shall use the [Link] module which implements a simple and efficient API for
parsing XML data.

Let’s consider the following XML File:



Step1. Create the following program

Step2. Run the program

Output:

DATAPRO COMPUTERS 56
Program: XML Data processing Demo2 – findall()

Let’s consider the XML file:

Step1. Write the following program

Step2. Run the program

Output:

DATAPRO COMPUTERS 57
Program: XML Data Processing Demo3 – iter()

Note: We shall use the previous XML file

Step1. Write the following program

Step2. Run the program

Output:

Program: Python JSON Data processing Demo1

Let’s consider the following JSON file:

DATAPRO COMPUTERS 58
Step1. Write the following program

Step2. Run the program

Output:

Note: Notice that “address” is an object (notice braces) and “phoneNumber” is an array (notice
squarebrackets)

Program: Python JSON data processing Demo2 – traversing the details of all Employees

DATAPRO COMPUTERS 59
Output:

Program: Python JSON Data processing Demo3 – getting a particular key’s value

DATAPRO COMPUTERS 60
Step1. Write the following program

Step2. Run the program

Output:

Program: Python JSON Data Processing Demo4 – getting nth key’s value in an array

Step1. Write the following program

Step2. Run the program

Output:

Program: Python JSON Data Processing Demo5 – len()

Step1. Write the following program

DATAPRO COMPUTERS 61
Step2. Run the program

Output:

DATAPRO COMPUTERS 62
Chapter-6: Python Database Programming - Accessing MySQL Database

Program: To connect to MySQL Database and get all the records

Note: Let’s begin with creating a new Database in MySQL Database Engine, Create a user, Create a table
and put some records into it. Then we shall write a program to get those records

Part-1
(Download, install and configure XAMPP Server)
Step1.

XAMPP: Cross Platorm (X), Apache Web Server, MySQL Database, PHP and Perl

Step2.

Click on “XAMPP for Windows”, download starts immediately

DATAPRO COMPUTERS 63
[Link] gets downloaded

Step3. Double Click on it and install

DATAPRO COMPUTERS 64
DATAPRO COMPUTERS 65
Click “Finish”, control panel gets started.

Step4. From the “Start” Menu:

DATAPRO COMPUTERS 66
Step5. Click on “NetStat” button, to check which port numbers are busy

DATAPRO COMPUTERS 67
So, at Port 80, some process is already running. So we have change the port id of Apache HTTP Server
whose default port id is 80.

Step6. For that, click on “Config” button of “Apache”

Open the [Link] file and modify the http listening port id:

Before Modification:

After modification:

Save it

Step7. Now Click on “Start” button of “Apache”

DATAPRO COMPUTERS 68
Now Apache server is up and running

Step8. For MySQL, also, click on “Start”

Note: Remember the Port id: it is 8080 for Apache Server.

Now MySQL also is up and running

Note: Now the following are ready:

• web server
• installation of MySQL Database

Step9. Open any web browser and in the address bar, type: localhost:8080

Step10. Click on phpMyAdmin link at the top right:

DATAPRO COMPUTERS 69
Part-2
(In MySQL, Create a Database, a table with some data and a user to access it)

Step1. Make Sure that XAMPP Server stands installed

Step2. Run the Web Server and MySQL Server

Step3. Create a new Database

Name it as “for_python_db1”

Step4. Create a new User

DATAPRO COMPUTERS 70
DATAPRO COMPUTERS 71
Step5. Creating a table in our database

DATAPRO COMPUTERS 72
Click “Save”

DATAPRO COMPUTERS 73
Step6. Now add data/records

Similarly add few more records

Finally, we have 5 records:

DATAPRO COMPUTERS 74
Part-3
(Connecting to the above MySQL database via a Python Program)

Step1. Install PyMySQL

Note: PyMySQL is an interface for connecting to a MySQL database server from a Python Program. It
implements the Python Database API v2.0 and contains a pure-Python MySQL client library.

Step2. Write the following program

Step3. Run the Program

DATAPRO COMPUTERS 75
Note: Make sure that XAMPP Server is up and running

Output:

Program: Get a particular Record(s) in a table

Output:

Program: Add/Insert Record

Step1. Write the following program

DATAPRO COMPUTERS 76
Step2. Run the program

Output:

Step3. Verify in the database

Program: Update a Record

Step1. Write the following program

Step2. Run the program

DATAPRO COMPUTERS 77
Output:

Step3. Verify in the database

Program: Delete a Record

Step1. Write the following program

Step2. Run the Program

Output:

Step3. Verify in the Database

DATAPRO COMPUTERS 78
Program: Establishing a connection with a public MySQL Database Engine Server and fetching all
records in a table

Part-1
(Creating a Live Public MySQL Database table)

Step1. Create account in [Link]

DATAPRO COMPUTERS 79
Step2. Create a new Database:

Step3. Check your email to know the Database credentials

Note: Name means Database name

Step4. Now we need PHPMyAdmin console to create tables etc

For that 1st click “PHPMyAdmin” link:

Automatically it gets redirected to:

DATAPRO COMPUTERS 80
Note: We can know these credentials from our Email

Step5. Now we land in:

Note: We are already aware of this console.

Step6. Now create a table

DATAPRO COMPUTERS 81
Step7. Add Records

DATAPRO COMPUTERS 82
Part-2
Step1. Write the following Python program

Step2. Run the program

Output:

DATAPRO COMPUTERS 83
Chapter-7: Python Database Programming – accessing Oracle Database

Establishing a connection with the Oracle Database and fetching all records in a table

Part-1
(Download, install Oracle Database Engine/Server)

Step1. Download Oracle Database Express Edition 11g

Note: Make sure that the Oracle Database software is a 64-bit version.

[Link]

After download:

(OR)

Go to: [Link]

DATAPRO COMPUTERS 84
Step2. Extract it to a folder

Step3. Double click “Setup” file

Step4.

DATAPRO COMPUTERS 85
During installation enter the DBA’s password and remember it, we need it for later use.

Username: “system”
Password: say “datapro”

Unlock “hr” account

Step1.

Step2. Connect to “hr” account/Schema

SQL>connect hr/hr

Now we get a message: account is locked.

Now we have to do the following procedure to unlock the hr account:

Step3. 1st connect to DBA account

SQL> Connect system/datapro

Step4. Now unlock the hr account

SQL> alter user hr account unlock identified by hr;

Note: “identified by hr” is required in 11g XE whereas it is not required in 10g XE

We should get the message: User altered.

Step5. Disconnect from DBA account

SQL> disconnect system

Step6. Now reconnect to hr account

SQL> connect hr/hr

DATAPRO COMPUTERS 86
Note: select * from tab;

Note: Let’s use the built-in “jobs” table

Part-2
(Connecting to the above Oracle database via a Python Program)

Step1. Installing Oracle Database’s Driver

Step2. Verify the installation

Step3.

DATAPRO COMPUTERS 87
Step4. Run the program

Note: The problem is we are using Python 32-bit but our Oracle driver is 64-bit and Oracle Database
server is 64-bit too

Solution: We need Python 64-bit software

Step5. Verify the Python Software OS architecture

Step6. Install 64-bit Python

(a) Download it

DATAPRO COMPUTERS 88
(b) Uninstall the 32-bit Python

(c) Install the 64-bit Python

Step7. Re-Verify

Step7. Re-Install the oracle driver

Step8. Re-run the program

Output:

Program: Add/Insert Record


DATAPRO COMPUTERS 89
Output:

Program: Update a Record

Output:

Verify:

DATAPRO COMPUTERS 90
Program: Invoking a stored procedure in Oracle DB

Stored Procedures and Functions in Oracle Database

A stored procedure is a set of Structured Query Language (SQL) statements with an assigned name,
which are stored in a relational database management system as a group, so it can be reused and
shared by multiple programs.

Note: A function is a subprogram that computes and returns a value whereas a procedure doesn’t
return any value.

Once compiled, it is a schema object known as a stored procedure or stored function, which can be
referenced or called any number of times by multiple applications connected to Oracle Database XE.
Both stored procedures and functions can accept parameters when they are executed (called).

Step1. Create a procedure

Note: Remember to put the “commit” statement, for python, explicit commit is required unlike JDBC in
Java.

Test it:
sql> exec updateraju1(201, 60000);

Step2. Invoke it from a Python Program

DATAPRO COMPUTERS 91
Output:

Before:

After (executing the Python Program)e:

Program: Invoking a stored procedure in Oracle DB with “out” parameters

Step1. Create a stored procedure with “out” parameter

Requirement: To get an employee’s first name the “out” parameter

Test it:

DATAPRO COMPUTERS 92
Step2. Invoke the above procedure

Output:

Program: Invoking a stored function in Oracle DB

Step1. Create a function in Oracle

Test it:
sql>select getavgraju1(100) from dual;

Step2. Invoke it from a Python Program

DATAPRO COMPUTERS 93
Output:

DATAPRO COMPUTERS 94
Chapter-8: Python Database Programming – accessing MongoDB Database

Program1: Establishing a connection with the MongoDB Database and fetching all documents in a
collection

Part-1

Installing MongoDB

Note: The installers for MongoDB are available in both the 32-bit and 64-bit format. The 32-bit installers
are good for development and test environments. But for production environments you should use the
64-bit installers. Otherwise, you can be limited to the amount of data that can be stored within
MongoDB.

Step1.

Under “Community Server”

An installer will be downloaded…


DATAPRO COMPUTERS 95
Step2. Double click the installer:

DATAPRO COMPUTERS 96
Step3. To Verify:

StepA:

Note: The “[Link]” – a command line application: is the actual MongoDB Database, whereas to
write the commands for adding /removing data, we have to use “[Link]”

StepB: Inside the “bin” folder window, Shift + Right Click on an empty area →

Notice that automatically, command prompt gets opened with path to “bin” directory:

StepC: To start the MongoDB Database:

DATAPRO COMPUTERS 97
Note: The problem is it looks for “data\db” directory in the C:

StepD: Create the directories:

StepE: Reopen the command prompt and restart the server:

Notice that some files get generated:

DATAPRO COMPUTERS 98
Note: Every time we want to start the MongoDB Database server to have to go to the “bin” directory,
instead we can set path to this directory:

Notice that we can’t type anything here unlike oracle database.

DATAPRO COMPUTERS 99
To type the commands to add and delete data, we have to open “[Link]”

StepF:

Just take a look at the database server side:

Part-2

Step1. Make sure that MongoDB Database Engine is up and running


(Use the commands – mongod and mongo)

DATAPRO COMPUTERS 100


Step2. Create a new Database

Step3. Create a new Collection and insert some documents into it

DATAPRO COMPUTERS 101


Note: Data in MongoDB is represented (and stored) using JSON-style documents.

Step4. View the documents

Part-3

Step1. Install MongoDB Python Driver

DATAPRO COMPUTERS 102


Step2. Write the following program

Step3. Run the program

Output:

Program: To exclude the _id field in a find query in pymongo

Step1. Write the following program

Step2. Run the program

Output:

DATAPRO COMPUTERS 103


Program: Get a particular Document(s) in a Collection – condition with logical operator

Step1. Write the following program

Step2. Run the program

Output:

Program: Get a particular Document(s) in a Collection – condition with equality

Step1. Write the following program

Step2. Run the program

Output:

Program: Add a Document to a MongoDB Database Collection


(By taking input dynamically)

DATAPRO COMPUTERS 104


Step1.

Step2. Run the program

Output:

Step3: Verify in the database:

Program: Update a Document

DATAPRO COMPUTERS 105


Step1. Write the following program

Step2. Run the program

Output:

Step3. Verify in the database

Program: Delete a Document

Step1. Write the following program

DATAPRO COMPUTERS 106


Step2. Run the program

Step3. Verify in the Database

Note: Notice that Employee with “EmpId” : 104 is no more present

Setting up a Remote live public MongoDB Database

Creating a Cluster in MongoDB Atlas

Step1.

DATAPRO COMPUTERS 107


Click the 1st link:

Step2. Get Registered

DATAPRO COMPUTERS 108


Step3. Choose a Plan

DATAPRO COMPUTERS 109


Step4. Then create a new Cluster

DATAPRO COMPUTERS 110


Step5. Enter Database user details

DATAPRO COMPUTERS 111


DATAPRO COMPUTERS 112
Step6.

DATAPRO COMPUTERS 113


DATAPRO COMPUTERS 114
DATAPRO COMPUTERS 115
Step7. Connect to Cluster

DATAPRO COMPUTERS 116


Click “Close”

Take a note of the connection string, which we shall use in the client program

mongodb+srv://bvraju2020:<password>@[Link]/test

Step8. Check the available “collections”:

DATAPRO COMPUTERS 117


Installing Mongo DB Compass Tool and connecting to the MongoDB Atlas Server

Step1. Download the MongoDB Compass GUI Tool (A MongoDB Client)

[Link]

DATAPRO COMPUTERS 118


(OR)

Note: Choose “msi” type instead of “zip” type

We can see the following in our Downloads folder:

Step2. Just open the above .exe file

Step3. Provide the authentication details

DATAPRO COMPUTERS 119


Step4. Create a new Database and new collection in the MongoDB Compass Tool

DATAPRO COMPUTERS 120


Step5. Add documents to our new collection

DATAPRO COMPUTERS 121


DATAPRO COMPUTERS 122
Similarly insert few more documents

Step6. View them on the Live Server (Cloud Cluster)

1st Login:

DATAPRO COMPUTERS 123


DATAPRO COMPUTERS 124
Program: Python Program accessing the MongoDB Live Server

Step1.

Step2. Run the Program

We may get the following error:

Step3. Solution:

DATAPRO COMPUTERS 125


Install “dnspython” package

Step4. Re-run the program

Output:

DATAPRO COMPUTERS 126


Chapter-9: Python Integration with Firebase Database
(Using python-firebase and Pyrebase packages)

What is Firebase

Firebase is essentially a real time database. The Firebase Realtime Database is a cloud-hosted NoSQL database
that lets you store and sync data between your users in Realtime.

Realtime Vs non-Realtime Databases

In normal database when data at back end get updated, to get the newest data, we need to refresh our browser
or android app screen in order to get updated data.

On other hand in Realtime databases like firebase we don't even need to do any refresh. Changes done from any
other client browser/Android App will be reflected to all connected clients without making any server-side call.

The data appears as JSON files.

Setting up the Firebase Database

Step1. [Link]

Step2.

DATAPRO COMPUTERS 127


DATAPRO COMPUTERS 128
DATAPRO COMPUTERS 129
Choose Firebase database type

Step1.

(OR)

Choose:

DATAPRO COMPUTERS 130


Note: Firebase database comes in two forms:

Note: Firestore is similar to MongoDB (Collections, Documents, Key-Value pairs)

Firebase Vs Firestore

Cloud Firestore is Firebase’s newest flagship database for mobile apps. It is a successor to the Realtime Database
with a new and more intuitive data model. Cloud Firestore is richer, faster, and more scalable than the Realtime
Database.

Realtime Database stores data as one large JSON tree which makes it easier to store simple data but harder to
organize complex, hierarchical data at scale.

Cloud Firestore stores data in documents arranged in collections. Simple data is stored in documents, which is
easy and similar to the way data is stored in JSON. Complex, hierarchical data is conveniently organized at scale
using subcollections within documents

Step2.

Remember the URL:

DATAPRO COMPUTERS 131


Note: If we want to create one more database, then we have to pay for it

Program: To add a new Record/Object in Firebase database (Connecting to Firebase using Python)

Note: We have multiple options regarding which package to install.

Note: Recollect our Project name: python-firebase-integration1

Step1. python-firebase highly makes use of the requests library so before you begin, you need to have that
package installed.

DATAPRO COMPUTERS 132


Step2. Install firebase package

Step3. Now write the following code

Note: Here “employees” would be the name of our collection.

Note: FirebaseApplication() takes two parameters; one is URL of database and second is authentication details for
database. But we don’t have any rules so we are passing None.

Step4. Run the program

DATAPRO COMPUTERS 133


Step5. Fixing the above error

Reason for the above error:

async is a keyword in Python 3.7 and it is used as module name in firebase package.

Now fixing it:

(a) Go to the location where “firebase” package has been installed

(b) Rename the [Link] to something else:

(c) Edit __init__.py file

DATAPRO COMPUTERS 134


DATAPRO COMPUTERS 135
(d) Now edit [Link]

DATAPRO COMPUTERS 136


Step6. Test the import statement

Now it is working

Step7. Re-run the program

Note: Now there is another problem


DATAPRO COMPUTERS 137
Step8. Fixing the above problem:

Step9. Re-run the program

Note: Now there is another problem

Step10. Fixing the above error

Open Firebase console

DATAPRO COMPUTERS 138


Step11. Re-run the program

Step12. Verify in the Firebase Server

DATAPRO COMPUTERS 139


Step13. Now add few more employees’ data by changing the values in the program and re-running

Step14. Verify in the Firebase Server

Expand them:

DATAPRO COMPUTERS 140


Program: Get all records from Firebase

Step1. Write the following code

Step2. Run the program

To display the output in a more readable way:

DATAPRO COMPUTERS 141


Program: Get a particular record from firebase

Step1. Write the following code

Step2. Run the program

Program: Update a record in Firebase

Step1. Write the following code

DATAPRO COMPUTERS 142


Step2. Run the program

Step3. Verify in the firebase server

Program: Delete a record in Firebase

Step1. Write the following code

DATAPRO COMPUTERS 143


Step2. Run the program

Step3. Verify in the firebase server

DATAPRO COMPUTERS 144


Round-2
Adding Security to our Firebase database (Authentication)

Firebase – Authentication Steps Demo

Step1.

DATAPRO COMPUTERS 145


DATAPRO COMPUTERS 146
Step2.

DATAPRO COMPUTERS 147


Note: Copy this code

Click “Continue to console” button

Step3.

DATAPRO COMPUTERS 148


DATAPRO COMPUTERS 149
Step4. Create some users (admins)

DATAPRO COMPUTERS 150


Step5. Create a database

DATAPRO COMPUTERS 151


Program1: Establishing authenticated connection to the Firebase database

DATAPRO COMPUTERS 152


Step1. Install the following package:


Note: make sure that we use the latest version of “pip”

If it doesn’t work, try:

pip3 install --upgrade setuptools

pip3 install --upgrade gcloud

then:

DATAPRO COMPUTERS 153


Step2. Write the following code

DATAPRO COMPUTERS 154


Note: Make sure that both the keys and values are given in quotes.
Note: Make sure that the key “databaseURL” is present.

Step3. Run the program

Output:

Note: The user token will need to be passed as a parameter to the get(), push(), set(), update() and remove()
functions in order to authorize the actions.

Step4. If we enter invalid credentials, eg: incorrect password

DATAPRO COMPUTERS 155


Program: Adding data to the Firebase database – using Authentication

Step1. Write the following code

Note: Here “bvrajuCourses” is the collection name.


“course” variable name represents a document.
The constants “course” and “fee” represents fields in a document.

Step2. Run the program

Step3. Run the program with different course details and then Verify in the Firebase database

DATAPRO COMPUTERS 156


Program: Add records with Custom ids

Step1. Write the following code

DATAPRO COMPUTERS 157


Step2. Run the program

Step3. Add few more records and Verify in the Firebase database

Note: If we mention an existing course id: say “datapro1” again, then course gets updated. So these ids are
unique.

Program: Getting data from the Firebase database – using Authentication

Step1. Write the following code

DATAPRO COMPUTERS 158


Step2. Run the program

Output:

DATAPRO COMPUTERS 159

You might also like