a sample code and step by step upload to python PIP repo - unix1998/technical_notes GitHub Wiki

To create a Python package from your code and publish it to the Python Package Index (PyPI), you'll need to follow these steps:

  1. Structure Your Project: Create a directory structure for your package.

    my_in_memory_db/
    ├── my_in_memory_db/
    │   ├── __init__.py
    │   └── db.py
    ├── tests/
    │   ├── __init__.py
    │   └── test_db.py
    ├── README.md
    ├── setup.py
    └── LICENSE
    
  2. Write Your Code: Place your database code in my_in_memory_db/db.py.

    # my_in_memory_db/db.py
    class InMemoryDatabase:
        def __init__(self):
            self.db = {}
    
        def set(self, key, value):
            if not isinstance(value, int):
                raise ValueError("Value must be an integer.")
            self.db[key] = value
    
        def get(self, key):
            return self.db.get(key, "Key not found.")
    
        def delete(self, key):
            if key in self.db:
                del self.db[key]
            else:
                return "Key not found."
    
        def size(self):
            return len(self.db)
    
        def begin_transaction(self):
            if self.transaction_db is None:
                self.transaction_db = self.db.copy()
            else:
                raise Exception("Transaction already in progress.")
    
        def commit_transaction(self):
            if self.transaction_db is not None:
                self.db = self.transaction_db
                self.transaction_db = None
            else:
                raise Exception("No transaction in progress.")
    
        def rollback_transaction(self):
            if self.transaction_db is not None:
                self.transaction_db = None
            else:
                raise Exception("No transaction in progress.")
    
  3. Create __init__.py: Make sure to include an __init__.py file to make my_in_memory_db a package.

    # my_in_memory_db/__init__.py
    from .db import InMemoryDatabase
    
  4. Write Your Tests: Create a simple test for your package.

    # tests/test_db.py
    import unittest
    from my_in_memory_db import InMemoryDatabase
    
    class TestInMemoryDatabase(unittest.TestCase):
        def test_set_and_get(self):
            db = InMemoryDatabase()
            db.set("Canada", 5000)
            self.assertEqual(db.get("Canada"), 5000)
    
        def test_size(self):
            db = InMemoryDatabase()
            db.set("Canada", 5000)
            self.assertEqual(db.size(), 1)
    
    if __name__ == '__main__':
        unittest.main()
    
  5. Create setup.py: Write the setup.py script to define your package metadata.

    # setup.py
    from setuptools import setup, find_packages
    
    with open("README.md", "r", encoding="utf-8") as fh:
        long_description = fh.read()
    
    setup(
        name="my_in_memory_db",
        version="0.0.1",
        author="Your Name",
        author_email="[email protected]",
        description="A simple in-memory database with transaction support",
        long_description=long_description,
        long_description_content_type="text/markdown",
        url="https://github.com/yourusername/my_in_memory_db",
        packages=find_packages(),
        classifiers=[
            "Programming Language :: Python :: 3",
            "License :: OSI Approved :: MIT License",
            "Operating System :: OS Independent",
        ],
        python_requires='>=3.6',
    )
    
  6. Create a README: Write a README.md file for your package.

    # my_in_memory_db
    
    A simple in-memory database with transaction support.
    
    ## Installation
    
    ```bash
    pip install my_in_memory_db
    

    Usage

    from my_in_memory_db import InMemoryDatabase
    
    db = InMemoryDatabase()
    db.set("Canada", 5000)
    print(db.get("Canada"))  # Output: 5000
    
    
    
  7. Create a LICENSE: Add a license file, for example, an MIT license.

    MIT License
    ...
    
  8. Build and Publish Your Package: Use setuptools and twine to build and publish your package to PyPI.

    # Install necessary tools
    pip install setuptools wheel twine
    
    # Build the package
    python setup.py sdist bdist_wheel
    
    # Upload the package to PyPI
    twine upload dist/*
    

Here's a summary of the commands for building and publishing:

  1. Create source distribution and wheel distribution:

    python setup.py sdist bdist_wheel
    
  2. Upload the package to PyPI using twine:

    twine upload dist/*
    

After these steps, your package should be available on PyPI, and users can install it using pip install my_in_memory_db.