Python programming Debugging

Similar documents
Python programming Testing

Introduction to Logging. Application Logging

Code::Blocks Student Manual

latest Release 0.2.6

Linux Syslog Messages in IBM Director

Django Two-Factor Authentication Documentation

Advanced Tornado TWENTYONE Advanced Tornado Accessing MySQL from Python LAB

Introduction to Python

Tutorial. Reference for more thorough Mininet walkthrough if desired

How to test and debug an ASP.NET application

DS-5 ARM. Using the Debugger. Version 5.7. Copyright 2010, 2011 ARM. All rights reserved. ARM DUI 0446G (ID092311)

Survey of Unit-Testing Frameworks. by John Szakmeister and Tim Woods

Version Control Using Subversion. Version Control Using Subversion 1 / 27

CS 1133, LAB 2: FUNCTIONS AND TESTING

Lab 2-2: Exploring Threads

MXwendler Javascript Interface Description Version 2.3

Determine the process of extracting monitoring information in Sun ONE Application Server

Title: Documentation for the Pi-UPS-Monitor-App

Python programming GUI

A Sample OFBiz application implementing remote access via RMI and SOAP Table of contents

University of Hull Department of Computer Science. Wrestling with Python Week 01 Playing with Python

Installing an Omnicast System Omnicast version 3.5

Ansible. Configuration management tool and ad hoc solution. Marcel Nijenhof

Visual Studio 2008 Express Editions

Computer Programming In QBasic

DS-5 ARM. Using the Debugger. Version Copyright ARM. All rights reserved. ARM DUI 0446M (ID120712)

Configuring Apache HTTP Server With Pramati

Python for Series 60 Platform

Introduction to Python for Text Analysis

Introduction to Eclipse

Profiling, debugging and testing with Python. Jonathan Bollback, Georg Rieckh and Jose Guzman

Unit testing with mock code EuroPython 2004 Stefan Schwarzer p.1/25

For Introduction to Java Programming, 5E By Y. Daniel Liang

Monitoring Oracle Enterprise Performance Management System Release Deployments from Oracle Enterprise Manager 12c

Portals and Hosted Files

id_prob_result_coredump_aix.ppt Page 1 of 15

PROCESSES LOADER 9.0 SETTING. Requirements and Assumptions: I. Requirements for the batch process:

Using Git for Project Management with µvision

Notepad++ The COMPSCI 101 Text Editor for Windows. What is a text editor? Install Python 3

Creating your personal website. Installing necessary programs Creating a website Publishing a website

DS-5 ARM. Using the Debugger. Version Copyright ARM. All rights reserved. ARM DUI0446P

Python programming databasing

DevKey Documentation. Release 0.1. Colm O Connor

Outline. hardware components programming environments. installing Python executing Python code. decimal and binary notations running Sage

IBM Operational Decision Manager Version 8 Release 5. Getting Started with Business Rules

To reduce or not to reduce, that is the question

Dreamweaver CS6 Basics

PYTHON Basics

Lecture 2 Mathcad Basics

Introduction to Programming and Computing for Scientists

JavaScript Testing. Beginner's Guide. Liang Yuxian Eugene. Test and debug JavaScript the easy way PUBLISHING MUMBAI BIRMINGHAM. k I I.

MultiDSLA v4.8.1: Release Notes at 07 October 2013

The New ABAP Debugger - An Introduction. Boris Gebhardt Christoph Stoeck SAP AG

SAPIP GUI INSTALLATION. Table of Contents

Q N X S O F T W A R E D E V E L O P M E N T P L A T F O R M v Steps to Developing a QNX Program Quickstart Guide

- User input includes typing on the keyboard, clicking of a mouse, tapping or swiping a touch screen device, etc.

PIM SOFTWARE TR50. Configuring the Syslog Feature TECHNICAL REFERENCE page 1

Setting up PostgreSQL

TSM for Windows Installation Instructions: Download the latest TSM Client Using the following link:

How to use the VMware Workstation / Player to create an ISaGRAF (Ver. 3.55) development environment?

CloudVPS Backup Manual. CloudVPS Backup Manual

Bulk Downloader. Call Recording: Bulk Downloader

M100 System File Manager Help

IBM VisualAge for Java,Version3.5. Remote Access to Tool API

MATLAB Functions. function [Out_1,Out_2,,Out_N] = function_name(in_1,in_2,,in_m)

Developing, Deploying, and Debugging Applications on Windows Embedded Standard 7

vmprof Documentation Release 0.1 Maciej Fijalkowski, Antonio Cuni, Sebastian Pawlus

Article title goes here unless article begins on this page. If article begins on this page, override rules and text using Ctrl + Shift.

CLC Server Command Line Tools USER MANUAL

List of FTP commands for the Microsoft command-line FTP client

Using RADIUS Agent for Transparent User Identification

10 STEPS TO YOUR FIRST QNX PROGRAM. QUICKSTART GUIDE Second Edition

Programming in Access VBA

Netop Remote Control for Linux Installation Guide Version 12.22

Tivoli Endpoint Manager BigFix Dashboard

CodeWarrior Development Studio for Freescale S12(X) Microcontrollers Quick Start

Installing AWStats on IIS 6.0 (Including IIS 5.1) - Revision 3.0

Ohio University Computer Services Center August, 2002 Crystal Reports Introduction Quick Reference Guide

Grinder Webtest Documentation

Integration for X-Tools and X32

Python Documentation & Startup

CS106B Handout #5P Winter January 14, 2008

Service Integration course. Cassandra

I don t intend to cover Python installation, please visit the Python web site for details.

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

XTraffic Plugin Quick Start Guide

CS 2112 Lab: Version Control

Capabilities of a Java Test Execution Framework by Erick Griffin

BA-1000 Software Package. Version 1.00

Integrated Accounting System for Mac OS X

Sophos Mobile Control Web service guide

Virto Create & Clone AD User Web Part for Microsoft SharePoint. Release Installation and User Guide

User Guide For ipodder on Windows systems

VTiger CRM + Joomla/ChronoForms Integration

Hypercom Key Loading and Management (HKLM) RS232 PPP Key Injection PC Setup Guide

here: styleguide.googlecode.com/svn/trunk/javascriptguide.xml.

Importing and Exporting With SPSS for Windows 17 TUT 117

Beginning to Program Python

Transcription:

Python programming Debugging Finn Årup Nielsen DTU Compute Technical University of Denmark October 30, 2013

Overview print, pprint logging pdb: The Python Debugger Spyder PuDB Introspection with Paul Butler s debugging decorator Finn Årup Nielsen 1 October 30, 2013

Before major debugging Write unit tests: Consider test-driven development where you first write the test code and then implement the functionality. Run pylint: This will check you code for style and perhaps discover real bugs Finn Årup Nielsen 2 October 30, 2013

Print print: While ok in development print statements should usually not occure in the finished code whether executed or not (comment out). For nested structures, such as dictionaries within lists within dictionaries consider pprint (note the extra p ) import pprint, requests response = requests.get("https://ofirehose.com/feed.json").json() pprint.pprint(response["items"][0]) This gives you a better indentation of the nested structure. Finn Årup Nielsen 3 October 30, 2013

logging module Set different level of logging messages: DEBUG, INFO, WARNING, ER- ROR Useful for, e.g., for a web application that never should error, but always return something useful. Consistent formatting with timing information Setting of the logging level Redirection of the logging output: standard error, log files. Finn Årup Nielsen 4 October 30, 2013

Simple logging example import logging, requests logging.debug("requesting feeds from ofirehose") try: response = requests.get("httpps://ofirehose.com/feed.json").json() feeds = response["items"] except Exception as e: feeds = [] logging.warn("could not download feeds from ofirehose: " + str(e)) for feed in feeds: print(feed[ content ]) This will lead to a logging message from the warn call WARNING:root:Could not download feeds from ofirehose: No connection... Finn Årup Nielsen 5 October 30, 2013

More elaborate logging example... Setting up logfile, format and logging level: import logging, os.path logger = logging.getlogger("openfeed") # Name of logger filename = os.path.expanduser("~/openfeed.log") # log file hdlr = logging.filehandler(filename) formatter = logging.formatter("%(asctime)s %(levelname)s %(message)s") hdlr.setformatter(formatter) # Format for each log line logger.addhandler(hdlr) logger.propagate = False # No stderr output logger.setlevel(logging.debug) # Changing log level logger.info("logging setup") # Logging that the log is setup Now the logger object is setup that we can use for logging: Finn Årup Nielsen 6 October 30, 2013

... More elaborate logging example logger.debug("requesting feeds from ofirehose") try: response = requests.get("httpps://ofirehose.com/feed.json").json() feeds = response[ items ] except Exception as e: feeds = [] logger.warn("could not download feeds from ofirehose: " + str(e)) for feed in feeds: print(feed[ content ]) The logfile openfeed.log now contains: 2013-10-02 16:20:39,604 INFO Logging setup 2013-10-02 16:21:01,034 DEBUG Requesting feeds from ofirehose 2013-10-02 16:21:01,054 WARNING Could not download feeds from ofireho... Finn Årup Nielsen 7 October 30, 2013

Logging in modules... In the module submodule.py: import logging from logging import NullHandler log = logging.getlogger( name ) log.addhandler(nullhandler()) def some_function(): log.debug("in some_function()") return "Hello, World" # The log gets the name of the module # Avoids "No handlers" message if no logger # A log message to the module log In importing module usermodule.py for example: import submodule log = logging.getlogger() # This includes the submodule logger too log.setlevel(logging.debug) handler = logging.streamhandler() handler.setformatter(logging.formatter( %(asctime)s %(levelname)s %(name)s: %(message)s )) log.addhandler(handler) submodule.some_function() Finn Årup Nielsen 8 October 30, 2013

... Logging in modules How to make it shut up: With no logger: import submodule submodule.some_function() Or by adjusting the logging level: import submodule log = logging.getlogger() # This includes the submodule logger too log.setlevel(logging.warning) handler = logging.streamhandler() handler.setformatter(logging.formatter( %(asctime)s %(levelname)s %(name)s: %(message)s )) log.addhandler(handler) submodule.some_function() Finn Årup Nielsen 9 October 30, 2013

Python debugger: pdb The Python Debugger is a module, pdb, for interactive code debugging The perhaps most simple usages is to insert a breakpoint: import pdb; pdb.set_trace() When reached, the debugger is started with the prompt (Pdb) Finn Årup Nielsen 10 October 30, 2013

Pdb command examples help/h: Displays the list of commands step/s: Single step the program, step into functions. next/n: Single step in the current function pp/p: Pretty printing/printing a variable cont/c: Continue execution quit/q: Quit the debugger... Finn Årup Nielsen 11 October 30, 2013

pdb example import requests import pdb try: pdb.set_trace() response = requests.get("httpps://ofirehose.com/feed.json").json() feeds = response[ items ] except Exception as e: feeds = [] (Pdb) n InvalidSchema: InvalidS...json ",) > <stdin>(3)<module>() (Pdb) pp requests.get("httpps://ofirehose.com/feed.json") *** InvalidSchema: InvalidSchema("No connection adapters were fou... Finn Årup Nielsen 12 October 30, 2013

pdb in Spyder pdb is available in Spyder. Breakpoints may be added with the mouse or keyboard (F12). Finn Årup Nielsen 13 October 30, 2013

PuDB PuDB, a console-based Python debugger. Consider the file firehose.py import requests try: response = requests.get("httpps://ofirehose.com/feed.json").json() feeds = response[ items ] except Exception as e: feeds = [] for feed in feeds: print(feed["content"]) Run pudb on the file: $ pudb firehose.py Finn Årup Nielsen 14 October 30, 2013

PuDB Finn Årup Nielsen 15 October 30, 2013

Regular expression debugging import re re.compile("...[here goes a complicated regular expression]", re.debug) re.compile(r"(?:-)*(?:\d{1,3}(?:,\d{3})*(?:\.\d*)? \d+(?:\.\d*)?)", flags=re.debug) Will perhaps(?) give a better overview of the regular expression. Finn Årup Nielsen 16 October 30, 2013

max_repeat 0 65535 subpattern None literal 45 subpattern None branch max_repeat 1 3 in category category_digit max_repeat 0 65535 subpattern None literal 44 max_repeat 3 3 in category category_digit max_repeat 0 1 subpattern None literal 46 max_repeat 0 65535 in category category_digit or max_repeat 1 65535 in category category_digit max_repeat 0 1 subpattern None literal 46 max_repeat 0 65535 in category category_digit Finn Årup Nielsen 17 October 30, 2013

Traceback and logging... Catching uncaught exceptions in the log with traceback. First, troublesome example code (where is the bug(s)), a simple daemon for monitoring file lengths in a directory: import os def monitor_lengths(dirname="."): lengths = {} while True: for filename in os.listdir(dirname): filename = os.path.join(dirname, filename) if os.path.isfile(filename): length = len(open(filename).read()) if filename in lengths: if lengths[filename]!= length: print(filename) else: lengths[filename] = length Finn Årup Nielsen 18 October 30, 2013

... Traceback and logging Exception traceback modified from the cookbook (Martelli et al., 2005, section 8.4) with output to a log. import logging import cstringio import traceback try: monitor_lengths() except Exception as e: f = cstringio.stringio() traceback.print_exc(file=f) msg = f.getvalue().replace("\n", "\\\\") logging.critical(msg) # On one line Finn Årup Nielsen 19 October 30, 2013

Paul Butler s debugging decorator Paul Butler debugging decorator to decorate a function that misbehaves def report(function): def wrap(*args, **kwargs): wrap.call_count += 1 indent = * report._indent fc = "{}({})".format(function. name, ", ".join(map(str, args) + map(lambda (k, v): "{}={}".format(k, v), kwargs.items()))) print("{}{} called #{}".format(indent, fc, wrap.call_count) ) report._indent += 1 return_value = function(*args, **kwargs) report._indent -= 1 print("{}{} returned with value {}".format(indent, fc, str(return_value)) ) return return_value wrap.call_count = 0 return wrap report._indent = 0 Now the @report decorator can be applied on functions: Finn Årup Nielsen 20 October 30, 2013

... Paul Butler-like debugging decorator Decorating the troublesome function with Paul Butler s debugging decorator: @report def not_really_fibonacci(n, dummy=0): if n in [0, 1, 2]: return n else: return not_really_fibonacci(n-1, dummy) + not_really_fibonacci(n-3, dummy) Run the program: >>> not_really_fibonacci(4, dummy=84) not_really_fibonacci(4, dummy=84) called #1 not_really_fibonacci(3, 84) called #2 not_really_fibonacci(2, 84) called #3 not_really_fibonacci(2, 84) returned with value 2 not_really_fibonacci(0, 84) called #4 not_really_fibonacci(0, 84) returned with value 0 not_really_fibonacci(3, 84) returned with value 2 not_really_fibonacci(1, 84) called #5 not_really_fibonacci(1, 84) returned with value 1 not_really_fibonacci(4, dummy=84) returned with value 3 3 Finn Årup Nielsen 21 October 30, 2013

More information Andrew Dalke, Tracing python code: The use of sys.settrace and linecache for printing executed lines. Finn Årup Nielsen 22 October 30, 2013

Summary print should rarely appear in a finished program Better to use logging module Pdb is the Python debugger with a simple command-line interface. Pdb functionality is available in Spyder and in Pudb (and likely other IDE) Python is a programming language with introspection: You can trace the program and, e.g., query the function name. Finn Årup Nielsen 23 October 30, 2013

References References Martelli, A., Ravenscroft, A. M., and Ascher, D., editors (2005). Python Cookbook. O Reilly, Sebastopol, California, 2nd edition. Finn Årup Nielsen 24 October 30, 2013