{"id":129,"date":"2023-03-25T18:12:23","date_gmt":"2023-03-25T16:12:23","guid":{"rendered":"https:\/\/www.mutareb.com\/?p=129"},"modified":"2023-04-02T22:11:42","modified_gmt":"2023-04-02T20:11:42","slug":"python-project-scaffold","status":"publish","type":"post","link":"https:\/\/www.mutareb.com\/index.php\/2023\/03\/25\/python-project-scaffold\/","title":{"rendered":"Python Project Scaffold"},"content":{"rendered":"\n<p>create a project scaffold or project template to get started faster with python projects<\/p>\n\n\n\n<p>We will create a virtual environment inside our project to avoid any incompatibility issues<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>python3 -m venv ~\/.scaffold\nsource ~\/.scaffold\/bin\/activate<\/code><\/pre>\n\n\n\n<p>Example code<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def toyou(x):\n    return f\"hi {x}\"\n\ndef add(x):\n    return x + 1\n\ndef subtract(x):\n    return x - 1\n    <\/code><\/pre>\n\n\n\n<p>Example test code<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from hello import toyou, add, subtract\n\n\ndef setup_function(function):\n    print(f\" Running Setup: {function.__name__}\")\n    function.x = 10\n\n\ndef teardown_function(function):\n    print(f\" Running Teardown: {function.__name__}\")\n    del function.x\n\n\n### Run to see failed test\n#def test_hello_add():\n#    assert add(test_hello_add.x) == 12\n\ndef test_hello_subtract():\n    assert subtract(test_hello_subtract.x) == 9<\/code><\/pre>\n\n\n\n<p>We need then a Makefile to automate builds and tests<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>install:\n\tpip install --upgrade pip &amp;&amp;\\\n\t\tpip install -r requirements.txt\n\t\t\nformat:\n\tblack *.py\n\t\nlint:\n\tpylint --disable=R,C hello.py\n\t\ntest:\n\tpython -m pytest -vv --cov=hello test_hello.py\n\t\nall: install lint test<\/code><\/pre>\n\n\n\n<p>Then we will create the requirements file<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pylint\npytest\nclick\nblack\npytest-cov<\/code><\/pre>\n\n\n\n<p>The .github\/workflows\/pythonapp.yml file that stores the commands to build and test the project on Github Actions:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>name: Python application test with Github Actions\n\non: &#91;push]\n\njobs:\n  build:\n\n    runs-on: ubuntu-latest\n\n    steps:\n    - uses: actions\/checkout@v2\n    - name: Set up Python 3.8\n      uses: actions\/setup-python@v1\n      with:\n        python-version: 3.8\n    - name: Install dependencies\n      run: |\n        make install\n    - name: Lint with pylint\n      run: |\n        make lint\n    - name: Test with pytest\n      run: |\n        make test\n    - name: Format code\n      run: |\n        make format\n    \n <\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>create a project scaffold or project template to get started faster with python projects We will create a virtual environment inside our project to avoid any incompatibility issues Example code Example test code We need then a Makefile to automate builds and tests Then we will create the requirements file The .github\/workflows\/pythonapp.yml file that stores&hellip; <a class=\"more-link\" href=\"https:\/\/www.mutareb.com\/index.php\/2023\/03\/25\/python-project-scaffold\/\">Continue reading <span class=\"screen-reader-text\">Python Project Scaffold<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[37,39,38],"series":[],"class_list":["post-129","post","type-post","status-publish","format-standard","hentry","category-automation","tag-cd","tag-github","tag-python","entry"],"_links":{"self":[{"href":"https:\/\/www.mutareb.com\/index.php\/wp-json\/wp\/v2\/posts\/129","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mutareb.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mutareb.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mutareb.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mutareb.com\/index.php\/wp-json\/wp\/v2\/comments?post=129"}],"version-history":[{"count":6,"href":"https:\/\/www.mutareb.com\/index.php\/wp-json\/wp\/v2\/posts\/129\/revisions"}],"predecessor-version":[{"id":137,"href":"https:\/\/www.mutareb.com\/index.php\/wp-json\/wp\/v2\/posts\/129\/revisions\/137"}],"wp:attachment":[{"href":"https:\/\/www.mutareb.com\/index.php\/wp-json\/wp\/v2\/media?parent=129"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mutareb.com\/index.php\/wp-json\/wp\/v2\/categories?post=129"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mutareb.com\/index.php\/wp-json\/wp\/v2\/tags?post=129"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/www.mutareb.com\/index.php\/wp-json\/wp\/v2\/series?post=129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}