Dashboard is the Observer (few observers possible). Observers are interested in any change in a Subject KPI source is a Publisher/Subject. Bug warning: dangling reference (it can stop garbage collector). Solution: add __enter__ and __exit__ methods to AbsObserver and AbsSubject classes (abstract observer classes) so it can detach itself. Use 'with' statements with context managers. These 2 functions are required to turn a class definition into a context manager. """ from kpis import KPIs from current_kpis import CurrentKPIs from forecast_kpis import ForecastKPIs ''' USING CONTEXT MANAGERS ''' with KPIs() as kpis: with CurrentKPIs(kpis), ForecastKPIs(kpis): kpis.set_kpis(25, 10, 5) kpis.set_kpis(100, 50, 30) kpis.set_kpis(50, 10, 20) print('\n***Exited context managers.***\n\n') kpis.set_kpis(150, 110, 120) # this will not display anything, no subscribers left ''' WITHOUT CONTEXT MANAGERS ## Report on current KPI values # instanciate KPIs kpis = KPIs() # instanciate 2 observers
from kpis import KPIs from currentkpis import CurrentKPIs from forecastkpis import ForecastKPIs # report on the current KPI values kpis = KPIs() currentKPIs = CurrentKPIs(kpis) forecastKPIs = ForecastKPIs(kpis) kpis.set_kpis(25, 10, 5) kpis.set_kpis(100, 50, 30) kpis.set_kpis(50, 10, 20) print('\n*** detaching the currentKPIs observer') kpis.detach(currentKPIs) kpis.set_kpis(150, 110, 120)
from kpis import KPIs from currentkpis import CurrentKPIs from forecastkpis import ForecastKPIs # Report on current KPI values kpis = KPIs() with CurrentKPIs(kpis), ForecastKPIs(kpis): kpis.set_kpis(25, 10, 5) kpis.set_kpis(100, 50, 30) kpis.set_kpis(50, 10, 20) print("*****No longer in context manager.\n\n") kpis.set_kpis(150, 110, 120)
from kpis import KPIs from currentkpis import CurrentKPIs from forecastkpis import ForecastKPIs # Report on current KPI values with KPIs() as kpis: # * Subject with CurrentKPIs(kpis), ForecastKPIs(kpis): # * Observers kpis.set_kpis(25, 10, 5) kpis.set_kpis(100, 50, 30) kpis.set_kpis(50, 10, 20) print('\n***Exited context managers.***\n\n') kpis.set_kpis(150, 110, 120)
from kpis import KPIs from currentkpis import CurrentKPIs from forecastkpis import ForecastKPIs # Report on current KPI values kpis = KPIs() currentKPIs = CurrentKPIs(kpis) forecastKPIs = ForecastKPIs(kpis) kpis.set_kpis(34, 46, 22) kpis.set_kpis(76, 29, 40) kpis.set_kpis(10, 20, 30) print('\n ***** DEtaching the currentKPI observer ** \n\n') kpis.detach(currentKPIs) kpis.set_kpis(150, 160, 170)
from kpis import KPIs from kpis_actuales import KPIs_Actuales from kpis_pronostico import KPIs_Pronostico #Reporte de KPI actuales kpis = KPIs() kpis_actuales = KPIs_Actuales(kpis) kpis_pronostico = KPIs_Pronostico(kpis) kpis.set_kpis(25, 10, 5) kpis.set_kpis(100, 50, 30) kpis.set_kpis(50, 10, 20) print('\n**** Desacoplando el observador KPIs_Actuales ****\n\n') kpis.detach(kpis_actuales) kpis.set_kpis(150, 110, 120)
from kpis import KPIs from currentkpis import CurrentKPIs from forecastkpis import ForecastKPIs # Report on current KPI values kpis = KPIs() # * Subject current_kpis = CurrentKPIs(kpis) # * Observer1 forecast_kpis = ForecastKPIs(kpis) # * Observer2 kpis.set_kpis(25, 10, 5) kpis.set_kpis(100, 50, 30) kpis.set_kpis(50, 10, 20) print('\n***Detaching the currentKPIs observer.***\n\n') kpis.detach(current_kpis) kpis.set_kpis(150, 110, 120)
from kpis import KPIs from currentkpis import CurrentKPIs from forecastkpis import ForecastKPIs # Report on current KPI values kpis = KPIs() currentKPIs = CurrentKPIs(kpis) forecastKPIs = ForecastKPIs(kpis) kpis.set_kpis(25, 10, 5) kpis.set_kpis(100, 50, 30) kpis.set_kpis(50, 10, 20) print ('\n***Detaching the currentKPIs observer.***\n\n') kpis.detach(currentKPIs) kpis.set_kpis(150, 110, 120)