Bonjour,
je suis occupé à essayer d'utiliser un module 'Workdays' permettant de calculer une différence entre deux dates en excluant les jours de congés que voici:
Pour son utilisation, il faut faire :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 from datetime import date, timedelta # started from the code of Casey Webster at # http://groups.google.com/group/comp.lang.python/browse_thread/thread/ddd39a02644540b7 # Define the weekday mnemonics to match the date.weekday function (MON, TUE, WED, THU, FRI, SAT, SUN) = range(7) weekends=(SAT,SUN) def networkdays(start_date, end_date, holidays=[]): delta_days = (end_date - start_date).days + 1 full_weeks, extra_days = divmod(delta_days, 7) # num_workdays = how many days/week you work * total # of weeks num_workdays = (full_weeks + 1) * (7 - len(weekends)) # subtract out any working days that fall in the 'shortened week' for d in range(1, 8 - extra_days): if (end_date + timedelta(d)).weekday() not in weekends: num_workdays -= 1 # skip holidays that fall on weekends holidays = [x for x in holidays if x.weekday() not in weekends] # subtract out any holidays for d in holidays: if d >= start_date and d <= end_date: num_workdays -= 1 return num_workdays def _in_between(a,b,x): #return cmp(a,x) * cmp(x,b) > 0 return a <= x and x <= b or b <= x and x <= a def workday(start_date,days, holidays=[]): full_weeks, extra_days = divmod(days,7 - len(weekends)) new_date = start_date + timedelta(weeks=full_weeks) for i in range(extra_days): new_date += timedelta(days=1) while new_date.weekday() in weekends: new_date += timedelta(days=1) delta = timedelta(days=1 * cmp(days,0)) # skip holidays that fall on weekends holidays = [x for x in holidays if x.weekday() not in weekends ] holidays = [x for x in holidays if x <> start_date ] for d in sorted(holidays, reverse = (days < 0)): # if d in between start and current push it out one working day if _in_between(start_date, new_date, d): new_date += delta while new_date.weekday() in weekends: new_date += delta return new_date
jusque là, ca fonctionne, je peux sans problème ajouter mes dates holidays à la liste...
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4 #workday(start_date,days,[list_of_holidays]) days = workday(date(2009,12,25),656, [ date(2009,12,25), date(2009,07,13),date(2011,02,02)] ) print days
Mais bon, pour mettre en application, je voudrais récupérer une liste depuis un fichier de style:
date(21,07,2013),date(22,07,2013),date(2013,07,14)
ou encore:
21-07-2013
22-07-2013
14-07-2013
et créer une liste avec:
mais la sortie me donne:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 # lecture d'un fichier et retourne une liste def lireFichierRetourTab(path): fichier = open(path,"r") ligne = fichier.read() ligne = ligne.split() ligne = list(ligne) return ligne; test= lireFichierRetourTab('F:\exos python\workdays-1.0.tar\workdays-1.0\conges.txt') days = workday(date(2013,07,01),12, test) print days
pourtant avec un print type(), j'ai bien un type liste ['date(21,07,2013),date(22,07,2013)']
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7 Traceback (most recent call last): File "F:\exos python\workdays-1.0.tar\workdays-1.0\workdays2.py", line 67, in <module> days = workday(date(2013,07,01),12, test) File "F:\exos python\workdays-1.0.tar\workdays-1.0\workdays2.py", line 43, in workday holidays = [x for x in holidays if x.weekday() not in weekends ] AttributeError: 'str' object has no attribute 'weekday'
Donc, en fin de compte, ma question est, comment faire pour obtenir une liste de dates dans un fichier lisibles par datetime
Un tout grand merci d'avance
Partager