Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'cache': 0.07; 'subject:file': 0.07; '(its': 0.09; 'http': 0.09; 'library?': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; '\xe2\x80\x94': 0.09; 'api': 0.11; 'python': 0.11; '(filename,': 0.16; 'access)': 0.16; 'cached': 0.16; 'caching': 0.16; 'exist.': 0.16; 'finney': 0.16; 'reason.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'recipe': 0.16; 'subject:URL': 0.16; 'timestamp': 0.16; 'url)': 0.16; 'url:example': 0.16; 'url:spam': 0.16; 'files.': 0.16; 'modification': 0.16; 'code.': 0.18; 'all,': 0.19; 'file,': 0.19; 'seems': 0.21; 'header:User- Agent:1': 0.23; 'library,': 0.24; 'earlier': 0.24; 'file.': 0.24; 'looks': 0.24; '2005': 0.26; 'header:X-Complaints-To:1': 0.27; 'raise': 0.29; "i'm": 0.30; 'code': 0.31; 'requests': 0.31; 'usually': 0.31; '"",': 0.31; 'object.': 0.31; 'file': 0.32; 'implemented': 0.33; 'older': 0.33; 'skip:d 20': 0.34; "i'd": 0.34; 'subject:from': 0.34; 'problem': 0.35; "can't": 0.35; 'except': 0.35; 'something': 0.35; 'case,': 0.35; 'but': 0.35; 'there': 0.35; 'corporation,': 0.36; 'done,': 0.36; 'object,': 0.36; 'similar': 0.36; 'url:org': 0.36; 'should': 0.36; 'changing': 0.37; 'error.': 0.37; 'wrong': 0.37; 'remote': 0.38; 'ben': 0.38; 'generic': 0.38; 'to:addr:python-list': 0.38; 'files': 0.38; 'anything': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'received:org': 0.40; 'how': 0.40; 'even': 0.60; 'read': 0.60; 'easy': 0.60; 'simply': 0.61; 'simple': 0.61; 'first': 0.61; 'kind': 0.63; 'detail.': 0.68; 'obvious': 0.74; 'hoping': 0.75; '8bit%:24': 0.84; 'lee,': 0.84; 'received:125': 0.84; 'canonical': 0.91; 'urls,': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Ben Finney Subject: Caching: Access a local file, but ensure it is up-to-date from a remote URL Date: Mon, 13 Oct 2014 17:36:47 +1100 Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Gmane-NNTP-Posting-Host: jigong.madmonks.org X-Public-Key-ID: 0xAC128405 X-Public-Key-Fingerprint: 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405 X-Public-Key-URL: http://www.benfinney.id.au/contact/bfinney-pubkey.asc X-Post-From: Ben Finney User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Cancel-Lock: sha1:cKCzuEjVy2k9VqRiTilvSZ4OtR4= X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 64 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1413182230 news.xs4all.nl 2934 [2001:888:2000:d::a6]:60928 X-Complaints-To: abuse@xs4all.nl X-Received-Body-CRC: 4230334512 X-Received-Bytes: 6489 Path: csiph.com!usenet.pasdenom.info!bete-des-vosges.org!feed.ac-versailles.fr!feeder1-2.proxad.net!proxad.net!feeder1-1.proxad.net!hq-usenetpeers.eweka.nl!hq-usenetpeers.eweka.nl!bcyclone04.am1.xlned.com!bcyclone04.am1.xlned.com!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Xref: csiph.com comp.lang.python:79034 Howdy all, I'm hoping that the problem I currently have is one already solved, either in the Python standard library, or with some well-tested obvious code. A program I'm working on needs to access a set of files locally; they're just normal files. But those files are local cached copies of documents available at remote URLs — each file has a canonical URL for that file's content. I'd like an API for ‘get_file_from_cache’ that looks something like:: file_urls = { "foo.txt": "http://example.org/spam/", "bar.data": "https://example.net/beans/flonk.xml", } for (filename, url) in file_urls.items(): infile = get_file_from_cache(filename, canonical=url) do_stuff_with(infile.read()) * If the local file's modification timestamp is not significantly earlier than the Last-Modified timestamp for the document at the corresponding URL, ‘get_file_from_cache’ just returns the file object without changing the file. * The local file might be out of date (its modification timestamp may be significantly older than the Last-Modified timestamp from the corresponding URL). In that case, ‘get_file_from_cache’ should first read the document's contents into the file, then return the file object. * The local file may not yet exist. In that case, ‘get_file_from_cache’ should first read the document content from the corresponding URL, create the local file, and then return the file object. * The remote URL may not be available for some reason. In that case, ‘get_file_from_cache’ should simply return the file object, or if that can't be done, raise an error. So this is something similar to an HTTP object cache. Except where those are usually URL-focussed with the local files a hidden implementation detail, I want an API that focusses on the local files, with the remote requests a hidden implementation detail. Does anything like this exist in the Python library, or as simple code using it? With or without the specifics of HTTP and URLs, is there some generic caching recipe already implemented with the standard library? This local file cache (ignoring the spcifics of URLs and network access) seems like exactly the kind of thing that is easy to get wrong in countless ways, and so should have a single obvious implementation available. Am I in luck? What do you advise? -- \ “If consumers even know there's a DRM, what it is, and how it | `\ works, we've already failed.” —Peter Lee, Disney corporation, | _o__) 2005 | Ben Finney