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


Groups > comp.lang.python > #4284 > unrolled thread

回复: Re: Need your help

Started by"1011_wxy"<1011_wxy@163.com>
First post2011-04-29 09:31 +0800
Last post2011-04-29 09:31 +0800
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python


Contents

  回复:  Re: Need your help "1011_wxy"<1011_wxy@163.com> - 2011-04-29 09:31 +0800

#4284 — 回复: Re: Need your help

From"1011_wxy"<1011_wxy@163.com>
Date2011-04-29 09:31 +0800
Subject回复: Re: Need your help
Message-ID<mailman.973.1304041527.9059.python-list@python.org>

[Multipart message — attachments visible in raw view] — view raw

Dear Thomas,JM,Chris Rebert:

I got it. Thank you very very very much.


Best Regards

2011-04-29



1011_wxy



发件人: Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de>
发送时间: 2011-04-28 21:26
主 题: Re: Need your help
收件人: python-list@python.org



Am 28.04.2011 13:14, schrieb Chris Rebert: 

> import a, b, sys 
> def c(): 
>      orig_stdout = sys.stdout 
>      sys.stdout = open('my_log_file.log', 'w') 
>      a.a() 
>      b.b() 
>      sys.stdout.close() 
>      sys.stdout = orig_stdout 
> 
> 
> Someone may have written a with-statement context manager that 
> abstracts away the swapping. 

@contextlib.contextmanager 
def swap_stdout(target): 
     orig_stdout = sys.stdout 
     sys.stdout = target 
     try: 
         yield target 
     finally: 
         sys.stdout = orig_stdout 

In this case, I can use all kinds of files (open() files, StringIO  
objects etc.), and can re-use them if necessary, in order to  
prepend/append data. 

Closing can happen additionally, if the user wants. 


Thomas 
--  
http://mail.python.org/mailman/listinfo/python-list 

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web