使用程序(python)的输出创建html文件
我必须编写一个程序,从键盘读取一些信息,然后通过工厂方法设计模式创建一个HTML文件和一个JSON文件。我有一个uml图来帮助忽略类文本文件 我就是这样开始的使用程序(python)的输出创建html文件,python,design-patterns,factory,factory-pattern,Python,Design Patterns,Factory,Factory Pattern,我必须编写一个程序,从键盘读取一些信息,然后通过工厂方法设计模式创建一个HTML文件和一个JSON文件。我有一个uml图来帮助忽略类文本文件 我就是这样开始的 from abc import ABCMeta, abstractmethod class File(metaclass=ABCMeta): def __init__(self, title, author, paragraphs): self.title = title self.autho
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 author", "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()
从abc导入ABCMeta,abstractmethod
类文件(元类=ABCMeta):
定义初始(自我、标题、作者、段落):
self.title=标题
self.author=作者
self.parages=段落
@抽象方法
def从标准数据(自身)读取文件:
通过
类HTMLFile(文件):
def从标准数据(自身)读取文件:
self.title=输入(“输入标题:”)
self.author=输入(“输入作者”)
段落数=输入(“输入段落数:”)
self.parations=输入(“输入段落:”)
def打印_html(自):
打印(“”)
打印(“+self.title+”