使用 argparse 选项重构
Refactor by using argparse options
尝试清理它以通过 argparse 而不是 sys 将时间作为参数传递。这个想法是将时间参数作为输入并再次搜索存储桶,转换它们并 return 匹配。它适用于 sys.argv 值,但这显然是有限的。尝试使用 argparse 选项进行重构。
你会看到
start_time = sys.argv[3]
end_time = sys.argv[5]
硬编码
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()
运行 进入 if start.startswith(start_time) 或 end.startswith(start_time) == start_time 中的问题:
尝试为 times
传递 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()
->
$ python3 test.py -s 09-17-2015 -e 12-01-2019
starts with start_time
尝试清理它以通过 argparse 而不是 sys 将时间作为参数传递。这个想法是将时间参数作为输入并再次搜索存储桶,转换它们并 return 匹配。它适用于 sys.argv 值,但这显然是有限的。尝试使用 argparse 选项进行重构。
你会看到
start_time = sys.argv[3] end_time = sys.argv[5] 硬编码
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()
运行 进入 if start.startswith(start_time) 或 end.startswith(start_time) == start_time 中的问题: 尝试为 times
传递 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()
->
$ python3 test.py -s 09-17-2015 -e 12-01-2019
starts with start_time