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


Groups > comp.lang.python > #58836

Re: chunking a long string?

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <roy@panix.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; 'url:pipermail': 0.05; '"""': 0.07; 'string': 0.09; 'chunk': 0.09; 'http': 0.09; 'inserted': 0.09; 'iterate': 0.09; 'subject:string': 0.09; 'cc:addr:python-list': 0.11; 'django': 0.11; 'def': 0.12; 'question.': 0.14; '12:59': 0.16; 'cc:name:python list': 0.16; 'chunks': 0.16; 'get,': 0.16; 'itertools': 0.16; 'manageable': 0.16; 'nick': 0.16; 'received:166.84': 0.16; 'received:166.84.1': 0.16; 'received:166.84.1.89': 0.16; 'received:24.136': 0.16; 'received:mailbackend.panix.com': 0.16; 'received:panix.com': 0.16; 'roy': 0.16; 'url:312443': 0.16; 'url:how-do-you-split-a -list-into-evenly-sized-chunks-in-python': 0.16; 'wrote:': 0.18; 'all,': 0.19; 'received:166': 0.19; 'previously': 0.22; 'proposed': 0.22; 'cc:addr:python.org': 0.22; '---': 0.24; 'cc:2**0': 0.24; 'solutions.': 0.26; 'this:': 0.26; 'header:In- Reply-To:1': 0.27; 'received:24': 0.27; 'testing': 0.29; 'url:bugs': 0.29; "i'm": 0.30; 'requests': 0.31; 'serve': 0.31; 'file': 0.32; 'url:python': 0.33; 'skip:_ 10': 0.34; 'common': 0.35; 'tool': 0.35; 'but': 0.35; 'really': 0.36; 'view,': 0.36; 'yield': 0.36; 'charset:us-ascii': 0.36; 'subject:?': 0.36; 'url:org': 0.36; 'nov': 0.38; 'needed': 0.38; 'pm,': 0.38; 'does': 0.39; 'url:mail': 0.40; 'skip:u 10': 0.60; 'up,': 0.60; 'back': 0.62; 'header:Message-Id:1': 0.63; 'due': 0.66; 'smith': 0.68; 'email addr:panix.com': 0.84; 'subject:long': 0.84; '2013,': 0.91
Subject Re: chunking a long string?
Mime-Version 1.0 (Apple Message framework v1283)
Content-Type text/plain; charset=us-ascii
From Roy Smith <roy@panix.com>
In-Reply-To <1455bf3b7e3f47808646c945ff7e6b46@BY2PR06MB233.namprd06.prod.outlook.com>
Date Fri, 8 Nov 2013 13:24:40 -0500
Content-Transfer-Encoding quoted-printable
References <9258483E-76BD-4D96-8C46-7262A95E3ED4@panix.com> <1455bf3b7e3f47808646c945ff7e6b46@BY2PR06MB233.namprd06.prod.outlook.com>
To Nick Cash <nick.cash@npcinternational.com>
X-Mailer Apple Mail (2.1283)
Cc Python List <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 <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2241.1383935082.18130.python-list@python.org> (permalink)
Lines 57
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1383935082 news.xs4all.nl 15990 [2001:888:2000:d::a6]:54730
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:58836

Show key headers only | View raw


Oh my, it turns out I don't really need to do this after all, due to previously undiscovered uber-coolness in the tools I'm using!

My use case is that from inside of a Django view, I needed to retrieve a large file via a HTTP GET, and serve that back up, with some time delays inserted into the data stream.  Turns out, requests (uber-cool tool #1) provides a way to iterate over the content of a GET, and Django (uber-cool tool #2) provides a way to build a HttpResponse from the data in an iterator.  Epiphany!  I ended up with (essentially) this:

def stream_slow(request, song_id):
    """Streams a song, but does it extra slowly, for client testing                                                                             
    purposes.                                                                                                                                   
                                                                                                                                                                                                                                                                                                
    """
    def _slow_stream(r, chunk_size):
        for chunk in r.iter_content(chunk_size):
            yield chunk
            time.sleep(0.1)

    url = get_url(song_id)
    response = requests.get(url, stream=True)
    return HttpResponse(_slow_stream(response, 1024))




On Nov 8, 2013, at 12:59 PM, Nick Cash wrote:

>> I have a long string (several Mbytes).  I want to iterate over it in manageable chunks
> 
> This is a weirdly common question. See http://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks-in-python for several solutions.
> 
> It's been proposed to be added to itertools before, but rejected: https://mail.python.org/pipermail/python-ideas/2012-July/015671.html and http://bugs.python.org/issue13095 
> 
> - Nick Cash 
> 


---
Roy Smith
roy@panix.com


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


Thread

Re: chunking a long string? Roy Smith <roy@panix.com> - 2013-11-08 13:24 -0500

csiph-web