-
Lenguaje
Python
-
Descripción
Para guardar información sobre libros, vamos a comenzar por crear una clase "Libro", que contendrá atributos "autor", "titulo", "ubicacion" (todos ellos strings) y métodos Get y Set adecuados para leer su valor y cambiarlo. Prepara también un Main (en la misma clase), que cree un objeto de la clase Libro, dé valores a sus tres atributos y luego los muestre.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import sys
from subprocess import call
from platform import system
path = 'libros.tsv'
os = system()
CLEAR_SCREEN = 'clear' if os == 'Linux' else 'cls'
main_option = '0'
def getch ():
if os == 'Linux':
import sys, tty, termios
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw (sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr (fd, termios.TCSADRAIN, old_settings)
return ch
else:
import msvcrt, sys
sys.stdout.flush()
return msvcrt.getwch()
def pause_screen (message):
print (message)
sys.stdout.write ('Presiona una tecla para continuar . . . ')
getch()
class Libro:
def __lt__(self, other):
return self.titulo < other.titulo
def __eq__(self, other):
return self.titulo == other.titulo
def __str__(self):
text = ''
text += 'titulo : ' + self.titulo + '\n'
text += 'autor : ' + self.autor + '\n'
text += 'ubicacion: ' + self.ubicacion + '\n'
return text
def print_on_file(self, file_stream):
file_stream.write (self.titulo + '\t')
file_stream.write (self.autor + '\t')
file_stream.write (self.ubicacion + '\n')
libro = Libro()
array_var = []
try:
file_stream = open (path, 'r')
for string_line in file_stream:
fields = string_line.strip().split('\t')
datum = Libro()
datum.titulo = titulo
datum.autor = autor
datum.ubicacion = ubicacion
array_var.append (datum)
file_stream.close()
except IOError:
file_stream = None
while main_option!='7':
call (CLEAR_SCREEN, shell = True)
print('MEN\u00DA')
print ('1.- Altas\n2.- Consultas\n3.- Actualizaciones')
print ('4.- Bajas\n5.- Ordenar registros\n6.- Listar registros')
sys.stdout.writeln ('7.- Salir');
sys.stdout.write ('Selecciona una opci\u00F3n: ')
main_option = '0'
while main_option<'1' or main_option>'7':
main_option = getch ()
print (main_option + '\n\n')
if len(array_var)==0 and main_option!='1' and main_option!='7':
pause_screen ('No hay registros.\n')
continue
if main_option<'5':
libro.titulo = input ('Ingresa el titulo del libro: ')
datum = array_var[array_var.index (libro)] if libro in array_var else None
if datum is not None:
print ()
print (datum)
if main_option=='1' and datum is not None:
print ('El registro ya existe.')
elif main_option>='2' and main_option<='4' and datum is None:
print ('\nRegistro no encontrado.')
elif main_option=='1':
datum = Libro()
datum.titulo = libro.titulo
datum.autor = input ('Ingresa el autor: ')
datum.ubicacion = input ('Ingresa el ubicacion: ')
array_var.append (datum)
print ('\nRegistro agregado correctamente.')
elif main_option=='3':
print ('Men\u00FA de actualizaci\u00F3n de campos')
print ('1.- autor')
print ('2.- ubicacion')
suboption = 0
while suboption<1 or suboption>2:
suboption = int (input ('Selecciona el n\u00FAmero de campo a modificar: '))
if suboption<1 or suboption>2:
print ('Opci\u00F3n incorrecta')
if suboption==1:
datum.autor = input ('Ingresa el autor: ')
elif suboption==2:
datum.ubicacion = input ('Ingresa el ubicacion: ')
print ('\nRegistro actualizado correctamente.')
elif main_option=='4':
array_var.remove (datum)
print ('Registro eliminado correctamente.')
elif main_option=='5':
array_var.sort()
print ('Registros ordenados correctamente.')
elif main_option=='6':
for e in array_var:
print (e)
print ('Total de registros: ' + repr (len (array_var)))
if main_option!='7':
pause_screen ('')
try:
file_stream = open (path, 'w')
for e in array_var:
e.print_on_file (file_stream)
file_stream.close()
except IOError:
file_stream = None
from subprocess import call
from platform import system
path = 'libros.tsv'
os = system()
CLEAR_SCREEN = 'clear' if os == 'Linux' else 'cls'
main_option = '0'
def getch ():
if os == 'Linux':
import sys, tty, termios
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw (sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr (fd, termios.TCSADRAIN, old_settings)
return ch
else:
import msvcrt, sys
sys.stdout.flush()
return msvcrt.getwch()
def pause_screen (message):
print (message)
sys.stdout.write ('Presiona una tecla para continuar . . . ')
getch()
class Libro:
def __lt__(self, other):
return self.titulo < other.titulo
def __eq__(self, other):
return self.titulo == other.titulo
def __str__(self):
text = ''
text += 'titulo : ' + self.titulo + '\n'
text += 'autor : ' + self.autor + '\n'
text += 'ubicacion: ' + self.ubicacion + '\n'
return text
def print_on_file(self, file_stream):
file_stream.write (self.titulo + '\t')
file_stream.write (self.autor + '\t')
file_stream.write (self.ubicacion + '\n')
libro = Libro()
array_var = []
try:
file_stream = open (path, 'r')
for string_line in file_stream:
fields = string_line.strip().split('\t')
datum = Libro()
datum.titulo = titulo
datum.autor = autor
datum.ubicacion = ubicacion
array_var.append (datum)
file_stream.close()
except IOError:
file_stream = None
while main_option!='7':
call (CLEAR_SCREEN, shell = True)
print('MEN\u00DA')
print ('1.- Altas\n2.- Consultas\n3.- Actualizaciones')
print ('4.- Bajas\n5.- Ordenar registros\n6.- Listar registros')
sys.stdout.writeln ('7.- Salir');
sys.stdout.write ('Selecciona una opci\u00F3n: ')
main_option = '0'
while main_option<'1' or main_option>'7':
main_option = getch ()
print (main_option + '\n\n')
if len(array_var)==0 and main_option!='1' and main_option!='7':
pause_screen ('No hay registros.\n')
continue
if main_option<'5':
libro.titulo = input ('Ingresa el titulo del libro: ')
datum = array_var[array_var.index (libro)] if libro in array_var else None
if datum is not None:
print ()
print (datum)
if main_option=='1' and datum is not None:
print ('El registro ya existe.')
elif main_option>='2' and main_option<='4' and datum is None:
print ('\nRegistro no encontrado.')
elif main_option=='1':
datum = Libro()
datum.titulo = libro.titulo
datum.autor = input ('Ingresa el autor: ')
datum.ubicacion = input ('Ingresa el ubicacion: ')
array_var.append (datum)
print ('\nRegistro agregado correctamente.')
elif main_option=='3':
print ('Men\u00FA de actualizaci\u00F3n de campos')
print ('1.- autor')
print ('2.- ubicacion')
suboption = 0
while suboption<1 or suboption>2:
suboption = int (input ('Selecciona el n\u00FAmero de campo a modificar: '))
if suboption<1 or suboption>2:
print ('Opci\u00F3n incorrecta')
if suboption==1:
datum.autor = input ('Ingresa el autor: ')
elif suboption==2:
datum.ubicacion = input ('Ingresa el ubicacion: ')
print ('\nRegistro actualizado correctamente.')
elif main_option=='4':
array_var.remove (datum)
print ('Registro eliminado correctamente.')
elif main_option=='5':
array_var.sort()
print ('Registros ordenados correctamente.')
elif main_option=='6':
for e in array_var:
print (e)
print ('Total de registros: ' + repr (len (array_var)))
if main_option!='7':
pause_screen ('')
try:
file_stream = open (path, 'w')
for e in array_var:
e.print_on_file (file_stream)
file_stream.close()
except IOError:
file_stream = None