微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用程序python的输出创建一个html文件

如何解决使用程序python的输出创建一个html文件

我必须编写一个程序,从键盘读取一些信息,然后通过工厂方法设计模式创建一个 HTML 文件一个 JSON 文件我有一个 uml 图寻求帮助 click here for image 忽略类 TextFile

我就是这样开始的

from abc import ABCMeta,abstractmethod


class File(Metaclass=ABCMeta):

    def __init__(self,title,author,paragraphs):
        self.title = title
        self.author = author
        self.paragraphs = paragraphs

    @abstractmethod
    def read_file_from_stdin(self):
        pass

class HTMLFile(File):
    def read_file_from_stdin(self):
        self.title = input("Enter the title: ")
        self.author = input("Enter the author")
        number_paragraphs = input("Enter the no of paragraphs:")
        self.paragraphs = input("Enter the paragraphs:")

    def print_html(self):
        print("<html>")
        print("<title>" + self.title + "</title")
        print("</html>")


class JSONFile(File):
    def read_file_from_stdin(self):
        self.title = input("Enter the title: ")
        self.author = input("Enter the author")
        number_paragraphs = input("Enter the no of paragraphs:")
        self.paragraphs = input("Enter the paragraphs:")

    def print_json(self):
        pass


class FileFactory:
    def factory(file_type):
        if file_type == "HTMLFile":
            return HTMLFile("Some title","Some author","Some paragraphs")
        if file_type == "JSONFile":
            return JSONFile("Some title","Some paragraphs")
        print("Invalid type")
        return -1

if __name__ == '__main__':
    choice = input("Enter the type of file:")
    file = FileFactory.factory(choice)
    file.read_file_from_stdin()
    file.print_html()


我的问题是关于每个类的打印机方法。我想我应该在创建文件后打印文件,对吗?但是如何用我的输出创建一个 html 文件

另外,在 uml 图中,来自 FileFactory 的方法工厂返回一个 File 但 File 类是抽象的,我得到一个错误。如何不带参数只返回 HTMLFile/JSONFile?例如,如果我只编写 return HTMLFile(),则会出现错误,因为它需要参数标题、作者和段落。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。