Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #6083
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <ian.g.kelly@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'context': 0.04; 'ok.': 0.04; 'exception,': 0.07; 'raises': 0.07; 'ret': 0.07; 'url:py': 0.07; '3.x': 0.09; 'called.': 0.09; 'dict': 0.09; 'highlight': 0.09; 'revision': 0.09; 'stack,': 0.09; 'subject:files': 0.09; 'pm,': 0.10; 'wrote:': 0.14; "'w')": 0.16; '3.2.': 0.16; 'called,': 0.16; 'contextlib': 0.16; 'deprecated.': 0.16; 'exc_type,': 0.16; 'exc_value,': 0.16; 'invoked.': 0.16; 'pythonic': 0.16; 'skip:\xa0 50': 0.16; 'url:hg': 0.16; '\xa0def': 0.16; '\xa0for': 0.16; 'traceback': 0.16; 'header:In-Reply-To:1': 0.21; 'smart': 0.23; 'suggests': 0.23; 'way?': 0.23; '\xa0if': 0.23; 'received:209.85.161.46': 0.23; 'received:mail- fx0-f46.google.com': 0.23; 'loaded': 0.26; 'statement': 0.26; 'classes': 0.26; 'received:209.85.161': 0.26; 'message- id:@mail.gmail.com': 0.28; 'bugs': 0.29; 'import': 0.29; 'order.': 0.29; 'class': 0.29; 'second': 0.30; 'module': 0.30; 'nested': 0.30; 'subject:Writing': 0.30; 'sun,': 0.30; 'to:addr:python- list': 0.33; 'handling': 0.33; 'list': 0.33; 'skip:v 20': 0.34; 'breaking': 0.35; 'using': 0.35; 'idea': 0.36; 'probably': 0.36; 'none': 0.37; 'received:google.com': 0.37; 'received:209.85': 0.37; 'another': 0.37; 'think': 0.38; 'url:python': 0.38; 'url:org': 0.38; 'but': 0.38; 'subject:: ': 0.38; 'should': 0.39; 'called': 0.39; 'received:209': 0.39; 'subject:with': 0.39; 'to:addr:python.org': 0.39; 'custom': 0.60; 'more': 0.60; 'your': 0.60; 'here:': 0.62; 'order': 0.62; 'removal': 0.65; 'container': 0.73; 'reverse': 0.73; 'guarantee.': 0.84; 'self:': 0.84; 'url:lib': 0.84; 'complexity': 0.93 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:from:date :message-id:subject:to:content-type:content-transfer-encoding; bh=8PZZilriSb510M2T5CMiCg5u/VccESAXqNtlriisGZY=; b=cKz8wTRjeTbSz0qR5G2CGXxy9VB2X+9YtSG1AZXSEvuDSQ1r5wtI8wyqewaCYmRKZz ko26fmEbmI29texqYGV0Ngt8U+Rr3F9PowCSNKv0sSFaQZ804uvLSTRmyXeiSldi54h6 BUe6BLIIFjSyPlL8b/VdLvJdRuLzbI65z3qRU= |
| DomainKey-Signature | a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type:content-transfer-encoding; b=VDSPAexaXEY8N1xz+A+Vndoc2BhZ66lON4q2cSlQT8pupogFz+GT5xaNz6hu9zYvWN hckYBBsyhw+t4I2+/l3o8Arc2vu2WJCDu7X57Yl+mnZDhQv5uKBmq+JXk0RGmsNp9lGP YlEBHtjEHSvjlPKKwUHYs5GY+hozW4g0Q+v4E= |
| MIME-Version | 1.0 |
| In-Reply-To | <5142852e-bab9-4e39-a49b-0229e900e97d@glegroupsg2000goo.googlegroups.com> |
| References | <5142852e-bab9-4e39-a49b-0229e900e97d@glegroupsg2000goo.googlegroups.com> |
| From | Ian Kelly <ian.g.kelly@gmail.com> |
| Date | Mon, 23 May 2011 11:31:22 -0600 |
| Subject | Re: Writing multiple files with with-context |
| To | python-list@python.org |
| Content-Type | text/plain; charset=ISO-8859-1 |
| Content-Transfer-Encoding | quoted-printable |
| 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.1981.1306171914.9059.python-list@python.org> (permalink) |
| Lines | 55 |
| NNTP-Posting-Host | 82.94.164.166 |
| X-Trace | 1306171914 news.xs4all.nl 49183 [::ffff:82.94.164.166]:54652 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.python:6083 |
Show key headers only | View raw
On Sun, May 22, 2011 at 8:48 PM, Shunichi Wakabayashi
<shunichi_wakabayashi@yahoo.co.jp> wrote:
> One idea is using contextlib.nested(),
>
> from contextlib import nested
>
> with nested(*[open('list_%d.txt' % i, 'w') for i in range(LIST_LEN)]) as fobjlist:
> for i in range(1000):
> fobjlist[random.randrange(LIST_LEN)].write(str(i)+"\n")
>
> with nested(*[open('dict_%s.txt' % k, 'w') for k in DICT_KEYS]) as fobjlist:
> fobjdict = dict(zip(DICT_KEYS, fobjlist)) #convert list to dict
> for i in range(1000):
> fobjdict[random.choice(DICT_KEYS)].write(str(i)+"\n")
>
> On Python2.x, this is OK. but 3.x warns that nested() is deprecated.
Not merely deprecated. It has already been removed in 3.2.
> Another idea is to make container classes having __exit__() myself.
>
> class MyList(list):
> def __enter__(self):
> return [ v.__enter__() for v in self ]
> def __exit__(self, exc_type, exc_value, traceback):
> ret = False
> for v in self:
> if v.__exit__(exc_type, exc_value, traceback):
> ret = True
> exc_type = exc_value = traceback = None
> return ret
This has a number of subtle bugs in it:
1) Each context manager's __exit__ method is not loaded before the
corresponding __enter__ method is invoked.
2) If the second context manager's __enter__ method raises an
exception, the first context manager's __exit__ method is never
called, breaking the with statement guarantee.
3) The __exit__ methods are called in the same order that the
__enter__ methods were called. Since they form a stack, they should
be called in the reverse order.
These highlight the complexity of handling context managers correctly,
which I think suggests that a custom implementation is probably a bad
idea.
> So, do you have another, more smart and pythonic way?
Copy the implementation of contextlib.nested to your own custom module
and use that. The last revision prior to its removal is here:
http://hg.python.org/cpython/file/45506be44514/Lib/contextlib.py
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
Writing multiple files with with-context Shunichi Wakabayashi <shunichi_wakabayashi@yahoo.co.jp> - 2011-05-22 19:48 -0700 Re: Writing multiple files with with-context Ian Kelly <ian.g.kelly@gmail.com> - 2011-05-23 11:31 -0600
csiph-web