[py-dev] py.log and multiple keywords

Grig Gheorghiu grig at gheorghiu.net
Mon Jun 20 15:56:00 CEST 2005


--- holger krekel <hpk at trillke.net> wrote:

> Hi Grig, 
> 
> On Fri, Jun 17, 2005 at 09:15 -0700, Grig Gheorghiu wrote:
> > Holger and others interested in this topic,
> > 
> > I was thinking that in many situations it would be helpful to be
> able
> > to specify multiple keywords that would each point to a different
> > consumer and that would be all 'kick in' on a call to the producer.
> > Basically, I'd want this test to pass:
> > 
> >     def test_multiple_keywords(self):
> >         log = py.log.Producer('console logfile db')
> >         py.log.setconsumer('console', py.log.STDOUT)
> >         logfile = tempdir.join('mylogfile.out')
> >         py.log.setconsumer('logfile', open(str(logfile), 'w',
> > buffering=1))
> >         dbfile = tempdir.join('mydbfile.out')
> >         py.log.setconsumer('db', open(str(dbfile), 'w',
> buffering=1))
> >         
> >         res, out, err = callcapture(log, "hello")
> >         assert out.strip() == "[console:logfile:db] hello" 
> > 
> >         assert logfile.readlines() == ['[console:logfile:db]
> hello\n']
> >         assert dbfile.readlines() == ['[console:logfile:db]
> hello\n']
> > 
> > Right now the last 2 assert statements fail, because _getconsumer
> > returns as soon as it finds a match for the first keyword, so only
> the
> > 'console' consumer gets a chance to consume the message.
> 
> Yes, however with the current svn version you can simply do: 
> 
>      def test_multiple_keywords(self):
>          log = py.log.Producer('console logfile db')
> 
>          logfile = open(tempdir.join('mylogfile.out'), 'a', 1)
>          dbfile = open(tempdir.join('mydbfile.out'), 'a', 1) 
> 
>          def multilogger(msg): 
>             if 'logfile' in msg.keywords: 
>                 print >>logfile, str(msg) 
>             if 'db' in msg.keywords: 
>                 print >>dbfile, str(msg) 
>                 
>          py.log.setconsumer('console', multilogger) 
>          
>          res, out, err = callcapture(log, "hello")
>          assert out.strip() == "[console:logfile:db] hello" 
>  
>          assert logfile.readlines() == ['[console:logfile:db]
> hello\n']
>          assert dbfile.readlines() == ['[console:logfile:db]
> hello\n']
> 
> So it seems that with the simple ability to associate functions
> to keywords you can basically do a lot of stuff without knowing 
> any big API or intricate details. 
> 

Hi, Holger

I see your point. It's still somehow convoluted to achieve my goal
using  the solution you give, but on the other hand I understand that
you want to keep the API 'lean and mean'. 

I still haven't figured out what a practical application of the current
semantics for multiple keywords would be -- can you give an example? I
noticed you changed the _getconsumer function to first match on the
whole multiple keyword tuple, and then on  prefixes of the tuple.

Grig



More information about the Pytest-dev mailing list