Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #3485

How to create a (transparent) decorator with status information?

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <timo.schmiade@gmx.de>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'wrapper': 0.07; 'decorator': 0.09; 'email name:': 0.09; 'from:addr:gmx.de': 0.09; 'usage.': 0.09; 'examples': 0.12; 'def': 0.13; 'skip:f 30': 0.14; '**kwargs)': 0.16; '**kwargs):': 0.16; '*args,': 0.16; 'advance!': 0.16; 'be?': 0.16; 'emits': 0.16; 'file).': 0.16; 'naturally': 0.16; 'obj,': 0.16; 'subject:create': 0.16; 'received:(qmail invoked by alias)': 0.16; 'received:213.165.64': 0.16; 'received:gmx.net': 0.19; 'e.g.': 0.22; 'somehow': 0.23; 'received:213.165': 0.25; '(e.g.': 0.26; "i'm": 0.26; 'object': 0.27; 'function': 0.27; 'thanks': 0.29; 'shared': 0.29; 'error': 0.29; 'problem': 0.29; 'subject:?': 0.29; 'class': 0.29; "python's": 0.29; 'implement': 0.30; 'decorators': 0.31; "can't": 0.31; 'all,': 0.31; 'to:addr:python-list': 0.32; 'idea': 0.32; 'thinking': 0.33; 'using': 0.34; 'question': 0.35; 'header:User- Agent:1': 0.35; 'point': 0.35; 'function.': 0.35; 'instances': 0.35; 'maintained': 0.35; 'maintains': 0.35; 'status,': 0.35; 'missing': 0.36; 'case,': 0.36; 'charset:us-ascii': 0.36; 'problems': 0.37; 'some': 0.37; 'however': 0.37; 'either': 0.37; 'subject:with': 0.37; 'logging': 0.38; 'user': 0.38; 'but': 0.38; 'so,': 0.38; 'completely': 0.38; 'unless': 0.38; 'help': 0.39; 'to:addr:python.org': 0.39; 'received:de': 0.39; 'subject: (': 0.39; 'how': 0.39; 'count': 0.40; 'would': 0.40; 'might': 0.40; 'design': 0.61; 'transparent': 0.65; 'encountered': 0.73; '(let': 0.84
X-Authenticated #18738959
X-Provags-ID V01U2FsdGVkX1/EJ0NdCYPEzyeBN1FfSqpEUEsIz+oaSnaTAJSTXG T9ufGl+30XbPS0
Date Mon, 18 Apr 2011 14:47:02 +0200
From Timo Schmiade <the_isz@gmx.de>
To python-list@python.org
Subject How to create a (transparent) decorator with status information?
MIME-Version 1.0
Content-Type text/plain; charset=us-ascii
Content-Disposition inline
X-OfflineIMAP x1104518509-676d7872656d6f7465-447261667473-1302977841-0983947115145-v6.3.2
User-Agent Mutt/1.5.21 (2010-09-15)
X-Y-GMX-Trusted 0
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.510.1303130820.9059.python-list@python.org> (permalink)
Lines 54
NNTP-Posting-Host 82.94.164.166
X-Trace 1303130820 news.xs4all.nl 32470 [::ffff:82.94.164.166]:36240
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:3485

Show key headers only | View raw


Hi all,

I'm currently occupying myself with python's decorators and have some
questions as to their usage. Specifically, I'd like to know how to
design a decorator that maintains a status. Most decorator examples I
encountered use a function as a decorator, naturally being stateless.

Consider the following:

def call_counts(function):
  @functools.wraps(function):
  def wrapper(*args, **kwargs):
    # No status, can't count #calls.
    return function(*args, **kwargs)
  return wrapper

Thinking object-orientedly, my first idea was to use an object as a
decorator:

class CallCounter:
  def __init__(self, decorated):
    self.__function = decorated
    self.__numCalls = 0

  def __call__(self, *args, **kwargs):
    self.__numCalls += 1
    return self.__function(*args, **kwargs)

  # To support decorating member functions
  def __get__(self, obj, objType):
    return functools.partial(self.__call__, obj)

This approach however has three problems (let "decorated" be a function
decorated by either call_counts or CallCounter):

* The object is not transparent to the user like call_counts is. E.g.
  help(decorated) will return CallCounter's help and decorated.func_name
  will result in an error although decorated is a function.
* The maintained status is not shared among multiple instances of the
  decorator. This is unproblematic in this case, but might be a problem
  in others (e.g. logging to a file).
* I can't get the information from the decorator, so unless CallCounter
  emits the information on its own somehow (e.g. by using print), the
  decorator is completely pointless.

So, my question is: What would the "pythonic" way to implement a
decorator with status information be? Or am I missing the point of
decorators and am thinking in completely wrong directions?

Thanks in advance!

Kind regards,

Timo

Back to comp.lang.python | Previous | Next | Find similar


Thread

How to create a (transparent) decorator with status information? Timo Schmiade <the_isz@gmx.de> - 2011-04-18 14:47 +0200

csiph-web