Thursday, November 18, 2021

Python Bytes: #259 That argument is a little late-bound

<p><strong>Watch the live stream:</strong></p> <a href='https://www.youtube.com/watch?v=IB4RBvz8sXU' style='font-weight: bold;'>Watch on YouTube</a><br> <br> <p><strong>About the show</strong></p> <p>Sponsored by <strong>Shortcut - Get started at</strong> <a href="http://shortcut.com/pythonbytes"><strong>shortcut.com/pythonbytes</strong></a></p> <p>Special guest: <strong>Renee Teate</strong></p> <p><strong>Michael #1:</strong> <a href="https://twitter.com/btskinn/status/1456293623599935490"><strong>pypi-changes</strong></a></p> <ul> <li><a href="https://twitter.com/btskinn/status/1456293623599935490"><strong>via Brian Skinn</strong></a>, created by <a href="https://twitter.com/gjbernat/status/1456207118470684674"><strong>Bernát Gábor</strong></a></li> <li>Visually show you which dependencies in an environment are out of date.</li> <li>See the age of everything you depend upon. </li> <li>Also, shoutout again to <a href="https://github.com/naiquevin/pipdeptree"><strong>pipdeptree</strong></a></li> </ul> <p><strong>Brian #2:</strong> <a href="https://lwn.net/SubscriberLink/875441/c29a2006cf489b7f/"><strong>Late-bound argument defaults for Python</strong></a></p> <ul> <li>Default values for arguments to functions are evaluated at function definition time.</li> <li>If a value is a short expression that uses a variable, that variable is in the scope of the function definition.</li> <li>The expression cannot use other arguments.</li> <li>Example of what you cannot do:</li> </ul> <pre><code> def foo(a, b = None, c = len(a)): ... </code></pre> <ul> <li>There’s a proposal by Chris Angelico to add a <code>=:</code> operator for late default evaluation. <ul> <li>syntax still up in the air. <code>=&gt;</code> and <code>?=</code> also discussed</li> </ul></li> <li>However, it’s non-trivial to add syntax to an established language, and this article notes: <ul> <li>At first blush, Angelico's idea to fix this "wart" in Python seems fairly straightforward, but the discussion has shown that there are multiple facets to consider. It is not quite as simple as "let's add a way to evaluate default arguments when the function is called"—likely how it was seen at the outset. That is often the case when looking at new features for an established language like Python; there is a huge body of code that needs to stay working, but there are also, sometimes conflicting, aspirations for features that could be added. It is a tricky balancing act.</li> </ul></li> </ul> <p><strong>Renee #3:</strong> <a href="https://pandas.pydata.org/docs/reference/api/pandas.read_sql.html"><strong>pandas.read_sql</strong></a></p> <ul> <li>Since I wrote my book SQL for Data Scientists, I’ve gotten several questions about how I use SQL in my python scripts. It’s really simple: </li> <li>You can save your SQL as a text file and then import the dataset into a pandas dataframe to do the rest of my data cleaning, feature engineering, etc. </li> <li>Pandas has a built-in way to use SQL as a data source. </li> <li>You set up a connection to your database using another package like SQL Alchemy, then send the SQL string and the connection to the pandas.read_sql function. </li> <li>It returns a dataframe with the results of your query. </li> </ul> <p><strong>Michael #4:</strong> <a href="https://talkpython.fm/episodes/show/340/time-to-jit-your-python-with-pyjion"><strong>pyjion</strong></a></p> <ul> <li>by Anthony Shaw</li> <li>Pyjion is a JIT for Python based upon CoreCLR</li> <li>Check out <a href="https://live.trypyjion.com/"><strong>live.trypyjion.com</strong></a> <strong><em>*to see it in action</strong>.</em>*</li> <li>Requires Python 3.10, .NET Core 6</li> <li>Enable with just a couple of lines:</li> </ul> <pre><code> &gt;&gt;&gt; import pyjion &gt;&gt;&gt; pyjion.enable() </code></pre> <p><strong>Brian #5:</strong> <a href="https://adamj.eu/tech/2021/10/08/tips-for-debugging-with-print/"><strong>Tips for debugging with print()</strong></a></p> <ul> <li>Adam Johnson</li> <li>7 tips altogether, but I’ll highlight a few I loved reading about</li> <li>Debug variables with f-strings and <code>=</code> <ul> <li><code>print(f``"``{myvar=}``"</code>) </li> <li>Saves typing over <code>print(f``"``myvar={myvar}</code>") with the same result</li> </ul></li> <li>Make output “pop” with emoji (Brilliant!) <ul> <li><code>print("👉 spam()")</code></li> <li>Here’s some cool ones to use <ul> <li>👉 ❌ ✅ </li> </ul></li> </ul></li> <li>Use <code>rich.print</code> or <code>pprint</code> for pretty printing <ul> <li>Also, cool rename example to have both print and rich.print available <ul> <li><code>from rich import print as rprint</code></li> </ul></li> <li>Both <code>rich.print</code> and <code>pprint.pprint</code> are essential for printing structures nicely</li> </ul></li> <li>Brian’s addition <ul> <li>In pytest, failed tests report the stdout contents by default from the test</li> <li>I love the idea of using <code>rich.print</code> and emoji for print statements in tests themselves.</li> <li>Even though you can use <code>--showlocals</code> to print local variables for failed tests, having control of some output to help you debug something if it ever fails is a good thing.</li> </ul></li> </ul> <p><strong>Renee #6:</strong> <a href="https://shap.readthedocs.io/en/latest/index.html"><strong>SHAP</strong></a> (and <a href="https://ift.tt/3qKqdXq plot</a>)</p> <ul> <li>Brought to my attention by my team member Brian Richards at HelioCampus, and now they’re becoming a standard part of some of our model evaluation/explanation outputs</li> <li>SHapley Additive exPlanations <ul> <li>Shapley values from game theory</li> </ul></li> <li>Additive: “SHAP values of all the input features will always sum up to the difference between baseline (expected) model output and the current model output for the prediction being explained” <ul> <li>Negative/positive - pushing the prediction towards one class or the other <ul> <li>There’s a SHAP value for every feature for every prediction</li> </ul></li> <li>Waterfall plots</li> <li>Scatterplots of input value vs SHAP value</li> <li>SHAP value can be outputted and pulled into other tools (I use them in Tableau)</li> </ul></li> <li>Correlation not causation</li> <li><a href="https://ift.tt/3CnBAXj plots</a> for feature importance with input value vs SHAP value</li> </ul> <p><strong>Extras</strong></p> <p><strong>Brian</strong>:</p> <ul> <li>Matthew Feickert recommended <code>pip index</code> and specifically <code>pip index versions</code> as a cool thing to try. <ul> <li>Example. <code>pip index versions pyhf</code> reports <ul> <li>all versions of <code>pyhf</code> available on pypi</li> <li>the latest version</li> <li>your installed version</li> </ul></li> <li>It’s currently “experimental” so conceptually the pypa could yank it. But we like it. I hope it stays.</li> </ul></li> </ul> <p><strong>Michael</strong>:</p> <ul> <li>My <a href="https://twitter.com/pycharm/status/1460975261948723208?s=12"><strong>PyCharm webcast</strong></a> </li> </ul> <p><strong>Renee</strong>: </p> <ul> <li>My book and companion website with interactive query editor: <a href="https://sqlfordatascientists.com/">SQL for Data Scientists</a></li> </ul> <p><strong>Joke:</strong> <a href="https://twitter.com/fvoron/status/1455979278936444930"><strong>git messages</strong></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...