Tuesday, August 11, 2020

Python Bytes: #194 Events and callbacks in the Python language!

<p>Sponsored by us! Support our work through:</p> <ul> <li>Our <a href="https://training.talkpython.fm/"><strong>courses at Talk Python Training</strong></a></li> <li><a href="https://testandcode.com/">Test &amp; Code</a> Podcast</li> </ul> <p><strong>Brian #1:</strong> <a href="https://opensource.com/article/20/7/mutmut-python"><strong>An introduction to mutation testing in Python</strong></a></p> <ul> <li>Moshe Zadka</li> <li>This article uses mutmut, but there are other mutation testing packages.</li> <li>The example shows 3 methods, and one test case that actually hits 100% code coverage.</li> <li>The mutmut is used and finds 16 surviving mutants.</li> <li>“Mutation testing algorithmically modifies source code and checks if any "mutants" survived each test. Any mutant that survives the unit test is a problem: it means that a modification to the code, likely introducing a bug, was not caught by the standard test suite.”</li> <li>“For each mutation test, <code>mutmut</code> modified portions of your source code that simulates potential bugs. An example of a modification is changing a <code>&gt;</code> comparison to <code>&gt;=</code> to see what happens. If there is no unit test for this boundary condition, this mutation will "survive": this is a potential bug that none of the tests will detect.”</li> <li>Cool example of how to check mission critical parts of your code and the tests around them above and beyond code coverage.</li> <li>BTW, mutmut is also used codechalleng.es in the challenges asking users to write the tests.</li> </ul> <p><strong>Michael #2:</strong> <a href="https://github.com/quora/asynq"><strong>asynq</strong></a></p> <ul> <li>From Quora, a little old but still interesting and active</li> <li>A library for asynchronous programming in Python with a focus on batching requests to external services.</li> <li>Also provides seamless interoperability with synchronous code, support for asynchronous context managers, and tools to make writing and testing asynchronous code easier.</li> <li>Developed at Quora and is a core component of Quora's architecture.</li> <li>The most important use case for <code>asynq</code> is batching.</li> <li><code>asynq</code>'s asynchronous functions are implemented as Python generator functions. Every time an asynchronous functions yields one or more Futures, it cedes control the asynq scheduler,</li> </ul> <p><strong>Brian #3:</strong> <a href="https://redislabs.com/blog/beyond-the-cache-with-python/"><strong>redis: Beyond the Cache</strong></a> </p> <ul> <li>Guy Royse</li> <li>Some cool examples with Python code of using redis for more than a cache. <ul> <li>as a queue, with rpush and blpop</li> <li>pub/sub, with publish and psubscribe</li> <li>data streaming, with xadd and xread</li> <li>as a search engine</li> <li>and of course, as a primary in-memory database</li> </ul></li> <li>examples use aioredis to access with async/await</li> </ul> <p><strong>Michael #4:</strong> <a href="https://github.com/ptmcg/littletable"><strong>LittleTable</strong></a></p> <ul> <li>Discovered as part of my multi-key dictionary quest (more on this later)</li> <li>By <a href="https://github.com/ptmcg">Paul McGuire</a></li> <li>A Python module to give ORM-like access to a collection of objects</li> <li>Provides a low-overhead, schema-less, in-memory database access to a collection of user objects.</li> <li>In addition to basic ORM-style insert/remove/query/delete access to the contents of a <code>Table</code>, <code>littletable</code> offers: <ul> <li>simple indexing for improved retrieval performance, and optional enforcing key uniqueness</li> <li>access to objects using indexed attributes</li> <li>simplified joins using '+' operator syntax between annotated Tables</li> <li>the result of any query or join is a new first-class littletable Table</li> </ul></li> </ul> <p><strong>Brian #5:</strong> <a href="https://pypi.org/project/pytest-timeout/"><strong>pytest-timeout</strong></a></p> <ul> <li>listener suggestion</li> <li>This is essential, I think.</li> <li>Make sure no test runs longer than a certain number of seconds.</li> <li>You can set a global timeout either via command line or via a config file.</li> <li>You can specify different times for specific tests via a mark decorator</li> <li>Great stopgap to make sure no test runs forever.</li> </ul> <p><strong>Michael #6:</strong> <a href="https://pypi.org/project/Events/"><strong>Events</strong></a></p> <ul> <li>Call me, maybe</li> <li>by Nicola Iarocci</li> <li>Adds event subscription and callback to the Python language</li> <li>Based on C# language’s events, provides a handy way to declare, subscribe to and fire events. </li> <li>Encapsulates the core to event subscription and event firing and feels like a “natural” part of the language.</li> <li>Example:</li> </ul> <pre><code> &gt;&gt;&gt; def something_changed(reason): ... print(f"something changed because {reason}") &gt;&gt;&gt; from events import Events &gt;&gt;&gt; events = Events() &gt;&gt;&gt; events.on_change += something_changed </code></pre> <ul> <li>Multiple callback functions can subscribe to the same event. When the event is fired, all attached event handlers are invoked in sequence.</li> </ul> <pre><code> &gt;&gt;&gt; events.on_change('it had to happen') 'something changed because it had to happen' </code></pre> <ul> <li>Gist for Michael’s example:</li> <li><a href="https://gist.github.com/mikeckennedy/7235543fd5964bebabe1e3546ce67d91">gist.github.com/mikeckennedy/7235543fd5964bebabe1e3546ce67d91</a></li> </ul> <p>Extras:</p> <p>Michael: </p> <ul> <li>Finished the memory management course, coming soon. Started one on Python design patterns</li> </ul> <p>Brian:</p> <ul> <li><a href="https://docs.pytest.org/en/stable/changelog.html">pytest 6.0.1 released</a> <ul> <li>I walk through some of the changes in <a href="https://testandcode.com/125">testandcode.com/125</a></li> </ul></li> </ul> <p>Joke:</p> <p><a href="https://xkcd.com/327/">https://xkcd.com/327/</a></p>

from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...