Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Adapt. Enjoy. Survive.


devel / comp.lang.python / Re: tail

SubjectAuthor
o Re: tailMarco Sulla

1
Re: tail

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: Marco.Su...@gmail.com (Marco Sulla)
Newsgroups: comp.lang.python
Subject: Re: tail
Date: Sat, 23 Apr 2022 23:12:29 +0200
Lines: 56
Message-ID: <mailman.220.1650748388.20749.python-list@python.org>
References: <CABbU2U98YKdcnJkDPfzE3Pqso+6LL72usB8hrSBVR0WbhauRoQ@mail.gmail.com>
<CAPTjJmr3AiCyvxXt=-nqNLrJfyQHmG=pvSsM7nU_XxhSe94zgA@mail.gmail.com>
<CABbU2U8TAvy0zMhUcNtTD0=WpQ6oNYEeZQuKDjnxhG85FVriDg@mail.gmail.com>
<CAPTjJmqnfoPjoNT2CNsrkMVxkzAMHHXHj-G3DuGrJ21SDRNsPA@mail.gmail.com>
<CABbU2U_sWyEmBXf0Psudwc-FLeRYqLX=B4x-_9TV0qc5ZVt3Bg@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
X-Trace: news.uni-berlin.de KByf7uGMFc0KxubAk8xSlwECxd4wINUWfqnnBFX2OUog==
Return-Path: <elbarbun@gmail.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=gmail.com header.i=@gmail.com header.b=kKbX8fQQ;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.004
X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'def': 0.04; '2022': 0.05;
'31,': 0.05; '"""': 0.09; 'angelico': 0.09; 'byte': 0.09; 'cc:addr
:python-list': 0.09; 'else:': 0.09; 'received:209.85.219': 0.09;
'&gt;': 0.14; '&lt;': 0.16; 'cc:name:python list': 0.16; 'char':
0.16; 'from:name:marco sulla': 0.16; 'general.': 0.16;
'splitting': 0.16; 'wrote:': 0.16; 'cc:addr:python.org': 0.20;
'sat,': 0.22; 'lines': 0.23; 'depends': 0.25; 'cc:2**0': 0.25;
'else': 0.27; 'function': 0.27; 'chris': 0.28; 'it,': 0.29;
'whole': 0.30; 'think': 0.32; 'guess': 0.32; 'message-
id:@mail.gmail.com': 0.32; 'but': 0.32; 'same': 0.34; 'header:In-
Reply-To:1': 0.34; 'received:google.com': 0.34;
'from:addr:gmail.com': 0.35; 'received:209.85': 0.37; 'file':
0.38; 'received:209': 0.39; 'quite': 0.39; 'break': 0.39; 'still':
0.40; 'likely': 0.61; 'skip:o 10': 0.61; 'simply': 0.63; 'skip:b
10': 0.63; 'well': 0.65; 'less': 0.65; 'per': 0.68; '2016': 0.70;
'backwards': 0.84; 'lose': 0.84; 'sulla': 0.84
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112;
h=mime-version:references:in-reply-to:from:date:message-id:subject:to
:cc; bh=cPFqkE6GnmzYYqUzQdRAGsyJjqjPdKN8yNto1LHBxaY=;
b=kKbX8fQQarJXeKaEPWXaTJ/i6QPSJ91MgyJKKWkW0H//3SBDubXyWGl7BI+Rc0B2Ry
KtQaa92FxtUMp7oBRm8f4V5Zs4qL/ZAz8OEHNo+Z8nBoiDjBkLLnaMOICoABllqFGI/O
8xYYsWiKv7Exi/Uc/C9gJx3Nc8CzKQiLZJnbPvX/lmOAhUiSW5cp9ms5e6zVA9s83pM1
lxoJJSP/8q5WIYj9y3i1VxGQl4X1mlkb3W1j/n9AHa3af3sqoLJO7dg2GaPdOGlq/2dE
2YP7nldbmfH3SUqCEa9xIu4zpThC+IW1Hh6Z1lcZirgAD8iWqBssKKlo4l3X5XWUVwpa
qanA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20210112;
h=x-gm-message-state:mime-version:references:in-reply-to:from:date
:message-id:subject:to:cc;
bh=cPFqkE6GnmzYYqUzQdRAGsyJjqjPdKN8yNto1LHBxaY=;
b=G9VYdGHlPKoeXD+ENf2VfxDjL1cElKysANYuR66SosFoB5yCCv4Fj0lBZt4+WodcRC
TnshOcSbtDKjnWVlS4q+emuNvg8osJEE6Z7aAGv9n7sLNo4P73T8HRd1AARgETVIsejP
U2DD412VGS56z7ifF4i/5CWZKHhdUa0Xs5Yy5i2bntSJmCwfw7myWy6xyJpjXlZEyQHp
SmBvUm91BbvSb7VgcA00ENaGq415RACIMxeeaJeiqWvSmLliM5M0is69SOra0S3+Ao1S
0mnRR3gtM+WIWWvZb0A5QaVLWfU+rmoZ1Dbe4UCOy+vc9bi5BNnH8XBzN8H9ykyGsKWH
jH+A==
X-Gm-Message-State: AOAM532LQWwwdH5mDCViHlWqGuBYLSLAYFCeVS6lw/tdd7BjbsCleSc+
xaAPE5SmVw5i1R4ONiW3AmJP/wJMuQldIQCXQEgUX/dnoH4=
X-Google-Smtp-Source: ABdhPJw6J7KqeLEAwPqHF548SGdLPm3lN7D+oyuDxFvaeHxhWLdOxJ8rJekCf4AZy3Bo5Z5b/sIYwAuVXgtUNAFTmC0=
X-Received: by 2002:a5b:8c8:0:b0:641:e8de:a6f4 with SMTP id
w8-20020a5b08c8000000b00641e8dea6f4mr10014874ybq.533.1650748385998; Sat, 23
Apr 2022 14:13:05 -0700 (PDT)
In-Reply-To: <CAPTjJmqnfoPjoNT2CNsrkMVxkzAMHHXHj-G3DuGrJ21SDRNsPA@mail.gmail.com>
X-Content-Filtered-By: Mailman/MimeDel 2.1.39
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: <CABbU2U_sWyEmBXf0Psudwc-FLeRYqLX=B4x-_9TV0qc5ZVt3Bg@mail.gmail.com>
X-Mailman-Original-References: <CABbU2U98YKdcnJkDPfzE3Pqso+6LL72usB8hrSBVR0WbhauRoQ@mail.gmail.com>
<CAPTjJmr3AiCyvxXt=-nqNLrJfyQHmG=pvSsM7nU_XxhSe94zgA@mail.gmail.com>
<CABbU2U8TAvy0zMhUcNtTD0=WpQ6oNYEeZQuKDjnxhG85FVriDg@mail.gmail.com>
<CAPTjJmqnfoPjoNT2CNsrkMVxkzAMHHXHj-G3DuGrJ21SDRNsPA@mail.gmail.com>
 by: Marco Sulla - Sat, 23 Apr 2022 21:12 UTC

On Sat, 23 Apr 2022 at 23:00, Chris Angelico <rosuav@gmail.com> wrote:
> > > This is quite inefficient in general.
> >
> > Why inefficient? I think that readlines() will be much slower, not
> > only more time consuming.
>
> It depends on which is more costly: reading the whole file (cost
> depends on size of file) or reading chunks and splitting into lines
> (cost depends on how well you guess at chunk size). If the lines are
> all *precisely* the same number of bytes each, you can pick a chunk
> size and step backwards with near-perfect efficiency (it's still
> likely to be less efficient than reading a file forwards, on most file
> systems, but it'll be close); but if you have to guess, adjust, and
> keep going, then you lose efficiency there.

Emh, why chunks? My function simply reads byte per byte and compares it to
b"\n". When it find it, it stops and do a readline():

def tail(filepath):
"""
@author Marco Sulla
@date May 31, 2016
"""

try:
filepath.is_file
fp = str(filepath)
except AttributeError:
fp = filepath

with open(fp, "rb") as f:
size = os.stat(fp).st_size
start_pos = 0 if size - 1 < 0 else size - 1

if start_pos != 0:
f.seek(start_pos)
char = f.read(1)

if char == b"\n":
start_pos -= 1
f.seek(start_pos)

if start_pos == 0:
f.seek(start_pos)
else:
for pos in range(start_pos, -1, -1):
f.seek(pos)

char = f.read(1)

if char == b"\n":
break

return f.readline()

This is only for one line and in utf8, but it can be generalised.


devel / comp.lang.python / Re: tail

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor