开发者

How do you debug pythonic GUI programs?

开发者 https://www.devze.com 2023-03-20 17:46 出处:网络
I want to debug a pythonic program, such as calibre. Normally, I was using pdb to debug from the console, but when I use pdb with pythonic GUI programs, the GUI part (canvas or what the heck it is) fr

I want to debug a pythonic program, such as calibre. Normally, I was using pdb to debug from the console, but when I use pdb with pythonic GUI programs, the GUI part (canvas or what the heck it is) freezes and it's really very hard to debug in th开发者_如何学Cat way.

Any suggestions for debugging pythonic GUI programs? How do you do it?


I would place calls to logging.debug at the top of each event handler function/method in my GUI code that represents a "user action", i.e. mouse clicks, enter key. Also any high-level function that updates the view would have a logging.debug call at the beginning. These log messages would report any important information used in the function/method. Because the messages are logged at DEBUG level, you can turn them on/off with a simple configuration change.

Alternatively, while unsophisticated, it might be faster to forget even the logging module and put in print statements temporarily until you find the problem.

Here's some code I wrote to initialize the logging module with a rotating log file:

import pytz

timestamp_detailed_format = '%Y-%m-%d %H:%M:%S.%f %Z'

def detailed_format(date):
  u"Given a datetime object return a detailed representation including fractional seconds and time zone"
  return unicode(date.strftime(timestamp_detailed_format))

def localize_epoch_time(epoch_time, timezone=pytz.UTC):
  u"Given an epoch time return an accurate, timezone-aware datetime object"
  t = localtime(epoch_time)
  epochdt = datetime(*(t[:6] + (int((epoch_time - long(epoch_time)) * 1000000),))).astimezone(timezone)
  if hasattr(timezone, 'normalize'):  # pytz tzinfo objects have this
    return timezone.normalize(epochdt)
  else: # tzinfo object not from pytz module
    return epochdt

class TimezoneAwareFormatter(logging.Formatter):
  u"custom log formatter using timezone-aware timestamps"
  def __init__(self, logformat=None, timezone=pytz.UTC):
    logging.Formatter.__init__(self, logformat)
    self._timezone = timezone
  def formatTime(self, record, _=None):
    u"times will be formatted as YYYY-MM-DD HH:MM:SS.ssssss TZ"
    return detailed_format(localize_epoch_time(record.created, self._timezone))

def simple_log_file(filename, logname=None, level=logging.NOTSET,
                    threshold=10485760, generations=2, quiet=False, timezone=pytz.UTC):
  u"initialize logging API for a simple generational log file, return logger object"
  formatter = TimezoneAwareFormatter('%(asctime)s %(levelname)s %(message)s', timezone)
  handler = logging.handlers.RotatingFileHandler(filename, 'a', threshold, generations, 'UTF-8')
  handler.setFormatter(formatter)
  logger = logging.getLogger(logname)
  logger.addHandler(handler)
  logger.setLevel(level)
  if not quiet: logger.info(u'Logging to this destination has started')
  return logger
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号