[py-dev] py.log suggestion

holger krekel hpk at trillke.net
Sat Oct 22 21:18:09 CEST 2005


On Sun, Oct 16, 2005 at 12:37 -0400, Fran?ois Pinard wrote:
> > [François Pinard]
> 
> > > > >     if trace.dossier:
> > > > >         diagnostic = prepare_diagnostic_information()
> > > > >         trace.dossier(diagnostic)
> 
> > >      if callable(trace.dossier):
> > >          diagnostic = prepare_diagnostic_information()
> > >          trace.dossier(diagnostic)
> 
> [holger krekel]
> 
> > i was thinking of just
> 
> >     trace.dossier(prepare_diagnostic_information)
> 
> > actually.  If there is no consumer then this would not call the
> > callable.  Does that make sense to you?
> 
> Of course, a lot.  This is a better idea, that would yield cleaner code.
> 
> > (we could additionally pass any remaining arguments to the callable
> > but that might be hard to conceptually define nicely).
> 
> Could not the producer prototype be a mere:
> 
>    producer(message, *args, **kws)
> 
> It could implement something like:
> 
>     [...]
>     if any_consumer_besides_None:
>         if callable(message):
>             text = message(*args, **kws)
>         elif args:
>             assert not kws
>             text = message % args
>         elif kws:
>             text = message % kws
>         else:
>             text = message
>     [...]
> 
> yielding the usual paradigm that message may be a format, for which
> interpolation does not uselessly occur.  Also, the format could use
> %(keyword)s, with keywords given while calling the producer.

I see the use of being able to lazify interpolation but it puts
the producer even further away from the print statement.  Also
it's incompatible to current usage.  Currently 

    print x, "hello", z 

is mostly equivalent to 

    mylog(x, "hello", z) 

and somehow i'd like to keep it that way. 
But if we just special case the first argument being callable 
(which is already somewhat on the risky side because you may
want to put arbitrary objects there which might have a __call__
although you just want to see them __repr__'ed) then you 
could use e.g.

    mylog("hello %(x)s".__mod__ , x=3) 

in which case people would learn what the percent-operator 
actually translates to at the same time :-) 

So, in short, i propose to treat the case of the first arg
being a callable, simply as: 

    if consumer is not None: 
        if args and callable(args[0]): 
            args = args[0](*args[1:], **kwargs)
            kwargs = None
        ... 

(you could make the 'if' even a 'while' :-) 

what do you think? 

    holger



More information about the Pytest-dev mailing list