Created
March 19, 2013 05:19
-
-
Save snit-ram/5193877 to your computer and use it in GitHub Desktop.
Arquivo usado pra exemplificar o uso do módulo timeslice aplicado ao problema de disponibilidade de links. Exemplo para o post de timeslice em meu blog
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from timeslice import TimeSlice, TimeSet | |
from datetime import datetime | |
import calendar | |
def monta_grade(): | |
grade = TimeSet() | |
dias_mes = calendar.monthrange(2013, 2)[1] | |
for dia in range(1, dias_mes + 1): | |
grade.append( | |
TimeSlice( | |
start=datetime(2013, 2, dia, 9, 0, 0), | |
end=datetime(2013, 2, dia, 18, 0, 0) | |
) | |
) | |
return grade | |
def calcula_tempo_infracao(grade): | |
queda = TimeSlice( | |
start=datetime(2013, 2, 5, 7, 0, 0), | |
end=datetime(2013, 2, 6, 22, 0, 0) | |
) | |
cliente_ausente = TimeSet([ | |
TimeSlice( | |
start=datetime(2013, 2, 5, 17, 0, 0), | |
end=datetime(2013, 2, 5, 19, 0, 0) | |
), | |
TimeSlice( | |
start=datetime(2013, 2, 6, 10, 0, 0), | |
end=datetime(2013, 2, 6, 11, 0, 0) | |
) | |
]) | |
tempo_fornecedor = queda - cliente_ausente | |
tempo_infracao = tempo_fornecedor.intersect(grade) | |
return tempo_infracao | |
def calcula_disponibilidade(tempo_infracao, grade): | |
return 100 - tempo_infracao.duration() * 100 / grade.duration() | |
grade = monta_grade() | |
tempo_infracao = calcula_tempo_infracao(grade) | |
print 'Tempo Infracao:', tempo_infracao | |
"""Out: | |
Tempo Infracao: [<TimeSlice intance (datetime.datetime(2013, 2, 5, 9, 0), datetime.datetime(2013, 2, 5, 17, 0)) >, | |
<TimeSlice intance (datetime.datetime(2013, 2, 6, 9, 0), datetime.datetime(2013, 2, 6, 10, 0)) >, | |
<TimeSlice intance (datetime.datetime(2013, 2, 6, 11, 0), datetime.datetime(2013, 2, 6, 18, 0)) >] | |
""" | |
print 'Disponibilidade: %.2f%%' % calcula_disponibilidade(tempo_infracao, grade) | |
"""Out: | |
Disponibilidade: 94.00% | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment