Function factory?

ftg at lutix.org ftg at lutix.org
Wed Oct 24 04:58:30 EDT 2018


Hello everyone,

I have 2 functions whose aim is to read a pdf file, the first one manages an uploaded file, the another one fecth a remote one (via an url).
They are quite the same:
########################
def handle_uploaded_file(path,file):
    #if os.path.isfile(path + '/' + file.name):
    #    file.name = '_' + file.name

    destination = open(path + '/' + file.name, 'wb+')
    for chunk in file.chunks():
        destination.write(chunk)
    destination.close()
    if check_file(path,file.name):
        return True
    else:
        return False

def handle_remote_file(url,path,file_name=''):
    if not file_name:
        file_name = url.split('/')[-1]

    with urllib.request.urlopen(url) as response:
        with open(path + '/' + file_name, 'wb+') as out_file:
            shutil.copyfileobj(response, out_file)
    if check_file(path,file_name):
        return True
    else:
        return False
####################

I am wondering about the way I could rationalize those 2 functions.
I have read about function factory and maybe it crosses my need.

Do you have some advices?

Thanks, best regards



More information about the Python-list mailing list