Python 使用argparse选项重构
正在尝试清理此问题,以便通过argparse而不是sys将时间作为参数传入。其思想是将时间参数作为输入,搜索bucket,转换它们并返回匹配项。它与sys.argv值一起工作,但这显然是有限的。尝试使用argparse选项进行重构 你会看到的 开始时间=sys.argv[3] 结束时间=sys.argv[5] 都是硬编码的Python 使用argparse选项重构,python,python-3.x,time,argparse,Python,Python 3.x,Time,Argparse,正在尝试清理此问题,以便通过argparse而不是sys将时间作为参数传入。其思想是将时间参数作为输入,搜索bucket,转换它们并返回匹配项。它与sys.argv值一起工作,但这显然是有限的。尝试使用argparse选项进行重构 你会看到的 开始时间=sys.argv[3] 结束时间=sys.argv[5] 都是硬编码的 import argparse import logging import os import sys import re import time ""
import argparse
import logging
import os
import sys
import re
import time
"""take CLI parameters and search against bucket, converts them and return matching dirs"""
FILE = 'bucket'
TIME_FORMAT = "%m-%d-%Y-%H:%M:%S"
def get_file():
"""Get to dir, fetch resources"""
s3 = resource('s3')
bucket = s3.Bucket(FILE)
index = sys.argv[2]
objects = bucket.objects.filter(Prefix=index)
return objects
def convert_index():
"""conversion of the epoch filenames output
"""
dir_files = get_file()
for file in dir_files:
output = file.key # file is a class type
if 'db_' in output:
output_dir = output.split('/')
times = output_dir # [1] is index name only
start_time = sys.argv[3]
end_time = sys.argv[5]
fields = times[2].split('_')
try:
start = convert_epoch(fields[1]) # first time field
end = convert_epoch(fields[2]) # second time field
if start.startswith(start_time) or end.startswith(start_time) == start_time:
print("{}\t\t is {}_{}".format(output, start, end))
logging.basicConfig(level=logging.INFO, filename='logfile.log', format='%(asctime)s - \
%(levelname)s - %(message)s')
elif end.startswith(end_time) or end.startswith(end_time) == end_time:
print("{}\t\t\t is {}_{}".format(output, start, end))
except ValueError as ve:
logging.info("did not convert due to {}".format(ve))
def convert_epoch(input_epoch):
"""takes epoch value and converts to human readable
time.strftime("%m-%d-%Y_%H-%M-%S", time.localtime(1442530758))
'09-17-2019_15-59-18'
"""
converted_epoch_value = time.strftime(TIME_FORMAT, time.localtime(int(input_epoch)))
return converted_epoch_value
def main():
parser: ArgumentParser = argparse.ArgumentParser(
description="""Supply the directory you want to search files for. Give earliest and latest time to query bucket for
usage: format_files.py [-h] [-e --earliest] [-l --latest]""")
parser.add_argument('-i', '--index', required=False, help='index name directory to query for ex:\n'
'format_files.py -i folder/folder')
parser.add_argument('-s', '--start_time', required=False, help='queries from start time (not necessarily earliest time)')
parser.add_argument('-e', '--end_time', required=False, help='queries data until end time (not necessarily latest time)')
args = parser.parse_args()
if len(sys.argv) < 2:
sys.exit(parser.description)
if args.index:
convert_index()
if __name__ == "__main__":
main()
如果start.startswithstart\u time或end.startswithstart\u time==开始时间,则会遇到问题:
当尝试传入时报的argparse选项时,请执行第行
非常感谢您的任何见解我已经删除了代码的许多复杂性,以向您展示一个简单的概念证明:
import argparse
def convert_index(args):
start_time = args.start_time
end_time = args.end_time
start = '09-17-2015_15-59-18'
end = '12-01-2019_15-59-18'
if start.startswith(start_time) or end.startswith(start_time) == start_time:
print('starts with start_time')
elif end.startswith(end_time) or end.startswith(end_time) == end_time:
print('starts with end_time')
def main():
parser: ArgumentParser = argparse.ArgumentParser()
parser.add_argument('-s', '--start_time', required=False, help='queries from start time (not necessarily earliest time)')
parser.add_argument('-e', '--end_time', required=False, help='queries data until end time (not necessarily latest time)')
args = parser.parse_args()
convert_index(args)
if __name__ == "__main__":
main()
->
start_time=args.start_time etc清理代码。如果使用argparse解析输入,则不需要使用sys.argv。请欣赏,我想我在我的function@zendannyy不客气,如果我的回答解决了你的问题,你能接受我的回答吗
$ python3 test.py -s 09-17-2015 -e 12-01-2019
starts with start_time