Brought to you by Michael and Brian - take a Talk Python course or get Brian's pytest book

Episode #202: Jupyter is back in black!

Published Fri, Oct 9, 2020, recorded Wed, Sep 30, 2020.



Sponsored by DataDog: pythonbytes.fm/datadog

Brian #1: New in Python 3.9

  • scheduled to be released Oct 5
  • Python 3.9.0rc2 released Sept 17
  • New features (highlights)
    • Dictionary merge (|) and update (|=) operators.
    • String str.removeprefix(prefix) and str.removesuffix(suffix). This have also been added to bytes, bytearray, and collections.UserString.
    • In type annotations you can now use built-in collection types such as list and dict as generic types instead of importing the corresponding capitalized types (e.g. List or Dict) from typing.
    • New PEG parser
    • Any valid expression can be used as a decorator. see PEP 614. Haven’t quite wrapped my head around the possibilities yet.
    • [zoneinfo](https://docs.python.org/3.9/library/zoneinfo.html#module-zoneinfo) module brings support for the IANA time zone database to the standard library.
  • Lots of other great stuff too, please check out the changelog and give 3.9 a spin

Michael #2: jupyter-black

  • via Mary Hoang
  • I recently tuned into the auto racing episode on Talk Python and liked Kane’s pypi suggestion of blackcellmagic.
  • There are a couple of other pypi packages that envelop the idea of black formatting Jupyter Notebooks and I recently started using a new pypi tool called jupyterblack!
  • This tool lets you black format Notebooks like you would Python files, only you call jblack instead of black.
  • Then the extension provides
    • a toolbar button
    • a keyboard shortcut for reformatting the current code-cell (default: Ctrl-B)
    • a keyboard shortcut for reformatting whole code-cells (default: Ctrl-Shift-B)
  • It will also point basic syntax errors.

Brian #3: Understanding and preventing DoS in web applications

  • listener submitted suggestion, which led me to a bit of a rabbit hole
  • by Jacob Kaplan-Moss
  • Great discussion of what a DoS attack is, and how to check for and prevent problems, including a focus on Python and django.
  • One example is ReDoS, regular expression DoS
  • “ReDoS bugs occur when certain types of strings can cause improperly crafted regular expressions to perform extremely poorly.”
  • Links to Finding Python ReDoS bugs at scale using Dlint and r2c, which talks about using dlint.
  • dlint

Michael #4: bbox-visualizer

  • via Shoumik Sharar Chowdhury (SHOH-mik CHOW-duh-ree)
  • I work with computer vision, and one of the pain points of working with something like object detection or object recognition is positioning the labels once you get the bounding boxes.
  • So for example, in the first image in the README, you get the positions of the boxes around the objects using any object-detection method. That part isn't hard. Positioning the labels like "person", "bicycle", "car" right on top of the boxes, however, is quite annoying. You have to do some clumsy math to make it work like that.
  • This library helps make that very easy. You just use the bounding box locations and their corresponding labels and the library takes care of everything. Moreover, there are some other cool visualizations that you can use, other than the standard label on top of the boxes.
  • Uses Open CV in Python to work with the image files and in memory drawing
  • Define the bounds, set the label text and you’re off.
    bbv.draw_rectangle(img, bbox)
    bbv.add_T_label(img, label, bbox)

Brian #5: How to NEVER use lambdas.

  • Another listener suggestion.
  • Starts off with a brief example showing how to rewrite a power function as a lambda.
  • Then jumps right into crazy code
  • Replacing import statements with __import__(``'``library``'``) expressions
  • Moving on to lambda-ifying class definitions
  • Ending with a complete Flask application as a lambda expression.
  • Truly horrible stuff

Michael #6: Uncommon Contributions: Making impact without touching the core of a library

  • via Alexander, by Vincent Warmerdam
  • Different ways that people can contribute to open source software besides the typical code contribution.
  • Often, contributions include adding features to a library, fixing bugs, or providing examples to a documentation page. But consider:
  • Info
    • rasa --version
    • Before, this command would list the current version of Rasa. In the new version, it lists:
    • The version of python.
    • The path to your virtual environment.
    • The versions of related packages.
  • Cron on Dependencies
    • A user for scikit-lego, a package that I maintain, discovered that the reason the code wasn’t working was because scikit-learn introduced a minor, but breaking, change. To fix this the user added a cronjob with Github actions to the project.
  • Spellcheck
    • Run a spellchecker, not just against our docs, but also on our source code! It turns out we had some issues in our docstrings as well.
  • Error Messages
    • In whatlies, we’ve recently allowed for optional dependencies. If you try to use a part of the library that requires a dependency that is not part of the base package, then you’ll get this error message.
    In order to use ConveRTLanguage you'll need to install via;
    > pip install whatlies[tfhub]
    See installation guide here: https://rasahq.github.io/whatlies/#installation
  • I added something like this to fluentcheck: github.com/csparpa/fluentcheck/pull/22
  • Failing Unit Tests
    • There’s a lovely plugin for mkdocs called mkdocs-jupyter. It allows you to easily add jupyter notebooks to your documentation pages. When I was playing with it, I noticed that it wasn’t compatible with a new version of mkdocs. Instead of just submitting a bug to Github, I went the extra mile. I created a PR that contained a failing unit-test for this issue.
  • Renaming files
    • Is there a file.py and a class File in file within a package? Careful there.

Extras:

Joke:

  • Suggested by Tim Skov Jacobsen
    • Kelsey Hightower’s project nocode
    • No Code: No code is the best way to write secure and reliable applications. Write nothing; deploy nowhere.”
    • No Code Style Guide: All no code programs are the same, regardless of use case, any code you write is a liability.”
    • 43.6k stars
    • 3.2k issues
    • 426 PRs

Click to show comments