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


Groups > comp.lang.python > #32768

Re: accepting file path or file object?

Path csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!194.109.133.84.MISMATCH!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <andrea.crotti.0@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; 'else:': 0.03; 'elif': 0.04; 'context': 0.05; 'sys': 0.05; 'mode:': 0.07; 'subject:file': 0.07; 'python': 0.09; 'open(file,': 0.09; 'sys.stdout': 0.09; 'termination': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; '"-":': 0.16; 'contextlib': 0.16; 'echo': 0.16; 'gamma': 0.16; 'mode)': 0.16; 'subject:object': 0.16; 'to:addr:web.de': 0.16; 'yield': 0.17; 'thanks,': 0.18; 'import': 0.21; 'delta': 0.22; 'cc:2**0': 0.23; 'this:': 0.23; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'skip:@ 10': 0.27; 'message-id:@mail.gmail.com': 0.27; 'cat': 0.29; 'url:mailman': 0.29; 'maybe': 0.29; 'gets': 0.32; 'url:python': 0.32; 'file': 0.32; 'print': 0.32; 'url:listinfo': 0.32; 'handle': 0.33; 'problem': 0.33; 'received:google.com': 0.34; 'false': 0.35; 'sometimes': 0.35; 'subject:?': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'there': 0.35; 'but': 0.36; 'url:org': 0.36; 'closing': 0.36; 'received:209': 0.37; 'subject:: ': 0.38; 'header:Received:5': 0.40; 'url:mail': 0.40; 'otten': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=T08u5KvRGjHEjvcagAqeUimgbp8wmlpFvCoSEOQGcI8=; b=JC+2VoNN1lbyaRdGjuSd7eZ7nYd8UbrN8SdWuIOAu/UDxfde/GnZaZFTlJRqvgXO/K KS0W/UBIJZ8Nz6OXBJfrtamuxWyosWzKTbIKhlvu15/Ci4pnSzebfCU2T6tuYRNpUrOl Y8JWDEf+9TTgl3BL78U9iQmbFdbV/uQ+OQ+rH8vDGfoVddN7feTyIdM9dOYTRtB+oG9H foEwYtv4D9p0DGCkVHz+yPTCuEOpqs9vDo3LJv7R9inw4VPWGzg6xPsjJ0/jl5t50H9N G7TrfUKmB4UO7WpIPoyyiaASF2D9nSR34/QlayZ0qjF2MdpZBo42eyaZ1ojr2okoBtfU yGtg==
MIME-Version 1.0
In-Reply-To <k7892k$g9d$1@ger.gmane.org>
References <CAF_E5JYQd5aPnjtpjiHVBpsro-85pV-4zQA9oaNdPUkhOkAZ9g@mail.gmail.com> <k7892k$g9d$1@ger.gmane.org>
Date Mon, 5 Nov 2012 13:16:47 +0000
Subject Re: accepting file path or file object?
From andrea crotti <andrea.crotti.0@gmail.com>
To Peter Otten <__peter__@web.de>
Content-Type text/plain; charset=ISO-8859-1
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
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.3281.1352121410.27098.python-list@python.org> (permalink)
Lines 51
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1352121410 news.xs4all.nl 6952 [2001:888:2000:d::a6]:42336
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:32768

Show key headers only | View raw


2012/11/5 Peter Otten <__peter__@web.de>:
> I sometimes do something like this:
>
> $ cat xopen.py
> import re
> import sys
> from contextlib import contextmanager
>
> @contextmanager
> def xopen(file=None, mode="r"):
>     if hasattr(file, "read"):
>         yield file
>     elif file == "-":
>         if "w" in mode:
>             yield sys.stdout
>         else:
>             yield sys.stdin
>     else:
>         with open(file, mode) as f:
>             yield f
>
> def grep(stream, regex):
>     search = re.compile(regex).search
>     return any(search(line) for line in stream)
>
> if len(sys.argv) == 1:
>     print grep(["alpha", "beta", "gamma"], "gamma")
> else:
>     with xopen(sys.argv[1]) as f:
>         print grep(f, sys.argv[2])
> $ python xopen.py
> True
> $ echo 'alpha beta gamma' | python xopen.py - gamma
> True
> $ echo 'alpha beta gamma' | python xopen.py - delta
> False
> $ python xopen.py xopen.py context
> True
> $ python xopen.py xopen.py gamma
> True
> $ python xopen.py xopen.py delta
> False
> $
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list

That's nice thanks, there is still the problem of closing the file
handle but that's maybe not so important if it gets closed at
termination anyway..

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: accepting file path or file object? andrea crotti <andrea.crotti.0@gmail.com> - 2012-11-05 13:16 +0000

csiph-web