mirror of
https://github.com/Asabeneh/30-Days-Of-Python.git
synced 2026-06-03 21:02:42 +08:00
packages
This commit is contained in:
parent
31d554368a
commit
249d409818
@ -9,10 +9,8 @@
|
||||
|
||||
<sub>Author:
|
||||
<a href="https://www.linkedin.com/in/asabeneh/" target="_blank">Asabeneh Yetayeh</a><br>
|
||||
<small> First Edition: Nov 22 - Dec 22, 2019</small>
|
||||
<small>Second Edition: July, 2021</small>
|
||||
</sub>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
[<< Day 19](../19_Day_File_handling/19_file_handling.md) | [Day 21 >>](../21_Day_Classes_and_objects/21_classes_and_objects.md)
|
||||
@ -39,19 +37,19 @@
|
||||
|
||||
### What is PIP ?
|
||||
|
||||
PIP stands for Preferred 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.
|
||||
PIP stands for Preferred 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.
|
||||
In programming, we do not have to write every utility program, instead we install packages and import them to our applications.
|
||||
|
||||
### Installing PIP
|
||||
|
||||
If you did not install pip, let us do it now. Go to your terminal or command prompt and copy and paste this:
|
||||
If you did not install pip, let us install it now. Go to your terminal or command prompt and copy and paste this:
|
||||
|
||||
```sh
|
||||
asabeneh@Asabeneh:~$ pip install pip
|
||||
```
|
||||
|
||||
Check if it is installed by writing
|
||||
Check if pip is installed by writing
|
||||
|
||||
```sh
|
||||
pip --version
|
||||
@ -59,16 +57,16 @@ pip --version
|
||||
|
||||
```py
|
||||
asabeneh@Asabeneh:~$ pip --version
|
||||
pip 19.3.1 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)
|
||||
pip 21.1.3 from /usr/local/lib/python3.7/site-packages/pip (python 3.9.6)
|
||||
```
|
||||
|
||||
As you can see, I am using pip version 19.3.1, if you see some number a bit below or above that, means you have pip installed.
|
||||
As you can see, I am using pip version 21.1.3, if you see some number a bit below or above that, means you have pip installed.
|
||||
|
||||
Let's check some of the packages used in the python community for different purposes. Just to let you know that there are lots of packages available for use with different applications.
|
||||
Let us check some of the packages used in the Python community for different purposes. Just to let you know that there are lots of packages available for use with different applications.
|
||||
|
||||
### Installing packages using pip
|
||||
|
||||
Let's try to install _numpy_, called numeric python. It is one of the most popular packages in machine learning and data science community.
|
||||
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
|
||||
@ -80,16 +78,16 @@ Let's try to install _numpy_, called numeric python. It is one of the most popul
|
||||
asabeneh@Asabeneh:~$ pip install numpy
|
||||
```
|
||||
|
||||
Lets start using numpy. Open your python interactive shell, write python and then import numpy as follows:
|
||||
Let us start using numpy. Open your python interactive shell, write python and then import numpy as follows:
|
||||
|
||||
```py
|
||||
asabeneh@Asabeneh:~$ python
|
||||
Python 3.7.5 (default, Nov 1 2019, 02:16:32)
|
||||
Python 3.9.6 (default, Jun 28 2021, 15:26:21)
|
||||
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
|
||||
Type "help", "copyright", "credits" or "license" for more information.
|
||||
>>> import numpy
|
||||
>>> numpy.version.version
|
||||
'1.17.3'
|
||||
'1.20.1'
|
||||
>>> lst = [1, 2, 3,4, 5]
|
||||
>>> np_arr = numpy.array(lst)
|
||||
>>> np_arr
|
||||
@ -103,7 +101,7 @@ array([3, 4, 5, 6, 7])
|
||||
>>>
|
||||
```
|
||||
|
||||
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's install the big brother of numpy, _pandas_:
|
||||
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_:
|
||||
|
||||
```sh
|
||||
asabeneh@Asabeneh:~$ pip install pandas
|
||||
@ -111,7 +109,7 @@ asabeneh@Asabeneh:~$ pip install pandas
|
||||
|
||||
```py
|
||||
asabeneh@Asabeneh:~$ python
|
||||
Python 3.7.5 (default, Nov 1 2019, 02:16:32)
|
||||
Python 3.9.6 (default, Jun 28 2021, 15:26:21)
|
||||
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
|
||||
Type "help", "copyright", "credits" or "license" for more information.
|
||||
>>> import pandas
|
||||
@ -119,7 +117,7 @@ Type "help", "copyright", "credits" or "license" for more information.
|
||||
|
||||
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's import a web browser module, which can help us to open any website. We do not 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 of use.
|
||||
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.
|
||||
|
||||
```py
|
||||
import webbrowser # web browser module to open websites
|
||||
@ -128,7 +126,7 @@ import webbrowser # web browser module to open websites
|
||||
url_lists = [
|
||||
'http://www.python.org',
|
||||
'https://www.linkedin.com/in/asabeneh/',
|
||||
'https://twitter.com/Asabeneh',
|
||||
'https://github.com/Asabeneh',
|
||||
'https://twitter.com/Asabeneh',
|
||||
]
|
||||
|
||||
@ -139,7 +137,7 @@ for url in url_lists:
|
||||
|
||||
### Uninstalling Packages
|
||||
|
||||
If you do not like to keep the installed packages, you can remove them.
|
||||
If you do not like to keep the installed packages, you can remove them using the following command.
|
||||
|
||||
```sh
|
||||
pip uninstall packagename
|
||||
@ -164,7 +162,7 @@ pip show packagename
|
||||
```sh
|
||||
asabeneh@Asabeneh:~$ pip show pandas
|
||||
Name: pandas
|
||||
Version: 0.25.3
|
||||
Version: 1.2.3
|
||||
Summary: Powerful data structures for data analysis, time series, and statistics
|
||||
Home-page: http://pandas.pydata.org
|
||||
Author: None
|
||||
@ -180,7 +178,7 @@ If we want even more details, just add --verbose
|
||||
```sh
|
||||
asabeneh@Asabeneh:~$ pip show --verbose pandas
|
||||
Name: pandas
|
||||
Version: 0.25.3
|
||||
Version: 1.2.3
|
||||
Summary: Powerful data structures for data analysis, time series, and statistics
|
||||
Home-page: http://pandas.pydata.org
|
||||
Author: None
|
||||
@ -211,7 +209,7 @@ Entry-points:
|
||||
|
||||
### PIP Freeze
|
||||
|
||||
Generate output suitable for a requirements file.
|
||||
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.
|
||||
|
||||
```sh
|
||||
asabeneh@Asabeneh:~$ pip freeze
|
||||
@ -227,9 +225,9 @@ The pip freeze gave us the packages used, installed and their version. We use it
|
||||
### 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 part of a CRUD.
|
||||
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's install _requests_:
|
||||
Let us install _requests_:
|
||||
|
||||
```py
|
||||
asabeneh@Asabeneh:~$ pip install requests
|
||||
@ -237,11 +235,11 @@ 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 (succes, error, etc)
|
||||
- _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 form this website, https://www.w3.org/TR/PNG/iso_8859-1.txt.
|
||||
Let's read a txt file from this website, https://www.w3.org/TR/PNG/iso_8859-1.txt.
|
||||
|
||||
```py
|
||||
import requests # importing the request module
|
||||
@ -261,7 +259,7 @@ print(response.text) # gives all the text from the page
|
||||
{'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's 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's read this API using _requests_ module.
|
||||
- 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.
|
||||
|
||||
```py
|
||||
import requests
|
||||
@ -329,10 +327,10 @@ We use _json()_ method from response object, if the we are fetching JSON data. F
|
||||
|
||||
### Creating a Package
|
||||
|
||||
We organize a large number of files in different folders and subfolders 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's create a package named mypackage, using the following steps:
|
||||
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 30DaysOfPython folder
|
||||
Create an empty **init**.py file in the mypackage folder.
|
||||
Create an empty **__init__**.py file in the mypackage folder.
|
||||
Create modules arithmetic.py and greet.py with following code:
|
||||
|
||||
```py
|
||||
@ -385,11 +383,11 @@ Now let's open the python interactive shell and try the package we have created:
|
||||
|
||||
```sh
|
||||
asabeneh@Asabeneh:~/Desktop/30DaysOfPython$ python
|
||||
Python 3.7.5 (default, Nov 1 2019, 02:16:32)
|
||||
Python 3.9.6 (default, Jun 28 2021, 15:26:21)
|
||||
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
|
||||
Type "help", "copyright", "credits" or "license" for more information.
|
||||
>>> from mypackage import arithmetics
|
||||
>>> arithmetics.add_numbers(1,2,3,5)
|
||||
>>> arithmetics.add_numbers(1, 2, 3, 5)
|
||||
11
|
||||
>>> arithmetics.subtract(5, 3)
|
||||
2
|
||||
@ -407,8 +405,8 @@ Type "help", "copyright", "credits" or "license" for more information.
|
||||
>>>
|
||||
```
|
||||
|
||||
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.
|
||||
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.
|
||||
|
||||
### Further Information About Packages
|
||||
|
||||
@ -418,10 +416,9 @@ The **init**.py exposes specified resources from its modules to be imported to o
|
||||
- Web Development
|
||||
- Django - High-level web framework.
|
||||
- _pip install django_
|
||||
- Flask - microframework for Python based on Werkzeug, Jinja 2. (It's BSD licensed)
|
||||
- Flask - micro framework for Python based on Werkzeug, Jinja 2. (It's BSD licensed)
|
||||
- _pip install flask_
|
||||
- HTML Parser
|
||||
|
||||
- [Beautiful Soup](https://www.crummy.com/software/BeautifulSoup/bs4/doc/) - 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.
|
||||
@ -433,7 +430,7 @@ The **init**.py exposes specified resources from its modules to be imported to o
|
||||
- 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 machine learning library in Python that provides data structures of high-level and a wide variety of tools for analysis.
|
||||
- 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.
|
||||
@ -442,15 +439,17 @@ The **init**.py exposes specified resources from its modules to be imported to o
|
||||
- requests: is a package which we can use to send requests to a server(GET, POST, DELETE, PUT)
|
||||
- _pip install requests_
|
||||
|
||||
|
||||
🌕 You are always progressing and you are a head of 20 steps to your way to greatness.
|
||||
🌕 You are always progressing and you are a head of 20 steps to your way to greatness. Now do some exercises for your brain and muscles.
|
||||
|
||||
## Exercises: Day 20
|
||||
|
||||
1. Read this url and find the 10 most frequent words. Romeo_and_juliet = 'http://www.gutenberg.org/files/1112/1112.txt'
|
||||
2. Read the cats api and cats_api = 'https://api.thecatapi.com/v1/breeds' and find the avarage weight of a cat in metric units.
|
||||
3. Read the countries api and find the 10 largest countries
|
||||
4. UCI is one the most common places to get data sets for data science and machine learning. Read the content of UCL (http://mlr.cs.umass.edu/ml/datasets.html). Without additional libraries it will be difficult, so you may try it with BeautifulSoup4
|
||||
2. Read the cats API and cats_api = 'https://api.thecatapi.com/v1/breeds' and find
|
||||
1. the min, max, mean, median, standard deviation of cats' weight in metric units.
|
||||
2. the min, max, mean, median, standard deviation of cats' lifespan in years.
|
||||
3. Create a frequency table of country and breed of cats
|
||||
3. Read the countries API and find the 10 largest countries
|
||||
4. UCI is one of the most common places to get data sets for data science and machine learning. Read the content of UCL (https://archive.ics.uci.edu/ml/datasets.php). Without additional libraries it will be difficult, so you may try it with BeautifulSoup4
|
||||
|
||||
🎉 CONGRATULATIONS ! 🎉
|
||||
|
||||
|
||||
0
20_Day_Python_package_manager/__init__.py
Normal file
0
20_Day_Python_package_manager/__init__.py
Normal file
25
20_Day_Python_package_manager/arithmetic.py
Normal file
25
20_Day_Python_package_manager/arithmetic.py
Normal file
@ -0,0 +1,25 @@
|
||||
def add_numbers(*args):
|
||||
total = 0
|
||||
for num in args:
|
||||
total += num
|
||||
return total
|
||||
|
||||
|
||||
def subtract(a, b):
|
||||
return (a - b)
|
||||
|
||||
|
||||
def multiple(a, b):
|
||||
return a * b
|
||||
|
||||
|
||||
def division(a, b):
|
||||
return a / b
|
||||
|
||||
|
||||
def remainder(a, b):
|
||||
return a % b
|
||||
|
||||
|
||||
def power(a, b):
|
||||
return a ** b
|
||||
2
20_Day_Python_package_manager/greet.py
Normal file
2
20_Day_Python_package_manager/greet.py
Normal file
@ -0,0 +1,2 @@
|
||||
def greet_person(firstname, lastname):
|
||||
return f'{firstname} {lastname}, welcome to 30DaysOfPython Challenge!'
|
||||
Loading…
Reference in New Issue
Block a user