Skip to content

Commit 249d409

Browse files
committed
packages
1 parent 31d5543 commit 249d409

File tree

4 files changed

+67
-41
lines changed

4 files changed

+67
-41
lines changed

20_Day_Python_package_manager/20_python_package_manager.md

Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@
99

1010
<sub>Author:
1111
<a href="https://www.linkedin.com/in/asabeneh/" target="_blank">Asabeneh Yetayeh</a><br>
12-
<small> First Edition: Nov 22 - Dec 22, 2019</small>
12+
<small>Second Edition: July, 2021</small>
1313
</sub>
14-
15-
</div>
1614
</div>
1715

1816
[<< Day 19](../19_Day_File_handling/19_file_handling.md) | [Day 21 >>](../21_Day_Classes_and_objects/21_classes_and_objects.md)
@@ -39,36 +37,36 @@
3937

4038
### What is PIP ?
4139

42-
PIP stands for Preferred installer program. We use _pip_ to install different python packages.
43-
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.
40+
PIP stands for Preferred installer program. We use _pip_ to install different Python packages.
41+
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.
4442
In programming, we do not have to write every utility program, instead we install packages and import them to our applications.
4543

4644
### Installing PIP
4745

48-
If you did not install pip, let us do it now. Go to your terminal or command prompt and copy and paste this:
46+
If you did not install pip, let us install it now. Go to your terminal or command prompt and copy and paste this:
4947

5048
```sh
5149
asabeneh@Asabeneh:~$ pip install pip
5250
```
5351

54-
Check if it is installed by writing
52+
Check if pip is installed by writing
5553

5654
```sh
5755
pip --version
5856
```
5957

6058
```py
6159
asabeneh@Asabeneh:~$ pip --version
62-
pip 19.3.1 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)
60+
pip 21.1.3 from /usr/local/lib/python3.7/site-packages/pip (python 3.9.6)
6361
```
6462

65-
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.
63+
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.
6664

67-
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.
65+
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.
6866

6967
### Installing packages using pip
7068

71-
Let's try to install _numpy_, called numeric python. It is one of the most popular packages in machine learning and data science community.
69+
Let us try to install _numpy_, called numeric python. It is one of the most popular packages in machine learning and data science community.
7270

7371
- NumPy is the fundamental package for scientific computing with Python. It contains among other things:
7472
- 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
8078
asabeneh@Asabeneh:~$ pip install numpy
8179
```
8280

83-
Lets start using numpy. Open your python interactive shell, write python and then import numpy as follows:
81+
Let us start using numpy. Open your python interactive shell, write python and then import numpy as follows:
8482

8583
```py
8684
asabeneh@Asabeneh:~$ python
87-
Python 3.7.5 (default, Nov 1 2019, 02:16:32)
85+
Python 3.9.6 (default, Jun 28 2021, 15:26:21)
8886
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
8987
Type "help", "copyright", "credits" or "license" for more information.
9088
>>> import numpy
9189
>>> numpy.version.version
92-
'1.17.3'
90+
'1.20.1'
9391
>>> lst = [1, 2, 3,4, 5]
9492
>>> np_arr = numpy.array(lst)
9593
>>> np_arr
@@ -103,23 +101,23 @@ array([3, 4, 5, 6, 7])
103101
>>>
104102
```
105103

106-
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_:
104+
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_:
107105

108106
```sh
109107
asabeneh@Asabeneh:~$ pip install pandas
110108
```
111109

112110
```py
113111
asabeneh@Asabeneh:~$ python
114-
Python 3.7.5 (default, Nov 1 2019, 02:16:32)
112+
Python 3.9.6 (default, Jun 28 2021, 15:26:21)
115113
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
116114
Type "help", "copyright", "credits" or "license" for more information.
117115
>>> import pandas
118116
```
119117

120118
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.
121119

122-
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.
120+
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.
123121

124122
```py
125123
import webbrowser # web browser module to open websites
@@ -128,7 +126,7 @@ import webbrowser # web browser module to open websites
128126
url_lists = [
129127
'http://www.python.org',
130128
'https://www.linkedin.com/in/asabeneh/',
131-
'https://twitter.com/Asabeneh',
129+
'https://github.com/Asabeneh',
132130
'https://twitter.com/Asabeneh',
133131
]
134132

@@ -139,7 +137,7 @@ for url in url_lists:
139137

140138
### Uninstalling Packages
141139

142-
If you do not like to keep the installed packages, you can remove them.
140+
If you do not like to keep the installed packages, you can remove them using the following command.
143141

144142
```sh
145143
pip uninstall packagename
@@ -164,7 +162,7 @@ pip show packagename
164162
```sh
165163
asabeneh@Asabeneh:~$ pip show pandas
166164
Name: pandas
167-
Version: 0.25.3
165+
Version: 1.2.3
168166
Summary: Powerful data structures for data analysis, time series, and statistics
169167
Home-page: http://pandas.pydata.org
170168
Author: None
@@ -180,7 +178,7 @@ If we want even more details, just add --verbose
180178
```sh
181179
asabeneh@Asabeneh:~$ pip show --verbose pandas
182180
Name: pandas
183-
Version: 0.25.3
181+
Version: 1.2.3
184182
Summary: Powerful data structures for data analysis, time series, and statistics
185183
Home-page: http://pandas.pydata.org
186184
Author: None
@@ -211,7 +209,7 @@ Entry-points:
211209

212210
### PIP Freeze
213211

214-
Generate output suitable for a requirements file.
212+
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.
215213

216214
```sh
217215
asabeneh@Asabeneh:~$ pip freeze
@@ -227,21 +225,21 @@ The pip freeze gave us the packages used, installed and their version. We use it
227225
### Reading from URL
228226

229227
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.
230-
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.
228+
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.
231229

232-
Let's install _requests_:
230+
Let us install _requests_:
233231

234232
```py
235233
asabeneh@Asabeneh:~$ pip install requests
236234
```
237235

238236
We will see _get_, _status_code_, _headers_, _text_ and _json_ methods in _requests_ module:
239237
- _get()_: to open a network and fetch data from url - it returns a response object
240-
- _status_code_: After we fetched data, we can check the status of the operation (succes, error, etc)
238+
- _status_code_: After we fetched data, we can check the status of the operation (success, error, etc)
241239
- _headers_: To check the header types
242240
- _text_: to extract the text from the fetched response object
243241
- _json_: to extract json data
244-
Let's read a txt file form this website, https://www.w3.org/TR/PNG/iso_8859-1.txt.
242+
Let's read a txt file from this website, https://www.w3.org/TR/PNG/iso_8859-1.txt.
245243

246244
```py
247245
import requests # importing the request module
@@ -261,7 +259,7 @@ print(response.text) # gives all the text from the page
261259
{'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'}
262260
```
263261

264-
- 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.
262+
- 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.
265263

266264
```py
267265
import requests
@@ -329,10 +327,10 @@ We use _json()_ method from response object, if the we are fetching JSON data. F
329327

330328
### Creating a Package
331329

332-
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:
330+
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:
333331

334332
Create a new folder named mypacakge inside 30DaysOfPython folder
335-
Create an empty **init**.py file in the mypackage folder.
333+
Create an empty **__init__**.py file in the mypackage folder.
336334
Create modules arithmetic.py and greet.py with following code:
337335

338336
```py
@@ -385,11 +383,11 @@ Now let's open the python interactive shell and try the package we have created:
385383

386384
```sh
387385
asabeneh@Asabeneh:~/Desktop/30DaysOfPython$ python
388-
Python 3.7.5 (default, Nov 1 2019, 02:16:32)
386+
Python 3.9.6 (default, Jun 28 2021, 15:26:21)
389387
[Clang 11.0.0 (clang-1100.0.33.8)] on darwin
390388
Type "help", "copyright", "credits" or "license" for more information.
391389
>>> from mypackage import arithmetics
392-
>>> arithmetics.add_numbers(1,2,3,5)
390+
>>> arithmetics.add_numbers(1, 2, 3, 5)
393391
11
394392
>>> arithmetics.subtract(5, 3)
395393
2
@@ -407,8 +405,8 @@ Type "help", "copyright", "credits" or "license" for more information.
407405
>>>
408406
```
409407

410-
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.
411-
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.
408+
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.
409+
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.
412410

413411
### Further Information About Packages
414412

@@ -418,10 +416,9 @@ The **init**.py exposes specified resources from its modules to be imported to o
418416
- Web Development
419417
- Django - High-level web framework.
420418
- _pip install django_
421-
- Flask - microframework for Python based on Werkzeug, Jinja 2. (It's BSD licensed)
419+
- Flask - micro framework for Python based on Werkzeug, Jinja 2. (It's BSD licensed)
422420
- _pip install flask_
423421
- HTML Parser
424-
425422
- [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.
426423
- _pip install beautifulsoup4_
427424
- 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
433430
- TkInter - The traditional Python user interface toolkit.
434431
- Data Analysis, Data Science and Machine learning
435432
- Numpy: Numpy(numeric python) is known as one of the most popular machine learning library in Python.
436-
- Pandas: is a machine learning library in Python that provides data structures of high-level and a wide variety of tools for analysis.
433+
- 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.
437434
- 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.
438435
- Scikit-Learn: It is NumPy and SciPy. It is considered as one of the best libraries for working with complex data.
439436
- 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
442439
- requests: is a package which we can use to send requests to a server(GET, POST, DELETE, PUT)
443440
- _pip install requests_
444441

445-
446-
🌕 You are always progressing and you are a head of 20 steps to your way to greatness.
442+
🌕 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.
447443

448444
## Exercises: Day 20
449445

450446
1. Read this url and find the 10 most frequent words. Romeo_and_juliet = 'http://www.gutenberg.org/files/1112/1112.txt'
451-
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.
452-
3. Read the countries api and find the 10 largest countries
453-
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
447+
2. Read the cats API and cats_api = 'https://api.thecatapi.com/v1/breeds' and find
448+
1. the min, max, mean, median, standard deviation of cats' weight in metric units.
449+
2. the min, max, mean, median, standard deviation of cats' lifespan in years.
450+
3. Create a frequency table of country and breed of cats
451+
3. Read the countries API and find the 10 largest countries
452+
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
454453

455454
🎉 CONGRATULATIONS ! 🎉
456455

20_Day_Python_package_manager/__init__.py

Whitespace-only changes.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
def add_numbers(*args):
2+
total = 0
3+
for num in args:
4+
total += num
5+
return total
6+
7+
8+
def subtract(a, b):
9+
return (a - b)
10+
11+
12+
def multiple(a, b):
13+
return a * b
14+
15+
16+
def division(a, b):
17+
return a / b
18+
19+
20+
def remainder(a, b):
21+
return a % b
22+
23+
24+
def power(a, b):
25+
return a ** b
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def greet_person(firstname, lastname):
2+
return f'{firstname} {lastname}, welcome to 30DaysOfPython Challenge!'

0 commit comments

Comments
 (0)