{"id":2418,"date":"2023-02-01T15:17:54","date_gmt":"2023-02-01T15:17:54","guid":{"rendered":"https:\/\/blog.samarthya.me\/wps\/?p=2418"},"modified":"2023-02-01T15:17:56","modified_gmt":"2023-02-01T15:17:56","slug":"python-sample-project","status":"publish","type":"post","link":"https:\/\/blog.samarthya.me\/wps\/2023\/02\/01\/python-sample-project\/","title":{"rendered":"Python: Sample Project"},"content":{"rendered":"\n<p>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.<\/p>\n\n\n\n<p>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 &#8220;square&#8221;.<\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-cyan-bluish-gray-background-color has-text-color has-background has-small-font-size\"><code>def square(x):\n    return x * x<\/code><\/pre>\n\n\n\n<p>Next, we will write unit tests for this module. We will use the <code>unittest<\/code> module to write tests that check if the square function is working correctly.<\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-cyan-bluish-gray-background-color has-text-color has-background has-small-font-size\"><code>import unittest\nimport square\n\nclass TestSquare(unittest.TestCase):\n    def test_square(self):\n        self.assertEqual(square.square(2), 4)\n        self.assertEqual(square.square(3), 9)\n\nif __name__ == '__main__':\n    unittest.main()<\/code><\/pre>\n\n\n\n<p>Once we have developed and tested our module, it&#8217;s time to distribute it. We will use the <code>pyinstaller<\/code> tool to generate a binary distribution of our module. Pyinstaller generates a standalone executable file that contains all the necessary dependencies, including python itself.<\/p>\n\n\n\n<p>To generate a binary distribution, we need to run the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-cyan-bluish-gray-background-color has-text-color has-background has-medium-font-size\"><code>pyinstaller --onefile square.py<\/code><\/pre>\n\n\n\n<p>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 <code>setup<\/code> module.<\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-pale-pink-background-color has-text-color has-background has-small-font-size\"><code>from setuptools import setup\n\nsetup(\n    name='square',\n    version='1.0',\n    description='A simple module to square a number',\n    author='Your Name',\n    author_email='your.email@example.com',\n    py_modules=&#91;'square'],\n    classifiers=&#91;\n        'Development Status :: 5 - Production\/Stable',\n        'Intended Audience :: Developers',\n        'Programming Language :: Python :: 3',\n        'Programming Language :: Python :: 3.5',\n        'Programming Language :: Python :: 3.6',\n        'Programming Language :: Python :: 3.7',\n        'Programming Language :: Python :: 3.8',\n        'Programming Language :: Python :: 3.9',\n    ],\n)<\/code><\/pre>\n\n\n\n<p>To create a source distribution, we need to run the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-cyan-bluish-gray-background-color has-text-color has-background has-medium-font-size\"><code>python3 setup.py sdist<\/code><\/pre>\n\n\n\n<p>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:<\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-cyan-bluish-gray-background-color has-text-color has-background has-medium-font-size\"><code>pip3 install dist\/square-1.0.tar.gz<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Conclusion<\/h3>\n\n\n\n<p class=\"has-medium-font-size\">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 <code>pyinstaller<\/code> tool to generate a binary distribution and the <code>setup<\/code> module to create a source distribution.<\/p>\n\n\n\n<p>Code available in <a href=\"https:\/\/github.com\/samarthya\/pysample-one\">github<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":2399,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"image","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[34],"tags":[274,270],"class_list":["post-2418","post","type-post","status-publish","format-image","has-post-thumbnail","hentry","category-technical","tag-project","tag-python","post_format-post-format-image"],"_links":{"self":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/2418","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/comments?post=2418"}],"version-history":[{"count":1,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/2418\/revisions"}],"predecessor-version":[{"id":2419,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/2418\/revisions\/2419"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media\/2399"}],"wp:attachment":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media?parent=2418"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/categories?post=2418"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/tags?post=2418"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}