Building A Python Package in Minutes - Analytics Vidhya - Medium
Building A Python Package in Minutes - Analytics Vidhya - Medium
2020, 17:55
― Sameh Elsayed
https://medium.com/analytics-vidhya/building-a-python-package-in-minutes-7bb702a6eb65 Strona 1 z 23
Building a Python Package in Minutes - Analytics Vidhya - Medium 07.04.2020, 17:55
https://scitechdaily.com/mit-develops-machine-learning-tool-to-make-code-run-faster/
I actually saw the need to put this piece out, so as to help new
data scientist, programmers and other code users in the python
world to help ace the process of writing some lines of code for
every project, why don't you just make it a package and reuse
whenever you like.
So this article aims to put yours through the basics on what and
how you need to build a python package using a case study of a
new distribution package I built. the process is going to be
listed using the following outlines below:
Getting Started
Conclusion
. . .
https://medium.com/analytics-vidhya/building-a-python-package-in-minutes-7bb702a6eb65 Strona 2 z 23
Building a Python Package in Minutes - Analytics Vidhya - Medium 07.04.2020, 17:55
Getting started
At Crst, you will surely need to set up one or more things to get
you in line on how to build a python package. so the list of
things need are highlighted as follows:
IDE(Vs code)
Python 3
The preferred IDE used for this process is Vscode and Python 3.
make sure these two things are well connected, if having any
https://medium.com/analytics-vidhya/building-a-python-package-in-minutes-7bb702a6eb65 Strona 3 z 23
Building a Python Package in Minutes - Analytics Vidhya - Medium 07.04.2020, 17:55
. . .
b_dist/
__init__.py
Binomialdistribution.py
Guassiandistribution.py
Generaldistribution.py
licence.txt
setup.cfg
README.md
setup.py
First, you will have to create an empty Cle of those listed above
in the map. Note b_dist is a folder and setup.py with
README.md is not in b_dist folder, otherwise, click here to see
a clearer picture.
Let talk about the distribution folder Crst then the setup Cle as
follows:
https://medium.com/analytics-vidhya/building-a-python-package-in-minutes-7bb702a6eb65 Strona 4 z 23
Building a Python Package in Minutes - Analytics Vidhya - Medium 07.04.2020, 17:55
b_dist/__init__.py
This Cle tells python that this folder contains a package. Also, a
package always consists of an init Cle even if it is empty. the
code __init__ Cle gets to run whenever you Import a package
inside a python program. In this case, the __init__ Cle is
importing the Gaussian, Binomial and Distribution model so as
to import these classes directly when using the package.
b_dist/Binomialdistribution.py
1 import math
2 import matplotlib.pyplot as plt
3 from .Generaldistribution import Distribution
4
5 class Binomial(Distribution):
6 """ Binomial distribution class for calculating and
7 visualizing a Binomial distribution.
8
9 Attributes:
10 mean (float) representing the mean value of the distribution
11 stdev (float) representing the standard deviation of the distribution
12 data_list (list of floats) a list of floats to be extracted from the data fi
13 p (float) representing the probability of an event occurring
14 n (int) number of trials
15
16
17 TODO: Fill out all functions below
18
https://medium.com/analytics-vidhya/building-a-python-package-in-minutes-7bb702a6eb65 Strona 5 z 23
Building a Python Package in Minutes - Analytics Vidhya - Medium 07.04.2020, 17:55
19 """
20
21
22 def __init__(self, prob=.5, size=20):
23
24 self.n = size
25 self.p = prob
26
27 Distribution.__init__(self, self.calculate_mean(), self.calculate_stdev
28
29
30
31 def calculate_mean(self):
32
33 """Function to calculate the mean from p and n
34
35 Args:
36 None
37
38 Returns:
39 float: mean of the data set
40
41 """
42
43 self.mean = self.p * self.n
44
45 return self.mean
46
47
48
49 def calculate_stdev(self):
50
51 """Function to calculate the standard deviation from p and n.
52
53 Args:
54 None
55
56 Returns:
57 float: standard deviation of the data set
58
59 """
60
https://medium.com/analytics-vidhya/building-a-python-package-in-minutes-7bb702a6eb65 Strona 6 z 23
Building a Python Package in Minutes - Analytics Vidhya - Medium 07.04.2020, 17:55
60
61 self.stdev = math.sqrt(self.n * self.p * (1 - self.p))
62
63 return self.stdev
64
65
66 def replace_stats_with_data(self):
67
68 """Function to calculate p and n from the data set
69
70 Args:
71 None
72
73 Returns:
74 float: the p value
75 float: the n value
76
77 """
78
79 self.n = len(self.data)
80 self.p = 1.0 * sum(self.data) / len(self.data)
81 self.mean = self.calculate_mean()
82 self.stdev = self.calculate_stdev()
83
84
85
86 def plot_bar(self):
87 """Function to output a histogram of the instance variable data using
88 matplotlib pyplot library.
89
90 Args:
91 None
92
93 Returns:
94 None
95 """
96
97 plt.bar(x = ['0', '1'], height = [(1 - self.p) * self.n, self.p
98 plt.title('Bar Chart of Data')
99 plt.xlabel('outcome')
100 plt.ylabel('count')
101
102
https://medium.com/analytics-vidhya/building-a-python-package-in-minutes-7bb702a6eb65 Strona 7 z 23
Building a Python Package in Minutes - Analytics Vidhya - Medium 07.04.2020, 17:55
102
103
104 def pdf(self, k):
105 """Probability density function calculator for the gaussian distribution.
106
107 Args:
108 x (float): point for calculating the probability density function
109
110
111 Returns:
112 float: probability density function output
113 """
114
115 a = math.factorial(self.n) / (math.factorial(k) * (math.factorial
116 b = (self.p ** k) * (1 - self.p) ** (self.n - k)
117
118 return a * b
119
120
121 def plot_bar_pdf(self):
122
123 """Function to plot the pdf of the binomial distribution
124
125 Args:
126 None
127
128 Returns:
129 list: x values for the pdf plot
130 list: y values for the pdf plot
131
132 """
133
134 x = []
135 y = []
136
137 # calculate the x values to visualize
138 for i in range(self.n + 1):
139 x.append(i)
140 y.append(self.pdf(i))
141
142 # make the plots
143 plt.bar(x, y)
https://medium.com/analytics-vidhya/building-a-python-package-in-minutes-7bb702a6eb65 Strona 8 z 23
Building a Python Package in Minutes - Analytics Vidhya - Medium 07.04.2020, 17:55
b_dist/Guassiandistribution.py
1 import math
2 import matplotlib.pyplot as plt
3 from .Generaldistribution import Distribution
4
5 class Guassian(Distribution):
6
7 """Guassian distribution class for calculating and visualizing a Guasian distrib
8 Attributes:
9 Attributes:
10 mean (float) representing the mean value of the distribution
11 stdev (float) representing the standrd deviation of the distribution
12 data_list (list of floats) a list of floats extracted from the data file
13
https://medium.com/analytics-vidhya/building-a-python-package-in-minutes-7bb702a6eb65 Strona 9 z 23
Building a Python Package in Minutes - Analytics Vidhya - Medium 07.04.2020, 17:55
14 """
15 def __init__(self,mu=0,sigma=1):
16
17 Distribution.__init__(self, mu, sigma)
18
19 def calculate_mean(self):
20
21 """Function to calculate the mean of the data set.
22 Args:
23 None
24 Returns:
25 float: mean of the data set
26
27 """
28
29 avg = 1.0 * sum(self.data) / len(self.data)
30
31 self.mean = avg
32
33 return self.mean
34
35 def calculate_stdev(self, sample=True):
36
37 """Function to calculate he standard deviation of the data set
38 Args:
39 sample (bool): whether the data represents a sample or population
40 Returns:
41 float: stantard deviation of the data set
42 """
43
44 if sample:
45 n = len(self.data) - 1
46 else:
47 n = len(self.data)
48
49 mean = self.calculate_mean()
50
51 sigma = 0
52
53 for d in self.data:
54 sigma += (d-mean) ** 2
55
https://medium.com/analytics-vidhya/building-a-python-package-in-minutes-7bb702a6eb65 Strona 10 z 23
Building a Python Package in Minutes - Analytics Vidhya - Medium 07.04.2020, 17:55
55
56 sigma = math.sqrt(sigma / n)
57
58 self.stdev = sigma
59
60 return self.stdev
61
62
63 def plot_histogram(self):
64 """Function to output a histogram of the instance variable data using matplo
65 Args:
66 None
67 Returns:
68 None
69 """
70
71 plt.hist(self.data)
72 plt.title('Histogram of Data')
73 plt.xlabel('data')
74 plt.ylabel('count')
75
76
77 def pdf(self, x):
78 """Probability desity function calculator for the guassian distribution
79 Args:
80 x (float): point for calculating the probability density function
81 Returns:
82 float: probability density function output
83
84 """
85
86 return (1.0 / (self.stdev * math.sqrt(2*math.pi))) *math.exp(-0.5
87
88 def plot_histogram_pdf(self, n_spaces=50):
89
90 """Function to plot the normalized histogram of the data and probability den
91 Args:
92 n_spaces (int): number of data points
93 Returns:
94 list: x values for the pdf plot
95 list: y values for the pdf plot
96 """
97
https://medium.com/analytics-vidhya/building-a-python-package-in-minutes-7bb702a6eb65 Strona 11 z 23
Building a Python Package in Minutes - Analytics Vidhya - Medium 07.04.2020, 17:55
97
98 mu = self.mean
99 sigma = self.stdev
100
101 min_range = min(self.data)
102 max_range = max(self.data)
103
104 # Calculates the interval between x values
105 interval = 1.0 * (max_range - min_range) / n_spaces
106
107 x = []
108 y = []
109
110 # calcilates the x values to visualize
111 for i in range(n_spaces):
112 tmp = min_range + interval * 1
113 x.append(tmp)
114 y.append(self.pdf(tmp))
115
116 # make the plots
117 fig, axes = plt.subplots(2, sharex=True)
118 fig.subplots_adjust(hspace=.5)
119 axes[0].hist(self.data, density=True)
120 axes[0].set_title('Normed Histogram of Data')
121 axes[0].set_ylabel('Density')
122
123 axes[1].hist(x, y, density=True)
124 axes[1].set_title('Normal Distribution for \n Sample Mean and Sample Standar
125 axes[1].set_ylabel('Density')
126 plt.show()
127
128 return x, y
129 def __add__(self, other):
b_dist/Generaldistribution.py
https://medium.com/analytics-vidhya/building-a-python-package-in-minutes-7bb702a6eb65 Strona 12 z 23
Building a Python Package in Minutes - Analytics Vidhya - Medium 07.04.2020, 17:55
1 class Distribution:
2
3 def __init__(self, mu=0, sigma=1):
4 """ Generic distribution class for calculating and visualizing a probaility d
5 Attributes:
6 mean (float) representing the mean value of the distribution
7 stdev (float) representing the standrd deviation of the distribution
8 data_list (list of floats) a list of floats extracted from the data file
9 """
10
11 self.mean = mu
12 self.stdev = sigma
13 self.data = []
14
15 def read_data_file(self, file_name):
16
17 """Function to read in data from a textfile. The text fuile should have one n
18 Args:
19 file_name (string): name of the file to read from
20
21 Returns:
22 None
23 """
24
25 with open(file_name) as file:
26 data_list = []
27 line = file.readline()
28 while line:
29 data_list.append(len(line))
b_dist/licence.txt
https://medium.com/analytics-vidhya/building-a-python-package-in-minutes-7bb702a6eb65 Strona 13 z 23
Building a Python Package in Minutes - Analytics Vidhya - Medium 07.04.2020, 17:55
b_dist/setup.cfg
[metadata]description-file = README.md
The setup.cfg Cle is a Cle that holds the data of your README
Cle.
https://medium.com/analytics-vidhya/building-a-python-package-in-minutes-7bb702a6eb65 Strona 14 z 23
Building a Python Package in Minutes - Analytics Vidhya - Medium 07.04.2020, 17:55
README.md
### Installation
### Example
setup.py
https://medium.com/analytics-vidhya/building-a-python-package-in-minutes-7bb702a6eb65 Strona 15 z 23
Building a Python Package in Minutes - Analytics Vidhya - Medium 07.04.2020, 17:55
13 author='Abideen Bello',
14 author_email='abideen.datascienceofficial@gmail.com',
15 long_description=long_description,
16 long_description_content_type='text/markdown'
. . .
Now that we are almost done with the Crst part, let's run our
package locally Crst by typing the code below:
~$ cd python_package
~/python_package:$
https://medium.com/analytics-vidhya/building-a-python-package-in-minutes-7bb702a6eb65 Strona 16 z 23
Building a Python Package in Minutes - Analytics Vidhya - Medium 07.04.2020, 17:55
Processing /python_package
Building wheels for collected packages: b-dist
Building wheel for b-dist (setup.py) ... done
Created wheel for b-dist: filename=b_dist-0.4-py3-none-
any.whl size=5108
sha256=d4c6f74daa1add07f37b01a74294e86ab07d655a6e0944bbb4
6ed6503ae493ef
Stored in directory: /tmp/pip-ephem-wheel-cache-
3pvdd9ue/wheels/1e/f9/a3/568195cccd4e2d1dcb1edaf9c2708f65
1b90b6af6fbdfd3f36
Successfully built b-dist
. . .
https://blog.sngine.com/how-to-handle-uploading-in-sngine/
First, create an account with the Test.PyPi and PyPi using the
same username and password for both sites. the following
article should put through the registration:
https://medium.com/analytics-vidhya/building-a-python-package-in-minutes-7bb702a6eb65 Strona 18 z 23
Building a Python Package in Minutes - Analytics Vidhya - Medium 07.04.2020, 17:55
Create an account
Skip to main content You are using TestPyPI - a separate
instance of the Python Package Index that allows you to…
try…
test.pypi.org
Create an account
The Python Package Index (PyPI) is a repository of
software for the Python programming language.
pypi.org
So, after install twine, enter the following codes to upload Crst
to TestPyPi:
https://medium.com/analytics-vidhya/building-a-python-package-in-minutes-7bb702a6eb65 Strona 19 z 23
Building a Python Package in Minutes - Analytics Vidhya - Medium 07.04.2020, 17:55
You will see two new folders after entering the code, then move
to the next line of code:
Now pip install from the TestPyPi site using this code:
https://medium.com/analytics-vidhya/building-a-python-package-in-minutes-7bb702a6eb65 Strona 20 z 23
Building a Python Package in Minutes - Analytics Vidhya - Medium 07.04.2020, 17:55
Conclusion
https://medium.com/analytics-vidhya/building-a-python-package-in-minutes-7bb702a6eb65 Strona 21 z 23
Building a Python Package in Minutes - Analytics Vidhya - Medium 07.04.2020, 17:55
Thank you.
Python Programming
https://medium.com/analytics-vidhya/building-a-python-package-in-minutes-7bb702a6eb65 Strona 22 z 23
Building a Python Package in Minutes - Analytics Vidhya - Medium 07.04.2020, 17:55
https://medium.com/analytics-vidhya/building-a-python-package-in-minutes-7bb702a6eb65 Strona 23 z 23