将所有函数替换为 'pass',将私有同步到 public github 存储库
Replacing all functions with 'pass', syncing private to public github repo
我想为专有 python 模块创建一个存储库,与此 python 包 mlfinlabs 的方式类似。他们清空了所有功能,如下所示:
def bet_size_dynamic(current_pos, max_pos, market_price, forecast_price, cal_divergence=10, cal_bet_size=0.95,
func='sigmoid'):
pass
def bet_size_budget(events_t1, sides):
pass
我找到了libcst模块,它解析了源代码,你可以对其进行操作。
这样做有更好的做法吗?例如:github 动作?我找不到任何其他好的解决方案。
Github 从私有库同步到 public 库的操作。
使用此 github 操作:https://github.com/marketplace/actions/github-repo-sync
libcst
(project site) 之后剥离功能(也作为 github 操作步骤)。下面是您应该放置的代码模型
- 在 libcst 文件夹中 (
\Lib\site-packages\libcst\codemod\commands
)
- 在你的 repo 目录中,然后指定它
.libcst.codemod.yaml
(如果你 运行 github actions 上的 codemod,则需要这样做):
# String that LibCST should look for in code which indicates that the
# module is generated code.
generated_code_marker: '@generated'
# Command line and arguments for invoking a code formatter. Anything
# specified here must be capable of taking code via stdin and returning
# formatted code via stdout.
formatter: ['black', '-']
# List of regex patterns which LibCST will evaluate against filenames to
# determine if the module should be touched.
blacklist_patterns: ['.*replace_functions\.py']
# List of modules that contain codemods inside of them.
modules:
- 'libcst.codemod.commands'
- 'mycodemod' # THIS IS THE NAME OF THE FOLDER
# Absolute or relative path of the repository root, used for providing
# full-repo metadata. Relative paths should be specified with this file
# location as the base.
repo_root: '.'
然后把你的codemod放在:
project_root
|--- mycodemod
|--- __init__.py (this is an empty file)
|--- replace_functions.py (the codemod pasted below)
在 replace_functions.py
中放入此片段:
from ast import Expression, literal_eval
from typing import Union
import libcst as cst
from libcst.codemod import CodemodContext, VisitorBasedCodemodCommand
from libcst.codemod.visitors import AddImportsVisitor
class ReplaceFunctionCommand(VisitorBasedCodemodCommand):
# Add a description so that future codemodders can see what this does.
DESCRIPTION: str = "Replaces the body of a function with pass."
def __init__(self, context: CodemodContext) -> None:
# Initialize the base class with context, and save our args.
print("this happens")
super().__init__(context)
def leave_FunctionDef(self, original_node: cst.FunctionDef, updated_node: cst.FunctionDef) -> cst.FunctionDef:
replace_function = cst.FunctionDef(
name=updated_node.name,
params=cst.Parameters(),
body= cst.SimpleStatementSuite((cst.Pass(),)),
returns=None)
return replace_function
我想为专有 python 模块创建一个存储库,与此 python 包 mlfinlabs 的方式类似。他们清空了所有功能,如下所示:
def bet_size_dynamic(current_pos, max_pos, market_price, forecast_price, cal_divergence=10, cal_bet_size=0.95,
func='sigmoid'):
pass
def bet_size_budget(events_t1, sides):
pass
我找到了libcst模块,它解析了源代码,你可以对其进行操作。
这样做有更好的做法吗?例如:github 动作?我找不到任何其他好的解决方案。
Github 从私有库同步到 public 库的操作。 使用此 github 操作:https://github.com/marketplace/actions/github-repo-sync
libcst
(project site) 之后剥离功能(也作为 github 操作步骤)。下面是您应该放置的代码模型
- 在 libcst 文件夹中 (
\Lib\site-packages\libcst\codemod\commands
) - 在你的 repo 目录中,然后指定它
.libcst.codemod.yaml
(如果你 运行 github actions 上的 codemod,则需要这样做):
# String that LibCST should look for in code which indicates that the
# module is generated code.
generated_code_marker: '@generated'
# Command line and arguments for invoking a code formatter. Anything
# specified here must be capable of taking code via stdin and returning
# formatted code via stdout.
formatter: ['black', '-']
# List of regex patterns which LibCST will evaluate against filenames to
# determine if the module should be touched.
blacklist_patterns: ['.*replace_functions\.py']
# List of modules that contain codemods inside of them.
modules:
- 'libcst.codemod.commands'
- 'mycodemod' # THIS IS THE NAME OF THE FOLDER
# Absolute or relative path of the repository root, used for providing
# full-repo metadata. Relative paths should be specified with this file
# location as the base.
repo_root: '.'
然后把你的codemod放在:
project_root
|--- mycodemod
|--- __init__.py (this is an empty file)
|--- replace_functions.py (the codemod pasted below)
在 replace_functions.py
中放入此片段:
from ast import Expression, literal_eval
from typing import Union
import libcst as cst
from libcst.codemod import CodemodContext, VisitorBasedCodemodCommand
from libcst.codemod.visitors import AddImportsVisitor
class ReplaceFunctionCommand(VisitorBasedCodemodCommand):
# Add a description so that future codemodders can see what this does.
DESCRIPTION: str = "Replaces the body of a function with pass."
def __init__(self, context: CodemodContext) -> None:
# Initialize the base class with context, and save our args.
print("this happens")
super().__init__(context)
def leave_FunctionDef(self, original_node: cst.FunctionDef, updated_node: cst.FunctionDef) -> cst.FunctionDef:
replace_function = cst.FunctionDef(
name=updated_node.name,
params=cst.Parameters(),
body= cst.SimpleStatementSuite((cst.Pass(),)),
returns=None)
return replace_function