Mario Orlandi's Snippets

06/09/2011

Count queries

Filed under: Uncategorized — Tags: , — morlandi @ 15:24

file “middleware.py”:

from django.db import connection
from django.utils.log import getLogger

logger = getLogger(__name__)

# http://dabapps.com/blog/logging-sql-queries-django-13/
class QueryCountDebugMiddleware(object):
    """
    This middleware will log the number of queries run and the total time taken for each request (with a
    status code of 200). It does not currently support multi-db setups.
    """
    def process_response(self, request, response):
        if response.status_code == 200:
            total_time = 0

            for query in connection.queries:
                query_time = query.get('time')
                if query_time is None:
                    # django-debug-toolbar monkeypatches the connection
                    # cursor wrapper and adds extra information in each
                    # item in connection.queries. The query time is stored
                    # under the key "duration" rather than "time" and is
                    # in milliseconds, not seconds.
                    query_time = query.get('duration', 0) / 1000
                total_time += float(query_time)

            logger.debug('%s queries run, total %s seconds' % (len(connection.queries), total_time))
            print '\'+'033[33m==> %s queries run, total %s seconds' % (len(connection.queries), total_time)

        return response

file "development.py":

MIDDLEWARE_CLASSES = list(MIDDLEWARE_CLASSES) + ['djangobase.middleware.QueryCountDebugMiddleware',]

05/07/2011

Django debugging

Filed under: Uncategorized — Tags: , — morlandi @ 17:13
from: http://stackoverflow.com/questions/1118183/how-to-debug-in-django-the-good-way

- import pdb; pdb.set_trace() 
- ipdb.set_trace() (requires iphyton)
- return HttpResponse({variable to inspect})
- raise Exception({variable to inspect})

- Werkzeug's interactive debugger: > manage runserver_plus, then 'raise' (requires django-extensions)

- {{ template_var|pdb }} ... enter pdb session to inspect element.
Requires this template tag:

@register.filter 
def pdb(element):
    """ Usage: {{ template_var|pdb }}
        then inspect 'element' from pdb
    """
    import pdb; pdb.set_trace()
    return element

- django-viewtools for debugging views interactively using pdb

21/05/2009

PyDev heuristics for type inference

Filed under: snippets — Tags: , — morlandi @ 07:45
  • Python: assert isinstance(xxx, Class)
  • PyProtocols: adapt(obj, Class)
  • Zope: assert Interface.implementedBy(Class)
  • Zope: assert Interface.providedBy(obj)

Tracing with plone_log or logging.getLogger()

Filed under: snippets — Tags: , — morlandi @ 07:41
Tracing with plone_log
context.plone_log('Invalid document type detected for mail %s' % (folder.id))

Tracing with logging.getLogger()
import sys,logging
from logging import getLogger
log = getLogger('Plone')
log.warn('Debug: %s \n%s', summary, text)

    def log(self,text):
        """trace helper"""
        log = getLogger('Plone')
        log.warn('>> '+text)
        print(text)

Allow pdb.set_trace() and pydevd.settrace() inside Script (Python) objects

Filed under: snippets — Tags: , — morlandi @ 07:38
# file "Product/myproduct/__init__.py

# allows pdb.set_trace() inside Script (Python) objects
from AccessControl import ModuleSecurityInfo
ModuleSecurityInfo('pdb').declarePublic('set_trace')
ModuleSecurityInfo('pydevd').declarePublic('settrace')

20/05/2009

send django queries to OutputDebugString (Win32)

Filed under: snippets — Tags: , — morlandi @ 07:25
Modify django/db/backends/util.py as follows:

import win32api

class CursorDebugWrapper(object):
    def __init__(self, cursor, db):
        self.cursor = cursor
        self.db = db # Instance of a BaseDatabaseWrapper subclass

    def debug_trace(self,category,message):
        win32api.OutputDebugString('|django              |%s|%s\n' % (category,message))

    def trace_entry (self, sql, elapsed):
        self.debug_trace('Q',sql)
        if elapsed >= 0.01:
            self.debug_trace(' ','elapsed: %.3f' % elapsed)

    def execute(self, sql, params=()):
        start = time()
        try:
            return self.cursor.execute(sql, params)
        finally:
            stop = time()
            sql = self.db.ops.last_executed_query(self.cursor, sql, params)
            self.db.queries.append({
                'sql': sql,
                'time': "%.3f" % (stop - start),
            })
            self.trace_entry(sql, stop - start) 

    def executemany(self, sql, param_list):
        start = time()
        try:
            return self.cursor.executemany(sql, param_list)
        finally:
            stop = time()
            self.db.queries.append({
                'sql': '%s times: %s' % (len(param_list), sql),
                'time': "%.3f" % (stop - start),
            })
            self.trace_entry(sql, stop - start) 

Theme: Silver is the New Black. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.