Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.078 X-Spam-Evidence: '*H*': 0.84; '*S*': 0.00; 'overwrite': 0.09; 'repeated': 0.09; 'subject:skip:f 10': 0.09; 'random': 0.14; '(without': 0.16; 'linefeed': 0.16; 'losing': 0.16; 'operation.': 0.16; 'storing': 0.16; 'truncating': 0.16; 'wrote:': 0.18; 'file,': 0.19; 'result.': 0.19; 'header:User-Agent:1': 0.23; 'file.': 0.24; 'right.': 0.26; 'second': 0.26; 'header:In-Reply- To:1': 0.27; 'am,': 0.29; "doesn't": 0.30; 'especially': 0.30; 'lines': 0.31; 'page.': 0.31; 'once,': 0.31; 'quotes': 0.31; 'file': 0.32; 'there.': 0.32; 'skip:m 30': 0.32; 'another': 0.32; 'quite': 0.32; 'could': 0.34; 'subject:with': 0.35; 'done': 0.36; 'doing': 0.36; 'two': 0.37; 'being': 0.38; 'bringing': 0.38; 'to:addr:python-list': 0.38; 'that,': 0.38; 'to:addr:python.org': 0.39; 'remove': 0.60; 'removing': 0.60; 'new': 0.61; 'first': 0.61; 'more': 0.64; 'pays': 0.65; 'line,': 0.68; 'received:74.208': 0.68; 'reverse': 0.68; 'risk': 0.72; '8bit%:27': 0.74; '8bit%:24': 0.84; 'calls,': 0.84; 'end.': 0.84 Date: Tue, 05 May 2015 13:10:39 -0400 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Step further with filebasedMessages References: <87oalzh0d5.fsf@Equus.decebal.nl> <877fsngi7i.fsf@Equus.decebal.nl> In-Reply-To: <877fsngi7i.fsf@Equus.decebal.nl> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit X-Provags-ID: V03:K0:oxpJNCHvRIBVJmoWz4yLjyA6apgzuIJ/hU+CZTE7rU3YV9hY7R5 XkYpd5Eh1VV/uQWhDU5oNIVw4br7a882yaOFh9M8X6dlDZSxIMTM2dS0CH8NRKd0rMkUi3t 5M+Wf0qfMHB76hmGp7EfH0rSN5eo3Snnx8jsVgs1cA3dSn7jwXtzK3gVaBVVPgRyHRukVbX 1kiO4j0P+L71R0YxCOQXw== X-UI-Out-Filterresults: notjunk:1; X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ 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: 29 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1430845859 news.xs4all.nl 2886 [2001:888:2000:d::a6]:46264 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:89978 On 05/05/2015 11:25 AM, Cecil Westerhof wrote: > > I have a file with quotes and a file with tips. I want to place random > messages from those two (without them being repeated to soon) on my > Twitter page. This I do with ‘get_random_message’. I also want to put > the first message of another file and remove it from the file. For > this I use ‘dequeue_message’. > Removing lines from the start of a file is an n-squared operation. Sometiomes it pays to reverse the file once, and just remove from the end. Truncating a file doesn't require the whole thing to be rewritten, nor risk losing the file if the make-new-file-rename-delete-old isn't quite done right. Alternatively, you could overwrite the line, or more especially the linefeed before it. Then you always do two readline() calls, using the second one's result. Various other games might include storing an offset at the begin of file, so you start by reading that, doing a seek to the place you want, and then reading the new line from there. Not recommending any of these, just bringing up alternatives. -- DaveA