前言
因为计算方法实验的缘故,需要用python写一些简单的算法,并进行实验、记录实验数据。其中一个实验需要记录一些多项式,而友好得显示公式的一个好办法就是产生Markdown文件。
然而python自带的Markdown库貌似更侧重于解析,不适合这种轻量化的写入性操作,因此决定自己造一个快速生成Markdown文件的库,也就是pyLabOn。
python模块的开发
理论上,一个文件就是一个python模块。但是更复杂的Python Package是由一个包含__init__.py的文件夹构成的。
我们在pyCharm中新建一个Python Package, 就是这种形式的。
然后我们添加功能性的源文件,对于pyLabOn而言,就是 LabReport.py
模块和文件信息
文件信息由头部的多行注释完成:
@File : LabReport.py
@Contact : ferdinandsu@outlook.com
@License : (C)Copyright 2020 HIT-ReFreSH
@Modify Time @Author @Version @Description
------------ ------- -------- -----------
2020-5-15 Ferdinand Sukhoi 0.1.4 Light-weight tool to generate markdown-formatted lab reports.
"""
模块的信息由以下语句说明:
__all__ = ['Report', 'ReportParagraph', 'ReportPlainContent', 'ReportTable']
__version__ = '0.1.4'
__author__ = 'Ferdinand Sukhoi'
__all__说明模块对外公开的内容,version和author说明版本和作者。
文档注释
像C# xmldoc, javadoc等一样,python也有文档注释。 对于类(以Report为例):
class Report(ReportParagraph):
"""A markdown report file
Attributes:
MarkdownFilePath: full-path of the markdown file
PDFPath: full-path of the PDF file
"""
对于函数,是:
def add_table(self, headers, data, align=0):
"""
Create and add a table to this paragraph
:param headers: Headers of the table
:type headers: list[str]
:param data: Data of the table
:type data list[list[str]]
:param align: Optional. Align of the table, default 0-Central
:type align: list or int
:return: The table added
:type ReportTable
"""
这样,对于一个模块的源文件而言,元素是完全的了。