Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

A language that doesn't affect the way you think about programming is not worth knowing.


devel / comp.lang.python / Re: on the importance of exceptions

SubjectAuthor
* on the importance of exceptionsMeredith Montgomery
+- Re: on the importance of exceptionsJulio Di Egidio
`- Re: on the importance of exceptionsMeredith Montgomery

1
on the importance of exceptions

<86o7vtjulr.fsf@levado.to>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!aioe.org!wV23SPyuRKrhUqTV5ggwqQ.user.46.165.242.91.POSTED!not-for-mail
From: mmontgom...@levado.to (Meredith Montgomery)
Newsgroups: comp.lang.python
Subject: on the importance of exceptions
Date: Mon, 05 Sep 2022 19:59:28 -0300
Organization: Aioe.org NNTP Server
Message-ID: <86o7vtjulr.fsf@levado.to>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: gioia.aioe.org; logging-data="61409"; posting-host="wV23SPyuRKrhUqTV5ggwqQ.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Notice: Filtered by postfilter v. 0.9.2
Cancel-Lock: sha1:qY7sHRrWo9Zb2IuMaX3PJjLnO2E=
 by: Meredith Montgomery - Mon, 5 Sep 2022 22:59 UTC

I'm trying to show people that exceptions are a very nice thing to have
when it comes to detecting when something went awry somewhere. I'd like
a real-world case, though.

Here's what I'm sort of coming up with --- given my limited experience
and imagination. Below, you have f calling g caling h calling j which
returns True or False based on a random thing. (This simulates a task
that sometimes succeeds and sometimes fails.) If while at f() I want to
know what happened at j(), I'm going to have to propagate that
information upwards through the execution stack. I can't do that right
now: I'd have to change f, g and h.

--8<---------------cut here---------------start------------->8---
from random import randint

def f():
g()

def g():
h()

def h():
if j():
print("I got a 2.")
else:
print("I got a 1.")

def j():
return randint(1,2) == 2
--8<---------------cut here---------------end--------------->8---

If instead of that, j() would be raising an exception when it fails,
then all I only need to change f() to know what happens.

I could replace j's test with opening a file, say. That would improve
it a little. I'm sure you guys know excellent cases to show. Would you
be so kind to share anything you might have in mind?

Re: on the importance of exceptions

<92c7cb8b-ca50-4b9e-8745-099999a16bfen@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
X-Received: by 2002:ac8:7d92:0:b0:344:aa94:4798 with SMTP id c18-20020ac87d92000000b00344aa944798mr41301835qtd.511.1662420632765;
Mon, 05 Sep 2022 16:30:32 -0700 (PDT)
X-Received: by 2002:a05:6871:8a8:b0:127:7ae0:92d3 with SMTP id
r40-20020a05687108a800b001277ae092d3mr2800600oaq.32.1662420632555; Mon, 05
Sep 2022 16:30:32 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.python
Date: Mon, 5 Sep 2022 16:30:32 -0700 (PDT)
In-Reply-To: <86o7vtjulr.fsf@levado.to>
Injection-Info: google-groups.googlegroups.com; posting-host=93.41.97.142; posting-account=F3H0JAgAAADcYVukktnHx7hFG5stjWse
NNTP-Posting-Host: 93.41.97.142
References: <86o7vtjulr.fsf@levado.to>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <92c7cb8b-ca50-4b9e-8745-099999a16bfen@googlegroups.com>
Subject: Re: on the importance of exceptions
From: jul...@diegidio.name (Julio Di Egidio)
Injection-Date: Mon, 05 Sep 2022 23:30:32 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 1270
 by: Julio Di Egidio - Mon, 5 Sep 2022 23:30 UTC

On Tuesday, 6 September 2022 at 01:00:03 UTC+2, Meredith Montgomery wrote:

> I'm trying to show people that exceptions are a very nice thing to have

Welcome to my kill-file.

*Plonk*

Julio

Re: on the importance of exceptions

<8635d5i6z0.fsf@levado.to>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!aioe.org!wV23SPyuRKrhUqTV5ggwqQ.user.46.165.242.91.POSTED!not-for-mail
From: mmontgom...@levado.to (Meredith Montgomery)
Newsgroups: comp.lang.python
Subject: Re: on the importance of exceptions
Date: Mon, 05 Sep 2022 23:15:15 -0300
Organization: Aioe.org NNTP Server
Message-ID: <8635d5i6z0.fsf@levado.to>
References: <86o7vtjulr.fsf@levado.to>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: gioia.aioe.org; logging-data="41748"; posting-host="wV23SPyuRKrhUqTV5ggwqQ.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Notice: Filtered by postfilter v. 0.9.2
Cancel-Lock: sha1:iwWRHHNVOHC+YMqWey4JNhOPBGw=
 by: Meredith Montgomery - Tue, 6 Sep 2022 02:15 UTC

Meredith Montgomery <mmontgomery@levado.to> writes:

> I'm trying to show people that exceptions are a very nice thing to have
> when it comes to detecting when something went awry somewhere. I'd like
> a real-world case, though.

Here's my contribution. I want to handle all errors in main() and the
real job is done in does_a_job(), which, in turns, needs to delegate
tasks to those other procedures that fail sometimes.

It's does_a_job() that /must/ distinguish the error codes because errors
come in as False from both opens_file() and reads_file(). So the checks
must be done both in does_a_job() and in main(). (We also notice that
does_a_job() has a return-type that's a union (sometimes an integer,
sometimes a string), which makes distinguishing error code and success a
bit harder.)

--8<---------------cut here---------------start------------->8---
from random import randint

def main():
r, data = does_a_job()
if r < 0:
if r == -1:
print("Fatal. Could not open file.")
return None
if r == -2:
print("Fatal. Could not read file")
return None
print(f"Great! We got the data: ``{r}''.")

def does_a_job():
okay = opens_file()
if not okay:
return -1
okay, data = reads_file()
if not okay:
return -2
closes_file()
return data

def open_file(): # Opens okay with probability 80%
return randint(1,10) <= 8

def read_file(): # Reads okay with probability 50%
return randint(1,10) <= 5, "data I am"

def closes_file(): # Works with probability 1
return True
--8<---------------cut here---------------end--------------->8---

If we could give the program a final destination at does_a_job(), the
job would be easier. But all we want to do in does_a_job() is to
propagate the error conditions upwards to main() to really decide what
to do. Exceptions lets us do that with a cleaner version.

--8<---------------cut here---------------start------------->8---
from random import randint

def main():
try:
data = does_a_job()
except FileNotFoundError:
print("Fatal. Could not open file.")
except MemoryError:
print("Fatal. Could not read file")
else:
print(f"Great! We got the data: ``{data}''.")

def does_a_job():
open_file()
data = reads_file()
close_file()
return data

def open_file(): # Opens okay with probability 80%
if randint(1,10) <= 8:
return True
raise FileNotFoundError("Sorry: the file system is /so/ busy right now.")

def reads_file(): # Reads okay with probability 50%
if randint(1,10) <= 5:
return "data I am"
raise MemoryError("Sorry: not enough memory for /all/ this data.")

def close_file(): # Works with probability 1
return True
--8<---------------cut here---------------end--------------->8---

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor