A markup gramma that embed python code into text
Project description
Pymaidol
Pymaidol 是一种标记语法,用于将 Python 代码嵌入文本中,使得文本在运行时可动态更改包含的内容。
与 ProMaid 相比,Pymaidol 不只是单纯的将数据嵌入至模板(Template)的对应位置,还能将复杂的处理逻辑呈现在模板中,甚至可使用在模板中定义的函数对数据进行处理。
⚠警告:Pymaidol 目前处于开发阶段,语法和一些对空白符、换行符的处理逻辑尚未确定,以后可能会被修改。强烈不建议在生产环境中使用。
环境要求与安装
python >= 3.10
pip install Pymaidol -i https://pypi.python.org/simple
第一个 Pymaidol 模板
创建模板文件
首先,假设你创建了一个名为 pymaidol_test 的文件夹。使用编辑器或者 IDE 打开该文件夹作为工作目录。
在 pymaidol_test 文件夹下打开命令行,输入以下命令:
python -m pymaidol -n FirstTemplate
FirstTemplate 是模板类的类名。命令行会输出以下内容,同时文件夹中会生成 FirstTemplate.pymd 和 FirstTemplate.py 两个文件:
Success: file "FirstTemplate.pymd" created
Success: file "FirstTemplate.py" created
编写模板设计文件
首先,用以下代码将 FirstTemplate.pymd 中的全部内容替换掉:
from pymaidol import TemplateBase
from pymaidol.AnnotationType import FULL_ANNOTATION_TYPE, AnnotationTypeEnum
class FirstTemplate(TemplateBase):
def __init__(self,
package_name:str,
template: str | None = None,
template_file_path: str | None = None,
supported_annotation_types: list[AnnotationTypeEnum] = FULL_ANNOTATION_TYPE) -> None:
super().__init__(template, template_file_path, supported_annotation_types)
self.package_name = package_name
def main():
template = FirstTemplate("Pymaidol")
string = template.Render({"says": "Hello World"})
print(string)
if __name__ == "__main__":
main()
这段代码主要修改了以下部分:
- 为
FirstTemplate类添加了package_name属性。 - 添加了
main函数,以实例化FirstTemplate类。向FirstTemplate类的构造函数传入了package_name参数,该参数的值为Pymaidol。 - 调用了
Render方法,向其传入了一个字典{"says": "Hello World"}。
然后,用以下代码替换 FirstTemplate 类中的全部内容:
@{import time}
Now (@(time.ctime())), Say "@(says)" using @(self.package_name)!
运行 FirstTemplate.py,命令行会输出以下内容:
Now (Tue May 23 19:21:19 2023), Say "Hello World" using Pymaidol!
另请参阅
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pymaidol-0.1.3.tar.gz.
File metadata
- Download URL: pymaidol-0.1.3.tar.gz
- Upload date:
- Size: 29.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
df74d9c90f682df1929fbe25335b49e5d9c746f983aa141320fd93a283eb4ef4
|
|
| MD5 |
a8982397920df8ce0a1ea1d13548e41b
|
|
| BLAKE2b-256 |
a2898995d0755a390289920e985eb4df888bdfed45f5579d7dfef0a7a8219532
|
File details
Details for the file pymaidol-0.1.3-py3-none-any.whl.
File metadata
- Download URL: pymaidol-0.1.3-py3-none-any.whl
- Upload date:
- Size: 21.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e2736d771b2769c70512e68655ebf75cbe30a7dc05fc50a3d98029be725c6b11
|
|
| MD5 |
95f5322af60135f3994cdb2d6ff097c7
|
|
| BLAKE2b-256 |
556e78913eba182910412d622a47cf731471f29cdbb34f3a92d29a5fc75771db
|