简体   繁体   中英

Check list item is present in Dictionary

I'm trying to extend Python - Iterate thru month dates and print a custom output and add an addtional functionality to check if a date in the given date range is national holiday, print "NH" and that corresponding date.

Ex: 2020-05-04 & 2020-05-14 are national holidays and that date alone should be printed like在此处输入图像描述

import sys
from datetime import date, datetime, timedelta

year = int(sys.argv[1])
month = int(sys.argv[2])
st_dt = int(sys.argv[3])
en_dt = int(sys.argv[4])
public_holiday = sys.argv[5].split(',')

ph = []
for d in public_holiday:
    ph.append(date(year, month, int(d)))

def daterange(startDate, endDate, delta=timedelta(days=1)):
    currentDate = startDate
    while currentDate <= endDate:
        yield currentDate
        currentDate += delta

allDays = {}
_lastDayType = None

for dte in daterange(date(year, month, st_dt), date(year, month, en_dt), delta=timedelta(days=1)):
    #print(f"sdfdsfsdf:  {dte.weekday()}")
    if dte.weekday() < 5:
         _dayType = 'working'
    else:
        _dayType = 'weekend'

    _weeknum = dte.strftime("%V")
    _key = (_weeknum, _dayType)
    if _key not in allDays:
        allDays[_key] = []
    allDays[_key].append(dte)

for k,v in allDays.items():
    week_end = ''
    gov_hol = ''
    if len(v) == 1:
        first, last = v[0], v[0]
    elif len(v) == 2 and k[1] == 'weekend':
        first, last, week_end = v[0], v[-1], 'YES'
    elif ph in allDays.values():
        gov_hol, first, last = 'NH', v[0], v[0]
    else:
        first, last = v[0], v[-1]


    print(f"{gov_hol}, {first} >> {last} >> {len(v)} >> {week_end}")

Input args: date.py 2020 5 1 31 "4, 14"

Currently, elif ph in allDays.values(): is not matching condition.

Current Output

2020-05-01 >> 2020-05-01 >> 1 >>  >> 
2020-05-02 >> 2020-05-03 >> 2 >> YES >> 
2020-05-04 >> 2020-05-08 >> 5 >>  >> 
2020-05-09 >> 2020-05-10 >> 2 >> YES >> 
2020-05-11 >> 2020-05-15 >> 5 >>  >> 
2020-05-16 >> 2020-05-17 >> 2 >> YES >> 
2020-05-18 >> 2020-05-22 >> 5 >>  >> 
2020-05-23 >> 2020-05-24 >> 2 >> YES >> 
2020-05-25 >> 2020-05-29 >> 5 >>  >> 
2020-05-30 >> 2020-05-31 >> 2 >> YES >> 

3rd column: number of days 4th column: prints "YES" if its a weekend

In general, everything you want the dates to be grouped by needs to be added to the dictionary keys:

year, month, st_dt, en_dt, public_holiday = 2020, 5, 1, 31, (4, 14)

ph = []
for d in public_holiday:
    ph.append(date(year, month, int(d)))

def daterange(startDate, endDate, delta=timedelta(days=1)):
    currentDate = startDate
    while currentDate <= endDate:
        yield currentDate
        currentDate += delta

allDays = {}
_lastDayType = None

for dte in daterange(date(year, month, st_dt), date(year, month, en_dt), delta=timedelta(days=1)):
    if dte.weekday() < 5:
         _dayType = 'working'
    else:
        _dayType = 'weekend'

    _hol = dte in ph                   # True or False
    _weeknum = dte.strftime("%V")
    _key = (_weeknum, _dayType, _hol)  # make unique rows!
    if _key not in allDays:
        allDays[_key] = []
    allDays[_key].append(dte)

for k,v in allDays.items():
    week_end = ''
    gov_hol = k[-1]
    if len(v) == 1:
        first, last = v[0], v[0]
    elif len(v) == 2 and k[1] == 'weekend':
        first, last, week_end = v[0], v[-1], 'YES'
    else:
        first, last = v[0], v[-1]

    print(f"{gov_hol}, {first} >> {last} >> {len(v)} >> {week_end}")

Output:

False, 2020-05-01 >> 2020-05-01 >> 1 >> NO
False, 2020-05-02 >> 2020-05-03 >> 2 >> YES
True, 2020-05-04 >> 2020-05-04 >> 1 >> NO
False, 2020-05-05 >> 2020-05-08 >> 4 >> NO
False, 2020-05-09 >> 2020-05-10 >> 2 >> YES
False, 2020-05-11 >> 2020-05-15 >> 4 >> NO
True, 2020-05-14 >> 2020-05-14 >> 1 >> NO
False, 2020-05-16 >> 2020-05-17 >> 2 >> YES
False, 2020-05-18 >> 2020-05-22 >> 5 >> NO
False, 2020-05-23 >> 2020-05-24 >> 2 >> YES
False, 2020-05-25 >> 2020-05-29 >> 5 >> NO
False, 2020-05-30 >> 2020-05-31 >> 2 >> YES

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM