Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

You! What PLANET is this! -- McCoy, "The City on the Edge of Forever", stardate 3134.0


devel / comp.lang.python / Re: Waht do you think about my repeated_timer class

SubjectAuthor
* Re: Waht do you think about my repeated_timer classChris Angelico
`* Re: Waht do you think about my repeated_timer classCecil Westerhof
 +* Re: Waht do you think about my repeated_timer classCecil Westerhof
 |+* Re: Waht do you think about my repeated_timer classCameron Simpson
 ||`- Re: Waht do you think about my repeated_timer classCecil Westerhof
 |+* Re: Waht do you think about my repeated_timer class2QdxY4RzWzUUiLuE
 ||`* Re: Waht do you think about my repeated_timer classCecil Westerhof
 || `- Re: Waht do you think about my repeated_timer class2QdxY4RzWzUUiLuE
 |+- Re: Waht do you think about my repeated_timer classChris Angelico
 |+- Re: Waht do you think about my repeated_timer class2QdxY4RzWzUUiLuE
 |`- Re: Waht do you think about my repeated_timer classChris Angelico
 `* Re: Waht do you think about my repeated_timer classChris Angelico
  `* Re: Waht do you think about my repeated_timer classCecil Westerhof
   +* Re: Waht do you think about my repeated_timer classChris Angelico
   |`- Re: Waht do you think about my repeated_timer classCecil Westerhof
   `* Re: Waht do you think about my repeated_timer classBarry
    `* Re: Waht do you think about my repeated_timer classCecil Westerhof
     `- Re: Waht do you think about my repeated_timer classMartin Di Paola

1
Re: Waht do you think about my repeated_timer class

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: ros...@gmail.com (Chris Angelico)
Newsgroups: comp.lang.python
Subject: Re: Waht do you think about my repeated_timer class
Date: Thu, 3 Feb 2022 09:58:46 +1100
Lines: 71
Message-ID: <mailman.23.1643842739.2976.python-list@python.org>
References: <CABbU2U8fCh-qR5QdyFwgBAmQEi+6nzsbNK=2Jjk=QZxHf=h3CQ@mail.gmail.com>
<177517EB-2AA2-47AA-B974-100188449961@barrys-emacs.org>
<CAPTjJmpqQW-T3OHAJi_n1BkzJCYE9VuRCv+n-v=T6A1_3o+Axw@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Trace: news.uni-berlin.de bC2f8qffUjtUvRGQY2wp0QjIkbvK0wVHiNnR7HSAshKg==
Return-Path: <rosuav@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=fYEY1o5p;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.015
X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'def': 0.04; '2022': 0.05;
'thread': 0.05; 'reason:': 0.07; 'depend': 0.09;
'didn\xe2\x80\x99t': 0.09; 'example:': 0.09; 'fact,': 0.09;
'subject:class': 0.09; '"go': 0.16; '"not': 0.16; '(it': 0.16;
'2022,': 0.16; 'aside': 0.16; 'barry': 0.16; 'chrisa': 0.16;
'classic': 0.16; 'cleaned': 0.16; 'closed,': 0.16;
'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16;
'interpreter': 0.16; 'objects.': 0.16; 'py2': 0.16; 'python3':
0.16; 'pythonic': 0.16; 'reopen': 0.16; 'surprising': 0.16;
'wrote:': 0.16; 'python': 0.16; 'code.': 0.17; 'feb': 0.17;
"can't": 0.17; 'calls': 0.19; 'thu,': 0.19; 'to:addr:python-list':
0.20; 'creates': 0.22; 'code': 0.23; 'run': 0.23; 'object': 0.26;
"isn't": 0.27; 'function': 0.27; 'done': 0.28; 'sense': 0.28;
'wrong': 0.28; 'collected': 0.32; 'here,': 0.32; 'objects': 0.32;
'said,': 0.32; 'transform': 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; 'yes,': 0.35; 'from:addr:gmail.com':
0.35; 'possibly': 0.36; 'main': 0.37; 'really': 0.37; "it's":
0.37; 'received:209.85': 0.37; 'file': 0.38; 'way': 0.38; 'could':
0.38; 'received:209': 0.39; 'quite': 0.39; 'use': 0.39; 'block':
0.39; 'still': 0.40; 'beginning': 0.40; 'something': 0.40; 'want':
0.40; 'reference': 0.60; 'method': 0.61; "there's": 0.61; 'mode':
0.62; 'true': 0.63; 'skip:r 20': 0.64; 'your': 0.64; 'spam': 0.65;
'required': 0.65; 'right': 0.68; 'area,': 0.69; 'perfectly': 0.69;
'skip:w 20': 0.69; 'del': 0.70; 'longer': 0.71; 'non': 0.75;
'guarantee': 0.76; 'highly': 0.78; 'up,': 0.84; 'subject:you':
0.84; 'absolutely': 0.84; 'earlier)': 0.84; 'garbage': 0.84;
'guaranteeing': 0.84; 'subject:think': 0.84; 'sulla': 0.84;
'reliable.': 0.91; 'safely': 0.91; 'time?': 0.91; 'will,': 0.91
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
:content-transfer-encoding;
bh=IlHmTqL3OOh630DOhy2+RmVBeEeofRPrYHKW36Dfh2A=;
b=fYEY1o5pBgMHvakxI6FDf4r46OT/a6VjIWvCGQuSfiA+21TfRqtkPge34mxN4W/PHO
2zuvziyz7LrcbRXGmVXA1gMLVY/NlctiyiFCzyyzubDmpSL4ygPRKkZ/LSWJVnAeMUo3
5Uv0z2bfzr/2ifCjJa8RU/F8/98Ya1kgaKd/Y2uZDwBWXyxB4ovnHxk3SFCJAZIhhNzp
SOzPOl4LAp05Tex+L2+sdKh1fzGHLrlT+2XUfQcabSauJiR1S6BABvw4Mtn7AZSmxU6/
1aWF4R8w49dT/MiuPKVAXhw1e7wzbwUj24wOSkWckhLin/0LKDX95M29Gr4WwHNSj8jS
FhNQ==
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:content-transfer-encoding;
bh=IlHmTqL3OOh630DOhy2+RmVBeEeofRPrYHKW36Dfh2A=;
b=yApiOubjNWk+oSa9nnSClMwrqViHYGeXcyLowd2/6vcGXCwN3v02KvdqEkgtoFu216
0PTozgTbmV7Ywulqh0J6hThzQP2Jn6j9dchpZCleqraBOFvLzVRRWCFkWNV9AnLDm4Mn
3EalVvRrdJxjen0kzrT+Vl+7msJ2n28XD/3ayteu/IzQucAB6ZtrE0zP4RACj0BY3JiV
SU16ORbOtMe1Cjn6bV6k2HQ+czU6af3NntVzkWy4lXvNilwT8VGA2RRjHWuy7Tt/HDFy
anUfiqeAcmAg9Ao17OPCEt53JaLxZ7uqGflmzc4pzFb1ihZuoFWmCb2+3rcNE+qk1FPI
RhoQ==
X-Gm-Message-State: AOAM532CQCPNSBpboZLfU23UNbLpU1O9PBWCPL0VA6qXm2GMrPnxzR1y
jGCOPwwW/QVXEM5GLIsZfenq6Ir9UUOxsEtqRvbXpNke
X-Google-Smtp-Source: ABdhPJyxJR5LuXHkMLbn6eK4vsuzhoIK6t8ZTjPV4Usr50EtPlFM3zJ/kL+RHZd37IJiQ243c0UxAQNbkFVmJhydTKk=
X-Received: by 2002:a1c:4386:: with SMTP id q128mr7793852wma.9.1643842737381;
Wed, 02 Feb 2022 14:58:57 -0800 (PST)
In-Reply-To: <177517EB-2AA2-47AA-B974-100188449961@barrys-emacs.org>
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: <CAPTjJmpqQW-T3OHAJi_n1BkzJCYE9VuRCv+n-v=T6A1_3o+Axw@mail.gmail.com>
X-Mailman-Original-References: <CABbU2U8fCh-qR5QdyFwgBAmQEi+6nzsbNK=2Jjk=QZxHf=h3CQ@mail.gmail.com>
<177517EB-2AA2-47AA-B974-100188449961@barrys-emacs.org>
 by: Chris Angelico - Wed, 2 Feb 2022 22:58 UTC

On Thu, 3 Feb 2022 at 09:33, Barry <barry@barrys-emacs.org> wrote:
>
>
>
> > On 2 Feb 2022, at 21:12, Marco Sulla <Marco.Sulla.Python@gmail.com> wrote:
> >
> > You could add a __del__ that calls stop :)
>
> Didn’t python3 make this non deterministic when del is called?
>
> I thought the recommendation is to not rely on __del__ in python3 code.
>

The __del__ method is called when the object is being disposed of.
Aside from some considerations about interpreter shutdown, it's
perfectly reliable... as long as you understand that objects don't get
disposed of when they "go out of scope", but when they run out of
references and get garbage collected. So for instance, you can't
depend on "x = Spam(); x = Ham()" to dispose of the Spam object
instantly, because it might not get garbage collected instantly; but
you can depend on __del__ getting called when the Spam object gets
garbage collected.

With that said, I can guarantee you that a __del__ method is NOT the
right way to call stop, for one simple reason: the object will keep
its own references (via the timer) so long as it is running. So it
won't get garbage collected, and in fact, this is very important to it
being reliable. Consider this simpler example:

def call_soon(func):
t = threading.Timer(10, func)
t.start()

When this function returns, you can no longer refer to the object 't'.
Will the function be called at the appropriate time? Yes, it
absolutely will, and it would be highly surprising if it didn't! So
the thread itself keeps a reference to the important objects.

(Side point: The OP's code is quite inefficient, as it creates a new
thread for each reiteration, but there's nothing wrong with that if
you're looking for something simple.)

The main reason to "not rely on __del__" (and, by the way, that's
nothing to do with whether it's Python 2 or Python 3, this has been
true since early Py2 and possibly earlier) is that you don't know when
the object will be disposed of. So if you want to guarantee that
something is cleaned up, what you need is a way for the object itself
to still exist, but the corresponding resource to be cleaned up. A
classic example is a file object:

def read_then_write(fn):
with open(fn) as read_file:
data = read_file.read()
print(read_file)
# ... transform the data as required ...
with open(fn, "w") as write_file:
write_file.write(data)
print(write_file)

After each 'with' block, the file object is still there. You can refer
to it. Nothing has destroyed the object. But the file has been closed,
guaranteeing that you can safely reopen it in a different mode
(regardless of your OS). The same could be done with this timer; an
__exit__ method would make a lot of sense here, and would allow the
timer to be used in a with block to govern its execution. (It also
isn't really necessary, but if you want a good Pythonic way to show
the beginning and end of its use area, a 'with' block is the way to
go.)

ChrisA

Re: Waht do you think about my repeated_timer class

<87k0eckd5w.fsf@munus.decebal.nl>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Cec...@decebal.nl (Cecil Westerhof)
Newsgroups: comp.lang.python
Subject: Re: Waht do you think about my repeated_timer class
Date: Thu, 03 Feb 2022 01:27:07 +0100
Organization: Decebal Computing
Lines: 23
Message-ID: <87k0eckd5w.fsf@munus.decebal.nl>
References: <CABbU2U8fCh-qR5QdyFwgBAmQEi+6nzsbNK=2Jjk=QZxHf=h3CQ@mail.gmail.com>
<177517EB-2AA2-47AA-B974-100188449961@barrys-emacs.org>
<CAPTjJmpqQW-T3OHAJi_n1BkzJCYE9VuRCv+n-v=T6A1_3o+Axw@mail.gmail.com>
<mailman.23.1643842739.2976.python-list@python.org>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="92931b4d56e0fad1a0ce13c194df753c";
logging-data="8843"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/kmdxBes0wfQE7PtUqjHDAeVgCe0EIBpI="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:xgMKU8EjiQVk2GtSnbGkL3XbV98=
sha1:DL9+FrvB/u5JTPjMqkJ4AQNM/ig=
 by: Cecil Westerhof - Thu, 3 Feb 2022 00:27 UTC

Chris Angelico <rosuav@gmail.com> writes:

> On Thu, 3 Feb 2022 at 09:33, Barry <barry@barrys-emacs.org> wrote:
> (Side point: The OP's code is quite inefficient, as it creates a new
> thread for each reiteration, but there's nothing wrong with that if
> you're looking for something simple.)

It is just something I wrote fast. How could I do this in a better way?

> (regardless of your OS). The same could be done with this timer; an
> __exit__ method would make a lot of sense here, and would allow the
> timer to be used in a with block to govern its execution. (It also
> isn't really necessary, but if you want a good Pythonic way to show
> the beginning and end of its use area, a 'with' block is the way to
> go.)

I will look into that.

--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

Re: Waht do you think about my repeated_timer class

<87fsp0kbvl.fsf@munus.decebal.nl>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Cec...@decebal.nl (Cecil Westerhof)
Newsgroups: comp.lang.python
Subject: Re: Waht do you think about my repeated_timer class
Date: Thu, 03 Feb 2022 01:54:54 +0100
Organization: Decebal Computing
Lines: 67
Message-ID: <87fsp0kbvl.fsf@munus.decebal.nl>
References: <CABbU2U8fCh-qR5QdyFwgBAmQEi+6nzsbNK=2Jjk=QZxHf=h3CQ@mail.gmail.com>
<177517EB-2AA2-47AA-B974-100188449961@barrys-emacs.org>
<CAPTjJmpqQW-T3OHAJi_n1BkzJCYE9VuRCv+n-v=T6A1_3o+Axw@mail.gmail.com>
<mailman.23.1643842739.2976.python-list@python.org>
<87k0eckd5w.fsf@munus.decebal.nl>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="92931b4d56e0fad1a0ce13c194df753c";
logging-data="17532"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+DGJ2mWdIqXOgFYCSdfiupgXkY2YN0hWM="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:7Yl2z+HxZq6S8bxaKidLZyCmeEo=
sha1:5IpZ+hsB1rgFelIA5YYPbG6Twd4=
 by: Cecil Westerhof - Thu, 3 Feb 2022 00:54 UTC

Cecil Westerhof <Cecil@decebal.nl> writes:

>> (regardless of your OS). The same could be done with this timer; an
>> __exit__ method would make a lot of sense here, and would allow the
>> timer to be used in a with block to govern its execution. (It also
>> isn't really necessary, but if you want a good Pythonic way to show
>> the beginning and end of its use area, a 'with' block is the way to
>> go.)
>
> I will look into that.

Implemented:
from threading import Timer

class repeated_timer(object):
def __init__(self, fn, interval, start = False):
if not callable(fn):
raise TypeError('{} is not a function'.format(fn))
self._fn = fn
self._check_interval(interval)
self._interval = interval
self._timer = None
self._is_running = False
if start:
self.start()

def __enter__(self):
return self

def __exit__(self, exc_type, exc_val, exc_tb):
self.stop()

def _check_interval(self, interval):
if not type(interval) in [int, float]:
raise TypeError('{} is not numeric'.format(interval))
if interval <= 0:
raise ValueError('{} is not greater as 0'.format(interval))

def _next(self):
self._timer = Timer(self._interval, self._run)
self._timer.start()

def _run(self):
self._next()
self._fn()

def set_interval(self, interval):
self._check_interval(interval)
self._interval = interval

def start(self):
if not self._is_running:
self._is_running = True
self._next()

def stop(self):
if self._is_running:
self._timer.cancel()
self._timer = None
self._is_running = False

--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

Re: Waht do you think about my repeated_timer class

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!news.szaf.org!fu-berlin.de!uni-berlin.de!not-for-mail
From: cs...@cskk.id.au (Cameron Simpson)
Newsgroups: comp.lang.python
Subject: Re: Waht do you think about my repeated_timer class
Date: Thu, 3 Feb 2022 12:39:43 +1100
Lines: 28
Message-ID: <mailman.24.1643853485.2976.python-list@python.org>
References: <87fsp0kbvl.fsf@munus.decebal.nl>
<YfsyX0swyT9ebSs2@cskk.homeip.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
X-Trace: news.uni-berlin.de ieFYjCrPHSkN64EpNIKdxgf6NAbbSM8qRGeZzR0boBGw==
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.003
X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'def': 0.04; ':-)': 0.09;
'fails': 0.09; 'float': 0.09; 'int': 0.09; 'myself.': 0.09;
'skip:_ 20': 0.09; 'subject:class': 0.09; 'cheers,': 0.11;
'"cannot': 0.16; '%s"': 0.16; 'cameron': 0.16; 'from:addr:cs':
0.16; 'from:addr:cskk.id.au': 0.16; 'from:name:cameron simpson':
0.16; 'message-id:@cskk.homeip.net': 0.16; 'received:10.10': 0.16;
'simpson': 0.16; 'tries': 0.16; 'probably': 0.17; 'to:addr:python-
list': 0.20; "i'd": 0.24; 'classes': 0.26; 'normally': 0.26;
'header:User-Agent:1': 0.30; 'raise': 0.31; 'but': 0.32; 'header
:In-Reply-To:1': 0.34; 'work,': 0.36; 'use': 0.39; 'method': 0.61;
'skip:i 20': 0.62; 'skip:b 10': 0.63; 'received:userid': 0.66;
'types': 0.67; 'header:Received:6': 0.67; 'float:': 0.74;
'handles': 0.76; 'subject:you': 0.84; 'subject:think': 0.84;
'written:': 0.84
X-RG-Spam: Unknown
X-RazorGate-Vade: gggruggvucftvghtrhhoucdtuddrgedvvddrgeeigdefiecutefuodetggdotefrodftvfcurfhrohhfihhlvgemucfupfevtfgpvffgnffuvffttedpqfgfvfenuceurghilhhouhhtmecugedttdenucenucfjughrpeffhffvuffkgggtuggjfghfsehttdertddtredvnecuhfhrohhmpeevrghmvghrohhnucfuihhmphhsohhnuceotghssegtshhkkhdrihgurdgruheqnecuggftrfgrthhtvghrnhepleevfeeghfeggfehuddvgfehhfffjeegvdeuiefhgeelgeekfeffiefguddtveehnecukfhppedurdduvdelrddvgeehrddvfeelnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehhvghlohepsghorhhgrdgtshhkkhdrhhhomhgvihhprdhnvghtpdhinhgvthepuddruddvledrvdeghedrvdefledpmhgrihhlfhhrohhmpegtrghmvghrohhnsegtshhkkhdrihgurdgruhdpnhgspghrtghpthhtohepuddprhgtphhtthhopehphihthhhonhdqlhhishhtsehphihthhhonhdrohhrgh
X-RazorGate-Vade-Verdict: clean 0
X-RazorGate-Vade-Classification: clean
X-RG-VS-CLASS: clean
X-Authentication-Info: Submitted using ID cskk@bigpond.com
Mail-Followup-To: python-list@python.org
Content-Disposition: inline
In-Reply-To: <87fsp0kbvl.fsf@munus.decebal.nl>
User-Agent: Mutt/2.1.5 (2021-12-30)
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: <YfsyX0swyT9ebSs2@cskk.homeip.net>
X-Mailman-Original-References: <87fsp0kbvl.fsf@munus.decebal.nl>
 by: Cameron Simpson - Thu, 3 Feb 2022 01:39 UTC

You have:

def _check_interval(self, interval):
if not type(interval) in [int, float]:
raise TypeError('{} is not numeric'.format(interval))

This check is better written:

if not isinstance(interval, (int,float)):

which handles subclasses of these types (but note that bool subclasses
int :-) normally we don't care), or behaviourally:

try:
interval = float(interval)
except ValueError as e:
raise TypeError(
"cannot convert %s:%r to float: %s"
% (type(interval).__name__, interval, e)) from e

which tries to convert to float and fails if that does not work, which
supports classes with a __float__ method (these classes are rare, but
decimal.Decimal is one example).

I'd probably use the isinstance() form myself.

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

Re: Waht do you think about my repeated_timer class

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: ros...@gmail.com (Chris Angelico)
Newsgroups: comp.lang.python
Subject: Re: Waht do you think about my repeated_timer class
Date: Thu, 3 Feb 2022 13:23:22 +1100
Lines: 30
Message-ID: <mailman.25.1643855014.2976.python-list@python.org>
References: <CABbU2U8fCh-qR5QdyFwgBAmQEi+6nzsbNK=2Jjk=QZxHf=h3CQ@mail.gmail.com>
<177517EB-2AA2-47AA-B974-100188449961@barrys-emacs.org>
<CAPTjJmpqQW-T3OHAJi_n1BkzJCYE9VuRCv+n-v=T6A1_3o+Axw@mail.gmail.com>
<mailman.23.1643842739.2976.python-list@python.org>
<87k0eckd5w.fsf@munus.decebal.nl>
<CAPTjJmp52PZc-toTuBYDdJUr=2Jku6uwtVeUFAiYi7fv4+WnJQ@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
X-Trace: news.uni-berlin.de 8ceBGlAxm5jY4NvjXeqzagrUIlAg3FCExNb2Y+FEJEfQ==
Return-Path: <rosuav@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=jA83h69K;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.004
X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; '2022': 0.05; 'bigger':
0.05; 'thread': 0.05; 'angelico': 0.09; 'subject:class': 0.09;
'way?': 0.09; 'writes:': 0.09; '12:24,': 0.16; 'barry': 0.16;
'chrisa': 0.16; 'default;': 0.16; 'from:addr:rosuav': 0.16;
'from:name:chris angelico': 0.16; 'question,': 0.16; 'seconds.':
0.16; 'things,': 0.16; 'wrote:': 0.16; 'feb': 0.17; 'thu,': 0.19;
'to:addr:python-list': 0.20; 'creates': 0.22; 'code': 0.23; 'run':
0.23; 'saying': 0.25; 'object': 0.26; 'creating': 0.27; 'else':
0.27; 'function': 0.27; 'chris': 0.28; 'wrong': 0.28; 'it,': 0.29;
"doesn't": 0.32; 'python-list': 0.32; 'message-
id:@mail.gmail.com': 0.32; 'but': 0.32; "i'm": 0.33; "i'll": 0.33;
'header:In-Reply-To:1': 0.34; 'received:google.com': 0.34;
'from:addr:gmail.com': 0.35; 'also,': 0.36; 'target': 0.36;
'change': 0.36; "it's": 0.37; 'received:209.85': 0.37; 'could':
0.38; 'received:209': 0.39; 'quite': 0.39; 'single': 0.39;
'methods': 0.39; 'wrote': 0.39; 'something': 0.40; "there's":
0.61; 'feel': 0.63; 'everything': 0.63; 'your': 0.64; 'look':
0.65; 'bad': 0.67; 'important.': 0.69; 'supposed': 0.76;
'subject:you': 0.84; 'subject:think': 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;
bh=ezbjp3c8Flbwmdp2+QM2S6p8URqWUNjezAooqt8QL/0=;
b=jA83h69KCEVW3+slzydn6bSuGLNCVOQhsVl3v4auYkFXQHJoVUz2EV2x9vljYdDNxg
unPknbfyh7yU+uGaehJq+NQlaMhzhrbKlaMZZwUdS8I4matpMz7JEgkg7af6YLpU8ENd
SwdG1DX1IFUbsBdjKgMXOmBIpfn6xlViv3ABj76UsLEuQRGlo9e6w4jk7H4EG6Pq6SE9
wUy16aVLXCZbWzWP6UOWQ4FESJmJzzCq23Lh4XW0VieUoAg5GPvEySPa582sbNvWOiyZ
W8W0vujlSN2YRJ1jDow+AsNICud6is3ePueOhsYLC3I0N5l4/ldlS9MXu7OznXVmT3tT
GHLA==
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=ezbjp3c8Flbwmdp2+QM2S6p8URqWUNjezAooqt8QL/0=;
b=vCltFP1qVCBV0ovmO2btGRIFrMe72NxTffSiHM9RTQLmqby3GvCWMoMBP07RrWh3UM
J4++dHplYaOeSXBrCp9czdJvQPtU+YJ9Db5Dc1JFF7SEWX25+oZRzI9codbQS1E1moHL
8Kn439rGbaEMQJCCqjxnY8ndB2ZxHywQcOFgcV+Uxysik0kAX38W2p6POgvx0LALJR/f
2+IxiRN4Vi1+BQWlls0pzLLDGWakFh/Ss90ut+ksU5n+4t01D+ha/B/Bu/svF/pi1jx2
05/kchqZSP/Z6znJSyIYFmWUS3YxylnLoCAQSG1E4bHwp7qomdEt3/iFMzViNHIjoWf5
Pl7Q==
X-Gm-Message-State: AOAM531zvdPhQpKARcUd1N9vVGv64m6EX2roe96Xv/zYjO19mZyk9NCY
JUaNt4WdOkyrqo/Pz8mBycUPfrodl0x8G8+EoeqRsZWX
X-Google-Smtp-Source: ABdhPJwpnNvbGEhY02tiZ0jin0u51PiJJWXap8k6wZLiUNdNIkBlxF/TQ0Ss518quKQ8nD5agQ8RsibUidFxD1c6ZRw=
X-Received: by 2002:adf:f141:: with SMTP id y1mr26953078wro.104.1643855012941;
Wed, 02 Feb 2022 18:23:32 -0800 (PST)
In-Reply-To: <87k0eckd5w.fsf@munus.decebal.nl>
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: <CAPTjJmp52PZc-toTuBYDdJUr=2Jku6uwtVeUFAiYi7fv4+WnJQ@mail.gmail.com>
X-Mailman-Original-References: <CABbU2U8fCh-qR5QdyFwgBAmQEi+6nzsbNK=2Jjk=QZxHf=h3CQ@mail.gmail.com>
<177517EB-2AA2-47AA-B974-100188449961@barrys-emacs.org>
<CAPTjJmpqQW-T3OHAJi_n1BkzJCYE9VuRCv+n-v=T6A1_3o+Axw@mail.gmail.com>
<mailman.23.1643842739.2976.python-list@python.org>
<87k0eckd5w.fsf@munus.decebal.nl>
 by: Chris Angelico - Thu, 3 Feb 2022 02:23 UTC

On Thu, 3 Feb 2022 at 12:24, Cecil Westerhof via Python-list
<python-list@python.org> wrote:
>
> Chris Angelico <rosuav@gmail.com> writes:
>
> > On Thu, 3 Feb 2022 at 09:33, Barry <barry@barrys-emacs.org> wrote:
> > (Side point: The OP's code is quite inefficient, as it creates a new
> > thread for each reiteration, but there's nothing wrong with that if
> > you're looking for something simple.)
>
> It is just something I wrote fast. How could I do this in a better way?

I'll answer your question, but first and foremost: Your code was fine,
and if something does what it's supposed to, that is the most
important. Everything else is minor.

But for other ways to do things, I would recommend creating a single
thread function and spawning a single thread to run it, and then
having that function call the target every N seconds. Also, consider
subclassing Thread rather than subclassing object (which, btw, is the
default; you don't need to say "class X(object)"), which will
automatically give your object all the methods of a timer.

If you feel like it, you could even look into ways to do things
without threads, but that would be a much bigger change :)

But remember: when your code does what it's supposed to, it is *fine*,
and doesn't need changing. I'm not saying that your code is bad :)

ChrisA

Re: Waht do you think about my repeated_timer class

<87bkzok82k.fsf@munus.decebal.nl>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Cec...@decebal.nl (Cecil Westerhof)
Newsgroups: comp.lang.python
Subject: Re: Waht do you think about my repeated_timer class
Date: Thu, 03 Feb 2022 03:17:07 +0100
Organization: Decebal Computing
Lines: 21
Message-ID: <87bkzok82k.fsf@munus.decebal.nl>
References: <87fsp0kbvl.fsf@munus.decebal.nl>
<YfsyX0swyT9ebSs2@cskk.homeip.net>
<mailman.24.1643853485.2976.python-list@python.org>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="92931b4d56e0fad1a0ce13c194df753c";
logging-data="9744"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+Qkk+6AGztLCJQbuIrlrucrdnhZZqbF6M="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:pjL1O3zPU0Bkj3nPTuvC/Pkad80=
sha1:BDFE9csL6J6CfMspovYlfNDB3Ac=
 by: Cecil Westerhof - Thu, 3 Feb 2022 02:17 UTC

Cameron Simpson <cs@cskk.id.au> writes:

> You have:
>
> def _check_interval(self, interval):
> if not type(interval) in [int, float]:
> raise TypeError('{} is not numeric'.format(interval))
>
> This check is better written:
>
> if not isinstance(interval, (int,float)):
>
> which handles subclasses of these types (but note that bool subclasses
> int :-)

Done, thanks.

--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

Re: Waht do you think about my repeated_timer class

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: 2QdxY4Rz...@potatochowder.com
Newsgroups: comp.lang.python
Subject: Re: Waht do you think about my repeated_timer class
Date: Wed, 2 Feb 2022 21:31:42 -0600
Lines: 42
Message-ID: <mailman.28.1643860267.2976.python-list@python.org>
References: <87fsp0kbvl.fsf@munus.decebal.nl>
<YfsyX0swyT9ebSs2@cskk.homeip.net> <YftMntVfWblJKlvB@scrozzle>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
X-Trace: news.uni-berlin.de 8bsJPwCnQKcNsJIruq4CpAx+kojipaQCasyc21xwyQMg==
Return-Path: <2QdxY4RzWzUUiLuE@potatochowder.com>
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.005
X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'def': 0.04; ':-)': 0.09;
'faced': 0.09; 'fails': 0.09; 'float': 0.09; 'int': 0.09;
'received:78': 0.09; 'skip:_ 20': 0.09; 'subject:class': 0.09;
'"cannot': 0.16; '%s"': 0.16; '+1100,': 0.16; 'cameron': 0.16;
'from:addr:2qdxy4rzwzuuilue': 0.16; 'from:addr:potatochowder.com':
0.16; 'message-id:@scrozzle': 0.16; 'nan,': 0.16;
'received:136.243': 0.16; 'received:www458.your-server.de': 0.16;
'received:your-server.de': 0.16; 'simpson': 0.16; 'tries': 0.16;
'wrote:': 0.16; 'python': 0.16; 'instead': 0.17; 'to:addr:python-
list': 0.20; 'code': 0.23; 'received:de': 0.23; "i'd": 0.24;
'(and': 0.25; 'classes': 0.26; 'normally': 0.26; 'raise': 0.31;
'think': 0.32; "doesn't": 0.32; 'negative': 0.32; 'received:136':
0.32; 'but': 0.32; "i'm": 0.33; 'header:In-Reply-To:1': 0.34;
'complex': 0.35; 'possibly': 0.36; 'work,': 0.36; "it's": 0.37;
'way': 0.38; 'text': 0.39; 'tell': 0.60; 'including': 0.60;
'method': 0.61; 'skip:i 20': 0.62; 'skip:b 10': 0.63; 'your':
0.64; 'numbers': 0.67; 'types': 0.67; 'right': 0.68; 'following:':
0.69; 'float:': 0.74; 'handles': 0.76; 'subject:you': 0.84;
'received:70': 0.84; 'stuff,': 0.84; 'subject:think': 0.84;
'written:': 0.84
Mail-Followup-To: python-list@python.org
Content-Disposition: inline
In-Reply-To: <YfsyX0swyT9ebSs2@cskk.homeip.net>
X-Authenticated-Sender: 2QdxY4RzWzUUiLuE@potatochowder.com
X-Virus-Scanned: Clear (ClamAV 0.103.5/26441/Wed Feb 2 10:43:13 2022)
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: <YftMntVfWblJKlvB@scrozzle>
X-Mailman-Original-References: <87fsp0kbvl.fsf@munus.decebal.nl>
<YfsyX0swyT9ebSs2@cskk.homeip.net>
 by: 2QdxY4Rz...@potatochowder.com - Thu, 3 Feb 2022 03:31 UTC

On 2022-02-03 at 12:39:43 +1100,
Cameron Simpson <cs@cskk.id.au> wrote:

> You have:
>
> def _check_interval(self, interval):
> if not type(interval) in [int, float]:
> raise TypeError('{} is not numeric'.format(interval))
>
> This check is better written:
>
> if not isinstance(interval, (int,float)):
>
> which handles subclasses of these types (but note that bool subclasses
> int :-) normally we don't care), or behaviourally:
>
> try:
> interval = float(interval)
> except ValueError as e:
> raise TypeError(
> "cannot convert %s:%r to float: %s"
> % (type(interval).__name__, interval, e)) from e
>
> which tries to convert to float and fails if that does not work, which
> supports classes with a __float__ method (these classes are rare, but
> decimal.Decimal is one example).

I think this can be simplified for time intervals to the following:

if interval <= 0:
raise ValueError(...)

which accepts non-negative real values; throws ValueError for negative
real values; and TypeError for other stuff, including complex numbers
(pathological types notwithstanding). One thing that doesn't work right
is NaNs, but I'm sure it's not the only code that acts weirdly when
faced with a NaM (curiously, Timer accepts a NaN, but the text I get
from help(Timer) in Python 3.10.2 is, well, broken).

FWIW, I'd find some way to tell users the units (seconds, milliseconds,
fortnights, etc.) instead of making them wade through your code to find
the call to (and possibly the [broken] help text of) Timer.

Re: Waht do you think about my repeated_timer class

<877dack3ee.fsf@munus.decebal.nl>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Cec...@decebal.nl (Cecil Westerhof)
Newsgroups: comp.lang.python
Subject: Re: Waht do you think about my repeated_timer class
Date: Thu, 03 Feb 2022 04:58:01 +0100
Organization: Decebal Computing
Lines: 38
Message-ID: <877dack3ee.fsf@munus.decebal.nl>
References: <CABbU2U8fCh-qR5QdyFwgBAmQEi+6nzsbNK=2Jjk=QZxHf=h3CQ@mail.gmail.com>
<177517EB-2AA2-47AA-B974-100188449961@barrys-emacs.org>
<CAPTjJmpqQW-T3OHAJi_n1BkzJCYE9VuRCv+n-v=T6A1_3o+Axw@mail.gmail.com>
<mailman.23.1643842739.2976.python-list@python.org>
<87k0eckd5w.fsf@munus.decebal.nl>
<CAPTjJmp52PZc-toTuBYDdJUr=2Jku6uwtVeUFAiYi7fv4+WnJQ@mail.gmail.com>
<mailman.25.1643855014.2976.python-list@python.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: reader02.eternal-september.org; posting-host="92931b4d56e0fad1a0ce13c194df753c";
logging-data="4094"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19E+8ZX7QLZSZbUwzqWkcNlqZRkdpAS898="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:VOuuKyMBpRuNZlwTvAnd7lr79VE=
sha1:NKEPb678dcxS6IIxp556Sx4dE6w=
 by: Cecil Westerhof - Thu, 3 Feb 2022 03:58 UTC

Chris Angelico <rosuav@gmail.com> writes:

>> > (Side point: The OP's code is quite inefficient, as it creates a new
>> > thread for each reiteration, but there's nothing wrong with that if
>> > you're looking for something simple.)
>>
>> It is just something I wrote fast. How could I do this in a better way?
>
> I'll answer your question, but first and foremost: Your code was fine,
> and if something does what it's supposed to, that is the most
> important. Everything else is minor.

I like to write efficient code and it never hurts to write better code
as just doing what it is supposed to do. ;-)

And in my case interval is .5 seconds and when someone is going to use
it with an even smaller interval …

> But for other ways to do things, I would recommend creating a single
> thread function and spawning a single thread to run it, and then
> having that function call the target every N seconds. Also, consider

Have to be careful that timing keeps correct when target takes a 'lot'
of time.
Something to ponder about, but can wait.

> subclassing Thread rather than subclassing object (which, btw, is the
> default; you don't need to say "class X(object)"), which will
> automatically give your object all the methods of a timer.

Of-course. I should have thought about that. :'-(

--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

Re: Waht do you think about my repeated_timer class

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: ros...@gmail.com (Chris Angelico)
Newsgroups: comp.lang.python
Subject: Re: Waht do you think about my repeated_timer class
Date: Thu, 3 Feb 2022 15:07:22 +1100
Lines: 70
Message-ID: <mailman.29.1643861254.2976.python-list@python.org>
References: <87fsp0kbvl.fsf@munus.decebal.nl>
<YfsyX0swyT9ebSs2@cskk.homeip.net> <YftMntVfWblJKlvB@scrozzle>
<CAPTjJmqcohrT3wiTYqhJVscHXi5Dn5AnUjCd=_uch=iiwVdPRA@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
X-Trace: news.uni-berlin.de U2xtFVf2RzpXvNgnyJZT1wLYWdYxjIZOJxEJD98ZCNtQ==
Return-Path: <rosuav@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=GsO+UdBO;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.003
X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'def': 0.04; '2022': 0.05;
'demands': 0.07; ':-)': 0.09; 'faced': 0.09; 'fails': 0.09;
'float': 0.09; 'int': 0.09; 'programmers,': 0.09; 'skip:_ 20':
0.09; 'subject:class': 0.09; 'though.': 0.09; 'web.': 0.09;
'"cannot': 0.16; '%s"': 0.16; '(unless': 0.16; '+1100,': 0.16;
'be,': 0.16; 'cameron': 0.16; 'chrisa': 0.16; 'float,': 0.16;
'frameworks.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris
angelico': 0.16; 'nan,': 0.16; 'resolution': 0.16; 'seconds.':
0.16; 'simpson': 0.16; 'sounds': 0.16; 'tries': 0.16; 'wrote:':
0.16; 'problem': 0.16; 'python': 0.16; 'feb': 0.17; 'instead':
0.17; 'thu,': 0.19; 'to:addr:python-list': 0.20; "i've": 0.22;
'languages': 0.22; 'code': 0.23; "i'd": 0.24; '(and': 0.25;
'anything': 0.25; 'python,': 0.25; 'programming': 0.25; 'classes':
0.26; 'normally': 0.26; "isn't": 0.27; 'code,': 0.31; 'takes':
0.31; 'raise': 0.31; 'think': 0.32; "doesn't": 0.32; 'question':
0.32; 'assume': 0.32; 'fine.': 0.32; 'negative': 0.32; 'zero':
0.32; 'message-id:@mail.gmail.com': 0.32; 'but': 0.32; "i'm":
0.33; 'mean': 0.34; 'header:In-Reply-To:1': 0.34;
'received:google.com': 0.34; 'complex': 0.35;
'from:addr:gmail.com': 0.35; 'possibly': 0.36; 'work,': 0.36;
'those': 0.36; "it's": 0.37; 'received:209.85': 0.37; 'way': 0.38;
'received:209': 0.39; 'two': 0.39; 'quite': 0.39; 'text': 0.39;
'seconds': 0.40; 'something': 0.40; 'tell': 0.60; 'including':
0.60; 'method': 0.61; 'here.': 0.61; "there's": 0.61; 'skip:i 20':
0.62; 'skip:b 10': 0.63; 'your': 0.64; 'less': 0.65; 'numbers':
0.67; 'types': 0.67; 'generally': 0.67; 'accept': 0.67; 'right':
0.68; 'cast': 0.69; 'following:': 0.69; 'too.': 0.70; 'float:':
0.74; 'handles': 0.76; 'unit': 0.81; 'subject:you': 0.84;
'casting': 0.84; 'stuff,': 0.84; 'subject:think': 0.84; 'weird,':
0.84; 'written:': 0.84; 'micro': 0.91; 'number.': 0.91
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=Ur7NxLqWFbAm9JEyPee/uIiPjHohdj4HLqvBneyWZyI=;
b=GsO+UdBO4/m/X9j7/bSN8/QptyH8sI97bbOMimy7oPwvkN4Qt/3bizFAbd00ACpD7H
1vZOpPWnnj9xXt5Gr4Vwy26TxLp4VZB8dBlnooZb0eZuTpWR6fJX2+9r9qTerJ+0l75X
UAP7bmi/djKGpeUW6WD/Jt8VmGpdQVB8qsX8ooyan+cW01315n0t3hW9f1wdPGawY68X
ErGzQabo1zeQ6gTeU6s2X07Lk7q+UgQU6djun2NDo9n54+KvJrAXEFZg5IZjtLV3UiM/
Q8IxPG54RBohftNQFlwZGg05kBQqtdg2WLucBqUVsirLhvGBeOxe5qIXdrcynQB2X2CB
cuoA==
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=Ur7NxLqWFbAm9JEyPee/uIiPjHohdj4HLqvBneyWZyI=;
b=rTc0eLxDqrv+dxZSFs+QsTgPWoE93tz4b+M/zK8/6o7T8IUOIZzzasToUDTW4FA2nF
iIV8efoZysnvDVf1HvLtrj9xcXDHaTG8E20ISubUO5/ndfpRc9QTKCLU8pVyN44NeK+e
5och/ZwadaB5oBXj+rF19WijSBRbPTP/8QMoo2XS0mX74K9/wonMt3jH2qUBJLjrw5Tm
pLf2afKSs8HNgy5XWYRiiUQiV2H2bNTfCtE8Wsdohmd9DfwvlyAZYN5T/8L8POtvSmLP
vUjl+Uk6gtLopzZbf5HgG03DAXohzaPorUtIfachAyFxQ4ZD22BjUYLdVNfXbond9KZg
TZtw==
X-Gm-Message-State: AOAM533dRkBlL/vK0Quu+xh1QqB4ANoOKY8/ejCXnHhYvdHFdZ90O/sj
/AG0h363imVQ/krkxjztGEjwBVJgeIjiyu9UZnjkx6us2eo=
X-Google-Smtp-Source: ABdhPJy2BlH7qb+8Kc4RpId+qDuIfue/6jzdiKjJH8anVwR6s6QzQ/U4BMs/VxoVgKoL5SUkIvCwddWVB+mV/fijBGA=
X-Received: by 2002:adf:fb4a:: with SMTP id c10mr26824448wrs.243.1643861252867;
Wed, 02 Feb 2022 20:07:32 -0800 (PST)
In-Reply-To: <YftMntVfWblJKlvB@scrozzle>
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: <CAPTjJmqcohrT3wiTYqhJVscHXi5Dn5AnUjCd=_uch=iiwVdPRA@mail.gmail.com>
X-Mailman-Original-References: <87fsp0kbvl.fsf@munus.decebal.nl>
<YfsyX0swyT9ebSs2@cskk.homeip.net>
<YftMntVfWblJKlvB@scrozzle>
 by: Chris Angelico - Thu, 3 Feb 2022 04:07 UTC

On Thu, 3 Feb 2022 at 14:52, <2QdxY4RzWzUUiLuE@potatochowder.com> wrote:
>
> On 2022-02-03 at 12:39:43 +1100,
> Cameron Simpson <cs@cskk.id.au> wrote:
>
> > You have:
> >
> > def _check_interval(self, interval):
> > if not type(interval) in [int, float]:
> > raise TypeError('{} is not numeric'.format(interval))
> >
> > This check is better written:
> >
> > if not isinstance(interval, (int,float)):
> >
> > which handles subclasses of these types (but note that bool subclasses
> > int :-) normally we don't care), or behaviourally:
> >
> > try:
> > interval = float(interval)
> > except ValueError as e:
> > raise TypeError(
> > "cannot convert %s:%r to float: %s"
> > % (type(interval).__name__, interval, e)) from e
> >
> > which tries to convert to float and fails if that does not work, which
> > supports classes with a __float__ method (these classes are rare, but
> > decimal.Decimal is one example).
>
> I think this can be simplified for time intervals to the following:
>
> if interval <= 0:
> raise ValueError(...)

That's checking something quite different, though. Casting to float
will accept anything that can be, well, cast to float, but checking
for less than or equal to zero demands that it already be some sort of
number. It's debatable which check is more correct, but certainly this
is not a simplification of the other code, it's a distinctly different
validation.

> which accepts non-negative real values; throws ValueError for negative
> real values; and TypeError for other stuff, including complex numbers
> (pathological types notwithstanding). One thing that doesn't work right
> is NaNs, but I'm sure it's not the only code that acts weirdly when
> faced with a NaM (curiously, Timer accepts a NaN, but the text I get
> from help(Timer) in Python 3.10.2 is, well, broken).

Strange. The text I get in 3.11.0a1 is fine. But in any case, there's
always the docs on the web.

https://docs.python.org/3/library/threading.html#timer-objects

> FWIW, I'd find some way to tell users the units (seconds, milliseconds,
> fortnights, etc.) instead of making them wade through your code to find
> the call to (and possibly the [broken] help text of) Timer.

In anything in Python, assume that the unit is seconds. With anything
that accepts floats (where these can be distinguished from integers),
assume the unit is seconds. If it accepts micro or nanoseconds, it'll
almost certainly be called "high resolution timer" (unless it accepts
two args, sec and us/ns, but that's pretty obvious), so you can
generally exclude those too. The only real question is whether
sleep(int) takes seconds or milliseconds, which isn't a problem here.

Citation: I've slept in many many programming languages and
frameworks. Which sounds seriously weird, but you're all programmers,
you know what I mean :)

ChrisA

Re: Waht do you think about my repeated_timer class

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: 2QdxY4Rz...@potatochowder.com
Newsgroups: comp.lang.python
Subject: Re: Waht do you think about my repeated_timer class
Date: Wed, 2 Feb 2022 22:27:24 -0600
Lines: 82
Message-ID: <mailman.30.1643862448.2976.python-list@python.org>
References: <87fsp0kbvl.fsf@munus.decebal.nl>
<YfsyX0swyT9ebSs2@cskk.homeip.net> <YftMntVfWblJKlvB@scrozzle>
<CAPTjJmqcohrT3wiTYqhJVscHXi5Dn5AnUjCd=_uch=iiwVdPRA@mail.gmail.com>
<YftZrHF20orBipmY@scrozzle>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
X-Trace: news.uni-berlin.de hJ4gt4wvna61Cjvcyu4TtgFuhIEa6OJeaqi2dpNqQDCg==
Return-Path: <2QdxY4RzWzUUiLuE@potatochowder.com>
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.001
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'def': 0.04; '2022': 0.05;
';-)': 0.07; 'demands': 0.07; ':-)': 0.09; 'angelico': 0.09;
'environments': 0.09; 'faced': 0.09; 'fails': 0.09; 'float': 0.09;
'int': 0.09; 'programmers,': 0.09; 'received:78': 0.09; 'skip:_
20': 0.09; 'subject:class': 0.09; 'though.': 0.09; 'web.': 0.09;
'"cannot': 0.16; '%s"': 0.16; '(unless': 0.16; '+1100,': 0.16;
'be,': 0.16; 'cameron': 0.16; 'float,': 0.16; 'frameworks.': 0.16;
'from:addr:2qdxy4rzwzuuilue': 0.16; 'from:addr:potatochowder.com':
0.16; 'message-id:@scrozzle': 0.16; 'nan,': 0.16; 'okay,': 0.16;
'received:136.243': 0.16; 'received:78.46': 0.16;
'received:78.46.172': 0.16; 'received:78.46.172.2': 0.16;
'received:sslproxy05.your-server.de': 0.16; 'received:www458.your-
server.de': 0.16; 'received:your-server.de': 0.16; 'resolution':
0.16; 'seconds.': 0.16; 'simpson': 0.16; 'sounds': 0.16; 'tries':
0.16; 'wrote:': 0.16; 'problem': 0.16; 'python': 0.16; 'feb':
0.17; 'instead': 0.17; 'thu,': 0.19; 'to:addr:python-list': 0.20;
"i've": 0.22; 'languages': 0.22; 'code': 0.23; 'received:de':
0.23; "i'd": 0.24; '(and': 0.25; 'anything': 0.25; 'python,':
0.25; 'actual': 0.25; 'examples': 0.25; 'programming': 0.25;
'classes': 0.26; 'normally': 0.26; "isn't": 0.27; 'chris': 0.28;
'example,': 0.28; 'code,': 0.31; 'takes': 0.31; 'raise': 0.31;
'think': 0.32; "doesn't": 0.32; 'question': 0.32; 'assume': 0.32;
'fine.': 0.32; 'negative': 0.32; 'received:136': 0.32; 'zero':
0.32; 'but': 0.32; "i'm": 0.33; 'mean': 0.34; 'header:In-Reply-
To:1': 0.34; 'complex': 0.35; 'built': 0.36; 'cases': 0.36;
'possibly': 0.36; 'work,': 0.36; 'those': 0.36; "it's": 0.37;
'way': 0.38; 'two': 0.39; 'quite': 0.39; 'added': 0.39; 'text':
0.39; 'enough': 0.39; 'use': 0.39; '(with': 0.39; 'seconds': 0.40;
'something': 0.40; 'tell': 0.60; 'including': 0.60; 'likely':
0.61; 'method': 0.61; 'here.': 0.61; "there's": 0.61; 'skip:i 20':
0.62; 'skip:b 10': 0.63; 'your': 0.64; 'less': 0.65; 'numbers':
0.67; 'types': 0.67; 'generally': 0.67; 'accept': 0.67; 'right':
0.68; 'cast': 0.69; 'following:': 0.69; 'too.': 0.70; 'float:':
0.74; 'handles': 0.76; 'unit': 0.81; 'known': 0.84; 'subject:you':
0.84; 'casting': 0.84; 'consumed': 0.84; 'received:70': 0.84;
'stuff,': 0.84; 'subject:think': 0.84; 'weird,': 0.84; 'written:':
0.84; 'micro': 0.91; 'number.': 0.91; 'word.': 0.91; 'believed':
0.93
Mail-Followup-To: python-list@python.org
Content-Disposition: inline
In-Reply-To: <CAPTjJmqcohrT3wiTYqhJVscHXi5Dn5AnUjCd=_uch=iiwVdPRA@mail.gmail.com>
X-Authenticated-Sender: 2QdxY4RzWzUUiLuE@potatochowder.com
X-Virus-Scanned: Clear (ClamAV 0.103.5/26441/Wed Feb 2 10:43:13 2022)
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: <YftZrHF20orBipmY@scrozzle>
X-Mailman-Original-References: <87fsp0kbvl.fsf@munus.decebal.nl>
<YfsyX0swyT9ebSs2@cskk.homeip.net>
<YftMntVfWblJKlvB@scrozzle>
<CAPTjJmqcohrT3wiTYqhJVscHXi5Dn5AnUjCd=_uch=iiwVdPRA@mail.gmail.com>
 by: 2QdxY4Rz...@potatochowder.com - Thu, 3 Feb 2022 04:27 UTC

On 2022-02-03 at 15:07:22 +1100,
Chris Angelico <rosuav@gmail.com> wrote:

> On Thu, 3 Feb 2022 at 14:52, <2QdxY4RzWzUUiLuE@potatochowder.com> wrote:
> >
> > On 2022-02-03 at 12:39:43 +1100,
> > Cameron Simpson <cs@cskk.id.au> wrote:
> >
> > > You have:
> > >
> > > def _check_interval(self, interval):
> > > if not type(interval) in [int, float]:
> > > raise TypeError('{} is not numeric'.format(interval))
> > >
> > > This check is better written:
> > >
> > > if not isinstance(interval, (int,float)):
> > >
> > > which handles subclasses of these types (but note that bool subclasses
> > > int :-) normally we don't care), or behaviourally:
> > >
> > > try:
> > > interval = float(interval)
> > > except ValueError as e:
> > > raise TypeError(
> > > "cannot convert %s:%r to float: %s"
> > > % (type(interval).__name__, interval, e)) from e
> > >
> > > which tries to convert to float and fails if that does not work, which
> > > supports classes with a __float__ method (these classes are rare, but
> > > decimal.Decimal is one example).
> >
> > I think this can be simplified for time intervals to the following:
> >
> > if interval <= 0:
> > raise ValueError(...)
>
> That's checking something quite different, though. Casting to float
> will accept anything that can be, well, cast to float, but checking
> for less than or equal to zero demands that it already be some sort of
> number. It's debatable which check is more correct, but certainly this
> is not a simplification of the other code, it's a distinctly different
> validation.

Okay, "simplified" isn't quite the right word. Given two examples (with
known deficiencies) and no actual use cases or specifications, I added a
third example, which I believed was simpler (and arguably better in one
or more ways, which I explained), than the others.

> > which accepts non-negative real values; throws ValueError for negative
> > real values; and TypeError for other stuff, including complex numbers
> > (pathological types notwithstanding). One thing that doesn't work right
> > is NaNs, but I'm sure it's not the only code that acts weirdly when
> > faced with a NaM (curiously, Timer accepts a NaN, but the text I get
> > from help(Timer) in Python 3.10.2 is, well, broken).
>
> Strange. The text I get in 3.11.0a1 is fine. But in any case, there's
> always the docs on the web.
>
> https://docs.python.org/3/library/threading.html#timer-objects

help(Timer) is built into my REPL (and likely consumed by development
systems and IDEs everywhere). No web necessary.

> > FWIW, I'd find some way to tell users the units (seconds, milliseconds,
> > fortnights, etc.) instead of making them wade through your code to find
> > the call to (and possibly the [broken] help text of) Timer.
>
> In anything in Python, assume that the unit is seconds. With anything
> that accepts floats (where these can be distinguished from integers),
> assume the unit is seconds. If it accepts micro or nanoseconds, it'll
> almost certainly be called "high resolution timer" (unless it accepts
> two args, sec and us/ns, but that's pretty obvious), so you can
> generally exclude those too. The only real question is whether
> sleep(int) takes seconds or milliseconds, which isn't a problem here.
>
> Citation: I've slept in many many programming languages and
> frameworks. Which sounds seriously weird, but you're all programmers,
> you know what I mean :)

I've slept in enough programming environments to know better than to
assume anything. ;-)

Re: Waht do you think about my repeated_timer class

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: ros...@gmail.com (Chris Angelico)
Newsgroups: comp.lang.python
Subject: Re: Waht do you think about my repeated_timer class
Date: Thu, 3 Feb 2022 15:37:01 +1100
Lines: 72
Message-ID: <mailman.31.1643863033.2976.python-list@python.org>
References: <87fsp0kbvl.fsf@munus.decebal.nl>
<YfsyX0swyT9ebSs2@cskk.homeip.net> <YftMntVfWblJKlvB@scrozzle>
<CAPTjJmqcohrT3wiTYqhJVscHXi5Dn5AnUjCd=_uch=iiwVdPRA@mail.gmail.com>
<YftZrHF20orBipmY@scrozzle>
<CAPTjJmpvNO66V0i9Q9-k0uBvBQzZKi3aC1LxqJ2m4CkvxrZtpw@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
X-Trace: news.uni-berlin.de UI67oM9wXQFFRHuEo4Z9CA4BchEeGeiRnjChCNK+2l4g==
Return-Path: <rosuav@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=FHkliGxt;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.005
X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'def': 0.04; '2022': 0.05;
';-)': 0.07; 'demands': 0.07; ':-)': 0.09; 'angelico': 0.09;
'environments': 0.09; 'fails': 0.09; 'float': 0.09; 'int': 0.09;
'skip:_ 20': 0.09; 'subject:class': 0.09; 'though.': 0.09;
'unsure': 0.09; 'web.': 0.09; '"cannot': 0.16; '%s"': 0.16;
'+1100,': 0.16; 'be,': 0.16; 'cameron': 0.16; 'chrisa': 0.16;
'float,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris
angelico': 0.16; 'okay,': 0.16; 'simpson': 0.16; 'tries': 0.16;
'wrote:': 0.16; 'feb': 0.17; 'probably': 0.17; 'thu,': 0.19;
'to:addr:python-list': 0.20; "i've": 0.22; '(and': 0.25;
'anything': 0.25; 'actual': 0.25; 'examples': 0.25; 'programming':
0.25; 'classes': 0.26; 'normally': 0.26; "isn't": 0.27; 'chris':
0.28; 'example,': 0.28; 'code,': 0.31; 'raise': 0.31; 'think':
0.32; 'assume': 0.32; 'fine.': 0.32; 'here,': 0.32; 'zero': 0.32;
'message-id:@mail.gmail.com': 0.32; 'but': 0.32; "i'm": 0.33;
'header:In-Reply-To:1': 0.34; 'received:google.com': 0.34; 'fine':
0.35; 'from:addr:gmail.com': 0.35; 'built': 0.36; 'cases': 0.36;
'work,': 0.36; "it's": 0.37; 'received:209.85': 0.37; 'could':
0.38; 'received:209': 0.39; 'two': 0.39; 'quite': 0.39; 'added':
0.39; 'text': 0.39; 'enough': 0.39; 'use': 0.39; '(with': 0.39;
'something': 0.40; 'best': 0.61; 'likely': 0.61; 'method': 0.61;
"there's": 0.61; 'skip:i 20': 0.62; 'skip:b 10': 0.63; 'less':
0.65; 'let': 0.66; 'types': 0.67; 'primary': 0.67; 'accept': 0.67;
'right': 0.68; 'cast': 0.69; 'following:': 0.69; 'float:': 0.74;
'(you': 0.76; 'handles': 0.76; 'known': 0.84; 'subject:you': 0.84;
'casting': 0.84; 'consumed': 0.84; 'subject:think': 0.84;
'written:': 0.84; 'number.': 0.91; 'word.': 0.91; 'believed': 0.93
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=7KVJmwsLGdI747BE2c+au2od7hMBW2u9JQnCOFfNQ4Q=;
b=FHkliGxt0D/fIPjlo+hthBzObaet41Nu7fAnNww9RVMoc2lNgK+q9j5N5E80T/ddmP
l36nhvmiFD1LkWPvNuriP6ECCC8L06s36n21c39awusmqvYgMjocq2fY5I/JLyssIqft
18QW5emWBCRgGPuquVxzemS0+JajmntyoxvylDiQ73jMJuuB+TizBZ5yk/1/yZTTOMra
he6fjThxVcSIp+614ILwnhvpLEFaEH3FIKWU2k9Bm79LRX8ugZYCC05TWm6sW0Sl6pLt
0YyEEWUuIGvjn1SiVNzEKj/eiDFQbsSJIy8bF4Ue8vESYL+2GP+maGbFmbFxQDdCb8cn
EOQg==
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=7KVJmwsLGdI747BE2c+au2od7hMBW2u9JQnCOFfNQ4Q=;
b=ZlfMBhJ6y75I6qTsZyae2tCzo7O0okOyaPNQZTHOO7lLfFu6wsIwL6LTruPXqD2lMh
FcXZeurG8vDxT6qkZUwS0Gu4xC77qUcX5QfIO31GFKuRw+o73Ailie7dFruUIZK8U8FL
9KMo15pBymGKNk7PtwZYVC2H3MFnUiycM0q0EJbWi6clBGxGCVrW/ncYdgleQY5I+VaN
NTuH0cyaAlNrAyJqYomjic07xdMogL8X/2MbB90zQRxVWs76Z/bJbO7TQzfxUFRq7jbP
MXw2zAjiWEki3vmJgLPxgy4+YQkgFbtuQCTW0OcWs6494YSSITx/QFep7iBx0LFPGjxS
YXqg==
X-Gm-Message-State: AOAM532mGRG68eT2ryv/vQuFszFMpWRBNgCkpljnm8yu/CP0mE8lIMSe
TZrSYR2kEz3cmp9OaNLBkSoyVPqwSzdmoFgQQfI7o5XO
X-Google-Smtp-Source: ABdhPJyfO+xe1VPNeb9jOxVb+9xD8FP0+xXMQyi4Gda2Itp8JatRcmqKthLZNZTFGT+ACSq/bXc8/SxCNp+I3vF9QMU=
X-Received: by 2002:adf:e350:: with SMTP id n16mr26620888wrj.160.1643863032207;
Wed, 02 Feb 2022 20:37:12 -0800 (PST)
In-Reply-To: <YftZrHF20orBipmY@scrozzle>
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: <CAPTjJmpvNO66V0i9Q9-k0uBvBQzZKi3aC1LxqJ2m4CkvxrZtpw@mail.gmail.com>
X-Mailman-Original-References: <87fsp0kbvl.fsf@munus.decebal.nl>
<YfsyX0swyT9ebSs2@cskk.homeip.net>
<YftMntVfWblJKlvB@scrozzle>
<CAPTjJmqcohrT3wiTYqhJVscHXi5Dn5AnUjCd=_uch=iiwVdPRA@mail.gmail.com>
<YftZrHF20orBipmY@scrozzle>
 by: Chris Angelico - Thu, 3 Feb 2022 04:37 UTC

On Thu, 3 Feb 2022 at 15:28, <2QdxY4RzWzUUiLuE@potatochowder.com> wrote:
>
> On 2022-02-03 at 15:07:22 +1100,
> Chris Angelico <rosuav@gmail.com> wrote:
>
> > On Thu, 3 Feb 2022 at 14:52, <2QdxY4RzWzUUiLuE@potatochowder.com> wrote:
> > >
> > > On 2022-02-03 at 12:39:43 +1100,
> > > Cameron Simpson <cs@cskk.id.au> wrote:
> > >
> > > > You have:
> > > >
> > > > def _check_interval(self, interval):
> > > > if not type(interval) in [int, float]:
> > > > raise TypeError('{} is not numeric'.format(interval))
> > > >
> > > > This check is better written:
> > > >
> > > > if not isinstance(interval, (int,float)):
> > > >
> > > > which handles subclasses of these types (but note that bool subclasses
> > > > int :-) normally we don't care), or behaviourally:
> > > >
> > > > try:
> > > > interval = float(interval)
> > > > except ValueError as e:
> > > > raise TypeError(
> > > > "cannot convert %s:%r to float: %s"
> > > > % (type(interval).__name__, interval, e)) from e
> > > >
> > > > which tries to convert to float and fails if that does not work, which
> > > > supports classes with a __float__ method (these classes are rare, but
> > > > decimal.Decimal is one example).
> > >
> > > I think this can be simplified for time intervals to the following:
> > >
> > > if interval <= 0:
> > > raise ValueError(...)
> >
> > That's checking something quite different, though. Casting to float
> > will accept anything that can be, well, cast to float, but checking
> > for less than or equal to zero demands that it already be some sort of
> > number. It's debatable which check is more correct, but certainly this
> > is not a simplification of the other code, it's a distinctly different
> > validation.
>
> Okay, "simplified" isn't quite the right word. Given two examples (with
> known deficiencies) and no actual use cases or specifications, I added a
> third example, which I believed was simpler (and arguably better in one
> or more ways, which I explained), than the others.

Fair :) I'm unsure which is the best check here, or whether it's worth
having any check at all (you could just let the Timer - or
time.sleep() - do the checks).

> > Strange. The text I get in 3.11.0a1 is fine. But in any case, there's
> > always the docs on the web.
> >
> > https://docs.python.org/3/library/threading.html#timer-objects
>
> help(Timer) is built into my REPL (and likely consumed by development
> systems and IDEs everywhere). No web necessary.

Agreed, and my primary response is that it's fine in my 3.11, so it's
probably something fixable. The web docs are a good fallback though.

> I've slept in enough programming environments to know better than to
> assume anything. ;-)

Excellent :)

ChrisA

Re: Waht do you think about my repeated_timer class

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!rocksolid2!news.neodome.net!fu-berlin.de!uni-berlin.de!not-for-mail
From: ros...@gmail.com (Chris Angelico)
Newsgroups: comp.lang.python
Subject: Re: Waht do you think about my repeated_timer class
Date: Thu, 3 Feb 2022 15:49:57 +1100
Lines: 47
Message-ID: <mailman.32.1643863810.2976.python-list@python.org>
References: <CABbU2U8fCh-qR5QdyFwgBAmQEi+6nzsbNK=2Jjk=QZxHf=h3CQ@mail.gmail.com>
<177517EB-2AA2-47AA-B974-100188449961@barrys-emacs.org>
<CAPTjJmpqQW-T3OHAJi_n1BkzJCYE9VuRCv+n-v=T6A1_3o+Axw@mail.gmail.com>
<mailman.23.1643842739.2976.python-list@python.org>
<87k0eckd5w.fsf@munus.decebal.nl>
<CAPTjJmp52PZc-toTuBYDdJUr=2Jku6uwtVeUFAiYi7fv4+WnJQ@mail.gmail.com>
<mailman.25.1643855014.2976.python-list@python.org>
<877dack3ee.fsf@munus.decebal.nl>
<CAPTjJmqL+8bDM6n=KBPGwa0xua9Up9jgR06MsDadX2jS6tDv=g@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Trace: news.uni-berlin.de Wsg1JqRd9An2QazaAqSW4AmDhvmUN2jSDCPVmIfdaaog==
Return-Path: <rosuav@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=fwyVtzC1;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.027
X-Spam-Evidence: '*H*': 0.95; '*S*': 0.00; '2022': 0.05; 'thread':
0.05; ';-)': 0.07; 'angelico': 0.09; 'smaller': 0.09;
'subject:class': 0.09; 'timing': 0.09; 'way?': 0.09; 'writes:':
0.09; 'chrisa': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris
angelico': 0.16; 'question,': 0.16; 'seconds.': 0.16; 'things,':
0.16; 'url:time': 0.16; 'wait.': 0.16; 'wrote:': 0.16; 'feb':
0.17; 'instead': 0.17; 'figure': 0.19; 'thu,': 0.19; 'to:addr
:python-list': 0.20; 'creates': 0.22; 'code': 0.23; 'run': 0.23;
'creating': 0.27; 'else': 0.27; 'function': 0.27; 'chris': 0.28;
'wrong': 0.28; 'it,': 0.29; 'takes': 0.31; 'putting': 0.31; 'do.':
0.32; 'python-list': 0.32; 'message-id:@mail.gmail.com': 0.32;
'but': 0.32; "i'll": 0.33; 'someone': 0.34; 'header:In-Reply-
To:1': 0.34; 'received:google.com': 0.34; 'from:addr:gmail.com':
0.35; 'also,': 0.36; 'target': 0.36; "it's": 0.37;
'received:209.85': 0.37; 'could': 0.38; 'received:209': 0.39;
'quite': 0.39; 'single': 0.39; 'use': 0.39; 'wrote': 0.39;
'seconds': 0.40; 'something': 0.40; "there's": 0.61; 'ever': 0.63;
'everything': 0.63; 'your': 0.64; 'look': 0.65; 'time.': 0.66;
'time,': 0.67; 'complexity': 0.69; 'important.': 0.69; 'them,':
0.70; 'waiting': 0.73; 'supposed': 0.76; 'time:': 0.81; 'happens':
0.84; 'subject:you': 0.84; 'about,': 0.84; 'moment?': 0.84;
'ponder': 0.84; 'remainder': 0.84; 'subject:think': 0.84;
'\xe2\x80\xa6': 0.91
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
:content-transfer-encoding;
bh=WLWMhe/1z6KJF+os2xeDYyhPv4gO24+tS4HafNBPKFo=;
b=fwyVtzC145DgdnVwaSdrsxYvVzA0dQN6jl3GT6fcxwOsTcK8PTl2MzX2GOPCdBWEoG
lV01BBTScyPbLXljBWp27SiaTFFGPgKruoIWzbbdHbTattZwCxgUCJJdH0ZJf0ilIDUk
btJucyc4Tq97prvCJxpF+0gZGiYaYZDYE+Lf/eF0nYT+A7rjPUuwggFQ9yTa1kCxJ5dY
gSMjs2/0mnmp2vRAQHGWbm/p0YuA7Kx9OU57oOxOztUb6n/FlvFBCEOlqyRDnQPfyS3w
VKdyumwFNuzOSFEjWgijZaPR8EjqIn1P2k5ryWoUpOpdCD2caVZfAYn9XkTrl6Cr/leU
bHlw==
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:content-transfer-encoding;
bh=WLWMhe/1z6KJF+os2xeDYyhPv4gO24+tS4HafNBPKFo=;
b=HtM0Vdvy2/MtG/W4/wBQNNjRgV3irv64xuFWWkDrhJJfYlthdhuWStoTc1lXNc2L9V
OfpS6tVMW4qyeaf5hf8OP9eTVOjDDNkP0Xgx0DQM6UvA2HWIuyQ4aCC4xjSyQh0bcUOZ
buA3UPqZaUgGYYpSOf61MBDfjoguznG8q4m31f1Eh2IKniw8GWlEuD93dwi0geM//vlb
KQ+lXKfTYzxqku/jJQs07LCuRA3saA8INHxxYSfyYlp03tGeAOnkt+9+rf6WzptMcuQK
9u+5iIAf2pXXg42Pf4PzrTtE7/UdwVrWRPE2wOKh3OaiDRLK5B153DU1Lo28FAmiQ3sS
rlcg==
X-Gm-Message-State: AOAM532rRN8kfubj6iKcx1mjXXWcT0AqIjTeCNAu+QNWjT6o0KHhvO0N
nO6kUyyrhI4855JoQ/nwB4LaEBLHG0Z/DyUtgSFmfIwr1uY=
X-Google-Smtp-Source: ABdhPJw45qNLpQquRjmfUnE1iDHoJnoR5sS4SffoVBLq+8ZGq0CfChrG4OhIc8i7pbV0NcFAbNH+hPADJNE5pvQtkbI=
X-Received: by 2002:adf:f141:: with SMTP id y1mr27254032wro.104.1643863808427;
Wed, 02 Feb 2022 20:50:08 -0800 (PST)
In-Reply-To: <877dack3ee.fsf@munus.decebal.nl>
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: <CAPTjJmqL+8bDM6n=KBPGwa0xua9Up9jgR06MsDadX2jS6tDv=g@mail.gmail.com>
X-Mailman-Original-References: <CABbU2U8fCh-qR5QdyFwgBAmQEi+6nzsbNK=2Jjk=QZxHf=h3CQ@mail.gmail.com>
<177517EB-2AA2-47AA-B974-100188449961@barrys-emacs.org>
<CAPTjJmpqQW-T3OHAJi_n1BkzJCYE9VuRCv+n-v=T6A1_3o+Axw@mail.gmail.com>
<mailman.23.1643842739.2976.python-list@python.org>
<87k0eckd5w.fsf@munus.decebal.nl>
<CAPTjJmp52PZc-toTuBYDdJUr=2Jku6uwtVeUFAiYi7fv4+WnJQ@mail.gmail.com>
<mailman.25.1643855014.2976.python-list@python.org>
<877dack3ee.fsf@munus.decebal.nl>
 by: Chris Angelico - Thu, 3 Feb 2022 04:49 UTC

On Thu, 3 Feb 2022 at 15:43, Cecil Westerhof via Python-list
<python-list@python.org> wrote:
>
> Chris Angelico <rosuav@gmail.com> writes:
>
> >> > (Side point: The OP's code is quite inefficient, as it creates a new
> >> > thread for each reiteration, but there's nothing wrong with that if
> >> > you're looking for something simple.)
> >>
> >> It is just something I wrote fast. How could I do this in a better way?
> >
> > I'll answer your question, but first and foremost: Your code was fine,
> > and if something does what it's supposed to, that is the most
> > important. Everything else is minor.
>
> I like to write efficient code and it never hurts to write better code
> as just doing what it is supposed to do. ;-)
>
> And in my case interval is .5 seconds and when someone is going to use
> it with an even smaller interval …
>
>
> > But for other ways to do things, I would recommend creating a single
> > thread function and spawning a single thread to run it, and then
> > having that function call the target every N seconds. Also, consider
>
> Have to be careful that timing keeps correct when target takes a 'lot'
> of time.
> Something to ponder about, but can wait.

Ah. In that case, I would recommend a different look at things.
Instead of waiting X time, then firing the event, then waiting X time,
consider instead an interval timer based on monotonic time:

https://docs.python.org/3/library/time.html#time.monotonic

When the timer starts, record the current monotonic time, and sleep
one interval. After the function returns, sleep the remainder of one
interval. It's up to you what happens if you ever find that the next
time point has already passed - do you call the function immediately,
or skip and wait for the next moment?

Interval timers have some complexity to them, but it's worth putting
in the time (pun intended) to figure out how these things work :)

ChrisA

Re: Waht do you think about my repeated_timer class

<87zgn8imbg.fsf@munus.decebal.nl>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Cec...@decebal.nl (Cecil Westerhof)
Newsgroups: comp.lang.python
Subject: Re: Waht do you think about my repeated_timer class
Date: Thu, 03 Feb 2022 05:52:19 +0100
Organization: Decebal Computing
Lines: 12
Message-ID: <87zgn8imbg.fsf@munus.decebal.nl>
References: <87fsp0kbvl.fsf@munus.decebal.nl>
<YfsyX0swyT9ebSs2@cskk.homeip.net> <YftMntVfWblJKlvB@scrozzle>
<mailman.28.1643860267.2976.python-list@python.org>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="92931b4d56e0fad1a0ce13c194df753c";
logging-data="19310"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/zyCxrhTQcOW+ZdlEv9784KrpAmQ1yepQ="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:g14S3k6fYVx3E820M07a5/pass4=
sha1:BygyZ3LOEov4F2pgTJqRXFQOmIE=
 by: Cecil Westerhof - Thu, 3 Feb 2022 04:52 UTC

2QdxY4RzWzUUiLuE@potatochowder.com writes:

> FWIW, I'd find some way to tell users the units (seconds, milliseconds,
> fortnights, etc.) instead of making them wade through your code to find
> the call to (and possibly the [broken] help text of) Timer.

You mean with docstring?

--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

Re: Waht do you think about my repeated_timer class

<87r18kijxb.fsf@munus.decebal.nl>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Cec...@decebal.nl (Cecil Westerhof)
Newsgroups: comp.lang.python
Subject: Re: Waht do you think about my repeated_timer class
Date: Thu, 03 Feb 2022 06:44:00 +0100
Organization: Decebal Computing
Lines: 53
Message-ID: <87r18kijxb.fsf@munus.decebal.nl>
References: <CABbU2U8fCh-qR5QdyFwgBAmQEi+6nzsbNK=2Jjk=QZxHf=h3CQ@mail.gmail.com>
<177517EB-2AA2-47AA-B974-100188449961@barrys-emacs.org>
<CAPTjJmpqQW-T3OHAJi_n1BkzJCYE9VuRCv+n-v=T6A1_3o+Axw@mail.gmail.com>
<mailman.23.1643842739.2976.python-list@python.org>
<87k0eckd5w.fsf@munus.decebal.nl>
<CAPTjJmp52PZc-toTuBYDdJUr=2Jku6uwtVeUFAiYi7fv4+WnJQ@mail.gmail.com>
<mailman.25.1643855014.2976.python-list@python.org>
<877dack3ee.fsf@munus.decebal.nl>
<CAPTjJmqL+8bDM6n=KBPGwa0xua9Up9jgR06MsDadX2jS6tDv=g@mail.gmail.com>
<mailman.32.1643863810.2976.python-list@python.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: reader02.eternal-september.org; posting-host="92931b4d56e0fad1a0ce13c194df753c";
logging-data="31899"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18HX67wkHQVKhGg+jxkrhqBW0H+OgImDyc="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:S8NJa3Am2FSQ5oEAmiAXm1cpurs=
sha1:Fy/0u/oV32wKL5NRR/AHFjbpAxo=
 by: Cecil Westerhof - Thu, 3 Feb 2022 05:44 UTC

Chris Angelico <rosuav@gmail.com> writes:

> On Thu, 3 Feb 2022 at 15:43, Cecil Westerhof via Python-list
> <python-list@python.org> wrote:
>>
>> Chris Angelico <rosuav@gmail.com> writes:
>>
>> >> > (Side point: The OP's code is quite inefficient, as it creates a new
>> >> > thread for each reiteration, but there's nothing wrong with that if
>> >> > you're looking for something simple.)
>> >>
>> >> It is just something I wrote fast. How could I do this in a better way?
>> >
>> > I'll answer your question, but first and foremost: Your code was fine,
>> > and if something does what it's supposed to, that is the most
>> > important. Everything else is minor.
>>
>> I like to write efficient code and it never hurts to write better code
>> as just doing what it is supposed to do. ;-)
>>
>> And in my case interval is .5 seconds and when someone is going to use
>> it with an even smaller interval …
>>
>>
>> > But for other ways to do things, I would recommend creating a single
>> > thread function and spawning a single thread to run it, and then
>> > having that function call the target every N seconds. Also, consider
>>
>> Have to be careful that timing keeps correct when target takes a 'lot'
>> of time.
>> Something to ponder about, but can wait.
>
> Ah. In that case, I would recommend a different look at things.
> Instead of waiting X time, then firing the event, then waiting X time,
> consider instead an interval timer based on monotonic time:
>
> https://docs.python.org/3/library/time.html#time.monotonic
>
> When the timer starts, record the current monotonic time, and sleep
> one interval. After the function returns, sleep the remainder of one
> interval. It's up to you what happens if you ever find that the next
> time point has already passed - do you call the function immediately,
> or skip and wait for the next moment?
>
> Interval timers have some complexity to them, but it's worth putting
> in the time (pun intended) to figure out how these things work :)

I will look into it.

--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

Re: Waht do you think about my repeated_timer class

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: 2QdxY4Rz...@potatochowder.com
Newsgroups: comp.lang.python
Subject: Re: Waht do you think about my repeated_timer class
Date: Thu, 3 Feb 2022 05:37:25 -0600
Lines: 24
Message-ID: <mailman.0.1643888250.27178.python-list@python.org>
References: <87fsp0kbvl.fsf@munus.decebal.nl>
<YfsyX0swyT9ebSs2@cskk.homeip.net> <YftMntVfWblJKlvB@scrozzle>
<mailman.28.1643860267.2976.python-list@python.org>
<87zgn8imbg.fsf@munus.decebal.nl> <Yfu+dQVpBVZRtVbF@scrozzle>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
X-Trace: news.uni-berlin.de WkkA0CBTAICi9cESuBBkHQaeHkwmPmKf69hrn2Yj1h6g==
Return-Path: <2QdxY4RzWzUUiLuE@potatochowder.com>
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.032
X-Spam-Evidence: '*H*': 0.94; '*S*': 0.00; 'fairly': 0.05;
'received:78': 0.09; 'situations': 0.09; 'subject:class': 0.09;
'writes:': 0.09; 'from:addr:2qdxy4rzwzuuilue': 0.16;
'from:addr:potatochowder.com': 0.16; 'message-id:@scrozzle': 0.16;
'received:136.243': 0.16; 'received:78.46': 0.16;
'received:78.46.172': 0.16; 'received:78.46.172.2': 0.16;
'received:sslproxy05.your-server.de': 0.16; 'received:www458.your-
server.de': 0.16; 'received:your-server.de': 0.16; 'urls': 0.16;
'wrote:': 0.16; 'instead': 0.17; 'to:addr:python-list': 0.20;
'code': 0.23; 'feedback': 0.23; 'received:de': 0.23; "i'd": 0.24;
'(and': 0.25; 'object': 0.26; 'code,': 0.31; 'question': 0.32;
'python-list': 0.32; 'received:136': 0.32; 'there': 0.33; 'mean':
0.34; 'skip:" 20': 0.34; 'header:In-Reply-To:1': 0.34; 'possibly':
0.36; 'source': 0.36; 'way': 0.38; 'text': 0.39; 'use': 0.39;
'potential': 0.60; 'reference': 0.60; 'tell': 0.60; 'your': 0.64;
'improve': 0.66; 'time.': 0.66; 'receive': 0.71; 'future': 0.72;
'(e.g.,': 0.76; 'perfect': 0.82; 'subject:you': 0.84; 'code)':
0.84; 'comments,': 0.84; 'received:70': 0.84; 'subject:think':
0.84; 'you)': 0.84
Mail-Followup-To: python-list@python.org
Content-Disposition: inline
In-Reply-To: <87zgn8imbg.fsf@munus.decebal.nl>
X-Authenticated-Sender: 2QdxY4RzWzUUiLuE@potatochowder.com
X-Virus-Scanned: Clear (ClamAV 0.103.5/26442/Thu Feb 3 10:22:25 2022)
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: <Yfu+dQVpBVZRtVbF@scrozzle>
X-Mailman-Original-References: <87fsp0kbvl.fsf@munus.decebal.nl>
<YfsyX0swyT9ebSs2@cskk.homeip.net>
<YftMntVfWblJKlvB@scrozzle>
<mailman.28.1643860267.2976.python-list@python.org>
<87zgn8imbg.fsf@munus.decebal.nl>
 by: 2QdxY4Rz...@potatochowder.com - Thu, 3 Feb 2022 11:37 UTC

On 2022-02-03 at 05:52:19 +0100,
Cecil Westerhof via Python-list <python-list@python.org> wrote:

> 2QdxY4RzWzUUiLuE@potatochowder.com writes:
>
> > FWIW, I'd find some way to tell users the units (seconds, milliseconds,
> > fortnights, etc.) instead of making them wade through your code to find
> > the call to (and possibly the [broken] help text of) Timer.
>
> You mean with docstring?

Docstring, comments, error/exception text, external documentation, URLs
or other references in the source code (docstring, comments), the name
of the object in question (e.g., instead of "interval," call it
"interval_seconds," or "seconds_between_runs"). *Something*. Or more
than one of the above. A reference to the Timer class's documentation.
There are a lot of options.

Pick a place (or more than one!) that will be fairly conspicuous in as
many situations to as many users (including future you) or potential
users (including your future code) as possible.

In a perfect world, you will receive feedback and/or use your own code,
and improve your choice(s) and method(s) over time.

Re: Waht do you think about my repeated_timer class

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: bar...@barrys-emacs.org (Barry)
Newsgroups: comp.lang.python
Subject: Re: Waht do you think about my repeated_timer class
Date: Thu, 3 Feb 2022 21:42:32 +0000
Lines: 25
Message-ID: <mailman.12.1643924563.27178.python-list@python.org>
References: <877dack3ee.fsf@munus.decebal.nl>
<43D1E67E-B1C9-4CC7-AC72-351B7005EC33@barrys-emacs.org>
Mime-Version: 1.0 (1.0)
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: quoted-printable
X-Trace: news.uni-berlin.de Boa1XOvfHkc7lSflRsVJZgxGMckvgoZluo672/Aeflyw==
Return-Path: <barry@barrys-emacs.org>
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.014
X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'cc:addr:python-list':
0.09; 'from:addr:barry': 0.09; 'received:217.70': 0.09;
'received:gandi.net': 0.09; 'received:mail.gandi.net': 0.09;
'subject:class': 0.09; 'timing': 0.09; 'cc:no real name:2**0':
0.14; '2022,': 0.16; 'barry': 0.16; 'from:addr:barrys-emacs.org':
0.16; 'message-id:@barrys-emacs.org': 0.16; 'min': 0.16; 'wait.':
0.16; 'wrote:': 0.16; 'feb': 0.17; 'cc:addr:python.org': 0.20;
'run': 0.23; 'cc:2**0': 0.25; 'function': 0.27; 'takes': 0.31;
'python-list': 0.32; 'but': 0.32; 'header:In-Reply-To:1': 0.34;
'fix': 0.36; 'target': 0.36; 'class': 0.37; 'way': 0.38; 'use':
0.39; 'something': 0.40; 'your': 0.64; 'time.': 0.66;
'received:217': 0.67; 'plus': 0.73; 'subject:you': 0.84; 'about,':
0.84; 'ponder': 0.84; 'subject:think': 0.84; 'locked': 0.93
In-Reply-To: <877dack3ee.fsf@munus.decebal.nl>
X-Mailer: iPad Mail (19D50)
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: <43D1E67E-B1C9-4CC7-AC72-351B7005EC33@barrys-emacs.org>
X-Mailman-Original-References: <877dack3ee.fsf@munus.decebal.nl>
 by: Barry - Thu, 3 Feb 2022 21:42 UTC

> On 3 Feb 2022, at 04:45, Cecil Westerhof via Python-list <python-list@python.org> wrote:
>
> Have to be careful that timing keeps correct when target takes a 'lot'
> of time.
> Something to ponder about, but can wait.

You have noticed that your class does call the function at the repeat interval but
rather at the repeat interval plus processing time.

The way to fix this is to subtract the last processing elapsed time for the next interval.
Sort of a software phase locked loop.

Just before you call the run function record the time.time() as start_time.
Then you can calculate next_interval = max( .001, interval - time.time() - start_time)
I use 1ms as the min interval.

Barry

Re: Waht do you think about my repeated_timer class

<87czk3indl.fsf@munus.decebal.nl>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Cec...@decebal.nl (Cecil Westerhof)
Newsgroups: comp.lang.python
Subject: Re: Waht do you think about my repeated_timer class
Date: Thu, 03 Feb 2022 23:41:42 +0100
Organization: Decebal Computing
Lines: 43
Message-ID: <87czk3indl.fsf@munus.decebal.nl>
References: <877dack3ee.fsf@munus.decebal.nl>
<43D1E67E-B1C9-4CC7-AC72-351B7005EC33@barrys-emacs.org>
<mailman.12.1643924563.27178.python-list@python.org>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="92931b4d56e0fad1a0ce13c194df753c";
logging-data="28769"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19I39ZaXziNqTAU9JsMf82kuA75woueIrI="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:08KYstWiXqyxN0JVHsXpb8qak08=
sha1:M/Ic96lom8kvgF6NT1nUSYOjgBI=
 by: Cecil Westerhof - Thu, 3 Feb 2022 22:41 UTC

Barry <barry@barrys-emacs.org> writes:

>> On 3 Feb 2022, at 04:45, Cecil Westerhof via Python-list <python-list@python.org> wrote:
>>
>> Have to be careful that timing keeps correct when target takes a 'lot'
>> of time.
>> Something to ponder about, but can wait.
>
> You have noticed that your class does call the function at the repeat interval but
> rather at the repeat interval plus processing time.

Nope:
def _next(self):
self._timer = Timer(self._interval, self._run)
self._timer.start()

def _run(self):
self._next()
self._fn()

In _run I first set the new timer and then I execute the function. So
that will go mostly OK.

> The way to fix this is to subtract the last processing elapsed time for the next interval.
> Sort of a software phase locked loop.
>
> Just before you call the run function record the time.time() as start_time.
> Then you can calculate next_interval = max( .001, interval - time.time() - start_time)
> I use 1ms as the min interval.

But I am working on a complete rewrite to create a more efficient
class. (This means I have to change also the code that uses it.) There
I have to do something like you suggest. (I am already working on it.)

Personally I am also of the opinion that the function should finish in
less as 10% from the interval. (That was one of my rewrites.)

--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

Re: Waht do you think about my repeated_timer class

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: martinp....@gmail.com (Martin Di Paola)
Newsgroups: comp.lang.python
Subject: Re: Waht do you think about my repeated_timer class
Date: Fri, 4 Feb 2022 23:07:54 +0000
Lines: 85
Message-ID: <mailman.17.1644016083.7010.python-list@python.org>
References: <877dack3ee.fsf@munus.decebal.nl>
<43D1E67E-B1C9-4CC7-AC72-351B7005EC33@barrys-emacs.org>
<mailman.12.1643924563.27178.python-list@python.org>
<87czk3indl.fsf@munus.decebal.nl>
<20220204230754.dfimkkaackqdzjrq@gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii; format=flowed
X-Trace: news.uni-berlin.de AFpc76kP/dG7sdVV10QBXAfrOamkNxMZxvEps0sERiVQ==
Return-Path: <martinp.dipaola@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=MZjWmeZm;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.010
X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'engineer': 0.02; 'def':
0.04; '2022': 0.05; 'delayed': 0.07; 'delay': 0.09; 'describe':
0.09; 'subject:class': 0.09; 'timing': 0.09; 'writes:': 0.09;
'url:github': 0.14; 'url:mailman': 0.15; 'problem.': 0.15;
'2022,': 0.16; 'arbitrary': 0.16; 'fixes': 0.16; 'iteration':
0.16; 'min': 0.16; 'ok.': 0.16; 'rewrite': 0.16; 'secs': 0.16;
'wait.': 0.16; 'wrote:': 0.16; 'feb': 0.17; 'message-
id:@gmail.com': 0.18; 'uses': 0.19; 'calls': 0.19; 'thu,': 0.19;
'to:addr:python-list': 0.20; 'code': 0.23; 'run': 0.23; '(and':
0.25; 'url-ip:188.166.95.178/32': 0.25; 'url-ip:188.166.95/24':
0.25; 'url:listinfo': 0.25; 'url-ip:188.166/16': 0.25; 'function':
0.27; '>>>': 0.28; 'mostly': 0.28; 'takes': 0.31; 'url-ip:188/8':
0.31; '(this': 0.32; 'assume': 0.32; 'python-list': 0.32;
'unknown': 0.32; 'but': 0.32; 'there': 0.33; 'header:In-Reply-
To:1': 0.34; 'received:google.com': 0.34; 'yes,': 0.35;
'from:addr:gmail.com': 0.35; 'fix': 0.36; 'target': 0.36;
'thanks,': 0.36; 'change': 0.36; 'received:209.85': 0.37; 'class':
0.37; 'url-ip:13.107/16': 0.38; 'way': 0.38; 'means': 0.38;
'received:209': 0.39; 'two': 0.39; 'use': 0.39; '(with': 0.39;
'on.': 0.39; 'wrote': 0.39; 'serious': 0.40; 'something': 0.40;
'should': 0.40; 'complete': 0.64; 'opinion': 0.64; 'your': 0.64;
'skip:t 20': 0.66; 'time.': 0.66; 'rate': 0.67; 'solutions': 0.70;
'depending': 0.70; 'longer': 0.71; 'plus': 0.73; '10%': 0.76;
'extra': 0.84; 'subject:you': 0.84; '(that': 0.84; 'about,': 0.84;
'martin.': 0.84; 'ponder': 0.84; 'sec.': 0.84; 'subject:think':
0.84; 'url:23': 0.84; 'locked': 0.93
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112;
h=date:from:to:subject:message-id:mail-followup-to:'user-agent
:references:mime-version:content-disposition:in-reply-to;
bh=pf8VnJGTnjFTYHN4Vh4w1XQ+Ay8W8ZGYcbzgY5KkzIg=;
b=MZjWmeZmLfjyyR1HhB32RWmpPtdb4GDfRB5xgwuLMhSq1pTQYy5DcVUQiZFTma/Wu+
N8kU3SEWubYZlF3wmqGgtq6lR1Zg/0Wu9zm2aSAfPuC0Q5O8m8wKJnsAuBWOK3PT5+63
HC9uQ4p9iVkYjEFjhx5+9sIsWtr+SjkY9mzU5tkxFyaLaaU4S5taIacFxKH5stx+NfRm
GbJCH47EOebXa79w6tf7odgOmqL4kn3xVUYa7hlq62RKELuzpdudzcaK3Fi312pA3Wmj
KW1kA6E6LD6H00f4/n+PbAINiHsE5uAh3dHTIIzLOJoxi3eG9LbW7HbrKBsIC6zDapmi
1Efg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20210112;
h=x-gm-message-state:date:from:to:subject:message-id:mail-followup-to
:'user-agent:references:mime-version:content-disposition:in-reply-to;
bh=pf8VnJGTnjFTYHN4Vh4w1XQ+Ay8W8ZGYcbzgY5KkzIg=;
b=X9VGIEIu7ulchThIT8047dTL1gRojo09H2Tivqu8sLoUnsTRu49AAUFdijzXRloA9I
6Z0KLJnDrHMY+Aqfv5A9Z91hA6pWkT8GlL1k4ZXX5zARv/PREOYOC5CBT4b5AlpNry3j
TdJCt+rkB2gzHm0XV8FPDgXE5f5b2JATgxbKdbFYYflhNLzdrGUc4Q5LpUfCtbZ3agg6
7F8SEM4ANO7asfG7FXpKZtK0A2LwCRMHvh2fWi+Kzi45zseCAUUZBtRBps9iVbo2o0iN
eOYegkged+xZVEl4ivBp8jDvzlo8T3SViUE4JIoplJTHh5DuiOi7PuRXKq0R1VofFY1J
6s/A==
X-Gm-Message-State: AOAM532FEBfTw/9TMbI5D47no58hHHprJnVgjj2w1gKCUXUJIVsoVjik
pdmzvfaZEFAIx4w7nH49ZcaDIon+XAjRvQ==
X-Google-Smtp-Source: ABdhPJygNxPpPYP6K+xVu1iyArVFBSc6eEdTELWnpmTUEZVa5YgMe+fpjWcDqtEd3AeqEliZKbOC9Q==
X-Received: by 2002:a05:6808:1452:: with SMTP id
x18mr2444909oiv.207.1644016080042;
Fri, 04 Feb 2022 15:08:00 -0800 (PST)
Mail-Followup-To: python-list@python.org
'User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101
Thunderbird/52.5.2 Lightning/5.4.5.2'
Content-Disposition: inline
In-Reply-To: <87czk3indl.fsf@munus.decebal.nl>
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: <20220204230754.dfimkkaackqdzjrq@gmail.com>
X-Mailman-Original-References: <877dack3ee.fsf@munus.decebal.nl>
<43D1E67E-B1C9-4CC7-AC72-351B7005EC33@barrys-emacs.org>
<mailman.12.1643924563.27178.python-list@python.org>
<87czk3indl.fsf@munus.decebal.nl>
 by: Martin Di Paola - Fri, 4 Feb 2022 23:07 UTC

>In _run I first set the new timer and then I execute the function. So
>that will go mostly OK.

Yes, that's correct however you are not taking into consideration the
imprecision of the timers.

Timer will call the next _run() after self._interval *plus* some unknown
arbitrary time (and extra delay).

Let's assume that when you setup an 1 sec Timer but the Timer calls
_run() after 1.01 secs due this unknown extra delay.

The first time fn() should be called after 1 sec since the begin but it
is called after 1.01 secs so the extra delay was of 0.01 sec.

The second time fn() should be called after 2 secs since the begin but
it is called after 2.02 secs. The second fn() was delayed not by 0.01
but by 0.02 secs.

The third fn() will be delayed by 0.03 secs and so on.

This arbitrary delay is very small however it will sum up on each
iteration and depending of your application can be a serious problem.

I wrote a post about this and how to create "constant rate loops" which
fixes this problem:
https://book-of-gehn.github.io/articles/2019/10/23/Constant-Rate-Loop.html

In the post I also describe two solutions (with their trade-offs) for
when the target function fn() takes longer than the self._interval time.

See if it helps.

Thanks,
Martin.

On Thu, Feb 03, 2022 at 11:41:42PM +0100, Cecil Westerhof via
Python-list wrote:
>Barry <barry@barrys-emacs.org> writes:
>
>>> On 3 Feb 2022, at 04:45, Cecil Westerhof via Python-list <python-list@python.org> wrote:
>>>
>>> Have to be careful that timing keeps correct when target takes a 'lot'
>>> of time.
>>> Something to ponder about, but can wait.
>>
>> You have noticed that your class does call the function at the repeat interval but
>> rather at the repeat interval plus processing time.
>
>Nope:
> def _next(self):
> self._timer = Timer(self._interval, self._run)
> self._timer.start()
>
> def _run(self):
> self._next()
> self._fn()
>
>In _run I first set the new timer and then I execute the function. So
>that will go mostly OK.
>
>
>> The way to fix this is to subtract the last processing elapsed time for the next interval.
>> Sort of a software phase locked loop.
>>
>> Just before you call the run function record the time.time() as start_time.
>> Then you can calculate next_interval = max( .001, interval - time.time() - start_time)
>> I use 1ms as the min interval.
>
>But I am working on a complete rewrite to create a more efficient
>class. (This means I have to change also the code that uses it.) There
>I have to do something like you suggest. (I am already working on it.)
>
>
>Personally I am also of the opinion that the function should finish in
>less as 10% from the interval. (That was one of my rewrites.)
>
>--
>Cecil Westerhof
>Senior Software Engineer
>LinkedIn: http://www.linkedin.com/in/cecilwesterhof
>--
>https://mail.python.org/mailman/listinfo/python-list

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor