[SciPy-User] Is there existing code to log-with-bells-on for runtime algorithm diagnostics?

Chris Angelico rosuav at gmail.com
Wed Apr 22 02:50:05 CEST 2015

On Wed, Apr 22, 2015 at 10:11 AM, Cameron Simpson <cs at zip.com.au> wrote:
> I have a context manager named "Pfx" which I use liberally in my code like
> this:
>  from cs.logutils import Pfx, info
>  def load(filename):
>    with Pfx("loading %r", filename):
>      with open(filename) as fp:
>        lineno = 0
>        for line in fp:
>          lineno += 1
>          with Pfx("%d", lineno):
>            ... do stuff with line ...
>            info("line value is foo=%r", foo)
>            ...
> This causes the info() call to report like this:
>  loading 'myfile.dat': 21: line value is foo=12345

Very nice! I'm guessing it depends on the use of info(), though,
rather than applying to pre-existing logging calls? Even so, all that
means is that you use a specific call to do your logging, and
everything works perfectly. The nesting is excellent.


More information about the Python-list mailing list