[py-dev] py.log suggestion

holger krekel hpk at trillke.net
Fri Oct 28 10:30:41 CEST 2005


Hey Francois, 

On Tue, Oct 25, 2005 at 22:22 -0400, Fran?ois Pinard wrote:
> [holger krekel]
> 
> > 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.
> 
> Oh!  I did not know that similarity with ``print``, which I always
> perceived as a debugging statement -- I can blindly get rid of any
> ``print`` statements at any time in my programs).
> 
> No doubt I can tame myself to the above! :-)

:-) 
 
> >     mylog("hello %(x)s".__mod__ , x=3)
> 
> Hmph!  That's too clever to be really useful! :-).

ok, ok :) 
 
> > 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
> >         ...
> 
> It surely solves the initial problem, anyway, which is to have some way
> to conditionalise out semi-lengthy computations whose sole purpose is to
> generate the text of log messages.
> 
> > (you could make the 'if' even a 'while' :-)
> > what do you think?
> 
> Maybe, yet there is a slight danger that logging be less fun, if it was
> requiring too much attention, and was a bit prone at creating looping
> problems or other obscure bugs, that would distract users away from the
> outside problems they are scrutinizing.  Or maybe I'm over-cautious,
> it's not easy to predict where is the correct equilibrium.

Well, the "while" really was a joke. 
 
> And there is this unexpected danger as well, which you underlined in one
> of your replies, and which I did not foresee (I should probably have),
> that mylog may receive a first argument which is unexpectedly callable.

Right.  So i have a new suggestion.  

    log.info[arg1, arg2, ...]

would behave exactly like the log.info(arg1, arg2, ...) does now, 
i.e. a dumb "print" emulation.  No special check for callables, 
formatting and such. 

But then we can put the "call function" syntax to new usage. 
So with 

    log.info(*args, **kwargs)

we would now have special treatment (rough sketch): 

    ...
    if consumer is not None:
        if callable(args[0]):
            consumer(args[0](*args[1:], **kwargs))
        else: 
            if kwargs: 
                assert len(args) == 1, "..."
                consumer(args[0] % kwargs)
            else:
                assert not kwargs, "..."
                consumer(args[0] % args[1:])

this should IMO allow a rather intuitive usage for percent-formatting
as well as for defering formatting to custom callables. 

This is unfortunately a somewhat incompatible change to
current usage but well, it's not used that much yet and we
probably have enough access to the code bases that currently
use it. 

> P.S. - Sorry for the delay in replying.  Job pressure is
> high in these days!

he, no problem! what do you think goes on here? :) 

cheers, 

    holger


More information about the py-dev mailing list