Sunday, August 2, 2020

Python Bytes: #192 Calculations by hand, but in the compter, with Handcalcs

<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://simonwillison.net/2020/Jul/10/self-updating-profile-readme/"><strong>Building a self-updating profile README for GitHub</strong></a></p> <ul> <li>Simon Willison, co-createor of Django</li> <li>“GitHub quietly released a new feature at some point in the past few days: profile READMEs. Create a repository with the same name as your GitHub account (in my case that’s <a href="https://github.com/simonw/simonw">github.com/simonw/simonw</a>), add a <code>README.md</code> to it and GitHub will render the contents at the top of your personal profile page—for me that’s <a href="https://github.com/simonw">github.com/simonw</a>”</li> <li>Simon takes it one further, and uses GitHub actions to keep the README up to date.</li> <li>Uses Python to: <ul> <li>Grab recent releases from certain GH repos using GH GraphQL API</li> <li>Links to blog entries using feedparser</li> <li>Retrieve latest links using SQL queries</li> </ul></li> </ul> <p><strong>Michael #2:</strong> <a href="https://github.com/connorferster/handcalcs"><strong>Handcalcs</strong></a></p> <ul> <li>Created by Connor Ferster</li> <li>In design engineering, you need to do lots of calculations and have those calculation sheets be kept as legal records as part of the project's design history. </li> <li>If they are not being done by hand, then often Excel is used but formatting calculations in Excel is time consuming and a maintenance nightmare. </li> <li>However, doing calculations in Jupyter is not any better even if you fill it up with print() statements and print to PDF: it just looks like a bunch of code output.</li> <li>Even proprietary software like MathCAD cannot render math as good as a hand calculation because it does not show the numerical substitution step. No software does </li> <li><strong>Why handcalcs exists:</strong> </li> <li>Type the formula once into a Jupyter cell</li> <li>Have the calculation be rendered out as beautifully as though you had written it by hand. </li> <li><p>Write your notebooks once, and use them for calculation again and again; the formula you write is the same as the representation of the formula. <img src="https://ift.tt/3i37774" alt="" /></p></li> <li><p><code>**Symbolic**</code> The primary purpose of <code>handcalcs</code> is to render the full calculation with the numeric substitution. This allows for easy traceability and verification of the calculation.</p></li> <li>However, there may be instances when it is preferred to simply display calculations symbolically. For example, you can use the <code># Symbolic</code> tag to use <code>handcalcs</code> as a fast way to render Latex equations symbolically.</li> <li>Includes <strong>longhand vs. shorthand</strong></li> <li>Use units (mm^3) for example.</li> </ul> <p><strong>Brian #3:</strong> <a href="https://lwn.net/Articles/823292/"><strong>The (non-)return of the Python print statement</strong></a></p> <ul> <li>Article by Jake Edge</li> <li>Idea by Guido van Rossum to bring back the print statement.</li> <li>Short answer: not gonna happen</li> </ul> <p><strong>Michael #4:</strong> <a href="https://amitness.com/2020/06/fastapi-vs-flask/"><strong>FastAPI for Flask Users</strong></a></p> <ul> <li>Flask has become the de-facto choice for API development</li> <li>FastAPI that has been getting a lot of community traction lately</li> <li>Benefits <ul> <li>Automatic data validation</li> <li>documentation generation</li> <li>baked-in best-practices such as pydantic schemas and python typing</li> </ul></li> <li>Running “Hello World” - super similar, but FastAPI is uvicorn out of the box </li> <li><code>@app.get('/')</code> vs <code>@app.route('/')</code></li> <li>FastAPI defers serving to a production-ready server called <code>uvicorn</code>.</li> <li>URL Variables: <ul> <li>Flask</li> </ul></li> </ul> <pre><code> @app.route('/users/[HTML_REMOVED]') def get_user_details(user_id): </code></pre> <pre><code>- FastAPI </code></pre> <pre><code> @app.get('/users/{user_id}') def get_user_details(user_id: int): </code></pre> <ul> <li>Query Strings <ul> <li>Flask</li> </ul></li> </ul> <pre><code> @app.route('/search') def search(): query = request.args.get('q') </code></pre> <ul> <li>FastAPI</li> </ul> <pre><code> @app.get('/search') def search(q: str): </code></pre> <ul> <li>Taking inbound JSON request in FastAPI: <code>def lower_case(json_data: Dict)</code></li> <li>Nice but if you define a <code>Sentence</code> model via pydantic:</li> </ul> <pre><code> @app.post('/lowercase') def lower_case(sentence: Sentence): </code></pre> <ul> <li>Blueprints == Routers</li> <li>Automatic validation via pydantic</li> </ul> <p><strong>Brian #5:</strong> <a href="https://gist.github.com/chrisalbon/b9bd4a6309c9f5f5eeab41377f27a670"><strong>Tweet deleting with tweepy</strong></a></p> <ul> <li>Chris Albon</li> <li>A useful and simple example of using tweepy to interact with Twitter</li> <li>Chris set up and shared a Python script that deletes tweets that are: <ul> <li>older than 62 days</li> <li>have been liked by less than a 100 people</li> <li>haven’t been liked by yourself</li> </ul></li> </ul> <p><strong>Michael #6:</strong> <a href="https://pythonspeed.com/articles/function-calls-prevent-garbage-collection/"><strong>Clinging to memory: how Python function calls can increase your memory usage</strong></a></p> <ul> <li>by <a href="#">Itamar Turner-Trauring</a></li> <li>I had Itamar on <a href="https://talkpython.fm/274">Talk Python episode 274</a> to discuss FIL which was recently covered.</li> <li>This article basically uses FIL to explore patterns for lowering memory usage within the context of a function.</li> <li>With simple code like this, we expected 2GB of memory usage, but we saw 3GB:</li> - </ul> <pre><code> def process_data(): data = load_1GB_of_data() return modify2(modify1(data)) </code></pre> <ul> <li>The problem is that first allocation: we don’t need it any more once <code>modify1()</code> has created the modified version. But because of the local variable <code>data</code> in <code>process_data()</code>, it is not freed from memory until <code>process_data()</code> returns.</li> <li><strong>Solution #1: No local variable at all</strong></li> </ul> <pre><code> return modify2(modify1(load_1GB_of_data())) </code></pre> <ul> <li><strong>Solution #2: Re-use the local variable</strong></li> </ul> <pre><code> data = load_1GB_of_data() data = modify1(data) data = modify2(data) return data </code></pre> <ul> <li><strong>Solution #3: Transfer object ownership</strong> <ul> <li>See article</li> </ul></li> </ul> <p>Extras:</p> <p>Michael: </p> <ul> <li><strong>Pickle Use Example via Adam.</strong> <ul> <li>I once had to work on an API that spoke to a 3rd party service that was a little unusual. That communication to the 3rd party service was over a raw socket connection, so we were responsible for crafting specifically formatted byte arrays to send to them, and we'd get specifically formatted byte arrays back which we'd then have to parse out to determine what pieces of data were in the message. The other wrinkle: that service wasn't available 24/7 but only during limited specific testing periods which had to be negotiated days in advance.</li> <li>We instrumented the code with a feature flag to enable pickling all received messages from that 3rd party service.</li> </ul></li> <li><a href="https://www.python.org/downloads/release/python-384/">Python 3.8.4 is out</a></li> <li><a href="https://github.com/mikeckennedy/">I'm an Arctic Code Vault Contributor</a> over at GitHub. You might be too.</li> </ul> <p>Joke:</p> <p><a href="http://geek-and-poke.com/geekandpoke/2019/10/20/qa-best-practices"></a> <img src="https://ift.tt/3gnsLTn" alt="https://ift.tt/2MxU8O7" /></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...