↓ Skip to main content
Python

Python: Sample Project

·2 mins
Table of Contents

Python is a high-level programming language that is widely used for web development, machine learning, scientific computing and more. In this blog post, we will be identify (and build) how to develop a simple python project with unit testing, publishing it finally as a binary and distribute it as source.

First, we will create a python module that contains a simple function. This function takes a number as an input and returns the square of that number. We will name this module as “square”.

def square(x):
    return x * x

Next, we will write unit tests for this module. We will use the unittest module to write tests that check if the square function is working correctly.

import unittest
import square

class TestSquare(unittest.TestCase):
    def test_square(self):
        self.assertEqual(square.square(2), 4)
        self.assertEqual(square.square(3), 9)

if __name__ == '__main__':
    unittest.main()

Once we have developed and tested our module, it’s time to distribute it. We will use the pyinstaller tool to generate a binary distribution of our module. Pyinstaller generates a standalone executable file that contains all the necessary dependencies, including python itself.

To generate a binary distribution, we need to run the following command:

pyinstaller --onefile square.py

We can also publish our module as a source distribution. This is useful if the users want to modify the code or want to see how it works. To create a source distribution, we will use the setup module.

from setuptools import setup

setup(
    name='square',
    version='1.0',
    description='A simple module to square a number',
    author='Your Name',
    author_email='your.email@example.com',
    py_modules=['square'],
    classifiers=[
        'Development Status :: 5 - Production/Stable',
        'Intended Audience :: Developers',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.5',
        'Programming Language :: Python :: 3.6',
        'Programming Language :: Python :: 3.7',
        'Programming Language :: Python :: 3.8',
        'Programming Language :: Python :: 3.9',
    ],
)

To create a source distribution, we need to run the following command:

python3 setup.py sdist

This will generate a source distribution archive in the dist directory. The users can download this archive, extract it, and install the module using the following command:

pip3 install dist/square-1.0.tar.gz

Conclusion
#

In this blog post, we have discussed how to develop a simple python project with unit testing, and publishing it as both a binary and source distribution. We have used the pyinstaller tool to generate a binary distribution and the setup module to create a source distribution.

Code available in github.

Author
Samarthya
Engineer, learner, tinkerer. This is my trophy shelf of everyday work and leisure.

Related

Python: Class

·1 min
Learning to write Python code? A Python class is a blueprint for creating objects. It defines a set of attributes and methods that can be used to manipulate those attributes. Here’s an example of a simple Python class:

Python: Decorators

·1 min
Let’s learn about decorators @iamdecorator Decorators are used to modify or extend the behavior of functions and classes. They are useful for wrapping a function or class with additional functionality, such as logging, access control, performance measurement, or adding attributes. This can reduce code duplication, increase readability and maintainability.

Python: Inheritance

·2 mins
Inheritance is a mechanism in object-oriented programming (OOP) that allows you to create new classes that are derived from existing classes. The derived class inherits attributes and behaviors from the base class, and can also have additional attributes and behaviors of its own.