Skip to main content

Callbacks and data analysis

  • Chapter
  • First Online:
Advanced Python Development
  • 2844 Accesses

Abstract

In Chapter 12, the final chapter, we use Python’s iterator and coroutine features to enhance the dashboards we’ve developed with features that aren’t passive data gatherers but actively introspect the data we’ve gathered, allowing us to build multistep analysis flows.

This is a preview of subscription content, log in via an institution to check access.

Access this chapter

Chapter
USD 29.95
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
eBook
USD 39.99
Price excludes VAT (USA)
  • Available as EPUB and PDF
  • Read on any device
  • Instant download
  • Own it forever
Softcover Book
USD 54.99
Price excludes VAT (USA)
  • Compact, lightweight edition
  • Dispatched in 3 to 5 business days
  • Free shipping worldwide - see info

Tax calculation will be finalised at checkout

Purchases are for personal use only

Institutional subscriptions

Notes

  1. 1.

    However, you may need to convert the data type using a second comprehension of the right type, as we did to convert an async iterator to a list with a list comprehension.

  2. 2.

    We did something similar with the get_data_by_deployment(...) iterator, which uses shared state to define a generator that impacts another. It’s by far the most complex example of an iterator in this book.

  3. 3.

    This name is taken from the Python Enhancement Proposal that added it, PEP342. Technically speaking, this software engineering pattern is a coroutine, which the title of PEP342 makes clear. This is an enhancement to Python from 2005, long before true coroutines using async def were introduced. I’ll be calling these enhanced generators or referring to sending data to a generator, to avoid confusion between these and asynchronous functions.

  4. 4.

    For example, a program that arranges images into collages might be implemented as a class that has methods for supplying images and getting the arranged result out, or it might be implemented as an enhanced generator where whenever an image is added, a new result is returned.

  5. 5.

    We’ve been using None as our sentinel value, but if None is a valid value that the coroutine might expect from the queue, then we’d need to pick another. A common choice is to create a module-level instance of object, like END_OF_QUEUE_SENTINEL = object(). This can then be compared using

    if value is END_OF_QUEUE_SENTINEL:

    break

  6. 6.

    Lambda functions are unnamed functions that contain only a return expression. They’re useful for writing trivial functions, especially trivial closures, but it can be tempting to overuse them. A concrete advantage of operator.gt is that tracebacks will display it as <built-in function gt> rather than <function <lambda> at 0x00DD0858>.

  7. 7.

    Publish/subscribe. This feature allows a connection to request to be given messages on a given “topic” and other connections to send messages.

  8. 8.

    Other BSD-inspired operating systems are available.

  9. 9.

    Jupyter also coopts the <CTRL+c> handler, to give information on the number of kernels running and prevent accidental termination, so this is not unprecedented.

Author information

Authors and Affiliations

Authors

Rights and permissions

Reprints and permissions

Copyright information

© 2020 Matthew Wilkes

About this chapter

Check for updates. Verify currency and authenticity via CrossMark

Cite this chapter

Wilkes, M. (2020). Callbacks and data analysis. In: Advanced Python Development. Apress, Berkeley, CA. https://doi.org/10.1007/978-1-4842-5793-7_12

Download citation

Publish with us

Policies and ethics