Page 1 sur 1

[RESOLU] Alleger ce code ? [ Python ]

Posté : dim. 11 janv. 2015 23:15
par EVOTk
Bonjour,
Je tatonne en python, et j'ai actuellement un code qui sert a recupérer une certaine variable suivant la tranche horaire ! Mais j'ai fait tout "manuellement" c'est donc compréhensible, fonctionnel mais pas trop beau et très long !

Des idées pour améliorer tout sa ??

( Attention les yeux ! )
if "00:00" <= horaire <= "00:29":
activchauf = horairechauf[0]
elif "00:30" <= horaire <= "00:59":
activchauf = horairechauf[1]
elif "01:00" <= horaire <= "01:29":
activchauf = horairechauf[2]
elif "01:30" <= horaire <= "01:59":
activchauf = horairechauf[3]
elif "02:00" <= horaire <= "02:29":
activchauf = horairechauf[4]
elif "02:30" <= horaire <= "02:59":
activchauf = horairechauf[5]
elif "03:00" <= horaire <= "03:29":
activchauf = horairechauf[6]
elif "03:30" <= horaire <= "03:59":
activchauf = horairechauf[7]
elif "04:00" <= horaire <= "04:29":
activchauf = horairechauf[8]
elif "04:30" <= horaire <= "04:59":
activchauf = horairechauf[9]
elif "05:00" <= horaire <= "05:29":
activchauf = horairechauf[10]
elif "05:30" <= horaire <= "05:59":
activchauf = horairechauf[11]
elif "06:00" <= horaire <= "06:29":
activchauf = horairechauf[12]
elif "06:30" <= horaire <= "06:59":
activchauf = horairechauf[13]
elif "07:00" <= horaire <= "07:29":
activchauf = horairechauf[14]
elif "07:30" <= horaire <= "07:59":
activchauf = horairechauf[15]
elif "08:00" <= horaire <= "08:29":
activchauf = horairechauf[16]
elif "08:30" <= horaire <= "08:59":
activchauf = horairechauf[17]
elif "09:00" <= horaire <= "09:29":
activchauf = horairechauf[18]
elif "09:30" <= horaire <= "09:59":
activchauf = horairechauf[19]
elif "10:00" <= horaire <= "10:29":
activchauf = horairechauf[20]
elif "10:30" <= horaire <= "10:59":
activchauf = horairechauf[21]
elif "11:00" <= horaire <= "11:29":
activchauf = horairechauf[22]
elif "11:30" <= horaire <= "11:59":
activchauf = horairechauf[23]
elif "12:00" <= horaire <= "12:29":
activchauf = horairechauf[24]
elif "12:30" <= horaire <= "12:59":
activchauf = horairechauf[25]
elif "13:00" <= horaire <= "13:29":
activchauf = horairechauf[26]
elif "13:30" <= horaire <= "13:59":
activchauf = horairechauf[27]
elif "14:00" <= horaire <= "14:29":
activchauf = horairechauf[28]
elif "14:30" <= horaire <= "14:59":
activchauf = horairechauf[29]
elif "15:00" <= horaire <= "15:29":
activchauf = horairechauf[30]
elif "15:30" <= horaire <= "15:59":
activchauf = horairechauf[31]
elif "16:00" <= horaire <= "16:29":
activchauf = horairechauf[32]
elif "16:30" <= horaire <= "16:59":
activchauf = horairechauf[33]
elif "17:00" <= horaire <= "17:29":
activchauf = horairechauf[34]
elif "17:30" <= horaire <= "17:59":
activchauf = horairechauf[35]
elif "18:00" <= horaire <= "18:29":
activchauf = horairechauf[36]
elif "18:30" <= horaire <= "18:59":
activchauf = horairechauf[37]
elif "19:00" <= horaire <= "19:29":
activchauf = horairechauf[38]
elif "19:30" <= horaire <= "19:59":
activchauf = horairechauf[39]
elif "20:00" <= horaire <= "20:29":
activchauf = horairechauf[40]
elif "20:30" <= horaire <= "20:59":
activchauf = horairechauf[41]
elif "21:00" <= horaire <= "21:29":
activchauf = horairechauf[42]
elif "21:30" <= horaire <= "21:59":
activchauf = horairechauf[43]
elif "22:00" <= horaire <= "22:29":
activchauf = horairechauf[44]
elif "22:30" <= horaire <= "22:59":
activchauf = horairechauf[45]
elif "23:00" <= horaire <= "23:29":
activchauf = horairechauf[46]
elif "23:30" <= horaire <= "23:59":
activchauf = horairechauf[47]
Merci

Re: Alleger ce code ? [ Python ]  [RESOLU]

Posté : lun. 12 janv. 2015 08:26
par Manfraid
Salut,

pourrais tu juste précisé ce qu'il y a exactement dans la variable horaire (genre texte, date, heure datetime)

Re: Alleger ce code ? [ Python ]

Posté : lun. 12 janv. 2015 09:44
par mikebzh44
Il faudrait pouvoir avoir une variable de type time ou datetime.

Voici un petit exemple avec pour récupérer l'heure actuelle et rechercher la consigne dans ton tableau horairechauff :

Code : Tout sélectionner

# Import de la classe datetime
import datetime
# Stockage des consigne de chauffage
horairechauf=[0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1]
# Obtention de l heure actuelle
heure = datetime.datetime.now().time().hour
# Affichage de l heure et de la consigne
print "Il est %i heure" %heure
print "Consigne = %i" %horairechauf[heure]
# Forcage de l heure a 8 heure
heure = 8
# Affichage de l heure et de la consigne
print "Il est %i heure" %heure
print "Consigne = %i" %horairechauf[heure]
Et ça marche :

Code : Tout sélectionner

Il est 9 heure
Consigne = 0
Il est 8 heure
Consigne = 1

Re: Alleger ce code ? [ Python ]

Posté : lun. 12 janv. 2015 10:51
par Manfraid
et si tu travaille a la demi heure comme j'ai pus le remarquer ta plage de chauffe compte 48 valeurs et pour avoir l'index de ta valeur la formule est :

Code : Tout sélectionner

id = heure*2 + minute//30
ce qui donnerai avec le code précédent

Code : Tout sélectionner

# Import de la classe datetime
import datetime
# Stockage des consigne de chauffage
horairechauf=[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1]
# Obtention de l heure actuelle
heure = datetime.datetime.now().time().hour
minute = datetime.datetime.now().time().minute
id = heure*2 + minute//30
# Affichage de l heure et de la consigne
print "Il est {}:{}" .format(heure,minute)
print "Consigne = %i" %horairechauf[id]
# Forcage de l heure a 8 heure
heure = 8
minute = 58
id = heure*2 + minute//30
# Affichage de l heure et de la consigne
print "Il est {}:{}" .format(heure,minute)
print "Consigne = %i" %horairechauf[id]

Re: Alleger ce code ? [ Python ]

Posté : lun. 12 janv. 2015 10:57
par mikebzh44
Exact, bien vu Manfraid ! J'avais pas fait attention :p

Re: Alleger ce code ? [ Python ]

Posté : lun. 12 janv. 2015 17:12
par EVOTk
Merci a vous, j'ai réussi a faire se que je voulais en environ 99% de lignes en moins :)

C'est le
id = heure*2 + minute//30
que je chercher désespérément sans trouver la bonne relation :)

Re: [RESOLU] Alleger ce code ? [ Python ]

Posté : mar. 13 janv. 2015 05:47
par Manfraid
pas de soucis, en espérant que ce petit bout de code t'aide