pymaidol


Namepymaidol JSON
Version 0.1.3 PyPI version JSON
download
home_page
SummaryA markup gramma that embed python code into text
upload_time2023-06-12 15:55:37
maintainer
docs_urlNone
authorEterance
requires_python>=3.10
license
keywords prompt markup language nlp ai gpt
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Pymaidol

Pymaidol 是一种标记语法,用于将 Python 代码嵌入文本中,使得文本在运行时可动态更改包含的内容。

与 [ProMaid](https://github.com/Eterance/ProMaid) 相比,Pymaidol 不只是单纯的将数据嵌入至模板(Template)的对应位置,还能将复杂的处理逻辑呈现在模板中,甚至可使用在模板中定义的函数对数据进行处理。

⚠**警告:Pymaidol 目前处于开发阶段,语法和一些对空白符、换行符的处理逻辑尚未确定,以后可能会被修改。强烈不建议在生产环境中使用。**

## 环境要求与安装

python >= 3.10

``` bash
pip install Pymaidol -i https://pypi.python.org/simple
```

## 第一个 Pymaidol 模板

### 创建模板文件

首先,假设你创建了一个名为 `pymaidol_test` 的文件夹。使用编辑器或者 IDE 打开该文件夹作为工作目录。

在 `pymaidol_test` 文件夹下打开命令行,输入以下命令:

``` bash
python -m pymaidol -n FirstTemplate
```

`FirstTemplate` 是模板类的类名。命令行会输出以下内容,同时文件夹中会生成 `FirstTemplate.pymd` 和 `FirstTemplate.py` 两个文件:

``` bash
Success: file "FirstTemplate.pymd" created
Success: file "FirstTemplate.py" created
```

### 编写模板设计文件

首先,用以下代码将 `FirstTemplate.pymd` 中的全部内容替换掉:

``` python
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` 类中的全部内容:

``` python
@{import time}
Now (@(time.ctime())), Say "@(says)" using @(self.package_name)!
```

运行 `FirstTemplate.py`,命令行会输出以下内容:

``` bash
Now (Tue May 23 19:21:19 2023), Say "Hello World" using Pymaidol!
```

## 另请参阅

- [Pymaidol 语法参考](docs/zh-cn/语法参考.md)
- [Pymaidol_API目录](docs/zh-cn/Pymaidol_API%E7%9B%AE%E5%BD%95.md)

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "pymaidol",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.10",
    "maintainer_email": "",
    "keywords": "Prompt,Markup Language,NLP,AI,GPT",
    "author": "Eterance",
    "author_email": "",
    "download_url": "https://files.pythonhosted.org/packages/a2/89/8995d0755a390289920e985eb4df888bdfed45f5579d7dfef0a7a8219532/pymaidol-0.1.3.tar.gz",
    "platform": null,
    "description": "# Pymaidol\n\nPymaidol \u662f\u4e00\u79cd\u6807\u8bb0\u8bed\u6cd5\uff0c\u7528\u4e8e\u5c06 Python \u4ee3\u7801\u5d4c\u5165\u6587\u672c\u4e2d\uff0c\u4f7f\u5f97\u6587\u672c\u5728\u8fd0\u884c\u65f6\u53ef\u52a8\u6001\u66f4\u6539\u5305\u542b\u7684\u5185\u5bb9\u3002\n\n\u4e0e [ProMaid](https://github.com/Eterance/ProMaid) \u76f8\u6bd4\uff0cPymaidol \u4e0d\u53ea\u662f\u5355\u7eaf\u7684\u5c06\u6570\u636e\u5d4c\u5165\u81f3\u6a21\u677f\uff08Template\uff09\u7684\u5bf9\u5e94\u4f4d\u7f6e\uff0c\u8fd8\u80fd\u5c06\u590d\u6742\u7684\u5904\u7406\u903b\u8f91\u5448\u73b0\u5728\u6a21\u677f\u4e2d\uff0c\u751a\u81f3\u53ef\u4f7f\u7528\u5728\u6a21\u677f\u4e2d\u5b9a\u4e49\u7684\u51fd\u6570\u5bf9\u6570\u636e\u8fdb\u884c\u5904\u7406\u3002\n\n\u26a0**\u8b66\u544a\uff1aPymaidol \u76ee\u524d\u5904\u4e8e\u5f00\u53d1\u9636\u6bb5\uff0c\u8bed\u6cd5\u548c\u4e00\u4e9b\u5bf9\u7a7a\u767d\u7b26\u3001\u6362\u884c\u7b26\u7684\u5904\u7406\u903b\u8f91\u5c1a\u672a\u786e\u5b9a\uff0c\u4ee5\u540e\u53ef\u80fd\u4f1a\u88ab\u4fee\u6539\u3002\u5f3a\u70c8\u4e0d\u5efa\u8bae\u5728\u751f\u4ea7\u73af\u5883\u4e2d\u4f7f\u7528\u3002**\n\n## \u73af\u5883\u8981\u6c42\u4e0e\u5b89\u88c5\n\npython >= 3.10\n\n``` bash\npip install Pymaidol -i https://pypi.python.org/simple\n```\n\n## \u7b2c\u4e00\u4e2a Pymaidol \u6a21\u677f\n\n### \u521b\u5efa\u6a21\u677f\u6587\u4ef6\n\n\u9996\u5148\uff0c\u5047\u8bbe\u4f60\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3a `pymaidol_test` \u7684\u6587\u4ef6\u5939\u3002\u4f7f\u7528\u7f16\u8f91\u5668\u6216\u8005 IDE \u6253\u5f00\u8be5\u6587\u4ef6\u5939\u4f5c\u4e3a\u5de5\u4f5c\u76ee\u5f55\u3002\n\n\u5728 `pymaidol_test` \u6587\u4ef6\u5939\u4e0b\u6253\u5f00\u547d\u4ee4\u884c\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a\n\n``` bash\npython -m pymaidol -n FirstTemplate\n```\n\n`FirstTemplate` \u662f\u6a21\u677f\u7c7b\u7684\u7c7b\u540d\u3002\u547d\u4ee4\u884c\u4f1a\u8f93\u51fa\u4ee5\u4e0b\u5185\u5bb9\uff0c\u540c\u65f6\u6587\u4ef6\u5939\u4e2d\u4f1a\u751f\u6210 `FirstTemplate.pymd` \u548c `FirstTemplate.py` \u4e24\u4e2a\u6587\u4ef6\uff1a\n\n``` bash\nSuccess: file \"FirstTemplate.pymd\" created\nSuccess: file \"FirstTemplate.py\" created\n```\n\n### \u7f16\u5199\u6a21\u677f\u8bbe\u8ba1\u6587\u4ef6\n\n\u9996\u5148\uff0c\u7528\u4ee5\u4e0b\u4ee3\u7801\u5c06 `FirstTemplate.pymd` \u4e2d\u7684\u5168\u90e8\u5185\u5bb9\u66ff\u6362\u6389\uff1a\n\n``` python\nfrom pymaidol import TemplateBase\nfrom pymaidol.AnnotationType import FULL_ANNOTATION_TYPE, AnnotationTypeEnum\n\nclass FirstTemplate(TemplateBase):\n    def __init__(self, \n                 package_name:str, \n                 template: str | None = None, \n                 template_file_path: str | None = None, \n                 supported_annotation_types: list[AnnotationTypeEnum] = FULL_ANNOTATION_TYPE) -> None:\n        super().__init__(template, template_file_path, supported_annotation_types)\n        self.package_name = package_name\n        \ndef main():\n    template = FirstTemplate(\"Pymaidol\")\n    string = template.Render({\"says\": \"Hello World\"})\n    print(string)\n    \nif __name__ == \"__main__\":\n    main()\n```\n\n\u8fd9\u6bb5\u4ee3\u7801\u4e3b\u8981\u4fee\u6539\u4e86\u4ee5\u4e0b\u90e8\u5206\uff1a\n\n- \u4e3a `FirstTemplate` \u7c7b\u6dfb\u52a0\u4e86 `package_name` \u5c5e\u6027\u3002\n- \u6dfb\u52a0\u4e86 `main` \u51fd\u6570\uff0c\u4ee5\u5b9e\u4f8b\u5316 `FirstTemplate` \u7c7b\u3002\u5411 `FirstTemplate` \u7c7b\u7684\u6784\u9020\u51fd\u6570\u4f20\u5165\u4e86 `package_name` \u53c2\u6570\uff0c\u8be5\u53c2\u6570\u7684\u503c\u4e3a `Pymaidol`\u3002\n- \u8c03\u7528\u4e86 `Render` \u65b9\u6cd5\uff0c\u5411\u5176\u4f20\u5165\u4e86\u4e00\u4e2a\u5b57\u5178 `{\"says\": \"Hello World\"}`\u3002\n\n\u7136\u540e\uff0c\u7528\u4ee5\u4e0b\u4ee3\u7801\u66ff\u6362 `FirstTemplate` \u7c7b\u4e2d\u7684\u5168\u90e8\u5185\u5bb9\uff1a\n\n``` python\n@{import time}\nNow (@(time.ctime())), Say \"@(says)\" using @(self.package_name)!\n```\n\n\u8fd0\u884c `FirstTemplate.py`\uff0c\u547d\u4ee4\u884c\u4f1a\u8f93\u51fa\u4ee5\u4e0b\u5185\u5bb9\uff1a\n\n``` bash\nNow (Tue May 23 19:21:19 2023), Say \"Hello World\" using Pymaidol!\n```\n\n## \u53e6\u8bf7\u53c2\u9605\n\n- [Pymaidol \u8bed\u6cd5\u53c2\u8003](docs/zh-cn/\u8bed\u6cd5\u53c2\u8003.md)\n- [Pymaidol_API\u76ee\u5f55](docs/zh-cn/Pymaidol_API%E7%9B%AE%E5%BD%95.md)\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "A markup gramma that embed python code into text",
    "version": "0.1.3",
    "project_urls": null,
    "split_keywords": [
        "prompt",
        "markup language",
        "nlp",
        "ai",
        "gpt"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "556e78913eba182910412d622a47cf731471f29cdbb34f3a92d29a5fc75771db",
                "md5": "95f5322af60135f3994cdb2d6ff097c7",
                "sha256": "e2736d771b2769c70512e68655ebf75cbe30a7dc05fc50a3d98029be725c6b11"
            },
            "downloads": -1,
            "filename": "pymaidol-0.1.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "95f5322af60135f3994cdb2d6ff097c7",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10",
            "size": 21284,
            "upload_time": "2023-06-12T15:55:35",
            "upload_time_iso_8601": "2023-06-12T15:55:35.707026Z",
            "url": "https://files.pythonhosted.org/packages/55/6e/78913eba182910412d622a47cf731471f29cdbb34f3a92d29a5fc75771db/pymaidol-0.1.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a2898995d0755a390289920e985eb4df888bdfed45f5579d7dfef0a7a8219532",
                "md5": "a8982397920df8ce0a1ea1d13548e41b",
                "sha256": "df74d9c90f682df1929fbe25335b49e5d9c746f983aa141320fd93a283eb4ef4"
            },
            "downloads": -1,
            "filename": "pymaidol-0.1.3.tar.gz",
            "has_sig": false,
            "md5_digest": "a8982397920df8ce0a1ea1d13548e41b",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10",
            "size": 29352,
            "upload_time": "2023-06-12T15:55:37",
            "upload_time_iso_8601": "2023-06-12T15:55:37.700959Z",
            "url": "https://files.pythonhosted.org/packages/a2/89/8995d0755a390289920e985eb4df888bdfed45f5579d7dfef0a7a8219532/pymaidol-0.1.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-06-12 15:55:37",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "pymaidol"
}
        
Elapsed time: 0.09348s