import os
import datetime
def get_holidays(holidays_file):
with open(holidays_file, 'r') as file:
holidays = file.read().splitlines()
holidays = [datetime.datetime.strptime(date, '%Y-%m-%d').date() for date in holidays]
return set(holidays)
def get_previous_workday(date, holidays):
previous_day = date - datetime.timedelta(days=1)
while previous_day.weekday() >= 5 or previous_day in holidays:
previous_day -= datetime.timedelta(days=1)
return previous_day
def get_last_saturday(date):
last_saturday = date - datetime.timedelta(days=(date.weekday() + 2) % 7 + 1)
return last_saturday
def rename_files_in_directory(directory, holidays_file):
today = datetime.date.today()
holidays = get_holidays(holidays_file)
previous_workday = get_previous_workday(today, holidays)
last_saturday = get_last_saturday(today)
for root, dirs, files in os.walk(directory):
for file in files:
new_date = None
if 'tweb' in file:
new_date = previous_workday
elif today.weekday() == 0: # Monday
new_date = last_saturday
else:
new_date = today
old_date_str = file.split('_')[1] # 假设日期在文件名的特定位置
old_date = datetime.datetime.strptime(old_date_str, '%Y%m%d').date()
new_date_str = new_date.strftime('%Y%m%d')
new_file = file.replace(old_date_str, new_date_str)
os.rename(os.path.join(root, file), os.path.join(root, new_file))
# 示例调用
directory_path = '/path/to/your/directory'
holidays_file = '/path/to/holidays.txt'
rename_files_in_directory(directory_path, holidays_file)
import os
import csv
from datetime import datetime
def extract_unique_dates(directory, date_field, date_format='%Y-%m-%d %H:%M:%S'):
unique_dates = set()
for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith('.csv'):
file_path = os.path.join(root, file)
with open(file_path, 'r') as f:
reader = csv.DictReader(f)
for row in reader:
date_str = row.get(date_field)
if date_str:
date_obj = datetime.strptime(date_str, date_format)
date_only = date_obj.date() # 提取日期部分
unique_dates.add(date_only)
return unique_dates
# 示例调用
directory_path = '/path/to/your/directory'
date_field = 'your_date_field'
unique_dates = extract_unique_dates(directory_path, date_field)
print("Unique Dates:")
for date in unique_dates:
print(date)
Feature: Hello world feature
Scenario Outline: Print hello world with <greeting>
Given I have a greeting "<greeting>"
When I print the greeting
Then I should see "<expected_output>"
Examples:
| greeting | expected_output |
| "Hello, World!" | "Hello, World!" |
| "Bonjour, monde!" | "Bonjour, monde!" |
import pytest
from pytest_bdd import given, when, then, parsers
# 加载 .feature 文件内容
with open('hello.feature', 'r') as f:
feature_content = f.read()
# 解析 Scenario Outline 中的 Examples 表格
scenarios = pytest.bdd.scenarios(feature_content)
# 手动实现 Scenario Outline 的参数化过程
for scenario in scenarios:
@pytest.bdd.scenario(scenario)
def test_scenario(scenario):
pass
@given(parsers.parse('I have a greeting "{greeting}"'))
def i_have_a_greeting(greeting):
return greeting
@when('I print the greeting')
def i_print_the_greeting(i_have_a_greeting, capsys):
print(i_have_a_greeting)
return i_have_a_greeting
@then(parsers.parse('I should see "{expected_output}"'))
def i_should_see_hello_world(expected_output, capsys):
captured = capsys.readouterr()
assert captured.out.strip() == expected_output
from datetime import datetime, timedelta
def read_public_holidays(file_path):
"""读取公共假期文本文件,并返回日期列表。"""
holidays = []
with open(file_path, 'r') as file:
for line in file:
line = line.strip()
if line:
try:
holiday_date = datetime.strptime(line, '%Y-%m-%d').date()
holidays.append(holiday_date)
except ValueError:
print(f"Ignoring invalid date format: {line}")
return holidays
def get_previous_workdays(date, num_days, holidays):
"""获取指定日期前num_days个工作日的日期,避开holidays中的日期。"""
workdays = []
delta = 1 # 从前一天开始查找
while len(workdays) < num_days:
# 计算前delta天的日期
previous_date = date - timedelta(days=delta)
# 检查日期是否为工作日且不是公共假期
if previous_date.weekday() < 5 and previous_date not in holidays:
workdays.append(previous_date)
delta += 1
return workdays
# 输入参数
specified_date = datetime(2024, 7, 9).date() # 替换为你的指定日期
num_previous_days = 2 # 获取前2个工作日
public_holidays_file = "public_holidays.txt" # 替换为你的公共假期文件路径
# 读取公共假期列表
holidays = read_public_holidays(public_holidays_file)
# 获取前num_previous_days个工作日
previous_workdays = get_previous_workdays(specified_date, num_previous_days, holidays)
# 打印结果
print(f"指定日期 {specified_date} 的前 {num_previous_days} 个工作日是:")
for i, day in enumerate(previous_workdays, 1):
print(f"工作日 {i}: {day}")