0% found this document useful (0 votes)
10 views

set3

Uploaded by

fuuny coc
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)
10 views

set3

Uploaded by

fuuny coc
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
You are on page 1/ 19

Module

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

it will import all the functions from that said module.


We can import specific functions from the said module by using

1 from {module name} import {specific function 1}, {specific


function 1},...,{specific function n}

Example

1 from math import pi, sqrt, pow


String Module
A string module is a useful module for many purposes. The example below shows
some use of the string module.

1 import string
2 print(string.ascii_letters) #
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
3 print(string.digits) # 0123456789
4 print(string.punctuation) # !"#$%&'()*+,-./:;<=>?
@[\]^_`{|}~

Random Module

1 from random import random, randint


2 print(random()) # it doesn't take any arguments; it
returns a value between 0 and 0.9999
3 print(randint(5, 20)) # it returns a random integer number
between [5, 20] inclusive

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 install pip

1 pip --version

Installing packages using pip


Let us try to install numpy, called numeric python. It is one of the most popular
packages in machine learning and data science community.

 NumPy is the fundamental package for scientific computing with Python. It


contains among other things:
 a powerful N-dimensional array object
 sophisticated (broadcasting) functions
 tools for integrating C/C++ and Fortran code
 useful linear algebra, Fourier transform, and random number capabilities

1 asabeneh@Asabeneh:~$ pip install numpy

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 >>>

Pandas is an open source, BSD-licensed library providing high-performance, easy-


to-use data structures and data analysis tools for the Python programming
language. Let us install the big brother of numpy, pandas:

1 asabeneh@Asabeneh:~$ pip install pandas

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.

1 pip uninstall packagename

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

1 pip show packagename

1 asabeneh@Asabeneh:~$ pip show pandas


2 Name: pandas
3 Version: 1.2.3
4 Summary: Powerful data structures for data analysis, time
series, and statistics
5 Home-page: http://pandas.pydata.org
6 Author: None
7 Author-email: None
8 License: BSD
9 Location: /usr/local/lib/python3.7/site-packages
10 Requires: python-dateutil, pytz, numpy
11 Required-by:

If we want even more details, just add --verbose

1 asabeneh@Asabeneh:~$ pip show --verbose pandas


2 Name: pandas
3 Version: 1.2.3
4 Summary: Powerful data structures for data analysis, time
series, and statistics
5 Home-page: http://pandas.pydata.org
6 Author: None
7 Author-email: None
8 License: BSD
9 Location: /usr/local/lib/python3.7/site-packages
10 Requires: numpy, pytz, python-dateutil
11 Required-by:
12 Metadata-Version: 2.1
13 Installer: pip
14 Classifiers:
15 Development Status :: 5 - Production/Stable
16 Environment :: Console
17 Operating System :: OS Independent
18 Intended Audience :: Science/Research
19 Programming Language :: Python
20 Programming Language :: Python :: 3
21 Programming Language :: Python :: 3.5
22 Programming Language :: Python :: 3.6
23 Programming Language :: Python :: 3.7
24 Programming Language :: Python :: 3.8
25 Programming Language :: Cython
26 Topic :: Scientific/Engineering
27 Entry-points:
28 [pandas_plotting_backends]
29 matplotlib = pandas:plotting._matplotlib

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.

1 asabeneh@Asabeneh:~$ pip freeze


2 docutils==0.11
3 Jinja2==2.7.2
4 MarkupSafe==0.19
5 Pygments==1.6
6 Sphinx==1.2.2

The pip freeze gave us the packages used, installed and their version. We use it
with requirements.txt file for deployment.

Reading from URL


By now you are familiar with how to read or write on a file located on you local
machine. Sometimes, we would like to read from a website using url or from an
API. API stands for Application Program Interface. It is a means to exchange
structured data between servers primarily as json data. To open a network
connection, we need a package called requests - it allows to open a network
connection and to implement CRUD(create, read, update and delete) operations.
In this section, we will cover only reading ore getting part of a CRUD.

Let us install requests:

1 asabeneh@Asabeneh:~$ pip install requests

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 import requests # importing the request module


2
3 url = 'https://www.w3.org/TR/PNG/iso_8859-1.txt' # text from
a website
4
5 response = requests.get(url) # opening a network and
fetching a data
6 print(response)
7 print(response.status_code) # status code, success:200
8 print(response.headers) # headers information
9 print(response.text) # gives all the text from the page

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!'

The folder structure of your package should look like this:

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

1 asabeneh@Asabeneh:~$ pip install virtualenv

Inside the virtual folder create a flask_project folder.

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

Activation of the virtual environment in Windows may very on Windows Power


shell and git bash.

For Windows Power Shell:

1 C:\Users\User\Documents\virtual\flask_project>
venv\Scripts\activate

For Windows Git bash:

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

Download Mysql Driver


open your terminal and run the command

1 pip install mysql-connector-python

Interface Python with MySQL


Now we will access and manipulate our database using a python program.
To connect our python script to a MySQL database, we need some special
functions,
for which we have a library mysql connector.

step 1

1 import mysql.connector as mysc

step2
To establish the connection to MySQL Database we’ll use connect( ) function.

<connection-object> = sqlcon.connect(host=<host-name>, user=


<username>, passwd=<password> [, database=<database>])​

1 mydb=sqlcon.connect(host='localhost',
user='rahul',passwd='IhateThis',database = 'test'(This one
is optional))

to check the connectivity is successful or not

1 if mycon.is_connected():
2 print('Successfully Connected to MySQL database')

Creating cursor instance


step3
When we send our query to the server where it gets executed, the result(what we
asked in query) is sent back to us in one go.
But if we want to access the retrieved data one row at a time, we can’t do it
directly. A control structure called database cursor can be created that gets access
of all the records retrieved as per query and allows us to traverse the result row by
row.

1 db = mydb.cursor()

cursor object connector object cursor function

step4

Create Database

1 db.execute("create database <databasename>")

Create table
step5

1 db.execute("create table <table_name>(<col_name>


<datatype>)")

example

1 db.execute("create table emp(id int, name varchar(20), age


int)")

see the tables

1 db.execute("show tables")
2 for i in db:
3 print(i)
Insert Data Into the Table

1 db.execute("insert into <table_name> values (<values>)")

example

1 db.execute("insert into Student values (1, 'John', 20)")

You might also like