More Transformations

Here's some example code for you to reference as you work through the assignment:

def multiply(x, y):
    return x * y

def add(x, y):
    return x + y

def self_math(math_func):
    def inner_func(x):
        return math_func(x, x)
    return inner_func

square_func = self_math(multiply)
double_func = self_math(add)

print(square_func(5))
# prints 25

print(double_func(5))
# prints 10

Assignment

Complete the doc_format_checker_and_converter function.

It takes a conversion_function and a list of valid_formats as parameters. It should return a new function that takes two parameters of its own:

  • filename: The name of the file to be converted

  • content: The content (body text) of the file to be converted

If the file extension of the filename is in the valid_formats list, then it should return the result of calling the conversion_function on the content. Otherwise, it should raise a ValueError with the message invalid file format

Tip

I used the .split() method on the filename to get the file extension. You can use the in keyword to check if a value is in a list.

The capitalize_content and reverse_content are "conversion functions" that will be passed into our doc_format_checker_and_converter function by the tests.

Solution

def doc_format_checker_and_converter(conversion_function, valid_formats):
    
    def  checker(filename,content):
        file_name, extension = filename.split(".")
        if extension in valid_formats:
            return conversion_function(content)
        else:
            raise ValueError("invalid file format")
    return checker


# Don't edit below this line


def capitalize_content(content):
    return content.upper()


def reverse_content(content):
    return content[::-1]

The solution defines the doc_format_checker_and_converter function, which takes two parameters:

  1. conversion_function: A function that will convert the content of a file if its format is valid.

  2. valid_formats: A list of valid file extensions.

What's happening:

  1. Inside doc_format_checker_and_converter, you define a new function called checker. This function accepts two parameters:

    • filename: The name of the file to be converted.

    • content: The content of the file to be converted.

  2. The filename.split(".") line splits the filename into the base name (file_name) and its extension (extension).

  3. If the extension is in the valid_formats list, the checker function will call the conversion_function with the content as an argument and return the result of the conversion.

  4. If the extension is not in the valid_formats list, a ValueError is raised with the message "invalid file format".

  5. Finally, doc_format_checker_and_converter returns the checker function.

This approach allows you to dynamically check the file format and apply different conversion functions to the content depending on the format.

Example Usage:

checker = doc_format_checker_and_converter(capitalize_content, ["txt", "md"])

# This will call capitalize_content because the extension is 'txt'
print(checker("document.txt", "hello world"))

# This will raise a ValueError since 'pdf' is not in the valid_formats list
print(checker("document.pdf", "hello world"))

In the code above, when you call checker("document.txt", "hello world"), it checks the file extension, finds it in valid_formats, and calls capitalize_content. If the extension is not valid, like in checker("document.pdf", "hello world"), it raises a ValueError.

Last updated