我有一个读取文件路径并确定其是否为本地的函数(S3)。如果文件在S3中,我创建了一个装饰器来将文件复制到本地。应用装饰器时,函数不返回任何内容(无)。
from functools import wraps
def handle_s3_download(fcn):
@wraps(fcn)
def inner(data_fp, *args, **kwargs):
# Handle S3 path
if data_fp.startswith('s3://'):
local_data_fp = os.path.join(FOLDER,
os.path.basename(data_fp))
# Copy file only if necessary.
if not os.path.isfile(local_data_fp):
copy_file_from_s3(data_fp, local_data_fp)
data_fp = local_data_fp
# Call decorated function
fcn(data_fp, *args, **kwargs)
return inner
# Returns None
@handle_s3_download
def load_csv(data_fp):
data = []
with open(data_fp, 'rb') as f:
data = list(csv.reader(f))
return data
def load_json(data_fp):
data = []
if data_fp.startswith('s3://'):
local_data_fp = os.path.join(FOLDER,
os.path.basename(data_fp))
# Copy file only if necessary.
if not os.path.isfile(local_data_fp):
copy_file_from_s3(data_fp, local_data_fp)
data_fp = local_data_fp
with open(data_fp, 'r') as input_file:
data = json.load(input_file)
logging.info('Returning data...')
return data
load_csv('s3://test/test.csv') # Doesn't work
load_json('s3://test/test.csv') # Works
答案 0 :(得分:1)
如果是我,我想编写如下代码:
from functools import wraps
from pathlib import Path
def handle_s3_download(fcn):
@wraps(fcn)
def inner(data_fp, *args, **kwargs):
# Handle S3 path
if data_fp.startswith("s3://"):
local_data_fp = Path(FOLDER) / os.path.basename(data_fp)
# Copy file only if necessary.
if not local_data_fp.is_file():
copy_file_from_s3(data_fp, str(local_data_fp))
data_fp = str(local_data_fp)
# Call decorated function
return fcn(data_fp, *args, **kwargs)
return inner
# Returns None
@handle_s3_download
def load_csv(data_fp):
data = list(csv.reader(Path(data_fp).read_bytes()))
return data
def load_json(data_fp):
if data_fp.startswith("s3://"):
local_data_fp = os.path.join(FOLDER, os.path.basename(data_fp))
# Copy file only if necessary.
if not os.path.isfile(local_data_fp):
copy_file_from_s3(data_fp, local_data_fp)
data_fp = local_data_fp
data = json.loads(Path(data_fp).read_text())
logging.info("Returning data...")
return data
load_csv("s3://test/test.csv") # Doesn't work
load_json("s3://test/test.csv") # Works