Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Computers are not intelligent. They only think they are.


devel / comp.lang.python / Re: tail

SubjectAuthor
o Re: tailMarco Sulla

1
Re: tail

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

  copy mid

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

  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: Fri, 6 May 2022 21:21:36 +0200
Lines: 56
Message-ID: <mailman.328.1651864936.20749.python-list@python.org>
References: <CABbU2U-RaZxGqhbWYXCojbXgkH2kKsR0sRNdx3ZtE4_Ycq_GZw@mail.gmail.com>
<Ym8HP9Gt+OE9l+1V@cskk.homeip.net>
<CABbU2U8gaDgqDKqn-GrL6dOs556kVWcU+1OS65k9cfC_3H=FGg@mail.gmail.com>
<CABbU2U9SXQ0jANm7JrxOcsM50jkE86-YeMzEwOme_0vhpd9i9A@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
X-Trace: news.uni-berlin.de poHKbixuJo/+4l8cvr98MQyqOaW8TrSFdbPMUIuWZDZQ==
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=Sy6C11Vj;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.056
X-Spam-Evidence: '*H*': 0.89; '*S*': 0.00; 'def': 0.04;
'received:209.85.219': 0.09; 'import': 0.15; 'problem.': 0.15;
'"\\n"': 0.16; '&lt;': 0.16; 'char': 0.16; 'characters.': 0.16;
'from:name:marco sulla': 0.16; 'problem': 0.16; 'to:addr:python-
list': 0.20; 'maybe': 0.22; 'lines': 0.23; 'tried': 0.26;
'object': 0.26; 'else': 0.27; 'message-id:@mail.gmail.com': 0.32;
'work.': 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; 'read': 0.38;
'received:209': 0.39; 'text': 0.39; 'break': 0.39; 'lower': 0.62;
'skip:o 20': 0.63; 'skip:b 10': 0.63; 'extend': 0.64; 'skip:e 20':
0.67; 'little': 0.73; 'skip:f 20': 0.75; 'position': 0.81;
'mode.': 0.89
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;
bh=nIxfSsHgGuHd0cEbM1PKdtBC9Gc1ldY6bY98fF2xRkY=;
b=Sy6C11VjKbCPwkPdmdsqJf2iaRtDpvc7wanTKo6/tNYohjYWPyXc2wbw8vvlgLxTiL
iil1DfnMWDJ3Dx1VdWvad0FJcs7WmM6gfu1v9wqItgt1sSToD6vUUWHJj/hCvYTOyUxx
sZKW9qBh+Ec01W3899Js2MH8oRWhL2ZoI9Vyfnlirs/n48UYz1Q4TfleJJcqBFkId5rT
+Z9taOsu7GmnZUE23G9H/6YuxDPzeGgYdbYpkq4NowF053s4D1LpO5AxfiPr4knccupl
CdC2QIilHupNwhyPeDaTcT9KdBhsKacSG9qZo4zLfGHomk2Y+A09in2XXG6SeA0p7V7h
IP0Q==
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;
bh=nIxfSsHgGuHd0cEbM1PKdtBC9Gc1ldY6bY98fF2xRkY=;
b=B+xYJH4HF41P0B9YiTdVm+O/GDMpqdGWtGjPj0MRpKtUJJucDe/k40sdBXcoGX7gCh
rPjFTLxNLiSZnBnfRMHtJv0SvEsHn/vhflHVp9PAWaa1nr07e2BuNcVf7QVH4Vc2D+1m
NK1O/Jki/Ep8qpM77wjvQe/NqxTWHlXLcMNvQBmavf0Bzk9YRYwuRq9bWFyM2qu+4LWo
xJs5moxI2ie/xbqak8T8Pc2kQDDdtSGzejTx/FDOedtkydMpSxqbRgsiVeKK7JqLPPSE
XQ645e87RwzzplGHniJtlklQipKdP22Yy6Be/jFOzn332i8LQ/h/81JY1T9srr43NL9v
PQtA==
X-Gm-Message-State: AOAM533azoDBYJz1kwxD4IdlYBqsj2U6SD5ZJXlxMGqwynDUHfCeR/GO
U1WiEtUPKxVZ9h5bWU3rjE4g3IK0Iqnv83BKpIjLjS0TpAE=
X-Google-Smtp-Source: ABdhPJxPD5PPi4TzY/DPOiJ5y2otLguMBy53V3whhSJuYGbzjVFfzSD6d1aDrvtZXRcwXeiYi6bDISmAxega/oOiupY=
X-Received: by 2002:a5b:88f:0:b0:649:6b56:38b4 with SMTP id
e15-20020a5b088f000000b006496b5638b4mr3948381ybq.82.1651864933322; Fri, 06
May 2022 12:22:13 -0700 (PDT)
In-Reply-To: <CABbU2U8gaDgqDKqn-GrL6dOs556kVWcU+1OS65k9cfC_3H=FGg@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: <CABbU2U9SXQ0jANm7JrxOcsM50jkE86-YeMzEwOme_0vhpd9i9A@mail.gmail.com>
X-Mailman-Original-References: <CABbU2U-RaZxGqhbWYXCojbXgkH2kKsR0sRNdx3ZtE4_Ycq_GZw@mail.gmail.com>
<Ym8HP9Gt+OE9l+1V@cskk.homeip.net>
<CABbU2U8gaDgqDKqn-GrL6dOs556kVWcU+1OS65k9cfC_3H=FGg@mail.gmail.com>
 by: Marco Sulla - Fri, 6 May 2022 19:21 UTC

I have a little problem.

I tried to extend the tail function, so it can read lines from the bottom
of a file object opened in text mode.

The problem is it does not work. It gets a starting position that is lower
than the expected by 3 characters. So the first line is read only for 2
chars, and the last line is missing.

import os

_lf = "\n"
_cr = "\r"
_lf_ord = ord(_lf)

def tail(f, n=10, chunk_size=100):
n_chunk_size = n * chunk_size
pos = os.stat(f.fileno()).st_size
chunk_line_pos = -1
lines_not_found = n
binary_mode = "b" in f.mode
lf = _lf_ord if binary_mode else _lf

while pos != 0:
pos -= n_chunk_size

if pos < 0:
pos = 0

f.seek(pos)
chars = f.read(n_chunk_size)

for i, char in enumerate(reversed(chars)):
if char == lf:
lines_not_found -= 1

if lines_not_found == 0:
chunk_line_pos = len(chars) - i - 1
print(chunk_line_pos, i)
break

if lines_not_found == 0:
break

line_pos = pos + chunk_line_pos + 1

f.seek(line_pos)

res = b"" if binary_mode else ""

for i in range(n):
res += f.readline()

return res

Maybe the problem is 1 char != 1 byte?

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor