make a custom python library - Massii94/GENERAL-TUTORIAL GitHub Wiki
1- Create a directory in which you want to put your library:
mkdir datasetconvertorlibrary
2- install dependencies:
pip3 install wheel setuptools twine
3- open "datasetconvertorlibrary" directory in vscode and
Create an empty file called setup.py
Create an empty file called README.md. This is the place where you can write markdown to describe the contents of your library for others.
Create a folder called datasetconvertorlib, or whatever you want your Python library to be called when you pip install it.
Create an empty file inside datasetconvertorlibrary that is called __init__.py
Alsoin the same folder, create a file called yolo2imagenet.py (in which, there are functions you want to use in your library)
Finally, create a folder tests in your root folder. Inside, create an empty __init__.py file and an empty test_myfunctions.py.
4- Create content for your library:
To put functions inside your library, you can place them in the yolo2imagenet.py file
from math import radians, cos, sin, asin, sqrt
def haversine(lon1: float, lat1: float, lon2: float, lat2: float) -> float:
"""
Calculate the great circle distance between two points on the
earth (specified in decimal degrees), returns the distance in
meters.
All arguments must be of equal length.
:param lon1: longitude of first place
:param lat1: latitude of first place
:param lon2: longitude of second place
:param lat2: latitude of second place
:return: distance in meters between the two sets of coordinates
"""
# Convert decimal degrees to radians
lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
# Haversine formula
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
c = 2 * asin(sqrt(a))
r = 6371 # Radius of earth in kilometers
return c * r
5- Whenever you write any code, it is highly encouraged to also write tests for this code. For testing with Python you can use the libraries pytest and pytest-runner:
pip3 install pytest==4.4.1
pip3 install pytest-runner==4.4
6- Let’s create a small test for the haversine function. Copy the following and place it inside the test_myfunctions.py:
from datasetconvertorlib import yolo2imagenet
def test_haversine():
assert myfunctions.haversine(52.370216, 4.895168, 52.520008,
13.404954) == 945793.4375088713
7- Finally, let’s create a setup.py file, that will help us to build the library:
from setuptools import find_packages, setup
import os
thelibFolder = os.path.dirname(os.path.realpath(__file__))
requirementPath = thelibFolder + '/requirements.txt'
if os.path.isfile(requirementPath):
with open(requirementPath) as f:
install_requires = f.read().splitlines()
setup(
name='datasetconvertorlib',
packages=find_packages(include=['datasetconvertorlib']),
# version='0.1',
description='My second Python library',
author='Masi',
license='NOJAN',
install_requires=install_requires,
# install_requires=load_requirements("requirements.txt"),
)
8- Running:
python setup.py pytest
9- Build your library:
python setup.py bdist_wheel
Your wheel file is stored in the “dist” folder that is now created. You can install your library by using:
pip install /path/to/wheelfile.whl
Once you have installed your Python library, you can import it using:
import datasetconvertorlib
from datasetconvertorlib import yolo2imagenet
reference:
https://medium.com/analytics-vidhya/how-to-create-a-python-library-7d5aea80cc3f
Notes:
your root directory and your library name should be somthing like this:
datasetconvertorlibrary --> datasetconvertorlib
you should add your desire dependendies in setup.py like the above. make a equirements in your library folder (datasetconvertorlib) and add libraries with their versions to it.