Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

重写 import 辅助函数的方式 #525

Open
wants to merge 23 commits into
base: master
Choose a base branch
from

Conversation

guozixu2001
Copy link
Contributor

PR Docs

单文件转换:如果只转换单个文件,就不单独生成 utils.py ,而是直接写到转换后的文件里直接使用,不需要再 import utils;
多文件转换:调用多次 PaConvert 也不会生成重复的代码

改动内容

  1. 重写了AuxFileHelper类:
    • 把原先的 write_code 分拆成了 add_code 和 write_code,在 converter 中调用 write_code 会一次性写入代码
    • 添加了写入的辅助代码段前后的标识 >>>><<<< ,在多次调用 PaConvert 的时候,只重写其中的内容。这样避免了写入重复的代码
  2. 重写 api_matcher 中涉及到的 generate_aux_code
    • 在 api_matcher 中原先都是通过 utils.foo 的形式来调用 utils 模块中的辅助函数的,现在直接插入到原文件的话,需要直接通过 foo 来调用,所以引入了 get_utils_prefix 来判断是单文件的情况还是多文件的情况
  3. 在各个 tranformer 中添加 is_dir_mode
    • 因为做了本次的改动之后,在 Matcher::generate_aux_code 中需要根据“单文件转换还是多文件转换”来决定 prefix,所以需要在 transformer 中添加 is_dir_mode 字段,以便 Matcher 可以判断

PR APIs

Copy link

paddle-bot bot commented Dec 6, 2024

Thanks for your contribution!

@paddle-bot paddle-bot bot added the contributor External developers label Dec 6, 2024
paconvert/base.py Outdated Show resolved Hide resolved
paconvert/converter.py Outdated Show resolved Hide resolved
paconvert/converter.py Outdated Show resolved Hide resolved
paconvert/transformer/basic_transformer.py Outdated Show resolved Hide resolved
paconvert/utils.py Outdated Show resolved Hide resolved
paconvert/utils.py Show resolved Hide resolved
paconvert/utils.py Outdated Show resolved Hide resolved
paconvert/utils.py Outdated Show resolved Hide resolved
scripts/code_unittest_check.sh Outdated Show resolved Hide resolved
tests/apibase.py Outdated Show resolved Hide resolved
…seMatcher, use 'from utils import *' instead of 'import utils', remove prefix in each Matcher
@zhwesky2010
Copy link
Collaborator

zhwesky2010 commented Dec 11, 2024

@guozixu2001 按上面的评论回复开发吧,冲突了需要处理一下

docs/CONTRIBUTING.md Outdated Show resolved Hide resolved
paconvert/api_matcher.py Outdated Show resolved Hide resolved
_instance = None
INIT_CONTENT = """
# This content is generated by PaConvert ToolKit, please Don't edit it!
############################## 相关utils函数,如下 ##############################
####################### PaConvert 自动生成的代码,请勿手动修改! ##################
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这一行在is_dir_mode下需要,非is_dir_mode就不打印了

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

意思是在转换单个文件的时候,完全不需要插入类似的标识了吗?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

意思是在转换单个文件的时候,完全不需要插入类似的标识了吗?

单文件不要这个 ####################### PaConvert 自动生成的代码,请勿手动修改! ##################,多文件需要

paconvert/utils.py Outdated Show resolved Hide resolved
paconvert/utils.py Outdated Show resolved Hide resolved
paconvert/utils.py Show resolved Hide resolved
tests/code_library/code_case/paddle_code/paddlenlp_Qwen.py Outdated Show resolved Hide resolved
@zhwesky2010
Copy link
Collaborator

zhwesky2010 commented Dec 13, 2024

@guozixu2001 处理下冲突,按上面意见改吧

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contributor External developers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants