• Lenguaje

    Python

  • Descripción

    Agenda telefónica

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
129
130
131
132
133
134
135
import sys
from subprocess import call
from platform import system

path = 'contactos.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 Contacto:

    def __lt__(self, other):
        return self.alias < other.alias

    def __eq__(self, other):
        return self.alias == other.alias

    def __str__(self):
        text = ''
        text += 'alias             : ' + self.alias + '\n'
        text += 'telefono fijo     : ' + self.telefono_fijo + '\n'
        text += 'telefono movil    : ' + self.telefono_movil + '\n'
        text += 'correo electronico: ' + self.correo_electronico + '\n'
        return text

    def print_on_file(self, file_stream):
        file_stream.write (self.alias + '\t')
        file_stream.write (self.telefono_fijo + '\t')
        file_stream.write (self.telefono_movil + '\t')
        file_stream.write (self.correo_electronico + '\n')

contacto = Contacto()
array_var = []
try:
    file_stream = open (path, 'r')
    for string_line in file_stream:
        fields = string_line.strip().split('\t')
        datum = Contacto()
        datum.alias = alias
        datum.telefono_fijo = telefono_fijo
        datum.telefono_movil = telefono_movil
        datum.correo_electronico = correo_electronico
        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':
        contacto.alias = input ('Ingresa el alias del contacto: ')
        datum = array_var[array_var.index (contacto)] if contacto 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 = Contacto()
        datum.alias = contacto.alias
        datum.telefono_fijo = input ('Ingresa el telefono fijo: ')
        datum.telefono_movil = input ('Ingresa el telefono movil: ')
        datum.correo_electronico = input ('Ingresa el correo electronico: ')
        array_var.append (datum)
        print ('\nRegistro agregado correctamente.')
    elif main_option=='3':
        print ('Men\u00FA de actualizaci\u00F3n de campos')
        print ('1.- telefono fijo')
        print ('2.- telefono movil')
        print ('3.- correo electronico')
        suboption = 0
        while suboption<1 or suboption>3:
            suboption = int (input ('Selecciona el n\u00FAmero de campo a modificar: '))
            if suboption<1 or suboption>3:
                print ('Opci\u00F3n incorrecta')
        if suboption==1:
            datum.telefono_fijo = input ('Ingresa el telefono fijo: ')
        elif suboption==2:
            datum.telefono_movil = input ('Ingresa el telefono movil: ')
        elif suboption==3:
            datum.correo_electronico = input ('Ingresa el correo electronico: ')
        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