set3
set3
What is a Module
A module is a file containing a set of codes or a set of functions which can be
included to an application. A module could be a file containing a single variable, a
function or a big code base.
To import the file we use the import keyword and the name of the file only.
For as an example we will import math module
1 import math
2 print(math.pi) # 3.141592653589793, pi constant
3 print(math.sqrt(2)) # 1.4142135623730951, square root
4 print(math.pow(2, 3)) # 8.0, exponential function
5 print(math.floor(9.81)) # 9, rounding to the lowest
6 print(math.ceil(9.81)) # 10, rounding to the highest
7 print(math.log10(100)) # 2, logarithm with 10 as base
Now, we have imported the math module which contains lots of function which
can help us to perform mathematical calculations.
To check what functions the module has got, we can use documentation for that
said module or we can use help(math) , or dir(math) . This will display the
available functions in the module.
If we use
1 import math
Example
1 import string
2 print(string.ascii_letters) #
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
3 print(string.digits) # 0123456789
4 print(string.punctuation) # !"#$%&'()*+,-./:;<=>?
@[\]^_`{|}~
Random Module
Creating a Module
To create a module we write our codes in a python script and we save it as a .py
file. Create a file named main.py inside your project folder.
1 # main.py file
2 def generate_full_name(firstname, lastname):
3 return firstname + ' ' + lastname
Create call.py file in your project directory and import the main.py file.
1 # call.py file
2 import mymodule
3 print(mymodule.generate_full_name('John', 'Doe')) # John Doe
Python Package Manager
What is PIP ?
PIP stands for P referred installer program . We use pip to install different Python
packages. Package is a Python module that can contain one or more modules or
other packages. A module or modules that we can install to our application is a
package.
Installing PIP
If you did not install pip, let us install it now. Go to your terminal or command
prompt and copy and paste this:
1 pip --version
Let us start using numpy. Open your python interactive shell, write python and
then import numpy as follows:
1 asabeneh@Asabeneh:~$ python
2 Python 3.9.6 (default, Jun 28 2021, 15:26:21)
3 [Clang 11.0.0 (clang-1100.0.33.8)] on darwin
4 Type "help", "copyright", "credits" or "license" for more
information.
5 >>> import numpy
6 >>> numpy.version.version
7 '1.20.1'
8 >>> lst = [1, 2, 3,4, 5]
9 >>> np_arr = numpy.array(lst)
10 >>> np_arr
11 array([1, 2, 3, 4, 5])
12 >>> len(np_arr)
13 5
14 >>> np_arr * 2
15 array([ 2, 4, 6, 8, 10])
16 >>> np_arr + 2
17 array([3, 4, 5, 6, 7])
18 >>>
1 asabeneh@Asabeneh:~$ python
2 Python 3.9.6 (default, Jun 28 2021, 15:26:21)
3 [Clang 11.0.0 (clang-1100.0.33.8)] on darwin
4 Type "help", "copyright", "credits" or "license" for more
information.
5 >>> import pandas
This section is not about numpy nor pandas, here we are trying to learn how to
install packages and how to import them. If it is needed, we will talk about
different packages in other sections.
Let us import a web browser module, which can help us to open any website. We
do not need to install this module, it is already installed by default with Python 3.
For instance if you like to open any number of websites at any time or if you like to
schedule something, this webbrowser module can be used.
1 import webbrowser # web browser module to open websites
2
3 # list of urls: python
4 url_lists = [
5 'http://www.python.org',
6 'https://www.linkedin.com/in/asabeneh/',
7 'https://github.com/Asabeneh',
8 'https://twitter.com/Asabeneh',
9 ]
10
11 # opens the above list of websites in a different tab
12 for url in url_lists:
13 webbrowser.open_new_tab(url)
Uninstalling Packages
If you do not like to keep the installed packages, you can remove them using the
following command.
List of Packages
To see the installed packages on our machine. We can use pip followed by list.
1 pip list
Show Package
To show information about a package
PIP Freeze
Generate installed Python packages with their version and the output is suitable to
use it in a requirements file. A requirements.txt file is a file that should contain all
the installed Python packages in a Python project.
The pip freeze gave us the packages used, installed and their version. We use it
with requirements.txt file for deployment.
We will see get, status_code, headers, text and json methods in requests module:
get(): to open a network and fetch data from url - it returns a response object
status_code: After we fetched data, we can check the status of the operation
(success, error, etc)
headers: To check the header types
text: to extract the text from the fetched response object
json: to extract json data Let's read a txt file from this
website, https://www.w3.org/TR/PNG/iso_8859-1.txt .
1 <Response [200]>
2 200
3 {'date': 'Sun, 08 Dec 2019 18:00:31 GMT', 'last-modified':
'Fri, 07 Nov 2003 05:51:11 GMT', 'etag': '"17e9-
3cb82080711c0;50c0b26855880-gzip"', 'accept-ranges':
'bytes', 'cache-control': 'max-age=31536000', 'expires':
'Mon, 07 Dec 2020 18:00:31 GMT', 'vary': 'Accept-Encoding',
'content-encoding': 'gzip', 'access-control-allow-origin':
'*', 'content-length': '1616', 'content-type': 'text/plain',
'strict-transport-security': 'max-age=15552000;
includeSubdomains; preload', 'content-security-policy':
'upgrade-insecure-requests'}
Let us read from an API. API stands for Application Program Interface. It is a
means to exchange structure data between servers primarily a json data. An
example of an API:https://restcountries.eu/rest/v2/all . Let us read this API
using requests module.
1 import requests
2 url = 'https://restcountries.eu/rest/v2/all' # countries
api
3 response = requests.get(url) # opening a network and
fetching a data
4 print(response) # response object
5 print(response.status_code) # status code, success:200
6 countries = response.json()
7 print(countries[:1]) # we sliced only the first country,
remove the slicing to see all countries
1 <Response [200]>
2 200
3 [{'alpha2Code': 'AF',
4 'alpha3Code': 'AFG',
5 'altSpellings': ['AF', 'Afġānistān'],
6 'area': 652230.0,
7 'borders': ['IRN', 'PAK', 'TKM', 'UZB', 'TJK', 'CHN'],
8 'callingCodes': ['93'],
9 'capital': 'Kabul',
10 'cioc': 'AFG',
11 'currencies': [{'code': 'AFN', 'name': 'Afghan afghani',
'symbol': '؋'}],
12 'demonym': 'Afghan',
13 'flag': 'https://restcountries.eu/data/afg.svg',
14 'gini': 27.8,
15 'languages': [{'iso639_1': 'ps',
16 'iso639_2': 'pus',
17 'name': 'Pashto',
18 'nativeName': '}'پښتو,
19 {'iso639_1': 'uz',
20 'iso639_2': 'uzb',
21 'name': 'Uzbek',
22 'nativeName': 'Oʻzbek'},
23 {'iso639_1': 'tk',
24 'iso639_2': 'tuk',
25 'name': 'Turkmen',
26 'nativeName': 'Türkmen'}],
27 'latlng': [33.0, 65.0],
28 'name': 'Afghanistan',
29 'nativeName': ''افغانستان,
30 'numericCode': '004',
31 'population': 27657145,
32 'region': 'Asia',
33 'regionalBlocs': [{'acronym': 'SAARC',
34 'name': 'South Asian Association for
Regional Cooperation',
35 'otherAcronyms': [],
36 'otherNames': []}],
37 'subregion': 'Southern Asia',
38 'timezones': ['UTC+04:30'],
39 'topLevelDomain': ['.af'],
40 'translations': {'br': 'Afeganistão',
41 'de': 'Afghanistan',
42 'es': 'Afganistán',
43 'fa': ''افغانستان,
44 'fr': 'Afghanistan',
45 'hr': 'Afganistan',
46 'it': 'Afghanistan',
47 'ja': 'アフガニスタン',
48 'nl': 'Afghanistan',
49 'pt': 'Afeganistão'}}]
We use json() method from response object, if the we are fetching JSON data. For
txt, html, xml and other file formats we can use text.
Creating a Package
We organize a large number of files in different folders and sub-folders based on
some criteria, so that we can find and manage them easily. As you know, a module
can contain multiple objects, such as classes, functions, etc. A package can contain
one or more relevant modules. A package is actually a folder containing one or
more module files. Let us create a package named mypackage, using the following
steps:
Create a new folder named mypacakge inside the main folder Create an
empty init.py file in the mypackage folder. Create modules arithmetic.py and
greet.py with following code:
1 # mypackage/arithmetics.py
2 # arithmetics.py
3 def add_numbers(*args):
4 total = 0
5 for num in args:
6 total += num
7 return total
8
9
10 def subtract(a, b):
11 return (a - b)
12
13
14 def multiple(a, b):
15 return a * b
16
17
18 def division(a, b):
19 return a / b
20
21
22 def remainder(a, b):
23 return a % b
24
25
26 def power(a, b):
27 return a ** b
1 # mypackage/greet.py
2 # greet.py
3 def greet_person(firstname, lastname):
4 return f'{firstname} {lastname}, welcome to Parul!'
1 ─ mypackage
2 ├── __init__.py
3 ├── arithmetic.py
4 └── greet.py
Now let's open the python interactive shell and try the package we have created:
1 asabeneh@Asabeneh:~/Desktop/main$ python
2 Python 3.9.6 (default, Jun 28 2021, 15:26:21)
3 [Clang 11.0.0 (clang-1100.0.33.8)] on darwin
4 Type "help", "copyright", "credits" or "license" for more
information.
5 >>> from mypackage import arithmetics
6 >>> arithmetics.add_numbers(1, 2, 3, 5)
7 11
8 >>> arithmetics.subtract(5, 3)
9 2
10 >>> arithmetics.multiple(5, 3)
11 15
12 >>> arithmetics.division(5, 3)
13 1.6666666666666667
14 >>> arithmetics.remainder(5, 3)
15 2
16 >>> arithmetics.power(5, 3)
17 125
18 >>> from mypackage import greet
19 >>> greet.greet_person('Asabeneh', 'Yetayeh')
20 'Asabeneh Yetayeh, welcome to Parul!'
21 >>>
As you can see our package works perfectly. The package folder contains a special
file called init.py - it stores the package's content. If we put init.py in the package
folder, python start recognizes it as a package. The init.py exposes specified
resources from its modules to be imported to other python files. An empty init.py
file makes all functions available when a package is imported. The init.py is
essential for the folder to be recognized by Python as a package.
Database
SQLAlchemy or SQLObject - Object oriented access to several different
database systems
pip install SQLAlchemy
Web Development
Django - High-level web framework.
pip install django
Flask - micro framework for Python based on Werkzeug, Jinja 2. (It's BSD
licensed)
pip install flask
HTML Parser
Beautiful Soup - HTML/XML parser designed for quick turnaround
projects like screen-scraping, will accept bad markup.
pip install beautifulsoup4
PyQuery - implements jQuery in Python; faster than BeautifulSoup,
apparently.
XML Processing
ElementTree - The Element type is a simple but flexible container object,
designed to store hierarchical data structures, such as simplified XML
infosets, in memory. --Note: Python 2.5 and up has ElementTree in the
Standard Library
GUI
PyQt - Bindings for the cross-platform Qt framework.
TkInter - The traditional Python user interface toolkit.
Data Analysis, Data Science and Machine learning
Numpy: Numpy(numeric python) is known as one of the most popular
machine learning library in Python.
Pandas: is a data analysis, data science and a machine learning library in
Python that provides data structures of high-level and a wide variety of tools
for analysis.
SciPy: SciPy is a machine learning library for application developers and
engineers. SciPy library contains modules for optimization, linear algebra,
integration, image processing, and statistics.
Scikit-Learn: It is NumPy and SciPy. It is considered as one of the best
libraries for working with complex data.
TensorFlow: is a machine learning library built by Google.
Keras: is considered as one of the coolest machine learning libraries in
Python. It provides an easier mechanism to express neural networks. Keras
also provides some of the best utilities for compiling models, processing
data-sets, visualization of graphs, and much more.
Network:
requests: is a package which we can use to send requests to a server(GET,
POST, DELETE, PUT)
pip install requests
Virtual Environment
Setting up Virtual Environments
To start with project, it would be better to have a virtual environment. Virtual
environment can help us to create an isolated or separate environment. This will
help us to avoid conflicts in dependencies across projects. If you write pip freeze
on your terminal you will see all the installed packages on your computer. If we
use virtualenv, we will access only packages which are specific for that project.
Open your terminal and install virtualenv
After installing the virtualenv package go to your project folder and create a virtual
env by writing:
For Mac/Linux:
1 asabeneh@Asabeneh:~/Desktop/30DaysOfPython/flask_project\$
virtualenv venv
2
For Windows:
1 C:\Users\User\Documents\virtual\flask_project>python -m venv
venv
I prefer to call the new project venv, but feel free to name it differently. Let us
check if the the venv was created by using ls (or dir for windows command
prompt) command.
1 asabeneh@Asabeneh:~/Desktop/virtual/flask_project$ ls
2 venv/
Let us activate the virtual environment by writing the following command at our
project folder.
For Mac/Linux:
1 asabeneh@Asabeneh:~/Desktop/virtual/flask_project$ source
venv/bin/activate
1 C:\Users\User\Documents\virtual\flask_project>
venv\Scripts\activate
1 C:\Users\User\Documents\virtual\flask_project>
venv\Scripts\. activate
After you write the activation command, your project directory will start with
venv. See the example below.
1 (venv) asabeneh@Asabeneh:~/Desktop/virtual/flask_project$
Now, lets check the available packages in this project by writing pip freeze. You
will not see any packages.
We are going to do a small flask project so let us install flask package to this
project.
1 (venv) asabeneh@Asabeneh:~/Desktop/virtual/flask_project$
pip install Flask
Now, let us write pip freeze to see a list of installed packages in the project:
1 (venv) asabeneh@Asabeneh:~/Desktop/virtual/flask_project$
pip freeze
2 Click==7.0
3 Flask==1.1.1
4 itsdangerous==1.1.0
5 Jinja2==2.10.3
6 MarkupSafe==1.1.1
7 Werkzeug==0.16.0
Python-Mysql
To interact with the Mysql database we need three things
1. Python
2. Mysql workbench
3. Mysql driver
step 1
step2
To establish the connection to MySQL Database we’ll use connect( ) function.
1 mydb=sqlcon.connect(host='localhost',
user='rahul',passwd='IhateThis',database = 'test'(This one
is optional))
1 if mycon.is_connected():
2 print('Successfully Connected to MySQL database')
1 db = mydb.cursor()
step4
Create Database
Create table
step5
example
1 db.execute("show tables")
2 for i in db:
3 print(i)
Insert Data Into the Table
example