Skip to main content

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.pymdFirstTemplate.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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pymaidol-0.1.3.tar.gz (29.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pymaidol-0.1.3-py3-none-any.whl (21.3 kB view details)

Uploaded Python 3

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

Hashes for pymaidol-0.1.3.tar.gz
Algorithm Hash digest
SHA256 df74d9c90f682df1929fbe25335b49e5d9c746f983aa141320fd93a283eb4ef4
MD5 a8982397920df8ce0a1ea1d13548e41b
BLAKE2b-256 a2898995d0755a390289920e985eb4df888bdfed45f5579d7dfef0a7a8219532

See more details on using hashes here.

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

Hashes for pymaidol-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 e2736d771b2769c70512e68655ebf75cbe30a7dc05fc50a3d98029be725c6b11
MD5 95f5322af60135f3994cdb2d6ff097c7
BLAKE2b-256 556e78913eba182910412d622a47cf731471f29cdbb34f3a92d29a5fc75771db

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page