Giter Club home page Giter Club logo

python-wisdom's Introduction

Python Wisdom

This file contains lessons in Python that I felt needed to be documented because I see them, and I consistently comment on them during code reviews.

Logging

String Interpolation

Using "hello {0}".format(thing) is almost always the correct choice for string interpolation. In the logging package, however, it is preferrable to use it's built-in interpolation to avoid the cost of formatting the string if it's not needed.

Consider the following:

log.info("About to make 10,000 requests")
for x in xrange(10000):
    response = make_request()
    log.debug("Response: {0}".format(response))

    # code continues...

Even if we're only at the INFO level we still pay to format the whole response into a string on every iteration just to have it thrown away by the logger.

Instead, use the built-in interpolation:

log.info("About to make 10,000 requests")
for x in xrange(10000):
    response = make_request()
    log.debug("Response: %s", response)

    # code continues...

Since the arguments are passed as references in Python you only pay for the interpolation if you're at that log level.

Logging exceptions

When logging in an exception handler the Python logging package provides a straight forward method for logging not only your human-consumable message but also the complete stack trace for the current exception.

This is a common anti-pattern:

try:
    raise_an_exception()
except Exception as exc:
    log.error("Something bad happened: %s", str(exc))

Instead, use the .exception method on the log handler:

try:
    raise_an_exception()
except Exception:
    log.exception("Something bad happened")

python-wisdom's People

Contributors

borgstrom avatar b-jazz avatar

Watchers

Loren Carvalho avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.