Software Testing|Use holidays module to handle holidays

Insert image description here

Preface

In Python, there is a holidaysmodule called which can help you handle holiday information easily. This module provides a convenient way to determine whether a specific date is a holiday, while also supporting holiday calculations in different countries and regions. This article details how to use holidaysthe module, including installation, basic usage, and examples.

Install holidaysmodule

First, we need to install holidaysthe module. We can use the pip command to install:

pip install holidays

Basic usage

holidaysThe module is mainly used to determine whether a specific date is a holiday, and can also obtain the holiday list of a specific country or region. Here are some basic usage examples.

  1. Determine whether a specific date is a holiday
import holidays

# 创建一个节假日对象,这里选择了美国作为示例国家
us_holidays = holidays.US()

date_to_check = "2023-07-04"
if date_to_check in us_holidays:
    print(f"{
      
      date_to_check} 是美国的独立日!")
else:
    print(f"{
      
      date_to_check} 不是节假日。")

-----------
输出结果如下:
2023-07-04 是美国的独立日!
  1. Get a list of holidays for a specific country or region
import holidays

# 获取美国2023年的节假日列表
us_holidays = holidays.US(years=2023)

print("美国2023年的节假日:")
for date, name in sorted(us_holidays.items()):
    print(f"{
      
      date}: {
      
      name}")

--------------
输出结果如下:
美国2023年的节假日:
2023-01-01: New Year's Day
2023-01-02: New Year's Day (Observed)
2023-01-16: Martin Luther King Jr. Day
2023-02-20: Washington's Birthday
2023-05-29: Memorial Day
2023-06-19: Juneteenth National Independence Day
2023-07-04: Independence Day
2023-09-04: Labor Day
2023-10-09: Columbus Day
2023-11-10: Veterans Day (Observed)
2023-11-11: Veterans Day
2023-11-23: Thanksgiving
2023-12-25: Christmas Day

Get a list of holidays in China

import holidays

# 获取中国2023年的节假日列表

cn_holidays = holidays.CN(years=2023)

print("中国2023年的节假日:")
for date, name in sorted(cn_holidays.items()):
    print(f"{
      
      date}: {
      
      name}")

----------------
中国2023年的节假日:
2023-01-01: New Year's Day
2023-01-22: Chinese New Year (Spring Festival)
2023-01-23: Chinese New Year (Spring Festival)
2023-01-24: Chinese New Year (Spring Festival)
2023-04-05: Tomb-Sweeping Day
2023-05-01: Labour Day
2023-06-22: Dragon Boat Festival
2023-09-29: Mid-Autumn Festival
2023-10-01: National Day
2023-10-02: National Day
2023-10-03: National Day

Supported countries and regions

holidaysThe module supports holiday calculations in many countries and regions, and we can specify the required country or region during use. For example, holidays.US()it represents holidays in the United States, holidays.CN() represents holidays in China, and so on. We can find a complete list of countries and regions in the module's documentation. As shown below:

Insert image description here

Usage example

Here is a complete example that demonstrates how to use holidays the module to determine if a specific date is a holiday and get a list of holidays:

import holidays


def main():
    # 选择国家或地区
    selected_country = holidays.CN()

    # 判断特定日期是否是节假日
    date_to_check = "2023-12-25"
    if date_to_check in selected_country:
        print(f"{
      
      date_to_check} 是节假日:{
      
      selected_country[date_to_check]}")
    else:
        print(f"{
      
      date_to_check} 不是节假日。")

    # 获取特定国家或地区的节假日列表
    year = 2023
    print(f"{
      
      year}年的节假日列表:")
    for date, name in sorted(selected_country.items()):
        print(f"{
      
      date}: {
      
      name}")


if __name__ == "__main__":
    main()

-----------------
输出结果如下:
2023-12-25 不是节假日。
2023年的节假日列表:
2023-01-01: New Year's Day
2023-01-22: Chinese New Year (Spring Festival)
2023-01-23: Chinese New Year (Spring Festival)
2023-01-24: Chinese New Year (Spring Festival)
2023-04-05: Tomb-Sweeping Day
2023-05-01: Labour Day
2023-06-22: Dragon Boat Festival
2023-09-29: Mid-Autumn Festival
2023-10-01: National Day
2023-10-02: National Day
2023-10-03: National Day

With the above code, we can easily use the holidays module to determine whether a specific date is a holiday and get the list of holidays for a specific country or region.

Summarize

In short, holidaysthe module provides a convenient method for holiday processing, and we can easily process holiday information in different countries and regions in Python. Whether it is to determine whether a specific date is a holiday or to obtain a list of holidays, this module can be used to achieve it.

Guess you like

Origin blog.csdn.net/Tester_muller/article/details/132586606