Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?) Date: Tue, 22 Mar 2016 22:15:40 +1100 Lines: 20 Message-ID: References: <56e44258$0$1598$c3e8da3$5496439d@news.astraweb.com> <8737rvxs89.fsf@elektro.pacujo.net> <56e7483d$0$1608$c3e8da3$5496439d@news.astraweb.com> <56f09973$0$1601$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de fnwn3Da4/009CPy86Q+7JAW/IxMEijK8jbiSAzCYQgTA== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.007 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'cc:addr:python-list': 0.09; '22,': 0.09; 'raised.': 0.09; 'returns,': 0.09; 'subject:which': 0.09; 'exception': 0.13; 'def': 0.13; 'file,': 0.15; '10:05': 0.16; '2016': 0.16; 'f.read()': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'in- memory': 0.16; 'interest,': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:?)': 0.16; 'wrote:': 0.16; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'header:In-Reply- To:1': 0.24; 'script': 0.25; 'message-id:@mail.gmail.com': 0.27; 'turns': 0.27; 'function': 0.28; 'block,': 0.29; "i'm": 0.30; 'problem': 0.33; 'tue,': 0.34; 'file': 0.34; 'received:google.com': 0.35; 'but': 0.36; 'received:209.85': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'thanks': 0.37; 'received:209': 0.38; 'means': 0.39; 'data': 0.39; 'takes': 0.39; 'skip:e 20': 0.39; 'subject:The': 0.61; 'mar': 0.65; 'guaranteed': 0.67; "'with'": 0.84; '**kw)': 0.84; '**kw):': 0.84; 'chrisa': 0.84; 'to:none': 0.91 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:cc; bh=CN8VwGFSU1xbLw94YF5dYP3MIcUVfwV4MQbad3JyY5Q=; b=C3oszfY5kSUJcTwyHUtNYS6rmzjJgRwA0QthdpUXEt2kj/EXfwyz9uYDDzEBWjbH0r oSf3yclogllUXuYRmSpKDHvod0qh6SSbqNrIhuox/3n5pqE7EXt2ftKaHn/vp1XECMG3 rkuXo1+qEHZTRnMASxSR6VqJEuc6S1J6M3vevwp3PAA3jS5vM4LlGWfhKR4CRgBOTiba d98OU6i4xBmuo5/AsjN09qHlWrTovNsTXj5qHxqyEsnpGrww1qdXeMHG8hARpjGGFUjH Aan9BL2IrLJh4AsFXkZ2koROmJm1Nj3wgZSFCgluEzRwkH7hjrIT41IRlWh8SAGa1pMk YHAA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:cc; bh=CN8VwGFSU1xbLw94YF5dYP3MIcUVfwV4MQbad3JyY5Q=; b=CohCcFc56OTpjnp8P4XIqeabC6/AgDiZr10zOojp95H1fb6H1gY3rSVbNdBS1z0FMy apIYbTaZzJEflv6d8yCDST6mDd8K67iUq5gXMLAJws4zwgCHY9tvuzs+N+Te8ldFRz5U blPO/S9+QPMQVkgF1GxqvSAO2GYKZDo/hp8Qrzi6d6qmnSKs+j9RRg34z8XWkpLkB5kN lGL47/5UzbFIBXxBEAlCoQDGDgfHWBa6qs6bNCAlR1TgItcuZflNPv3bfXGAl7smeLFl Afp9Vte6kSBJRXdWA57JP3D0V3lVQb92hvdJonssYeWfSvDACznwRFJ5/p9FKtJcbZmJ ppKA== X-Gm-Message-State: AD7BkJIph5DQyaAwciDPvyLNxK3LxrtfOFE9mhZUtYQe/T0l4iE9YeLWk2AlX8Xcf8BmYEACFxs5qtf5QrKLrQ== X-Received: by 10.25.78.137 with SMTP id c131mr10510913lfb.21.1458645340988; Tue, 22 Mar 2016 04:15:40 -0700 (PDT) In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:105461 On Tue, Mar 22, 2016 at 10:05 PM, BartC wrote: > But out of interest, how would /you/ write a function that takes a file-spec > and turns it into an in-memory string? And what would its use look like? def read_file(fn, *a, **kw): with open(fn, *a, **kw) as f: return f.read() Usage: script = read_file(".bashrc") data = read_file("Ellalune_AlicePortrait.jpg", "rb") decoded = read_file("greek.srt", encoding="ISO-8859-7") If there's any problem with reading the file, an exception will be raised. Also, thanks to the 'with' block, I'm guaranteed that the file will have been closed before read_file() returns, which means I can immediately go and write to the file without a conflict. ChrisA