为了账号安全,请及时绑定邮箱和手机立即绑定

具有 3 个选项的重新模式匹配

具有 3 个选项的重新模式匹配

凤凰求蛊 2023-06-27 16:46:55
这是当前的代码,我正在尝试围绕替代方案进行思考:目前我们有:URL_PREFIX = "http://ourrepo:8081/artifactory"pattern = re.compile(r'^.*-(ngwebui|nodeservice).*$')if pattern.match(artifact):    return URL_PREFIX + "/npm-local/region/%s/-/region/%s-%s" % (artifact, artifact, version)else:    return URL_PREFIX + "/libs-releases-local/org/region/%s/%s/%s-%s" % (artifact, version, artifact, version)我想做的是将另一种称为“dockerservice”的类型合并到其中,其URL_PREFIX值通过URL_PREFIX + "/docker-dev-local/%s-%s" % (artifact, artifact, version)用 ?if ngwebui|nodeservice URL_PREFIX, dockerservice URL_PREFIX, else URL_PREFIX:
查看完整描述

1 回答

?
繁星点点滴滴

TA贡献1803条经验 获得超3个赞

一个简单的方法是使用第二个条件:


URL_PREFIX = "http://ourrepo:8081/artifactory"

nodePattern = re.compile(r'^.*-(ngwebui|nodeservice).*$')

dockerPattern = re.compile(r'patternForDocker')


if nodePattern.match(artifact):

    return URL_PREFIX + "/npm-local/region/%s/-/region/%s-%s" % (artifact, artifact, version)

elif dockerPattern.match(artifact):

    return URL_PREFIX + "docker/path/..."

else:

    return URL_PREFIX + "/libs-releases-local/org/region/%s/%s/%s-%s" % (artifact, version, artifact, version)

但一种更具可扩展性的方法是创建模式和路径的映射:


URL_PREFIX = "http://ourrepo:8081/artifactory"

PATHS = {

  '^.*-(ngwebui|nodeservice).*$': "/npm-local/region/%s/-/region/%s-%s" % (artifact, artifact, version),

  '^patternForDocker$': "docker/path/...",

  # other pairs

}


for pattern, path in PATHS.items():

  compiled = re.compile(pattern)

  if compiled.match(artifact):

    return URL_PREFIX + path


return URL_PREFIX + "/libs-releases-local/org/region/%s/%s/%s-%s" % (artifact, version, artifact, version)



查看完整回答
反对 回复 2023-06-27
  • 1 回答
  • 0 关注
  • 121 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信