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

Episode #267: Python on the beach

Published Fri, Jan 21, 2022, recorded Wed, Jan 19, 2022.


Watch the live stream:


About the show

Sponsored by us:

Michael #1: Box: Python dictionaries with advanced dot notation access

  • Want to treat dictionaries like classes? Box.
    small_box = Box({'data': 2, 'count': 5})
    small_box.data == \
        small_box['data'] == \
        getattr(small_box, 'data') == \
        small_box.get('data')
  • There are over a half dozen ways to customize your Box and make it work for you: Check out the new Box github wiki for more details and examples!

  • Superset of dict

  • See Types of Boxes as well

Brian #2: Reading tracebacks in Python

  • Trey Hunner
  • “When Python encounters an error in your code, it will print out a traceback. Let's talk about how to use tracebacks to fix our code.”
  • Brian’s commentary
    • Tracebacks can feel like brick wall of error telling you “you suck”.
    • But they are really meant to help you, and do, once you know how to read them.
    • Probably should be one of the earliest things we teach people new to coding. Like maybe:
      1. hello world
      2. tracebacks
      3. testing
    • Anyway, back to Trey
  • Start at the bottom. Read the last line first
    • That will have the type of exception and an error message
  • The two lines above that are
    • The exact filename and line number where the exception occurs
    • a copy of the line
  • Those two lines are a stack frame.
  • Keep going up and it’s other stack frames for the callstack of how you got here.
  • Trey walks through this with an example and shows how to solve an error at a high level stack frame using the traceback.

Michael #3: Raspberry Pi: These two new devices just went live on the International Space Station

  • The International Space Station has connected new Raspberry 4 Model B units to run experiments from 500 student programmer teams in Europe.
  • From the education-focused European Astro Pi Challenge
  • These are new space-hardened Raspberry Pi units, dubbed Astro Pi
  • 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.
  • 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.

Brian #4: Make Simple Mocks With SimpleNamespace

  • Adam Johnson
    • Who’s crushing it recently, BTW, lots of recent blog posts
  • SimpleNamespace is in the types standard library package.
  • Works great as a test double, especially as a stub or fake object.
  • “It’s as simple as possible, with no faff around being callable, tracking usage, etc.”
  • Example:
>from types import SimpleNamespace
>obj = SimpleNamespace(x=12, y=17, verbose=True)
>obj
namespace(x=12, y=17, verbose=True)
>obj.x
12
>obj.verbose
True
  • unittest.mock.Mock also works, but has the annoying feature that, unless you pass in a spec, any attribute will be allowed.
  • The SimpleNamespace solution doesn’t allow any typos or other attributes.
  • Example:
>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'?

Michael #5: Extra, extra, exta

Brian #6: 3 Things You Might Not Know About Numbers in Python

  • David Amos
  • 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”
  • There’s more to numbers than many people realize
  • The 3 things
    • numbers have methods
      • integers have
        • to_bytes(length=1, byteorder="big")
        • int.from_bytes(b'\x06\xc1', byteorder="big") class method
        • bit_length() and a bunch of others
      • floats have
      • use variables or parentheses, though.
        • 5.bit_length() doesn’t work
        • n=5; n.bit_length() and (5).bit_length() works
    • numbers have hierarchy
      • Every number in Python is an instance of the Number class.
        • so isinstance(value, Number) should work for any number type
      • Then there’s 4 abstract types encompassing other types
        • Complex: has type complex
        • Real: has float
        • Rational: has Fraction
        • Integral: has int and bool
      • Where’s Decimal?
        • It’s not part of those abstract types, it directly inherits from Number
      • Also, floats are weird
    • Numbers are extensible
      • You can derive from numeric classes, both abstract and concrete, and create your own
      • However, to do this effectively, you gotta implement A LOT of dunder methods.

Joke:


Want to go deeper? Check our projects