Skip to main content

Mixing breakpoints with print debugging.

Project description

breakword

A description of the problem

Let's say you have a bug somewhere in your code, but it doesn't raise an exception and you're not entirely sure where things went wrong, so you start peppering your code with print statements in order to get an idea of what's going on.

At some point you see something wrong. You would like to investigate closer.

However, it seems to happen on the 174th iteration of some loop, and you're not entirely sure how to make a breakpoint that triggers right where something went wrong, and not before. It's not clear what condition to write for the breakpoint. You don't know what to do except print more stuff.

Now, what if you had two commands:

  • breakword.log(), a function to print some easily identifiable word.
  • breakword.brk(word), a function to break after some word has been printed.

Then you'd just run the script once, see that something happens after the word potato, and then you'd set a breakpoint to trigger after that word, and run the script again (hopefully your script is deterministic -- if it isn't, only God can help you).

That's basically what this library does.

Least-effort API

The PYTHONBREAKPOINT environment variable tells Python what to call when it sees breakpoint(). You can set it to breakword.logbrk to get some magic.

The BREAKWORD environment variable tells breakword which word to break on. If not set, no breakpoints will be entered unless explicitly specified in the Python code.

Basically, do this:

# Install the library
pip install breakword
# Run the script without entering a breakpoint
env PYTHONBREAKPOINT=breakword.logbrk python yourscript.py
# Run the script and break on the word "fish"
env PYTHONBREAKPOINT=breakword.logbrk BREAKWORD=fish python yourscript.py

Here's a screenshot to show what it looks like:

demo

Marginally-more-effort API

  • breakword.log(*things, **config): Print a word and optionally other things after it.

  • breakword.brk(word=None, **config): Sets a breakpoint to trigger after log printed out the given word. If word is None or not given, the BREAKWORD environment variable is consulted. If the variable is not set, nothing will happen.

    • This is equivalent to breakword.after(word).breakpoint().
  • breakword.after(word=None, **config): Returns an object that evaluates to True right after log printed out the given word. As with brk, if word is None or not given, the BREAKWORD environment variable is consulted.

  • breakword.word(**config): Returns the next word as a string. You can print it yourself, in which case it's basically like log, or you can store it in an object.

  • breakword.logbrk(**config): Calls log and then brk.

  • breakword.wordbrk(**config): Calls word and then brk. The word is returned.

  • breakword.set_default_logger(logger): Set the logging function to use (defaults to print)

demo

Configuration

log, word, after and brk all take keyword arguments:

  • group: A string that represents a "group" for the words. Each group is independent: log(group="abc") and log(group="xyz") will not interfere with each other, so you can add more log statements in your code without changing the words printed out by the existing logs.

    • You can also do something like: if after("fish"): log(group="xyz"). That statement will log extra words, but only after "fish" in the main sequence.
    • Different groups will (probably) have different colors, to help telling them apart.
  • logger The logger to use. Defaults to the default logger, which is print.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

breakword-0.1.2.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

breakword-0.1.2-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file breakword-0.1.2.tar.gz.

File metadata

  • Download URL: breakword-0.1.2.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.7.0

File hashes

Hashes for breakword-0.1.2.tar.gz
Algorithm Hash digest
SHA256 f2eb0ca79583bff7d5f1cf75dfb05e6602f22aef03f1cc19eb3d6e5cce098697
MD5 4f8fa812e749f63b461e652231be9756
BLAKE2b-256 1dcf411cb1b53479a2418b2a8e3216430db02a5f03ccddc1306e09fa43cb4d45

See more details on using hashes here.

File details

Details for the file breakword-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: breakword-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 8.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.7.0

File hashes

Hashes for breakword-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d1912c5193302f7ee31208b91f36689a912853782077e6496c788aa36d09749c
MD5 c74670f6e7d56c122041492511b68015
BLAKE2b-256 ceb0fa85b2add53c562bf0fc90b9f46c337e3b4386c3b638b5d762f71eb53c52

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page