To create a Python library and release it on PyPI, follow these steps:
1. Create Your Library:
Start by creating your Python library with your desired functionality. Organize the code into a package, and make sure to include a `setup.py` file for packaging information.
bindecocthex/ #your folder name
├── bindecocthex/
│ ├── __init__.py
│ └── conversion.py #your file name
├── tests/ #unittest folder
│ └── test_conversion.py
├── setup.py
└── README.md
2. Write yout library file in "your_file.py"
Example: My library "bindecocthex"
file :conversion.py program:
class Conversion:
def init(self):
pass
def decimal_to_binary(self, number):
try:
decimal_number = int(number)
binary_representation = format(decimal_number, 'b')
return binary_representation
except ValueError:
return "Invalid input"
def decimal_to_octal(self, number):
try:
decimal_number = int(number)
octal_representation = format(decimal_number, 'o')
return octal_representation
except ValueError:
return "Invalid input"
def decimal_to_hexadecimal(self, number):
try:
decimal_number = int(number)
hexadecimal_representation = format(decimal_number, 'X')
return hexadecimal_representation
except ValueError:
return "Invalid input"
def binary_to_decimal(self, number):
try:
decimal_number = int(number, 2)
return str(decimal_number)
except ValueError:
return "Invalid input"
def binary_to_octal(self, number):
try:
decimal_number = int(number, 2)
octal_representation = format(decimal_number, 'o')
return octal_representation
except ValueError:
return "Invalid input"
def binary_to_hexadecimal(self, number):
try:
decimal_number = int(number, 2)
hexadecimal_representation = format(decimal_number, 'X')
return hexadecimal_representation
except ValueError:
return "Invalid input"
def octal_to_decimal(self, number):
try:
decimal_number = int(number, 8)
return str(decimal_number)
except ValueError:
return "Invalid input"
def octal_to_binary(self, number):
try:
decimal_number = int(number, 8)
binary_representation = format(decimal_number, 'b')
return binary_representation
except ValueError:
return "Invalid input"
def octal_to_hexadecimal(self, number):
try:
decimal_number = int(number, 8)
hexadecimal_representation = format(decimal_number, 'X')
return hexadecimal_representation
except ValueError:
return "Invalid input"
def hexadecimal_to_decimal(self, number):
try:
decimal_number = int(number, 16)
return str(decimal_number)
except ValueError:
return "Invalid input"
def hexadecimal_to_binary(self, number):
try:
decimal_number = int(number, 16)
binary_representation = format(decimal_number, 'b')
return binary_representation
except ValueError:
return "Invalid input"
def hexadecimal_to_octal(self, number):
try:
decimal_number = int(number, 16)
octal_representation = format(decimal_number, 'o')
return octal_representation
except ValueError:
return "Invalid input"
3. Create unittest in test folder
For a Python project, especially one that's intended to be shared or maintained, having tests is considered good practice to ensure the correctness and stability of your code. Here are the essential files related to testing in your project:
1.`tests/` Directory:
- This directory will contain all your test files.
- In your case, it includes the `test_conversion.py` file.
2.`test_conversion.py` (or similar):
- This file contains the actual test cases for your `Conversion` class.
- It should include test methods using a testing framework like `unittest` to check various functionalities of your code.
file test_yourfilename:
import unittest
from bindecocthex.conversion import Conversion
class TestConversion(unittest.TestCase):
def setUp(self):
self.converter = Conversion()
def test_decimal_to_binary(self):
self.assertEqual(self.converter.decimal_to_binary(10), '1010')
self.assertEqual(self.converter.decimal_to_binary(25), '11001')
def test_binary_to_decimal(self):
self.assertEqual(self.converter.binary_to_decimal('1010'), '10')
self.assertEqual(self.converter.binary_to_decimal('11001'), '25')
# Add more test methods for other conversion functions
if __name__ == '__main__':
unittest.main()
3. Running Tests:
To run your tests, you typically use a test runner. In this case, you can run your tests by executing `python -m unittest tests/test_conversion.py` in the terminal from your project's root directory.
Including tests and a test suite in your project is crucial for ensuring that your code functions correctly and to catch regressions when you make changes. It's also valuable for others who might contribute to or use your library.
4. Create setup.py
Example `setup.py`:
from setuptools import setup, find_packages
setup(
name='bindecocthex',
version='0.4',
packages=['bindecocthex'],
package_data={
'': ['README.md'],
},
entry_points={
'console_scripts': [
'bindecocthex = bindecocthex.conversion:main'
]
},
install_requires=[
# List your dependencies here
],
python_requires='>=3.6',
author='your_name',
author_email='your email',
description='project description',
long_description=open('README.md').read(),
long_description_content_type='text/markdown',
url='your githun link',
classifiers=[
'Programming Language :: Python :: 3',
'License :: OSI Approved :: MIT License',
'Operating System :: OS Independent',
],
data_files=[('', ['LICENSE'])],
)
5. Create a Distribution Package:
Use `setuptools` to create a distribution package of your library.
Command:
python3 setup.py sdist bdist_wheel
6. Install `twine`:
If you haven't installed `twine` yet, do so using:
command:
pip install twine
7. Upload to PyPI:
Use `twine` to upload your distribution package to PyPI.
command:
twine upload dist/*
Make sure to replace `dist/*` with the appropriate path to your distribution package.
( or )
Create API token on your pypi account:
Go to your account setting :
Click Add API token to create your token:
command:
twine upload --verbose --username "__token__" --password "<your-API token>" dist/*
8. Test Installation:
Create a virtual environment and install your library to ensure everything works.
command:
python3 -m venv venv
source venv/bin/activate # On Linux,
pip install your-library
9. Documentation:
Write documentation for your library. You can use tools like Sphinx or MkDocs.
10. Release on GitHub:
Consider creating a GitHub repository for your library to make it more accessible and allow for collaboration.
That's it! Your Python library should now be available on PyPI for others to install and use.
Remember to keep your package version updated in the `setup.py` file with each release.
No comments:
Post a Comment