Skip to content

Commit

Permalink
Modernize Exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
its-ChaTTy committed Mar 8, 2024
1 parent 64f9da8 commit 0a887f6
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 55 deletions.
39 changes: 23 additions & 16 deletions _sources/lectures/TWP54/TWP54_2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,31 @@ Atrapando la excepción
Manejo de excepciones
---------------------

.. code-block :: python
def salvar_dados():
try:
fileD = open("encomendas.txt","a")
fileD.write("Destino:\n")
fileD.write("%s\n" %destino.get())
fileD.write("Descripcion:\n")
fileD.write("%s\n" %descripcion.get())
fileD.write("Habla:\n")
fileD.write("%s\n" %Habla.get("1.0",END))
destino.set(None)
descricao.delete(0,END)
endereco.delete("1.0",END)
except Excepción como excepciones:
app.title('archivo de grabación %s' %excepcion)
.. activecode:: ac_l54_2a
:nocodelens:
:language: python3
:python3_interpreter: brython

from browser import window, prompt, alert

def guardar_datos():
try:
destino = prompt("Ingrese Destino")
if not destino:
raise ValueError("El destino no puede estar vacío")
window.sessionStorage["Destino"] = destino
descripcion = prompt("Ingrese Descripción")
if not descripcion:
raise ValueError("La descripción no puede estar vacía")
window.sessionStorage["Descripción"] = descripcion
discurso = prompt("Ingrese Discurso")
if not discurso:
raise ValueError("El discurso no puede estar vacío")
window.sessionStorage["Discurso"] = discurso
except Exception as excepcion:
alert(f'Error: {excepcion}')

guardar_datos()

+ Obs.: para você testar a exceção deverá alterar as propriedades do arquivo

Expand Down
32 changes: 20 additions & 12 deletions _sources/lectures/TWP54/TWP54_2_en.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,31 @@ Catching the Exception
Handling Exceptions
---------------------

.. code-block :: python
.. activecode:: ac_l54_2a_en
:nocodelens:
:language: python3
:python3_interpreter: brython

from browser import window, prompt, alert

def save_data():
try:
fileD = open("orders.txt","a")
fileD.write("Destination:\n")
fileD.write("%s\n" %destination.get())
fileD.write("Description:\n")
fileD.write("%s\n" %description.get())
fileD.write("Speech:\n")
fileD.write("%s\n" %Speech.get("1.0",END))
destination.set(None)
description.delete(0,END)
address.delete("1.0",END)
destination = prompt("Enter Destination")
if not destination:
raise ValueError("Destination cannot be empty")
window.sessionStorage["Destination"] = destination
description= prompt("Enter Description")
if not description:
raise ValueError("Description cannot be empty")
window.sessionStorage["Description"] = description
speech= prompt("Enter Speech")
if not speech:
raise ValueError("Speech cannot be empty")
window.sessionStorage["Speech"] = speech
except Exception as exception:
app.title('Recording file %s' %exception)
alert(f'Error: {exception}')

save_data()


+ Note: to test the exception you will need to change the properties of the file
Expand Down
52 changes: 38 additions & 14 deletions _sources/lectures/TWP54/TWP54_3.rst
Original file line number Diff line number Diff line change
@@ -1,23 +1,47 @@
Usando un messagebox
====================

.. code-block :: python
.. activecode:: ac_l54_3a
:nocodelens:
:language: python3
:python3_interpreter: brython

def salvar_dados():
from browser import document, alert, window, html

def guardar_datos(event):
try:
fileD = open("encomendas.txt","a")
fileD.write("Destino:\n")
fileD.write("%s\n" %destino.get())
fileD.write("Descricao:\n")
fileD.write("%s\n" %descricao.get())
fileD.write("Endereco:\n")
fileD.write("%s\n" %endereco.get("1.0",END))
destino.set(None)
descricao.delete(0,END)
endereco.delete("1.0",END)
except Exception as excecao:
app.title('Erro de gravacao no arquivo %s' %excecao)
destino = entrada_destino.value
descripcion = entrada_descripcion.value
direccion = entrada_direccion.value

if not destino or not descripcion or not direccion:
raise ValueError("Todos los campos deben estar llenos")

window.sessionStorage["Destino"] = destino
window.sessionStorage["Descripción"] = descripcion
window.sessionStorage["Dirección"] = direccion
except Exception as excepcion:
alert(f'Error: {excepcion}')

app = html.DIV()

app <= html.LABEL('Destino: ')
entrada_destino = html.INPUT()
app <= entrada_destino

app <= html.LABEL('Descripción: ')
entrada_descripcion = html.INPUT()
app <= entrada_descripcion

app <= html.LABEL('Dirección: ')
entrada_direccion = html.INPUT()
app <= entrada_direccion

boton = html.BUTTON('Guardar')
boton.bind('click', guardar_datos)
app <= boton

document <= app

.. image:: ../img/TWP54_009.jpg
:height: 8.042cm
Expand Down
51 changes: 38 additions & 13 deletions _sources/lectures/TWP54/TWP54_3_en.rst
Original file line number Diff line number Diff line change
@@ -1,22 +1,47 @@
Using a messagebox
====================

.. code-block:: python
.. activecode:: ac_l54_3a_en
:nocodelens:
:language: python3
:python3_interpreter: brython

def save_data():
from browser import document, alert, window, html

def save_data(event):
try:
fileD = open("orders.txt","a")
fileD.write("Destination:\n")
fileD.write("%s\n" %destination.get())
fileD.write("Description:\n")
fileD.write("%s\n" %description.get())
fileD.write("Address:\n")
fileD.write("%s\n" %address.get("1.0",END))
destination.set(None)
description.delete(0,END)
address.delete("1.0",END)
destination = destination_input.value
description = description_input.value
address = address_input.value

if not destination or not description or not address:
raise ValueError("All fields must be filled")

window.sessionStorage["Destination"] = destination
window.sessionStorage["Description"] = description
window.sessionStorage["Address"] = address
except Exception as exception:
app.title('File writing error: %s' %exception)
alert(f'Error: {exception}')

app = html.DIV()

app <= html.LABEL('Destination: ')
destination_input = html.INPUT()
app <= destination_input

app <= html.LABEL('Description: ')
description_input = html.INPUT()
app <= description_input

app <= html.LABEL('Address: ')
address_input = html.INPUT()
app <= address_input

button = html.BUTTON('Save')
button.bind('click', save_data)
app <= button

document <= app


.. image:: ../img/TWP54_009.jpg
Expand Down

0 comments on commit 0a887f6

Please sign in to comment.