Friday, January 21, 2022

Python Bytes: #267 Python on the beach

<p><strong>Watch the live stream:</strong></p> <a href='https://www.youtube.com/watch?v=4zYVzAn0I-Q' style='font-weight: bold;'>Watch on YouTube</a><br> <br> <p><strong>About the show</strong></p> <p>Sponsored by <strong>us:</strong></p> <ul> <li>Check out the <a href="https://training.talkpython.fm/courses/all"><strong>courses over at Talk Python</strong></a></li> <li>And <a href="https://pythontest.com/pytest-book/"><strong>Brian’s book too</strong></a>!</li> </ul> <p><strong>Michael #1:</strong> <a href="https://github.com/cdgriffith/Box"><strong>Box: Python dictionaries with advanced dot notation access</strong></a></p> <ul> <li>Want to treat dictionaries like classes? Box.</li> </ul> <pre><code> small_box = Box({'data': 2, 'count': 5}) small_box.data == \ small_box['data'] == \ getattr(small_box, 'data') == \ small_box.get('data') </code></pre> <ul> <li><p>There are over a half dozen ways to customize your Box and make it work for you: Check out the new <a href="https://github.com/cdgriffith/Box/wiki">Box github wiki</a> for more details and examples!</p></li> <li><p>Superset of <code>dict</code></p></li> <li><p>See <a href="https://github.com/cdgriffith/Box/wiki/Types-of-Boxes">Types of Boxes</a> as well</p></li> </ul> <p><strong>Brian #2:</strong> <a href="https://www.pythonmorsels.com/topics/reading-tracebacks-in-python/"><strong>Reading tracebacks in Python</strong></a></p> <ul> <li>Trey Hunner</li> <li>“When Python encounters an error in your code, it will print out a traceback. Let's talk about <strong>how to use tracebacks to fix our code</strong>.”</li> <li>Brian’s commentary <ul> <li>Tracebacks can feel like brick wall of error telling you “you suck”. </li> <li>But they are really meant to help you, and do, once you know how to read them.</li> <li>Probably should be one of the earliest things we teach people new to coding. Like maybe: <ol> <li>hello world</li> <li>tracebacks</li> <li>testing</li> </ol></li> <li>Anyway, back to Trey</li> </ul></li> <li>Start at the bottom. Read the last line first <ul> <li>That will have the type of exception and an error message</li> </ul></li> <li>The two lines above that are <ul> <li>The exact filename and line number where the exception occurs</li> <li>a copy of the line</li> </ul></li> <li>Those two lines are a stack frame.</li> <li>Keep going up and it’s other stack frames for the callstack of how you got here.</li> <li>Trey walks through this with an example and shows how to solve an error at a high level stack frame using the traceback.</li> </ul> <p><strong>Michael #3:</strong> <a href="https://www.zdnet.com/article/raspberry-pi-these-two-new-devices-just-went-live-on-the-international-space-station/#ftag=RSSbaffb68"><strong>Raspberry Pi: These two new devices just went live on the International Space Station</strong></a></p> <ul> <li>The International Space Station has connected new Raspberry 4 Model B units to run experiments from 500 student programmer teams in Europe.</li> <li>From the education-focused European Astro Pi Challenge</li> <li>These are new space-hardened Raspberry Pi units, dubbed Astro Pi</li> <li>The AstroPi units are part of a project run by the European Space Agency (ESA) for the Earth-focused Mission Zero and Mission Space Lab. </li> <li>The former allows young Python programmers to take humidity readings on board ISS while the latter lets students run various scientific experiments on the space station using its sensors. </li> </ul> <p><strong>Brian #4:</strong> <a href="https://adamj.eu/tech/2022/01/14/make-simple-mocks-with-simplenamespace/"><strong>Make Simple Mocks With</strong></a> <a href="https://adamj.eu/tech/2022/01/14/make-simple-mocks-with-simplenamespace/"><strong>SimpleNamespace</strong></a></p> <ul> <li>Adam Johnson <ul> <li>Who’s crushing it recently, BTW, lots of recent blog posts</li> </ul></li> <li><code>SimpleNamespace</code> is in the <a href="https://docs.python.org/3/library/types.html#types.SimpleNamespace">types</a> standard library package.</li> <li>Works great as a test double, especially as a stub or fake object.</li> <li>“It’s as simple as possible, with no faff around being callable, tracking usage, etc.”</li> <li>Example:</li> </ul> <pre><code>&gt;from types import SimpleNamespace &gt;obj = SimpleNamespace(x=12, y=17, verbose=True) &gt;obj namespace(x=12, y=17, verbose=True) &gt;obj.x 12 &gt;obj.verbose True </code></pre> <ul> <li><code>unittest.mock.Mock</code> also works, but has the annoying feature that, unless you pass in a spec, any attribute will be allowed.</li> <li>The <code>SimpleNamespace</code> solution doesn’t allow any typos or other attributes.</li> <li>Example:</li> </ul> <pre><code>&gt;obj.vrebose Traceback (most recent call last): File "[HTML_REMOVED]", line 1, in [HTML_REMOVED] AttributeError: 'types.SimpleNamespace' object has no attribute 'vrebose'. Did you mean: 'verbose'? </code></pre> <p><strong>Michael #5: Extra, extra, exta</strong></p> <ul> <li><a href="https://twitter.com/wha_hoppen/status/1482924073571078146"><strong>Marak Squires, supply chain issues (NPM), and terrorism?</strong></a> <strong>[</strong><a href="https://www.cpomagazine.com/cyber-security/open-source-developer-intentionally-corrupts-npm-libraries-suspected-hack-turns-out-to-be-mischief/"><strong>npm issues</strong></a><strong>]</strong></li> <li><a href="https://twitter.com/brandymedia/status/1483089289294589963"><strong>css outlines</strong></a>!</li> <li><a href="https://docs.python.org/release/3.10.2/whatsnew/changelog.html#python-3-10-2-final"><strong>python 3.10.2</strong></a></li> <li><a href="https://twitter.com/mkennedy/status/1482037909456785409"><strong>Python Shorts YouTube series</strong></a> <ul> <li>#1 <a href="https://youtu.be/aHv7-6WIxNM"><strong>Parsing data with Pydantic</strong></a></li> <li>#2 <a href="https://youtu.be/0IHAm8cwlbQ"><strong>Counting the number of times items appear with collections.Counter</strong></a></li> </ul></li> <li><a href="https://youtu.be/yaPer4f1QgM"><strong>Stream Deck + PyCharm</strong></a> <strong>video,</strong> <a href="https://github.com/mikeckennedy/streamdeck-developer-profiles/"><strong>github repo</strong></a></li> </ul> <p><strong>Brian #6:</strong> <a href="https://davidamos.dev/three-things-you-might-not-know-about-numbers-in-python/"><strong>3 Things You Might Not Know About Numbers in Python</strong></a></p> <ul> <li>David Amos</li> <li>Most understated phrase I’ve read in a long time: “… there's a good chance that you've used a number in one of your programs”</li> <li>There’s more to numbers than many people realize</li> <li>The 3 things <ul> <li>numbers have methods <ul> <li>integers have <ul> <li><code>to_bytes(length=1, byteorder="big")</code></li> <li><code>int.from_bytes(b'\x06\xc1', byteorder="big")</code> class method</li> <li><code>bit_length()</code> and <a href="https://docs.python.org/3/library/stdtypes.html?highlight=to_bytes#additional-methods-on-integer-types">a bunch of others</a></li> </ul></li> <li>floats have <ul> <li><code>is_integer()</code>, <code>as_integer_ratio()</code></li> <li>and <a href="https://docs.python.org/3/library/stdtypes.html?highlight=to_bytes#additional-methods-on-float">a bunch more</a></li> </ul></li> <li>use variables or parentheses, though. <ul> <li><code>5.bit_length()</code> doesn’t work</li> <li><code>n=5; n.bit_length()</code> and <code>(5).bit_length()</code> works</li> </ul></li> </ul></li> <li>numbers have hierarchy <ul> <li>Every number in Python is an instance of the <code>Number</code> class. <ul> <li>so <code>isinstance(value, Number)</code> should work for any number type</li> </ul></li> <li>Then there’s 4 abstract types encompassing other types <ul> <li><code>Complex</code>: has type <code>complex</code></li> <li><code>Real</code>: has <code>float</code></li> <li><code>Rational</code>: has <code>Fraction</code></li> <li><code>Integral</code>: has <code>int</code> and <code>bool</code></li> </ul></li> <li>Where’s <code>Decimal</code>? <ul> <li>It’s not part of those abstract types, it directly inherits from <code>Number</code></li> </ul></li> <li>Also, floats are weird</li> </ul></li> <li>Numbers are extensible <ul> <li>You can derive from numeric classes, both abstract and concrete, and create your own</li> <li>However, to do this effectively, you gotta implement A LOT of dunder methods.</li> </ul></li> </ul></li> </ul> <p><strong>Joke:</strong> </p> <p><img src="https://ift.tt/3AoEOuA" alt="" /></p> <p><img src="https://ift.tt/33yiJha" alt="" /></p> <p><img src="https://ift.tt/3IiC9Fo" alt="" /></p> <p><img src="https://ift.tt/3FKGeAB" alt="" /></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...