<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Peter Kogan on Medium]]></title>
        <description><![CDATA[Stories by Peter Kogan on Medium]]></description>
        <link>https://medium.com/@peterkogan?source=rss-69da385f5784------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*6i5bv306BBrlw0w0CuBUJg.gif</url>
            <title>Stories by Peter Kogan on Medium</title>
            <link>https://medium.com/@peterkogan?source=rss-69da385f5784------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 09:36:45 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@peterkogan/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Publish your open-source project TODAY!]]></title>
            <link>https://peterkogan.medium.com/publish-your-open-source-project-today-9c972b8b2857?source=rss-69da385f5784------2</link>
            <guid isPermaLink="false">https://medium.com/p/9c972b8b2857</guid>
            <category><![CDATA[open-source]]></category>
            <category><![CDATA[libraries]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[projects]]></category>
            <dc:creator><![CDATA[Peter Kogan]]></dc:creator>
            <pubDate>Sun, 19 Jun 2022 13:34:47 GMT</pubDate>
            <atom:updated>2022-06-19T13:34:47.300Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/416/1*z1uOUxm29NTpVl5-1NbdFg.png" /><figcaption>Uncle Sam: I want you to Publish your open-source project TODAY!</figcaption></figure><p>This is a follow-up story on <a href="https://peterkogan.medium.com/creating-and-promoting-python-pypi-libraries-b0d576efcbc0">how to create and maintain an open-source project</a> and my new <a href="https://pypi.org/project/pytest-fastapi-deps/">FastAPI dependency library</a>.</p><p>I want to show you how you can start and publish an open-source library in under 24 hours.</p><p>You can use existing tools and best practices to publish your work, even if you’re a novice python developer.</p><p><strong>Getting Started</strong></p><p><strong>Figure out what your project is about</strong>: it can be a simple and trivial piece of code or a sophisticated framework. In my latest open-source project, the gist was to expose functionality through a <a href="https://docs.pytest.org/en/6.2.x/fixture.html">pytest fixture</a> in a few dozen lines of code shown in <a href="https://peterkogan.medium.com/introducing-pytest-fastapi-dependencies-57da5643fed7">my previous post</a>.</p><p><strong>Create the project using the </strong><a href="https://github.com/TezRomacH/python-package-template"><strong>python-package-template</strong></a>: it’s pretty self-explanatory, see the video below or enter the project home page.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/467/1*_Fx4n4-2Y_R7_ulSWIRNmA.gif" /><figcaption>Creating a project using python-package-template</figcaption></figure><p><strong>Edit the created project</strong>:</p><p>Your project is almost ready, and now you have to customize it:</p><ol><li>Add your code and tests.</li><li>Push your project into GitHub according to the template instructions.</li><li>Set up the publishing to PyPI:</li></ol><ul><li>If you haven’t signed up for PyPI, do it now: sign up for free for the <a href="https://test.pypi.org/account/register/">test PyPI</a> and <a href="https://pypi.org/account/register/">production PyPI</a>.</li><li>Create API tokens for both the test and production PyPI. API tokens are under <a href="https://test.pypi.org/manage/account/">Account Settings</a>.</li><li>Save the tokens you created as GitHub secrets named TEST_PYPI_PASSWORD and PROD_PYPI_PASSWORD. <br>Add the secrets inside the GitHub project settings -&gt; secrets -&gt; actions.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iwy_7H2YDwfSTylnOL9y7g.png" /><figcaption>GitHub Action Secrets for PyPI publishing</figcaption></figure><ul><li>Copy the <a href="https://github.com/pksol/pytest-fastapi-deps/blob/main/.github/workflows/publish-to-pypi.yml">publish-to-pypi.yml</a> file from my repository into yours. Unfortunately, the template doesn’t include this bit out of the box, but you can add it yourself: copy the actions file and replace pytest_fastapi_deps and pytest-fastapi-deps with your project name.</li></ul><p>3. <strong>Optional</strong>: remove/carve out stuff you don’t need from the template project. For example, you might want to remove the docker configuration if you don’t plan on releasing a docker image of your project, or perhaps you don’t want the automatic package dependency bot to run every day (you can change it to run once a month or disable it entirely).</p><p><strong>Publish to PyPI</strong></p><p>Remember to update the project version in <a href="https://github.com/pksol/pytest-fastapi-deps/blob/c209588aa101e43bd9d534961755718791d4ee5d/pyproject.toml#L8">pyproject.toml</a>. Every time you push new code into a feature branch, the action would push your new code into the test PyPI. And when you release a tag — the code would be pushed into the actual production PyPI everyone uses!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SxzpGsHgODpuepFI_yR38A.png" /><figcaption>pytest-fastapi-deps version history</figcaption></figure><p><strong>You&#39;re done!</strong> In a matter of a few easy steps and a couple of hours, you can have your python project available for everyone.</p><p>Ping me if you have encountered any issues during this tutorial or if you have cool ideas for an open-source project that you want help with.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9c972b8b2857" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Introducing Pytest FastAPI Dependencies]]></title>
            <link>https://peterkogan.medium.com/introducing-pytest-fastapi-dependencies-57da5643fed7?source=rss-69da385f5784------2</link>
            <guid isPermaLink="false">https://medium.com/p/57da5643fed7</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[fastapi]]></category>
            <category><![CDATA[dependency-injection]]></category>
            <category><![CDATA[pytest]]></category>
            <dc:creator><![CDATA[Peter Kogan]]></dc:creator>
            <pubDate>Mon, 23 May 2022 08:58:39 GMT</pubDate>
            <atom:updated>2022-05-23T08:58:39.159Z</atom:updated>
            <content:encoded><![CDATA[<p>Recently, I was working on a FastAPI project. Things were going great until I had to do some unit testing to ensure that the functionality behaves the way I intended: FastAPI does provide a rich <a href="https://fastapi.tiangolo.com/tutorial/testing/#using-testclient">TestClient</a>, but I was looking to do something specific — replace a dependency function.</p><p>FastAPI provides the ability to <a href="https://fastapi.tiangolo.com/advanced/testing-dependencies/#use-the-appdependency_overrides-attribute">replace dependency functions</a> like so:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/753/1*qixrK1O4TbbSPvNjfkYD4A.png" /></figure><p>However, this is not native to Pytest — if you copy &amp; paste this code as is to your test directories, you would be getting some unexpected behavior since your override would affect other tests and modules. This happened to developers <a href="https://stackoverflow.com/questions/70953171/fastapi-app-dependency-overrides-affects-other-test-files/72203783">here</a> and <a href="https://stackoverflow.com/questions/71457448/how-can-i-skip-authentication-in-a-single-test-with-a-fixture-in-fast-api-togeth/72203895">here</a>.</p><p>If only Pytest had a standard way of setup and cleanup for testing… fixtures of course!</p><p>From here, the road ahead was trivial: I’ll create a fixture that would make the dependency replacement natural.</p><p>Better yet, I’ll make an open-source pytest plugin out of it, so everyone can use it.</p><p>If you care about the implementation details — read this section. If not, you can jump to the next section where I show the usage of this plugin.</p><p>The first thing we need is a clean way of setting and un-setting overrides. A Python context manager is an obvious solution and one that <a href="https://stackoverflow.com/a/68687967/3800552">was suggested in this stack overflow answer</a>. The code is:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b2242f8793c480699e9d09c01eed3f8d/href">https://medium.com/media/b2242f8793c480699e9d09c01eed3f8d/href</a></iframe><p>The code above is pretty trivial, we get a FastAPI app and a dictionary of overrides where the key is the old function and the value is the replacement function. When we enter the context, the dictionary replacements are applied and when we exit the context, they are removed and the previous functions are restored.</p><p>Next, we need to convert this context manager into something that is usable in Pytest, i.e. a fixture:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/999cacda16d19c09a413bf19a3c30ac3/href">https://medium.com/media/999cacda16d19c09a413bf19a3c30ac3/href</a></iframe><p>This code needs a bit more explaining.</p><p>Let’s start with the fastapi_dep function: it defines a fixture with the same name. There are two ways to use the fixture: with or without parameters.</p><p>If we use the fixture without parameters, then we would get back the FixtureDependencyOverrider class, which can be used directly in the test. See test_main.py for a usage example in the section below.</p><p>If we want to make our test body cleaner, we can provide the FastAPI and the overrides directly to the fixture, so the context manager can be created there. The matching example is shown later in test_main_indirect.py.</p><p>To use the library, install it via pip:</p><pre>pip install pytest-fastapi-deps</pre><p>Then, if your code has an endpoint like this which uses <a href="https://fastapi.tiangolo.com/tutorial/dependencies/">depends</a>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/10c15e3f82a78d53e99a0f82a4b546c4/href">https://medium.com/media/10c15e3f82a78d53e99a0f82a4b546c4/href</a></iframe><p>You can add tests to it like so:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ffbb634996c60de7aa34abd96de9fd83/href">https://medium.com/media/ffbb634996c60de7aa34abd96de9fd83/href</a></iframe><p>Note the fastapi_dep fixture and the context manager — you create it using the FastAPI app and provide it with a dictionary of replacement functions.</p><p>The moment you step out of the context, the overrides are removed your code returns to its normal behavior — other tests won’t be affected.</p><p>The above example is very expressive in terms of code. If you want a more minimal signature inside your test body, you can use the following syntax alternative, which has the same effect:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2daf5f9b9b2d6af5396ddcea6cdf7de2/href">https://medium.com/media/2daf5f9b9b2d6af5396ddcea6cdf7de2/href</a></iframe><p>The making of the project took me a few hours and that includes everything — the coding itself, testing, documenting, and publishing to PyPI.</p><p>Feel free to use this plugin and spread the word.</p><p>If you have any comments or issues, you can comment below or <a href="https://github.com/pksol/pytest-fastapi-deps/issues/new/choose">open an issue</a> on the <a href="https://github.com/pksol/pytest-fastapi-deps">project homepage</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=57da5643fed7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Creating and Promoting Python PyPI Libraries]]></title>
            <link>https://peterkogan.medium.com/creating-and-promoting-python-pypi-libraries-b0d576efcbc0?source=rss-69da385f5784------2</link>
            <guid isPermaLink="false">https://medium.com/p/b0d576efcbc0</guid>
            <category><![CDATA[open-source]]></category>
            <category><![CDATA[libraries]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[pypi]]></category>
            <dc:creator><![CDATA[Peter Kogan]]></dc:creator>
            <pubDate>Thu, 31 Mar 2022 07:11:00 GMT</pubDate>
            <atom:updated>2022-03-31T07:11:00.006Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*O9OcvYwZJboB9sEbQ-khFw.jpeg" /></figure><p>This article will provide you with the knowledge, skills, insight, and encouragement you need to start an open-source library now and maintain and promote it for the next few years.</p><p>For the past few years, I’ve been promoting <a href="https://pypi.org/project/pytest-mock-generator/">a python library I’ve created</a>. This was a journey full of revelations, many of them outside the scope of engineering and software design.</p><p>I came across many obstacles and a general lack of knowledge about the most effective ways of promoting a code library. My intuition and many good friends helped along the way.</p><p>In this article, I’ll share the best solutions I’ve found for creating and promoting open source libraries.</p><p><strong>Getting Started</strong></p><p>Let’s begin with the knowledge and encouragement you need to start your open source project today.</p><p><strong>Myth-Busting</strong></p><p>These are some of the common misconceptions I’ve heard throughout the years:</p><blockquote>“Only the most experienced developers can create open-source projects.”</blockquote><blockquote>“It’s difficult to publish to PyPI.”</blockquote><blockquote>“It takes a lot of time to support a project.”</blockquote><p><strong>It turns out that the above statements are false.</strong></p><p>I was a novice Python developer when I published my open-source library for the first time. It was pretty easy then. Nowadays, it’s even easier — there are a lot more tools and resources available.</p><p>I recommend that you use a <a href="https://github.com/TezRomacH/python-package-template">project template like this one</a>. You can create and configure your project in a matter of minutes, <a href="https://github.com/">add it to GitHub for free</a> and <a href="https://pypi.org/account/register/">publish it on PyPI</a> in a few hours (also free).</p><p>I speak from experience — I used this template to:</p><ul><li><a href="https://github.com/pksol/pytest-mock-generator">Create my project</a></li><li><a href="https://github.com/pksol/pytest-mock-generator/actions">Configure it and run tests</a></li><li><a href="https://pypi.org/project/pytest-mock-generator/">Publish it on PyPI</a></li></ul><p>It was quick, intuitive, and I learned a few things along the way.</p><p><strong>In my experience, supporting an open-source project doesn’t take a lot of time. It can vary, depending on how big is your project, your audience size, and the feature request ratio. If you do have a large and vibrant community using your library, some of them should be able to help you out with pull requests and such.</strong></p><p><strong>Maintaining and Improving</strong></p><p>Now that I’ve shown you the tools to build and publish your project, let’s talk about the specifics and how to make it great!</p><p><strong>The Need</strong></p><p>You should create a library that solves a real need. If there is no real need, the library would have a very low adoption. The need can start as your personal need, but you should assess the need for your library in the target community.</p><p>Assessing and adapting to the needs of your users is key.</p><p>I started by solving my immediate needs and then used an internal developer workshop from within my company to help me take my library to the next level.</p><p><strong>Quantity Does Not Equal Quality</strong></p><p>It’s easy to get confused between the two; you can have broad attention by posting to large crowds, but if most of the people there are not your actual target audience, it would do little in terms of getting useful feedback or actual traction of your library.</p><p>Try to find the right communities and forums to promote the library: newsletters, meetups, conferences, email lists, etc.</p><p>The point here is to be narrow: target the exact audience.</p><p><strong>Meeting Your Customers Early</strong></p><p>You have to meet your customers early. By “meet” I mean any interaction that your customers have with your product and that is reported back to you by any means.</p><p>This can be achieved by: answering questions on various forums, writing blog posts, collecting usage statistics, running a workshop, observing a few selected customers while they use your product, etc.</p><p>The two most effective ways for me to “meet” customers were through the workshops mentioned earlier and by answering and discussing stack-overflow questions of people who had similar problems.</p><p><strong>Getting Feedback</strong></p><p>Getting feedback is possible by meeting the customers. You can’t have feedback unless you meet customers. Responding to feedback is a crucial ingredient throughout the entire cycle of your library development, promotion, support, and maintenance.</p><p>You have to be willing to accept good feedback and pivot when the time is right. Failing to do so might leave you with a product you like, but has no real use to almost anyone else.</p><p><strong>Quick Iterations</strong></p><p>When you get useful feedback — act quickly.</p><p>One good example in my case was simplifying the API: the library was working, but it wasn’t trivial to use. I simplified the API and it made a big difference.</p><p>Another example is updating the documentation based on user feedback: if they can’t understand how to use the library — they won’t. Responding to difficulties your users encounter, can have a huge effect both on them and on future users.</p><p>The biggest pivot I made in my library was a feature request by a novice Python engineer — he expected the library to be more intuitive as he had no prior background. This “feature request” turned out to be a massive code rewrite that took my library to the next level and helped many more engineers, including those with a stronger background in Python.</p><p><strong>Having several funnels</strong></p><p>Have several funnels to feed the interest of your library.</p><p>Leverage your social media and online presence, even if it’s not directly tied to the library.</p><p>If you are well known on social media for other things you do, including and especially technical programming, you can use that when promoting — because you have a big community of followers.</p><p>Posts on multiple social media networks, events (physical and virtual), mail lists and newsletters, stack overflow answers, and many other ideas can help you get some traction. You never know which of them would be a game-changer.</p><blockquote><em>Most of the people are shy. Don’t be shy. If you publish your work you will stand out almost immediately. (Peter Kogan 2022)</em></blockquote><p><strong>Playing the Long Game</strong></p><p>Success takes time, don’t waste your energy too quickly, you’ll need it later.</p><p>Be ready to apply many different plans and strategies: answer questions in <a href="https://stackoverflow.com/questions/28192283/problems-trying-to-mock-a-model-within-flask-sqlalchemy/58202038#58202038">stack-overflow</a> and let it reach more and more audiences, get into <a href="https://pycoders.com/issues/514#:~:text=Mocking%20in%20Python%20Has%20Never%20Been%20Easier">mailing lists and publications</a>, publish <a href="https://peterkogan.medium.com/mocking-in-python-has-never-been-easier-5f9b15e1498f">several</a> <a href="https://medium.datadriveninvestor.com/go-beyond-mock-on-mocks-stubs-and-fakes-e8b1888932f9">posts</a> and conduct training, have ambassadors of your library. These are just a few of the examples I used.</p><p>You can do more than that — consider heavy use of multiple social media channels, doing or joining existing podcasts, conferences and the list goes on… apply some thought and come up with even more ideas. Try to think outside the box and outside of your comfort area.</p><p>Try several vectors instead of focusing on just one tactic: most of the steps you would do, would have a limited effect, so it’s important to mix and spread them over time.</p><p><strong>Have Fun</strong></p><p>Remember why you started the project in the first place, think about the joy you make from it. Is it the desire to do something new and innovative? Is it to learn new skills? Maybe improving the life of your customers? Perhaps you wanted to get out of your comfort zone and engage with people?</p><p>Having fun can increase the chance of you reaching your final destination and in any case, it would make the journey more joyful.</p><p>I hope that this guide helped you. If you are promoting your library, I’d be happy if you add a comment in this article:</p><ol><li>What were the steps that worked best for you?</li><li>Can you share the barriers you have encountered in your journey?</li><li>Any additional methods you may have to promote libraries?</li></ol><p>P.S<br>Help me promote <a href="https://pypi.org/project/pytest-mock-generator/">Pytest Mock Generator</a>: a pull request, a <a href="https://peterkogan.medium.com/mocking-in-python-has-never-been-easier-5f9b15e1498f">social media share</a>, or even a <a href="https://github.com/pksol/pytest-mock-generator/stargazers">simple star</a> can go a long way.</p><p>Thanks to Joshua Sheats for his timeless “<a href="https://www.radicalpersonalfinance.com/">Radical Personal Finance</a>” podcast intro:</p><blockquote>“…the show dedicated to providing you with the knowledge, skills, insight, and encouragement you need to live a rich and meaningful life now while building a plan for financial freedom in 10 years or less.”</blockquote><p>I’ve borrowed it and modified it to fit this post.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b0d576efcbc0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Mocking in Python Has Never Been Easier]]></title>
            <link>https://peterkogan.medium.com/mocking-in-python-has-never-been-easier-5f9b15e1498f?source=rss-69da385f5784------2</link>
            <guid isPermaLink="false">https://medium.com/p/5f9b15e1498f</guid>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[mocking]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[pytest]]></category>
            <category><![CDATA[mock]]></category>
            <dc:creator><![CDATA[Peter Kogan]]></dc:creator>
            <pubDate>Sun, 20 Feb 2022 10:10:43 GMT</pubDate>
            <atom:updated>2022-02-20T10:10:43.045Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Gyy_A_LmfnF3IqNKxI8I6w.jpeg" /><figcaption>A mocked deer :)</figcaption></figure><blockquote>This article assumes you already know your way around Python mocks and Pytest and that you’re familiar with the Arrange-Act-Assert pattern. <br>If you need a short recap, I find the following articles useful: <br><a href="https://yeraydiazdiaz.medium.com/what-the-mock-cheatsheet-mocking-in-python-6a71db997832">What the mock</a>? A cheatsheet for mocking in Python<br><a href="https://automationpanda.com/2020/07/07/arrange-act-assert-a-pattern-for-writing-good-tests/">Arrange-Act-Assert</a>: A Pattern for Writing Good Tests</blockquote><p><em>I often struggle with Python mocks, this is why I wrote a library to assist me.</em></p><p><strong>The </strong><a href="https://pypi.org/project/pytest-mock-generator/"><strong>Pytest Mock Generator</strong></a><strong> library simplifies writing the ‘arrange’ and ‘assert’ sections. It generates the ‘arrange’ and ‘assert’ sections for you and allows you to focus on the ‘act’ section of your tests.</strong></p><p><em>I’m sharing it with you, so it can save you time and effort.</em></p><p>Let’s assume you want to do some complex logic on a string and then write it to a zip file. Here is a short piece of code to do just that:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b32a94dd023d70c1651c9c7dd43b45f4/href">https://medium.com/media/b32a94dd023d70c1651c9c7dd43b45f4/href</a></iframe><p>This is the function that you want to test (aka Unit Under Test or UUT). You don’t want to create a file on the hard drive and wish to mock it instead.</p><p>Here is where the <a href="https://pypi.org/project/pytest-mock-generator/">mock generator</a> (mg for short) fixture comes to play — it would <strong>generate both the arrange and assert sections for you</strong>.</p><p>An outline of the test method in an arrange-act-assert pattern looks like so:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e2c36bc05a0e5a62011b0a4005d6ae67/href">https://medium.com/media/e2c36bc05a0e5a62011b0a4005d6ae67/href</a></iframe><p>Please note this line in particular: <strong>mg.generate_uut_mocks_with_asserts(process_and_zip)</strong>. It receives the tested function as a parameter and does a static code analysis to mock the various dependencies of the function.</p><p>When you execute the test, the following output is printed to the console and copied to the clipboard:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3faaae872679d1d874860b83ef48546d/href">https://medium.com/media/3faaae872679d1d874860b83ef48546d/href</a></iframe><p>This generated code is ready to use and can be embedded in your test:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/08f0924b21798d05adec69fd74e20d4f/href">https://medium.com/media/08f0924b21798d05adec69fd74e20d4f/href</a></iframe><p>The arrange part is complete and we’re left with filling the assert section. Luckily for us, this line would generate the assert section: <strong>mg.generate_asserts(mock_ZipFile, name=’mock_ZipFile’)</strong>. Run the test function once more and you’ll get the asserts copied to the clipboard and printed to the console:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/98ec4279ff441fac2a42ddf9bc5f04ed/href">https://medium.com/media/98ec4279ff441fac2a42ddf9bc5f04ed/href</a></iframe><p>These asserts are the exact calls made to the mock we prepared earlier. They include the creation of the zip file, entering and exiting the context, and the call to create a file inside the zip and fill it with data.</p><p>Some of them are pretty low level, so I’m omitting them from the final version of the test function:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6f8f859657ebeec18102f8773f4c68b9/href">https://medium.com/media/6f8f859657ebeec18102f8773f4c68b9/href</a></iframe><p>That’s it! We have done several great things in this article, so here is a short recap:</p><ul><li>We have a beautiful test method in no time.</li><li>Most of the code was auto-generated.</li><li>The helper library allowed us to focus on the main code sections.</li><li>We have saved time and effort doing the above.</li><li>Mocking in Python has never been easier 😊</li></ul><p><strong>To get started, visit </strong><a href="https://pypi.org/project/pytest-mock-generator/"><strong>https://pypi.org/project/pytest-mock-generator/</strong></a><strong>.</strong></p><p><strong>Did this tool save you some time? If so, let me know.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5f9b15e1498f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[GitHub Actions: The Good, the Bad, and the Ugly]]></title>
            <link>https://peterkogan.medium.com/github-actions-the-good-the-bad-and-the-ugly-eb4aa6105ef6?source=rss-69da385f5784------2</link>
            <guid isPermaLink="false">https://medium.com/p/eb4aa6105ef6</guid>
            <category><![CDATA[github-actions]]></category>
            <category><![CDATA[workflow]]></category>
            <category><![CDATA[pipeline]]></category>
            <category><![CDATA[ci]]></category>
            <category><![CDATA[github]]></category>
            <dc:creator><![CDATA[Peter Kogan]]></dc:creator>
            <pubDate>Thu, 30 Dec 2021 09:15:31 GMT</pubDate>
            <atom:updated>2021-12-30T09:15:31.728Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*a36ysWcsQcznI0qLrmDZFg.jpeg" /></figure><p>In late 2019, GitHub had finally released GitHub Actions, a long-awaited move by many developers and followers of the beta version.</p><p>To mark their 2nd anniversary, I’ve decided to check out the progress they’ve made.</p><p>So what are GitHub Actions and are they worth your time? I’ll try to answer these questions in this post.</p><p><strong>What Are GitHub Actions?</strong></p><p>I found out that the best way to learn about GitHub Actions is through examples. Let’s start with a code project of mine, the <a href="https://github.com/pksol/mock_autogen">Mock Generator</a>.</p><p>Actions are placed inside yml files in the .github/workflows folder. You may put several yml files and name them as you like.</p><p>Here is the beginning of <a href="https://github.com/pksol/mock_autogen/blob/master/.github/workflows/continuous-integration-workflow.yml">continuous-integration-workflow.yml</a>, which is responsible for the CI:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/74a2871b3698444af491ef34d32da82d/href">https://medium.com/media/74a2871b3698444af491ef34d32da82d/href</a></iframe><p>The name key is pretty obvious, and the on key indicates when to trigger this flow. I wanted it to be triggered every time I push new code into any of my branches.</p><p>The next section of the file is jobs, where you can put a list of jobs that comprise your workflow. Jobs are run in parallel.</p><p>Every job has properties like the operating system and the steps, which are sequential tasks.</p><p>For the first job, I used two existing actions to check out my code and perform file formatting. All I had to do is mention which actions to use (uses) and send them some arguments.</p><p>Pretty neat, right?</p><p>The automated tests are configured later on the same file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e296ec3eb1a98957bffd4edeb6bf4f79/href">https://medium.com/media/e296ec3eb1a98957bffd4edeb6bf4f79/href</a></iframe><p>This job runs parallel to the first job, on a combination of Python versions and OS versions to ensure compatibility and avoid regressions.</p><p>Here are some additional elements that are new to this example:</p><ul><li>strategy: this allows you to run a matrix of combinations instead of a single version of your job. Extremely useful and powerful.</li><li>runs-on: ${{ matrix.os }}: using expressions instead of hardcoded values.</li><li>A couple of steps use run instead of uses: this way you can execute custom shell commands in cases where there is no suitable action out of the box.</li></ul><p>This covers the basics of workflow and action definitions. If you want a more thorough understanding of the syntax of the workflow files, you can find it <a href="https://docs.github.com/en/actions/learn-github-actions/understanding-github-actions">in the official documentation</a>.</p><p>As I mentioned earlier, the CI workflow is executed on every push to the repository. The Actions tab in the GitHub repository shows both the currently running workflows and their execution history:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Qzs08cVZs_cB8G36QXehPA.png" /></figure><p>Select the desired workflow, execution, job, and step for more details and the console output:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7e6GUR7uXnorur3luf22Hg.png" /></figure><p>Now that we have covered the basic usage of GitHub Actions, we can proceed to the conclusions.</p><p><strong>The Good</strong></p><p>GitHub now allows you to execute code.</p><p><strong>For free</strong>. Yes, you get computing resources like CPU and memory at your disposal for free. Public repositories get 2000 minutes of CPU time every month, that’s over 33 hours.</p><p>In addition, you can <strong>use and share common code snippets </strong>in the <a href="https://github.com/marketplace?category=&amp;query=&amp;type=actions&amp;verification=">GitHub Actions Marketplace</a>. This saves time and it’s a cool way of collaborating with the huge GitHub community.</p><p><strong>The Bad</strong></p><p>Using code snippets of other people is not trivial at times:</p><ul><li>The documentation is lacking for many snippets, making it harder to integrate.</li><li>Some actions don’t work well with others, and there is no way of knowing this in advance.</li></ul><p>To make those points clear, let’s use an analogy of a mobile app store: imagine that you are looking for a Chess game. You download several Chess apps from the store, some of them fail to launch due to your phone’s version while some other apps require you to change your display settings so they can run.</p><p><strong>The Ugly</strong></p><p>The marketplace:</p><ul><li>The search mechanism returns the actions in an assorted way: you often see niche actions before you get to see the most popular actions.</li><li>The marketplace does not indicate which actions are more mature than others, you have to try them out yourself.</li></ul><p>If we continue the mobile AppStore analogy from before: after your bad experience trying to download a good Chess app, you decide to settle for Checkers and you find several Checkers apps, but you don’t know how to choose between them: there is no indication of the number of downloads and many Checkers apps don’t have screenshots/videos to show the game experience. You end up installing one app after another till you find something that is satisfying.</p><p><strong>Wrapping Up</strong></p><p>Despite its flows, GitHub Actions is a welcomed addition to GitHub, making it a one-stop-shop for both source control and CI/CD.</p><p>The platform behind it is very powerful and can be used to achieve a great variety of things.</p><p>Besides, you had me on free CPU :)</p><p>Why don’t you try it out and let me know what you think?</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=eb4aa6105ef6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Getting the Stack Trace of a Running Python Program]]></title>
            <link>https://levelup.gitconnected.com/getting-the-stack-trace-of-a-running-python-program-f6e2f156f53a?source=rss-69da385f5784------2</link>
            <guid isPermaLink="false">https://medium.com/p/f6e2f156f53a</guid>
            <category><![CDATA[python-programming]]></category>
            <category><![CDATA[stacktrace]]></category>
            <category><![CDATA[utilities]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[debugging]]></category>
            <dc:creator><![CDATA[Peter Kogan]]></dc:creator>
            <pubDate>Sat, 14 Aug 2021 19:18:48 GMT</pubDate>
            <atom:updated>2021-08-17T18:36:10.272Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ej8WJLvjwuFntD9Kkc14jg.jpeg" /></figure><p>I recently wanted to examine a multi-threaded Python program, which took a long time to complete, appeared to be stuck, and even crashed occasionally.</p><p>Having a previous background in java, I was hoping to get the stack trace with some utility like <a href="https://docs.oracle.com/javase/8/docs/technotes/guides/troubleshoot/tooldescr016.html">jstack</a>. To my surprise, <strong>Python doesn’t have such a utility, but it’s easy to make one ourselves!</strong></p><h3>The code</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/45c43710fbb545e336f6ea00bb936e44/href">https://medium.com/media/45c43710fbb545e336f6ea00bb936e44/href</a></iframe><p>It’s pretty obvious — go over the running threads, format the stack trace and append it, then return it.</p><p>It uses pure Python libraries, no addons are required.</p><p>A sample output might look like this (focus mainly on the bottom part):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bf641cd6f8bf51d08b574a5a283caabf/href">https://medium.com/media/bf641cd6f8bf51d08b574a5a283caabf/href</a></iframe><h3>Testing</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0c18a1d3209a88d805c99cad23ad893c/href">https://medium.com/media/0c18a1d3209a88d805c99cad23ad893c/href</a></iframe><p>The output above is the result of the print statement in the test — note that we are expecting to see two threads: the main test thread and the one we just created and the test function and test statements in the stack trace.</p><h3>Next Steps / Caveats</h3><p>So we have the util function and it works, but we need to incorporate it in our code:</p><ol><li>We can have an entry point like a REST API if our program is a web server to execute it upon demand.</li><li>Or, we can have a daemon thread running and occasionally printing the output so we have an ongoing understanding of what’s going on with our program.</li></ol><p>That’s it! I hope that this small utility can help you out in times of need.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f6e2f156f53a" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/getting-the-stack-trace-of-a-running-python-program-f6e2f156f53a">Getting the Stack Trace of a Running Python Program</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[On Writing]]></title>
            <link>https://peterkogan.medium.com/on-writing-a0da4adecf55?source=rss-69da385f5784------2</link>
            <guid isPermaLink="false">https://medium.com/p/a0da4adecf55</guid>
            <category><![CDATA[blogging]]></category>
            <category><![CDATA[writing-tips]]></category>
            <category><![CDATA[writing]]></category>
            <category><![CDATA[tips]]></category>
            <category><![CDATA[blog]]></category>
            <dc:creator><![CDATA[Peter Kogan]]></dc:creator>
            <pubDate>Sun, 01 Aug 2021 06:32:52 GMT</pubDate>
            <atom:updated>2021-08-01T06:32:52.717Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AIdghG9iIdeJAmAvhXDNjg.jpeg" /></figure><p>A friend of mine asked me to share my writing tips and thoughts.</p><p>What better way to do it than to write about it?</p><p><strong>Understand if and Why Do You Want to Write</strong></p><ol><li>Do you want to learn to express yourself?</li><li>Experiment with a potential hobby?</li><li>Have more visibility on social media?</li><li>Get some ego boost?</li><li>Maybe even become (somewhat) famous?</li></ol><p><strong>Writing Is an Art</strong></p><ol><li>It requires some kind of <strong>spark and ingenuity.</strong></li><li>Some people are natural, some are not.</li><li><strong>You don’t have to be a writer to write</strong>, much like you don’t have to be a painter to draw.</li></ol><p><strong>Writing Is Also a Skill</strong></p><ol><li><strong>It gets better with time and practice: </strong>writing becomes easier, it gets more fluent, the content you produce becomes of higher quality.</li><li>If you are not a natural writer, you can become much better by practicing.</li><li>Even if you are a natural writer, you still need to practice.</li></ol><p><strong>Getting Started</strong></p><ol><li><strong>Read before you write</strong>: to be a good writer, you need to start reading first. Read a lot, pay attention and understand what makes some writings better than others.</li><li><strong>Write:</strong> you learn by doing. It doesn’t have to be perfect, just good enough.</li><li><strong>Get feedback:</strong> ask for colleagues, friends, and other people you trust to give you feedback. Try to get feedback from unbiased people you don’t know, who won’t mind telling you the whole truth.</li><li><strong>Reflect: </strong>the feedback you get on your writing is key to getting better.</li><li><strong>Repeat: </strong>go back to step 1 :)</li></ol><p><strong>Being Realistic</strong></p><ol><li>Most of us won’t become well-known writers.</li><li>Your writing will likely reach a narrow audience.</li><li>Your writing will probably be mediocre at best.</li><li>It’s OK! <strong>You don’t have to do it for a living.</strong></li><li><strong>Enjoy the ride: most often, the process of writing is a rewarding journey by itself, no matter how well you do it or how many people have read and reacted to it.</strong></li></ol><p>One final thing: I’m on my personal writing journey and <strong>would appreciate your feedback</strong>.</p><p><strong>Was this article useful to you? Do you have some ideas to make it better? Can you share some additional writing tips to help me and others?</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a0da4adecf55" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Hypermodern Python Reflections]]></title>
            <link>https://peterkogan.medium.com/hypermodern-python-reflections-8121412d4672?source=rss-69da385f5784------2</link>
            <guid isPermaLink="false">https://medium.com/p/8121412d4672</guid>
            <category><![CDATA[makefile]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[poetry]]></category>
            <category><![CDATA[pip]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Peter Kogan]]></dc:creator>
            <pubDate>Fri, 28 May 2021 06:22:21 GMT</pubDate>
            <atom:updated>2021-05-28T06:29:07.485Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/782/1*-AC-lJBzHjq5elaWmqFfow.png" /></figure><p>Around a year ago, I read one of the more influential articles about Python I’ve ever read: <a href="https://cjolowicz.github.io/posts/hypermodern-python-01-setup/">Hypermodern Python</a>.</p><p>That article introduced many new tools to me and changed some of my thinking about programming in Python.</p><p>A year later, after experiencing many of the tools listed in the article, I feel comfortable sharing my impressions.</p><p>You don’t have to read <a href="https://cjolowicz.github.io/posts/hypermodern-python-01-setup/">the original article</a> to follow this short blog post, but I strongly recommend it:</p><ol><li>The original article includes many useful Python tools and frameworks.</li><li>It touches on some theories and best practices.</li><li>It’s a step-by-step coding tutorial, allowing you to experience and explore the various tools freely.</li><li>On top of that, there are additional references to tools that are not covered in the article itself, so you can expand your knowledge even further.</li><li>Finally, it’s split into several sections, making it easy to follow.</li></ol><p>Here are the main tools and frameworks from the article, and an extra few of my own:</p><p><strong>Poetry</strong></p><p><a href="https://python-poetry.org/">Poetry</a> is by far <strong>the best dependency resolution mechanism for Python </strong>I have used to date.</p><p>Its approach to dependency management is simple:</p><ol><li><strong>You define your direct dependencies</strong>, with general version constraints.</li><li><strong>Poetry resolves the dependencies</strong>, finding exact versions for the packages to meet all constraints. This includes derived packages recursively.</li><li>All of your packages, including <strong>direct and derived dependencies, are fixated and their versions are recorded and stored in </strong><strong>poetry.lock .</strong></li><li>Whenever you, a fellow developer, or a build machine, tries to install the dependencies, it uses the <strong>poetry.lock</strong> file to <strong>ensure exact versions for all the direct and derived packages, leading to a deterministic build and uniformity across the entire project</strong>.</li></ol><p>It’s also easy to learn and master, especially if you have experience with <a href="https://pip.pypa.io/en/stable/">pip</a> and <a href="https://docs.python.org/3/library/venv.html#module-venv">virtual environments</a>.</p><p><strong>Pyenv</strong></p><p>Pyenv is a great way of installing local versions of Python, dedicated to specific projects, without impacting the whole system.</p><p><strong>It’s also a great way of defining your python version explicitly, removing ambiguity, and helping to create uniformity in your project.</strong></p><p>This uniformity will reduce the likelihood of bugs caused by developers having a different Python version than the one deployed in production.</p><p><strong>Nox</strong></p><p><a href="https://nox.thea.codes/en/stable/">Nox</a> aims to replace the popular <a href="https://tox.readthedocs.io/en/latest/">tox</a>.</p><p>Its main idea is to use Python code to specify the various virtual environments, unlike its predecessor tox, which uses configuration files/scripts.</p><p><strong>Personally, I think that Nox is a step in the wrong direction: the build process should be simple and straightforward. Using code during your build is a potential opening to complex functionality and bugs.</strong></p><p><em>Since I didn’t like Nox but had to have entry points for the build process, I had to find an alternative. That alternative is Makefiles, which are described in the section below.</em></p><p><strong>GNU make, aka Makefile</strong></p><p><em>Makefiles were not mentioned in the original article, but they integrate nicely with Poetry to provide a better coding experience.</em></p><p><a href="https://opensource.com/article/18/8/what-how-makefile">Makefiles</a> date before Python, they have allowed developers to build their software for over 40 years now (!!!).</p><p>Some developers find them archaic. I had a similar notion in the past. But now, <strong>I strongly believe that Pyenv + Makefiles + Poetry is superior to </strong><a href="https://tox.readthedocs.io/en/latest/"><strong>tox</strong></a><strong>/</strong><a href="https://nox.thea.codes/en/stable/"><strong>Nox</strong></a><strong>. Makefiles allow a standard, simple and resilient build process, which is exactly what you want of your build process.</strong></p><p>I covered the problems with Nox in the previous section, now let’s focus on tox’s disadvantages:</p><ol><li>At the core of tox are the environments: every entry point has a separate environment, which is cached for performance. The main disadvantage of this approach is that environments become stale and have to be manually updated by developers. This causes unexpected behavior: the code might pass on one environment, but fail in another environment on the same computer, a fellow developer’s computer, or on a build machine. This wastes a lot of time.</li><li>Other benefits of tox, like the ability to test your code on different Python versions, can be achieved with other modern tools, making tox less appealing nowadays.</li><li>So we are left with tox’s ability to have entry points — but you can have those easily with Makefiles.</li></ol><p><strong>pre-commit</strong></p><p><a href="https://pre-commit.com/">pre-commit</a> is a “framework for managing and maintaining multi-language pre-commit hooks”, a simple statement that gets the gist of it.</p><p>This framework allows you to add scripts on certain git events like before every commit/push. The scripts are usually around code verification.</p><p>A common use case is to add style checks before commits. This way you can ensure that:</p><ol><li>Your code looks the same</li><li>Speed up development cycles by providing the developers a quicker, local feedback loop instead of a slower, remote CI pipeline.</li></ol><p><strong>pyproject.toml</strong></p><p>pyproject.toml is defined in <a href="https://www.python.org/dev/peps/pep-0621/">PEP 621</a>. Its purpose is to store your project’s metadata in a single place. <strong>It’s super convenient and useful, allowing quick installation, setup, and onboarding for developers.</strong></p><p>It plays nicely with Poetry and other common tools, although some tools don’t support it yet.</p><p>The sections above cover most of my thinking about Hypermodern Python, there are additional tools and frameworks mostly around Code Style, Linting and Formatting, Type Checking, etc. which I did not cover here. Those deserve a post of their own.</p><p><strong>If you want to experience the Hypermodern Python approach, you can create a new preconfigured project in seconds, </strong><a href="https://github.com/TezRomacH/python-package-template#demo"><strong>using this cookie cutter</strong></a><strong>.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8121412d4672" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Solving pip Breakage]]></title>
            <link>https://python.plainenglish.io/solving-pip-breakage-de1e75d4d196?source=rss-69da385f5784------2</link>
            <guid isPermaLink="false">https://medium.com/p/de1e75d4d196</guid>
            <category><![CDATA[python-programming]]></category>
            <category><![CDATA[pip]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[dependencies]]></category>
            <dc:creator><![CDATA[Peter Kogan]]></dc:creator>
            <pubDate>Fri, 01 Jan 2021 10:03:49 GMT</pubDate>
            <atom:updated>2021-01-02T18:51:37.185Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5iDtXClfjR_jgTFqaDOnkA.jpeg" /><figcaption>Looking under the hood of pip</figcaption></figure><h4>For years, pip was notorious for its inconsistencies when downloading dependencies and during conflict resolution.</h4><p>During the last year, the pip team has been putting in a great effort to improve those two fronts. This effort is very impressive, however, it exposes some problems and breaks down existing software.</p><p>Here is one example of such a breakdown, how it was fixed, and some thoughts about the future of pip usage.</p><p>One day, out of the blue, our CI pipeline broke down: pip could not get the dependencies specified in our requirements.txt. This file did not change for a few weeks prior to that incident.</p><p>Turns out that our pip was upgraded to a newer version, in which the dependency resolution algorithm was changed, causing this regression.</p><p>To deal with pip’s failure, we had to understand what was the root cause. Since pip’s error message was unclear, we used a tool called <a href="https://pypi.org/project/pipdeptree/">pipdeptree</a>. This command-line tool allows developers to see the root of their dependency conflict.</p><p>The tool shows the entire dependency tree (unlike pip freeze which shows a flat list of libraries), making it easy to understand relationships between libraries.</p><p>Here is an example of an output, got by executing pipdeptree -r -w:</p><pre>Warning!!! Possibly conflicting dependencies found:<br>* moto==1.3.14<br>  - idna [required: &gt;=2.5,&lt;2.9, installed: 2.10]<br>* jsonschema==3.2.0<br>  - six [required: &gt;=1.11.0, installed: 1.10.0]<br>* cfn-lint==0.43.0<br>  - six [required: ~=1.11, installed: 1.10.0]<br>* aws-sam-translator==1.32.0<br>  - six [required: ~=1.15, installed: 1.10.0]</pre><pre>----------------------------------------------------------------<br>...<br>idna==2.10<br>  - moto==1.3.14 [requires: idna&gt;=2.5,&lt;2.9]<br>  - requests==2.23.0 [requires: idna&gt;=2.5,&lt;3]<br>...<br>six==1.10.0<br>  - aws-sam-translator==1.32.0 [requires: six~=1.15]<br>    - cfn-lint==0.43.0 [requires: aws-sam-translator&gt;=1.25.0]<br>      - moto==1.3.14 [requires: cfn-lint&gt;=0.4.0]<br>  - cfn-lint==0.43.0 [requires: six~=1.11]<br>    - moto==1.3.14 [requires: cfn-lint&gt;=0.4.0]<br>  - jsonschema==3.2.0 [requires: six&gt;=1.11.0]<br>    - aws-sam-translator==1.32.0 [requires: jsonschema~=3.2]<br>      - cfn-lint==0.43.0 [requires: aws-sam-translator&gt;=1.25.0]<br>        - moto==1.3.14 [requires: cfn-lint&gt;=0.4.0]<br>    - cfn-lint==0.43.0 [requires: jsonschema~=3.0]<br>      - moto==1.3.14 [requires: cfn-lint&gt;=0.4.0]<br>...</pre><p>Based on this output we can see that the idna package has an installed version of 2.10, but we need an older version for moto.</p><p>Another problem is that six has several libraries that depend on it with slightly conflicting requirements.</p><p>The <strong>simple solution</strong> in our case was to <strong>pin down only the conflicting libraries</strong> and any library that depends on them, namely specify a single version of idna and six, and pin down all the libraries which depend on them.</p><p>This simple solution does not address two root cause issues:</p><ol><li>pip algorithm might change in the future, causing unexpected results</li><li>Any unpinned (i.e. loose) library might change its version in the future</li></ol><p>Addressing the first issue is straightforward: <strong>pin down pip as well</strong>. Ensure that the same version is used by your developers and also in the CI.</p><p>The second issue is very important: pip often gets the newest version for libraries that are not pinned down. <strong>This makes your build non-repeatable:</strong></p><ol><li>Builds are prone to breakdown when a newer library version of one library contradicts a dependency made by a second library you have.</li><li>Builds might succeed, but produce artifacts that have bugs in them. One example is a software release that you worked on for a few months, using one version of a library. Then, right before you release it to production, that dependent library has a new version with a bug in it. pip downloads the newest version. This dependent library was never thoroughly tested and the development team was not even aware of its upgrade. This puts your production environment at risk.</li></ol><p>To solve the second issue, we chose to <strong>pin down all the libraries</strong> in the various requirements.txt files we have.</p><p>Once more, <a href="https://pypi.org/project/pipdeptree/">pipdeptree</a> came to our rescue: it has a nice freeze flag, making it easy to produce hardened requirements files.</p><p>How can we improve the process further and make our software more stable?</p><p>Fixate any other loose components you may have in your development or build process: the OS image you use, any OS components or low-level dependencies, the building tools you use like the docker version, any python related tools like tox, and the exact python version you use.</p><p>Once in a while, there is a need to upgrade the dependencies: it could be that you want to use a new feature, have a bug fixed, or address a security issue. When you upgrade one of your dependencies, consider if you want to upgrade any other components or tools to stay up to date. Be sure to allow yourself enough time for thorough testing of your newly created artifacts.</p><p>Pin down the requirements.txt files manually, or using pipdeptree --freeze can be troublesome. You might want to consider some newer alternatives like or <a href="https://github.com/jazzband/pip-tools">pip-tools</a> or <a href="https://python-poetry.org/">poetry</a>. These tools replace pip and offer some very convenient ways of having a consistent dev environment across the development team and the CI.</p><p>Special thanks to <a href="https://www.linkedin.com/in/omerlivnesimha/">Omer Livne</a>, who brought <a href="https://pypi.org/project/pipdeptree/">pipdeptree</a> to my attention and offered some very good advice and best practices.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=de1e75d4d196" width="1" height="1" alt=""><hr><p><a href="https://python.plainenglish.io/solving-pip-breakage-de1e75d4d196">Solving pip Breakage</a> was originally published in <a href="https://python.plainenglish.io/">Python in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Go Beyond Mock: on Mocks, Stubs and Fakes]]></title>
            <link>https://medium.datadriveninvestor.com/go-beyond-mock-on-mocks-stubs-and-fakes-e8b1888932f9?source=rss-69da385f5784------2</link>
            <guid isPermaLink="false">https://medium.com/p/e8b1888932f9</guid>
            <category><![CDATA[mocking]]></category>
            <category><![CDATA[mock]]></category>
            <category><![CDATA[best-practices]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[testing]]></category>
            <dc:creator><![CDATA[Peter Kogan]]></dc:creator>
            <pubDate>Sun, 04 Oct 2020 17:27:06 GMT</pubDate>
            <atom:updated>2020-10-08T18:11:49.301Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uv6CD4XQ3yQMF1IkT18Rlw.jpeg" /><figcaption>Go Beyond ̶R̶o̶c̶k̶ Mock :)</figcaption></figure><p>As developers, we use the term “mocks” and “mocking” when referring to several different testing practices. In this post, I will standardize the terminology of Mocks, Stubs and Fakes: their capabilities, the differences between them, and when to use each one.</p><p>Knowing about different Mocking alternatives can help you write more robust and maintainable code, enjoy faster execution time, and raise the overall code quality.</p><p>Let’s begin with the definitions:</p><h3><strong>Mock</strong></h3><p>A mock replaces the original “mocked” object. The basic mock has <strong>no implementation</strong> of its own. Mocks record all calls made to them, so they can be verified later.</p><p>Use mocks when you don’t want the original object/functionality to be called.</p><h3><strong>Stub</strong></h3><p>A stub is a more advanced mock: it has a <strong>predefined implementation that returns static data</strong>.</p><p>Use stubs when you want to test a specific use case that relies on fixed data.</p><h3><strong>Fake</strong></h3><p>A fake is an even more advanced mock: it has an <strong>actual, simplified, working implementation.</strong></p><p>Use fakes when you want to have simple, lightweight logic instead of a real implementation.</p><p>That’s enough theoretical background, now for some Python code!</p><p>We want to test this code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4df8c82f9424d0bacc454a04a2e80947/href">https://medium.com/media/4df8c82f9424d0bacc454a04a2e80947/href</a></iframe><p>This short method uses <a href="https://boto3.amazonaws.com/v1/documentation/api/latest/index.html">boto3</a> (a wrapper for Amazon S3), to move an existing object to a unique, random place. There is no “move” method in boto, so we have to copy and delete the object.<br>The uniqueness is ensured by <a href="https://docs.python.org/3.7/library/uuid.html#uuid.uuid4">uuid4</a>, which returns identifiers in the form of a4a8ec45–3c6f-4a75-b7d7–112611f302 which is then manipulated to be more directory like structure: every hyphen (‘-’) is replaced with a slash (‘/’). In the above example it’s: a4a8ec45/3c6f/4a75/b7d7/112611f302.</p><p>The first tests we‘ll write will demonstrate Mocks and Stubs:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/083a93ecb93655cf8720844af920c23a/href">https://medium.com/media/083a93ecb93655cf8720844af920c23a/href</a></iframe><p>test_move_to_unique_location_ensure_uniqueness is an excellent introduction to <strong>Mocks</strong>: all we want to test in this method is to get unique destinations. We care about the algorithm logic and don’t care about s3 operations. The obvious solution is to <strong>mock s3 without any implementation.</strong> @mock.patch(&#39;s3._s3&#39;) does precisely that — it replaces the boto3 s3 handle with a mock. The body of the test function is pretty straight forward — call the tested method a thousand times and collect the output to verify they are all unique.</p><p><a href="https://www.datadriveninvestor.com/2020/07/07/introduction-to-time-series-forecasting-of-stock-prices-with-python/">Introduction to Time Series Forecasting of Stock Prices with Python | DataDrivenInvestor</a></p><p>Next we have test_move_to_unique_location_destination_format which is an example of a <strong>stub</strong>: notice the fixed return_value=&#39;a4a8ec45–3c6f-4a75-b7d7–112611f302&#39;. It allows us to verify that our formatting produces the right destination. We have to stub uuid4 to get the value we want, otherwise every test execution would produce a different destination.</p><p>Mocks also <strong>record the calls made to them, those calls can be verified</strong>. Let’s see this in action:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b909f7184a3a08381abeab619da23e96/href">https://medium.com/media/b909f7184a3a08381abeab619da23e96/href</a></iframe><p>The calls to the s3 mock are verified using assert_called_once_with. This example verifies the exact parameters used, but we can be less strict if we want to. We’ll get back to this example later.</p><p>So far, we have <strong>tested the algorithm logic</strong> and seen a few examples of mocks and stubs. We <strong>haven’t verified the s3 operations</strong>, more specifically, we haven’t answered the following questions:</p><ol><li>How do we know if we used the right s3 methods with the right parameters?</li><li>How do we know that the actual s3 object is moved correctly?</li><li>How do we know that the tested code doesn’t lose touch with reality?</li></ol><p>To make all those theoretical questions into something concrete, imagine that you add the following line to move_to_unique_location: _s3.typo(). Your tests would all pass, but the code would be completely broken in real life!</p><p>To address those concerns (and get a lot of additional benefits), we can use <strong>fakes. </strong>In the examples below we use a simple implementation of s3, called <a href="https://pypi.org/project/moto/">moto</a>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e56deafa8bc34c371aaba716ced36054/href">https://medium.com/media/e56deafa8bc34c371aaba716ced36054/href</a></iframe><p>That is a bit more to take on than previous examples, so let’s break it down.</p><p>This file has three methods:empty_bucket is a <a href="https://pytest.org/en/latest/fixture.html#fixture">pytest fixture</a> which sets up a clean, fake, s3 bucket for the other two test methods.</p><p>If you look closely at test_move_to_unique_location_no_source_object, you’ll see that it’s different from the previous tests:</p><ol><li>It tests a negative use case</li><li>It tries to move the object</li></ol><p>This test shows how our previous tests were not in touch with reality: to move an object you first need to ensure that it’s present. If you don’t believe me, try to run the previous tests without @mock.patch(&#39;s3._s3&#39;) and see the exceptions you’ll get.</p><p>Now we’re ready for the last test, test_move_to_unique_location_verify_read:</p><ol><li>It creates an object</li><li>Moves it using the tested code</li><li>Verifies that the object was moved correctly by checking its contents</li><li>Verifies that the object is no longer in the original place we created it (we expect the function to move the object and not just copy it)</li></ol><p>Wow, an end-to-end test!</p><p>We’re all done, or are we…</p><p>After a while, you decide to change the implementation of move_to_unique_location:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/960a59a7a49b655bf4299bf286adb724/href">https://medium.com/media/960a59a7a49b655bf4299bf286adb724/href</a></iframe><p>This alternative uses boto3.resource instead of boto3.client to achieve the same result.</p><p>Q: What would happen to your tests?</p><p>A: All but one would pass. The tests that tested the right things, using the proper techniques, would pass. The best example is test_move_to_unique_location_verify_read which verifies that the object is present in the new location without checking how it got there.</p><p>test_move_to_unique_location_destination_format is another good example: it makes sure that the destination structure doesn’t change.</p><p>The only test that broke down is test_move_to_unique_location_verify_calls. The reason is that it checked for a specific implementation that was changed. You can delete this test, as its primary goal was to demonstrate mocking capabilities.</p><p><strong>To summarize: Mocks, Stubs and Fakes all have their purpose. Be sure to use them appropriately: take advantage of their strengths, avoid using them when they don’t provide real value and would only slow you down.</strong></p><p>Some final words:</p><p>Theoretical knowledge alone is not enough! The code snippets in this article are fully working so put this information to practical use by running, modifying, and exploring these codes.</p><p>You’d get the most benefit if you use several mock types in your actual codebase to see what’s working for specific situations and get a deeper intuition about the common cases.</p><p><a href="https://blog.pragmatists.com/test-doubles-fakes-mocks-and-stubs-1a7491dfa3da">For further reading, check out This article</a> which does a great job of explaining the same subjects using Java programming language.</p><h4>Gain Access to Expert View — <a href="https://datadriveninvestor.com/ddi-intel">Subscribe to DDI Intel</a></h4><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e8b1888932f9" width="1" height="1" alt=""><hr><p><a href="https://medium.datadriveninvestor.com/go-beyond-mock-on-mocks-stubs-and-fakes-e8b1888932f9">Go Beyond Mock: on Mocks, Stubs and Fakes</a> was originally published in <a href="https://medium.datadriveninvestor.com/">DataDrivenInvestor</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>