3



I am trying to read a text file in Python using a new Class and OOP but I can't find the right way to do it without using imperative programming like this.

def read_operators_file(file_name):
        in_file=open(file_name)
        for i in range(constants.HEADER_TOTAL_LINES):
            in_file.readline()
        operators=[]
        for line in in_file:
            name, nationality, domain, hours, duration = line.strip().split(', ')
            duration=int(duration)
            domain=tuple(domain.strip('(').strip(')').split('; '))
            operators.append((name, nationality, domain, hours, duration))
        in_file.close()
        return operators


def read_requests_file(file_name):
        in_file = open(file_name)
        for i in range(constants.HEADER_TOTAL_LINES):
            in_file.readline()
        requests = []
        for line in in_file:
            name, language, domain, service, duration = line.strip().split(', ')
            duration = int(duration)
            requests.append((name, language, domain, service, duration))
        in_file.close()
        return requests

Thanks,
mikeysantana

1
  • You can certainly wrap those functions in a class, but what's the benefit of using a class? Do you need some state to survive between invocations? Commented Mar 6, 2018 at 15:22

1 Answer 1

2

While you certainly could convert this to an OOP style, I don't think that it would help much. Instead, I'd suggest trying an FP style. You can define another function, taking the different conversion functions as parameters, e.g. str as basically no-operation, int, or a lambda for more complex stuff. Also, you should use with for opening files.

Something like this (not tested):

def read_fields(file_name, functions):
    with open(file_name) as in_file:
        for i in range(constants.HEADER_TOTAL_LINES):
            in_file.readline()
        result = []
        for line in in_file:
            fields = [f(x) for f, x in zip(functions) line.strip().split(', ')]
            result.extend(fields)
        return result

def read_operators_file(file_name):
    f_domain = lambda d: tuple(d.strip('(').strip(')').split('; '))
    # fields:                    name nat  domain    hrs  dur
    return read_fields(file_name, (str, str, f_domain, str, int))

def read_requests_file(file_name):
    # fields:                    name lang domn serv dur
    return read_fields(file_name, (str, str, str, str, int))

Or using a generator function:

def read_fields(file_name, functions):
    with open(file_name) as in_file:
        for i in range(constants.HEADER_TOTAL_LINES):
            in_file.readline()
        for line in in_file:
            yield from (f(x) for f, x in zip(functions) line.strip().split(', '))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.