1 |
#!/usr/bin/env python |
#!/usr/bin/env python |
2 |
# |
# |
3 |
#Minë. Mundo Interactivo-Narrativo en Español [Tierra-Media] |
#Minë. Mundo Interactivo-Narrativo en Español [Tierra-Media] |
4 |
#Copyright (C) 2002 Pablo Ruiz Múzquiz |
#Copyright (C) 2002 Pablo Ruiz Múzquiz |
5 |
# |
# |
6 |
# |
# |
7 |
#This program is free software; you can redistribute it and/or modify |
#This program is free software; you can redistribute it and/or modify |
8 |
#it under the terms of the GNU General Public License as published by |
#it under the terms of the GNU General Public License as published by |
9 |
#the Free Software Foundation; either version 2 of the License, or |
#the Free Software Foundation; either version 2 of the License, or |
10 |
#(at your option) any later version. |
#(at your option) any later version. |
11 |
# |
# |
12 |
#This program is distributed in the hope that it will be useful, |
#This program is distributed in the hope that it will be useful, |
13 |
#but WITHOUT ANY WARRANTY; without even the implied warranty of |
#but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
#GNU General Public License for more details. |
#GNU General Public License for more details. |
16 |
# |
# |
17 |
#You should have received a copy of the GNU General Public License |
#You should have received a copy of the GNU General Public License |
18 |
#along with this program; if not, write to the Free Software |
#along with this program; if not, write to the Free Software |
19 |
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
20 |
# |
# |
21 |
# |
# |
22 |
#Fichero: cronomine.py |
#Fichero: cronomine.py |
23 |
|
|
24 |
"""Módulo para manejar cronómetros basados en el calendario de Minë. |
"""Módulo para manejar cronómetros basados en el calendario de Minë. |
25 |
|
|
26 |
Contiene dos clases: |
Contiene dos clases: |
27 |
- CronoMine |
- CronoMine |
28 |
- CronoMineControl |
- CronoMineControl |
29 |
""" |
""" |
30 |
|
|
31 |
from time import * |
from time import * |
32 |
from fechahoramine import * |
from fechahoramine import * |
33 |
|
|
34 |
class CronoMine: |
class CronoMine: |
35 |
"""Cronometro que puede ser programado para llamar a una función al cabo de cierto tiempo. |
"""Cronometro que puede ser programado para llamar a una función al cabo de cierto tiempo. |
36 |
|
|
37 |
No es necesario guardar un CronoMine en una variable local. Él mismo se guarda dentro |
No es necesario guardar un CronoMine en una variable local. Él mismo se guarda dentro |
38 |
de su CronoMineControl, y es automáticamente destruido cuando vence. |
de su CronoMineControl, y es automáticamente destruido cuando vence. |
39 |
""" |
""" |
40 |
|
|
41 |
def __init__(self, control, vencimiento, vencimiento_func): |
def __init__(self, control, vencimiento, vencimiento_func): |
42 |
"""Crea un cronometro que empieza a correr inmediatamente. |
"""Crea un cronometro que empieza a correr inmediatamente. |
43 |
|
|
44 |
Cuando el tiempo ha vencido, la función es llamada. |
Cuando el tiempo ha vencido, la función es llamada. |
45 |
|
|
46 |
control -- el CronoMineControl que controlará a este cronometro. |
control -- el CronoMineControl que controlará a este cronometro. |
47 |
vencimiento -- el intervalo de tiempo en forma de FechaHoraMineRel. |
vencimiento -- el intervalo de tiempo en forma de FechaHoraMineRel. |
48 |
vencimiento_func -- la función que será llamada al vencimiento. La función |
vencimiento_func -- la función que será llamada al vencimiento. La función |
49 |
recibirá un parámetro: este crono. |
recibirá un parámetro: este crono. |
50 |
""" |
""" |
51 |
|
|
52 |
control.lista_cronos.append(self) |
control.lista_cronos.append(self) |
53 |
|
|
54 |
self.control = control |
self.control = control |
55 |
self.vencimiento_func = vencimiento_func |
self.vencimiento_func = vencimiento_func |
56 |
|
|
57 |
# No guardamos el intervalo de tiempos, sino la fecha en la que la |
# No guardamos el intervalo de tiempos, sino la fecha en la que la |
58 |
# cuenta termina. |
# cuenta termina. |
59 |
self.fecha_vencimiento = FechaHoraMine_ahora() + vencimiento |
self.fecha_vencimiento = FechaHoraMine_ahora() + vencimiento |
60 |
|
|
61 |
|
|
62 |
def avanzar(self, fecha_ahora): |
def avanzar(self, fecha_ahora): |
63 |
"""Para ser llamada sólo desde CronoMineControl. |
"""Para ser llamada sólo desde CronoMineControl. |
64 |
|
|
65 |
Compara la fecha actual con la de vencimiento, y si es igual o superior, |
Compara la fecha actual con la de vencimiento, y si es igual o superior, |
66 |
llama a la función de vencimiento y destruye el crono. |
llama a la función de vencimiento y destruye el crono. |
67 |
""" |
""" |
68 |
if fecha_ahora >= self.fecha_vencimiento: |
if fecha_ahora >= self.fecha_vencimiento: |
69 |
self.vencimiento_func(self) |
self.vencimiento_func(self) |
70 |
self.control.lista_cronos.remove(self) |
self.control.lista_cronos.remove(self) |
71 |
|
|
72 |
|
|
73 |
def parar(self): |
def parar(self): |
74 |
"""Detiene la cuenta y destruye el crono.""" |
"""Detiene la cuenta y destruye el crono.""" |
75 |
self.control.lista_cronos.remove(self) |
self.control.lista_cronos.remove(self) |
76 |
|
|
77 |
|
|
78 |
class CronoMineControl: |
class CronoMineControl: |
79 |
"""Controlador que contiene y avanza automáticamente a los cronómetros.""" |
"""Controlador que contiene y avanza automáticamente a los cronómetros.""" |
80 |
|
|
81 |
def __init__(self): |
def __init__(self): |
82 |
self.lista_cronos = [] |
self.lista_cronos = [] |
83 |
|
|
84 |
|
|
85 |
def run(self): |
def run(self): |
86 |
"""Visitar cíclicamente los cronos, llamando a las funciones de vencimiento |
"""Visitar cíclicamente los cronos, llamando a las funciones de vencimiento |
87 |
que correspondan. |
que correspondan. |
88 |
|
|
89 |
Esta función es llamada desde dentro de un bucle en el programa principal. |
Esta función es llamada desde dentro de un bucle en el programa principal. |
90 |
""" |
""" |
91 |
fecha_ahora = FechaHoraMine_ahora() |
fecha_ahora = FechaHoraMine_ahora() |
92 |
for crono in self.lista_cronos: |
for crono in self.lista_cronos: |
93 |
crono.avanzar(fecha_ahora) |
crono.avanzar(fecha_ahora) |
94 |
|
|
95 |
|
|
96 |
|
|
97 |
# Código para pruebas unitarias del módulo. |
# Código para pruebas unitarias del módulo. |
98 |
|
|
99 |
if (__name__ == '__main__'): |
if (__name__ == '__main__'): |
100 |
|
|
101 |
from random import random |
from random import random |
102 |
|
|
103 |
def venc_f(crono): |
def venc_f(crono): |
104 |
print "vencido" |
print "vencido" |
105 |
nuevo_venc = FechaHoraMineRel(segundos = random() * 5 + 0.5) |
nuevo_venc = FechaHoraMineRel(segundos = random() * 5 + 0.5) |
106 |
print "creado crono a " + "%0.2f" % nuevo_venc.segundos() + " segundos" |
print "creado crono a " + "%0.2f" % nuevo_venc.segundos() + " segundos" |
107 |
t = CronoMine(control, nuevo_venc, venc_f) |
t = CronoMine(control, nuevo_venc, venc_f) |
108 |
|
|
109 |
control = CronoMineControl() |
control = CronoMineControl() |
110 |
venc = FechaHoraMineRel(segundos = random() * 5 + 0.5) |
venc = FechaHoraMineRel(segundos = random() * 5 + 0.5) |
111 |
print "creado crono a " + "%0.2f" % venc.segundos() + " segundos" |
print "creado crono a " + "%0.2f" % venc.segundos() + " segundos" |
112 |
t = CronoMine(control, venc, venc_f) |
t = CronoMine(control, venc, venc_f) |
113 |
|
|
114 |
while 1: |
while 1: |
115 |
control.run() |
control.run() |