Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

"It runs like _x, where _x is something unsavory" -- Prof. Romas Aleliunas, CS 435


devel / comp.lang.python / Re: tail

SubjectAuthor
o Re: tailCameron Simpson

1
Re: tail

<mailman.285.1651443531.20749.python-list@python.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18095&group=comp.lang.python#18095

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: cs...@cskk.id.au (Cameron Simpson)
Newsgroups: comp.lang.python
Subject: Re: tail
Date: Mon, 2 May 2022 08:18:39 +1000
Lines: 66
Message-ID: <mailman.285.1651443531.20749.python-list@python.org>
References: <CABbU2U-RaZxGqhbWYXCojbXgkH2kKsR0sRNdx3ZtE4_Ycq_GZw@mail.gmail.com>
<Ym8HP9Gt+OE9l+1V@cskk.homeip.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
X-Trace: news.uni-berlin.de T100HoDUGpvv5DqohgMelAD0cDwpxtpwvSjURykD5jdg==
Return-Path: <cameron@cskk.id.au>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=none reason="no signature";
dkim-adsp=none (unprotected policy); dkim-atps=neutral
X-Spam-Status: OK 0.006
X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'this:': 0.03; 'byte':
0.09; 'else:': 0.09; 'cheers,': 0.11; '-1:': 0.16; 'cameron':
0.16; 'from:addr:cs': 0.16; 'from:addr:cskk.id.au': 0.16;
'from:name:cameron simpson': 0.16; 'mentioned,': 0.16; 'message-
id:@cskk.homeip.net': 0.16; 'newline': 0.16; 'received:13.237':
0.16; 'received:13.237.201': 0.16; 'received:13.237.201.189':
0.16; 'received:cskk.id.au': 0.16; 'received:id.au': 0.16;
'received:mail.cskk.id.au': 0.16; 'simpson': 0.16; 'wrote:': 0.16;
'instead': 0.17; "can't": 0.17; 'to:addr:python-list': 0.20;
'maybe': 0.22; 'code': 0.23; 'run': 0.23; "i'd": 0.24; 'anything':
0.25; 'skip:- 10': 0.25; 'seems': 0.26; 'tried': 0.26; 'else':
0.27; 'chris': 0.28; 'expect': 0.28; 'header:User-Agent:1': 0.30;
'wondering': 0.31; 'think': 0.32; 'unless': 0.32; 'but': 0.32;
'100': 0.33; 'header:In-Reply-To:1': 0.34; 'received:au': 0.35;
'special': 0.37; 'file': 0.38; 'could': 0.38; 'put': 0.38;
'single': 0.39; 'text': 0.39; 'break': 0.39; 'something': 0.40;
'want': 0.40; 'skip:o 20': 0.63; 'simply': 0.63; 'range': 0.64;
'received:13': 0.64; 'received:userid': 0.66; 'position': 0.81;
'inclined': 0.84; 'positions:': 0.84; 'sulla': 0.84; 'body.':
0.91; 'positions': 0.96
Mail-Followup-To: python-list@python.org
Content-Disposition: inline
In-Reply-To: <CABbU2U-RaZxGqhbWYXCojbXgkH2kKsR0sRNdx3ZtE4_Ycq_GZw@mail.gmail.com>
User-Agent: Mutt/2.2.1 (2022-02-19)
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
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: <https://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>
X-Mailman-Original-Message-ID: <Ym8HP9Gt+OE9l+1V@cskk.homeip.net>
X-Mailman-Original-References: <CABbU2U-RaZxGqhbWYXCojbXgkH2kKsR0sRNdx3ZtE4_Ycq_GZw@mail.gmail.com>
 by: Cameron Simpson - Sun, 1 May 2022 22:18 UTC

On 01May2022 18:55, Marco Sulla <Marco.Sulla.Python@gmail.com> wrote:
>Something like this is OK?
[...]
>def tail(f):
> chunk_size = 100
> size = os.stat(f.fileno()).st_size

I think you want os.fstat().

> positions = iter(range(size, -1, -chunk_size))
> next(positions)

I was wondering about the iter, but this makes sense. Alternatively you
could put a range check in the for-loop.

> chunk_line_pos = -1
> pos = 0
>
> for pos in positions:
> f.seek(pos)
> chars = f.read(chunk_size)
> chunk_line_pos = chars.rfind(b"\n")
>
> if chunk_line_pos != -1:
> break

Normal text file _end_ in a newline. I'd expect this to stop immediately
at the end of the file.

> if chunk_line_pos == -1:
> nbytes = pos
> pos = 0
> f.seek(pos)
> chars = f.read(nbytes)
> chunk_line_pos = chars.rfind(b"\n")

I presume this is because unless you're very lucky, 0 will not be a
position in the range(). I'd be inclined to avoid duplicating this code
and special case and instead maybe make the range unbounded and do
something like this:

if pos < 0:
pos = 0
... seek/read/etc ...
if pos == 0:
break

around the for-loop body.

> if chunk_line_pos == -1:
> line_pos = pos
> else:
> line_pos = pos + chunk_line_pos + 1
> f.seek(line_pos)
> return f.readline()
>
>This is simply for one line and for utf8.

And anything else where a newline is just an ASCII newline byte (10) and
can't be mistaken otherwise. So also ASCII and all the ISO8859-x single
byte encodings. But as Chris has mentioned, not for other encodings.

Seems sane. I haven't tried to run it.

Cheers,
Cameron Simpson <cs@cskk.id.au>

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor