Home > front end >  Is there anyway these 2 almost similar functions can be squished into 1 general function?
Is there anyway these 2 almost similar functions can be squished into 1 general function?

Time:12-08

I have these 2 functions that are really similar except for the different format of log it will receive and return. One look and return 4 values when the other return 3. Is there any way I can make 1 general function for these 2? Thank you

> - Borrow book: B#<day>#<Student Name>#<Book name>#<days borrowed for>
> - Return book: R#<day>#<Student Name>#<Book name>


   

 def read_borrow_log(log):
    
    borrow_day = []
    borrow_student = []
    borrow_book = []
    borrow_duration = []
    
    for line in log:
        hash_func = line.find("#")
        hash_day = line.find("#", hash_func 1)
        hash_student = line.find("#", hash_day 1)
        hash_book = line.find("#", hash_student 1)
        hash_duration = line.find("#", hash_book 1)
        
        borrow_day.append(int(line[(hash_func 1):(hash_day)]))
        borrow_student.append(line[(hash_day 1):(hash_student)])
        borrow_book.append(line[(hash_student 1):(hash_duration)])
        borrow_duration.append(line[(hash_duration 1):])
            
    return borrow_day, borrow_student, borrow_book, borrow_duration

 def read_return_log(log):
    
    return_day = []
    return_student = []
    return_book = []
    
    for line in log:
        hash_func = line.find("#")
        hash_day = line.find("#", hash_func 1)
        hash_student = line.find("#", hash_day 1)
        
        return_day.append(int(line[(hash_func 1):(hash_day)]))
        return_student.append(line[(hash_day 1):(hash_student)])
        return_book.append(line[(hash_student 1):])
            
    return return_day, return_student, return_book

def main():
    borrow_day, borrow_student, borrow_book, borrow_duration = read_borrow_log(borrow_log)
    return_day, return_student, return_book = read_return_log(return_log)

CodePudding user response:

Try using python's built-in string split:

def extract_log_parts(log):
    recs = []
    for line in log:
        recs.append(line.split('#'))
    # we want the record *columns* -- transpose the table
    return tuple(map(list, zip(*recs)))

CodePudding user response:

one thing you might do is to make the 'extra' work done only when a certain optional parameter is passed in as shown:

def read_borrow_log(log,borrow_log=True):
    borrow_day = []
    borrow_student = []
    borrow_book = []
    if borrow_log is True:
        borrow_duration = []

    for line in log:
        hash_func = line.find("#")
        hash_day = line.find("#", hash_func   1)
        hash_student = line.find("#", hash_day   1)
        if borrow_log is True:
            hash_book = line.find("#", hash_student   1)
            hash_duration = line.find("#", hash_book   1)

        borrow_day.append(int(line[(hash_func   1):(hash_day)]))
        borrow_student.append(line[(hash_day   1):(hash_student)])
        borrow_book.append(line[(hash_student   1):(hash_duration)])
        if borrow_log is True:
            borrow_duration.append(line[(hash_duration   1):])

    if borrow_log is True:
        return borrow_day, borrow_student, borrow_book, borrow_duration
    else:
        return borrow_day, borrow_student, borrow_book

def main():
    borrow_day, borrow_student, borrow_book, borrow_duration = read_borrow_log(borrow_log)
    return_day, return_student, return_book = read_borrow_log(return_log,borrow_log=False)

however you might want to rethink the naming convention used since this function will now do more than one thing, which is bad for documentation purposes (and is generally a bad practice to have functions do more than one thing, bad enough that i should downvote my own answer if i can)

  • Related