Skip to content

Commit 49b8479

Browse files
09
1 parent ff21ce2 commit 49b8479

File tree

4 files changed

+396
-0
lines changed

4 files changed

+396
-0
lines changed

M09_errorhandling/Practica1.ipynb

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 5,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"## Pruebas de caja negra\n",
10+
"import unittest"
11+
]
12+
},
13+
{
14+
"cell_type": "code",
15+
"execution_count": 6,
16+
"metadata": {},
17+
"outputs": [],
18+
"source": [
19+
"def suma (num1, num2):\n",
20+
" return num1 + num2"
21+
]
22+
},
23+
{
24+
"cell_type": "code",
25+
"execution_count": 8,
26+
"metadata": {},
27+
"outputs": [],
28+
"source": [
29+
"class CajaNegraTest(unittest.TestCase):\n",
30+
"\n",
31+
" def test_suma_dos_positivos(self):\n",
32+
" num1 = 10\n",
33+
" num2 = 5\n",
34+
"\n",
35+
" resultado = suma(num1, num2)\n",
36+
"\n",
37+
" self.assertEqual(resultado, 15)\n",
38+
"\n",
39+
" def test_suma_dos_negativos(self):\n",
40+
" num1 = -10\n",
41+
" num2= -7\n",
42+
"\n",
43+
" resultado = suma(num1, num2)\n",
44+
"\n",
45+
" self.assertEqual(resultado, -17)\n"
46+
]
47+
},
48+
{
49+
"cell_type": "code",
50+
"execution_count": 1,
51+
"metadata": {},
52+
"outputs": [
53+
{
54+
"ename": "NameError",
55+
"evalue": "name 'unittest' is not defined",
56+
"output_type": "error",
57+
"traceback": [
58+
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
59+
"\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
60+
"Cell \u001b[1;32mIn[1], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43munittest\u001b[49m\u001b[38;5;241m.\u001b[39mmain(argv \u001b[38;5;241m=\u001b[39m[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m\"\u001b[39m], verbosity\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m2\u001b[39m, exit \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mFalse\u001b[39;00m)\n\u001b[0;32m 2\u001b[0m test_suma_dos_positivos(__main__\u001b[38;5;241m.\u001b[39mCajaNegraTest)\n\u001b[0;32m 3\u001b[0m test_suma_dos_negativos(__main__\u001b[38;5;241m.\u001b[39mCajaNegraTest)\n",
61+
"\u001b[1;31mNameError\u001b[0m: name 'unittest' is not defined"
62+
]
63+
}
64+
],
65+
"source": [
66+
"unittest.main(argv =[\"\"], verbosity=2, exit = False)\n",
67+
"test_suma_dos_positivos(__main__.CajaNegraTest)\n",
68+
"test_suma_dos_negativos(__main__.CajaNegraTest)\n"
69+
]
70+
}
71+
],
72+
"metadata": {
73+
"kernelspec": {
74+
"display_name": "Python 3",
75+
"language": "python",
76+
"name": "python3"
77+
},
78+
"language_info": {
79+
"codemirror_mode": {
80+
"name": "ipython",
81+
"version": 3
82+
},
83+
"file_extension": ".py",
84+
"mimetype": "text/x-python",
85+
"name": "python",
86+
"nbconvert_exporter": "python",
87+
"pygments_lexer": "ipython3",
88+
"version": "3.12.0"
89+
}
90+
},
91+
"nbformat": 4,
92+
"nbformat_minor": 2
93+
}

M09_errorhandling/Practica2.ipynb

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"import unittest"
10+
]
11+
},
12+
{
13+
"cell_type": "code",
14+
"execution_count": 2,
15+
"metadata": {},
16+
"outputs": [],
17+
"source": [
18+
"def es_mayor_de_edad(edad):\n",
19+
" if edad >= 18:\n",
20+
" return True\n",
21+
" else:\n",
22+
" return False"
23+
]
24+
},
25+
{
26+
"cell_type": "code",
27+
"execution_count": 3,
28+
"metadata": {},
29+
"outputs": [],
30+
"source": [
31+
"class Prueba_de_cristal_test(unittest.TestCase):\n",
32+
"\n",
33+
" def es_mayor_de_edad(self): # El metodo verifica si la función devuelve true si se le da una edad de 20\n",
34+
"\n",
35+
" edad = 20 # Definimos una variable con un valor de 20\n",
36+
"\n",
37+
" resultado = es_mayor_de_edad(edad) # llamamos a la función pasando edad como argumento y se almacena el resultado en la variable resultado\n",
38+
"\n",
39+
" self.assertEqual(resultado, True) # metodo de la clase test.case para comprobar si el resultado es True\n",
40+
" \n",
41+
" \n",
42+
" def es_menor_de_edad(self): # El metodo verifica si la función devuelve false\n",
43+
"\n",
44+
" edad = 15 \n",
45+
"\n",
46+
" resultado = es_mayor_de_edad(edad) \n",
47+
"\n",
48+
" self.assertEqual(resultado, False)"
49+
]
50+
},
51+
{
52+
"cell_type": "code",
53+
"execution_count": 4,
54+
"metadata": {},
55+
"outputs": [
56+
{
57+
"name": "stderr",
58+
"output_type": "stream",
59+
"text": [
60+
"\n",
61+
"----------------------------------------------------------------------\n",
62+
"Ran 0 tests in 0.000s\n",
63+
"\n",
64+
"NO TESTS RAN\n"
65+
]
66+
},
67+
{
68+
"data": {
69+
"text/plain": [
70+
"<unittest.main.TestProgram at 0x235fa691760>"
71+
]
72+
},
73+
"execution_count": 4,
74+
"metadata": {},
75+
"output_type": "execute_result"
76+
}
77+
],
78+
"source": [
79+
"unittest.main(argv= [\"\"], verbosity=2, exit = False)\n"
80+
]
81+
}
82+
],
83+
"metadata": {
84+
"kernelspec": {
85+
"display_name": "Python 3",
86+
"language": "python",
87+
"name": "python3"
88+
},
89+
"language_info": {
90+
"codemirror_mode": {
91+
"name": "ipython",
92+
"version": 3
93+
},
94+
"file_extension": ".py",
95+
"mimetype": "text/x-python",
96+
"name": "python",
97+
"nbconvert_exporter": "python",
98+
"pygments_lexer": "ipython3",
99+
"version": "3.12.0"
100+
}
101+
},
102+
"nbformat": 4,
103+
"nbformat_minor": 2
104+
}

M09_errorhandling/debugging.ipynb

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"def multiplica_por_dos(lista):\n",
10+
" lista_multiplicada = []\n",
11+
" for num in lista:\n",
12+
" lista_multiplicada.append(num * 2)\n",
13+
" return lista_multiplicada\n",
14+
"\n"
15+
]
16+
},
17+
{
18+
"cell_type": "code",
19+
"execution_count": 3,
20+
"metadata": {},
21+
"outputs": [],
22+
"source": [
23+
"def divide_elementos_de_lista(lista, divisor):\n",
24+
" '''\n",
25+
" Cada elemento de una lista es dividida por un divisor definido.\n",
26+
" En caso de error de tipo ZeroDivisionError que\n",
27+
" significa error al dividir en cero\n",
28+
" la función devuelve la lista inicial\n",
29+
" '''\n",
30+
"\n",
31+
" try:\n",
32+
" return [i / divisor for i in lista]\n",
33+
"\n",
34+
" except ZeroDivisionError as e:\n",
35+
" print(e)\n",
36+
" return lista\n"
37+
]
38+
},
39+
{
40+
"cell_type": "code",
41+
"execution_count": 5,
42+
"metadata": {},
43+
"outputs": [
44+
{
45+
"name": "stdout",
46+
"output_type": "stream",
47+
"text": [
48+
"division by zero\n",
49+
"[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n"
50+
]
51+
}
52+
],
53+
"source": [
54+
"lista = list(range(10))\n",
55+
"divisor = 0\n",
56+
"\n",
57+
"print(divide_elementos_de_lista(lista, divisor))"
58+
]
59+
}
60+
],
61+
"metadata": {
62+
"kernelspec": {
63+
"display_name": "Python 3",
64+
"language": "python",
65+
"name": "python3"
66+
},
67+
"language_info": {
68+
"codemirror_mode": {
69+
"name": "ipython",
70+
"version": 3
71+
},
72+
"file_extension": ".py",
73+
"mimetype": "text/x-python",
74+
"name": "python",
75+
"nbconvert_exporter": "python",
76+
"pygments_lexer": "ipython3",
77+
"version": "3.12.0"
78+
}
79+
},
80+
"nbformat": 4,
81+
"nbformat_minor": 2
82+
}

M09_errorhandling/exep_afirm.ipynb

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"def dividir(num1, num2):\n",
10+
" try:\n",
11+
" resultado = num1 / num2\n",
12+
" return resultado\n",
13+
" except ZeroDivisionError:\n",
14+
" print(\"No se puede dividir por cero.\")\n",
15+
" return None"
16+
]
17+
},
18+
{
19+
"cell_type": "code",
20+
"execution_count": 2,
21+
"metadata": {},
22+
"outputs": [
23+
{
24+
"name": "stdout",
25+
"output_type": "stream",
26+
"text": [
27+
"No se puede dividir por cero.\n"
28+
]
29+
}
30+
],
31+
"source": [
32+
"dividir(12, 0)"
33+
]
34+
},
35+
{
36+
"cell_type": "code",
37+
"execution_count": 3,
38+
"metadata": {},
39+
"outputs": [
40+
{
41+
"name": "stdout",
42+
"output_type": "stream",
43+
"text": [
44+
"El archivo no existe.\n"
45+
]
46+
}
47+
],
48+
"source": [
49+
"try:\n",
50+
" with open(\"archivo.txt\", \"r\") as f:\n",
51+
" contenido = f.read()\n",
52+
"except FileNotFoundError:\n",
53+
" print(\"El archivo no existe.\")"
54+
]
55+
},
56+
{
57+
"cell_type": "code",
58+
"execution_count": 4,
59+
"metadata": {},
60+
"outputs": [],
61+
"source": [
62+
"def primera_letra(lista_de_palabras):\n",
63+
" primeras_letras = []\n",
64+
"\n",
65+
" for palabra in lista_de_palabras:\n",
66+
" assert type(palabra) == str, f'{palabra} no es str'\n",
67+
" assert len(palabra) > 0, 'No se permiten str vacíos'\n",
68+
"\n",
69+
" primeras_letras.append(palabra[0])\n",
70+
" return primeras_letras"
71+
]
72+
},
73+
{
74+
"cell_type": "code",
75+
"execution_count": 6,
76+
"metadata": {},
77+
"outputs": [
78+
{
79+
"ename": "AssertionError",
80+
"evalue": "42 no es str",
81+
"output_type": "error",
82+
"traceback": [
83+
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
84+
"\u001b[1;31mAssertionError\u001b[0m Traceback (most recent call last)",
85+
"Cell \u001b[1;32mIn[6], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43mprimera_letra\u001b[49m\u001b[43m(\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mHola\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mMundo\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m42\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m\n",
86+
"Cell \u001b[1;32mIn[4], line 5\u001b[0m, in \u001b[0;36mprimera_letra\u001b[1;34m(lista_de_palabras)\u001b[0m\n\u001b[0;32m 2\u001b[0m primeras_letras \u001b[38;5;241m=\u001b[39m []\n\u001b[0;32m 4\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m palabra \u001b[38;5;129;01min\u001b[39;00m lista_de_palabras:\n\u001b[1;32m----> 5\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m \u001b[38;5;28mtype\u001b[39m(palabra) \u001b[38;5;241m==\u001b[39m \u001b[38;5;28mstr\u001b[39m, \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mpalabra\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m no es str\u001b[39m\u001b[38;5;124m'\u001b[39m\n\u001b[0;32m 6\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(palabra) \u001b[38;5;241m>\u001b[39m \u001b[38;5;241m0\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mNo se permiten str vacíos\u001b[39m\u001b[38;5;124m'\u001b[39m\n\u001b[0;32m 8\u001b[0m primeras_letras\u001b[38;5;241m.\u001b[39mappend(palabra[\u001b[38;5;241m0\u001b[39m])\n",
87+
"\u001b[1;31mAssertionError\u001b[0m: 42 no es str"
88+
]
89+
}
90+
],
91+
"source": [
92+
"primera_letra([\"Hola\", \"Mundo\", 42])"
93+
]
94+
}
95+
],
96+
"metadata": {
97+
"kernelspec": {
98+
"display_name": "Python 3",
99+
"language": "python",
100+
"name": "python3"
101+
},
102+
"language_info": {
103+
"codemirror_mode": {
104+
"name": "ipython",
105+
"version": 3
106+
},
107+
"file_extension": ".py",
108+
"mimetype": "text/x-python",
109+
"name": "python",
110+
"nbconvert_exporter": "python",
111+
"pygments_lexer": "ipython3",
112+
"version": "3.12.0"
113+
}
114+
},
115+
"nbformat": 4,
116+
"nbformat_minor": 2
117+
}

0 commit comments

Comments
 (0)