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

这是当前的代码,我正在尝试围绕替代方案进行思考:


目前我们有:


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:


凤凰求蛊
浏览 122回答 1
1回答

繁星点点滴滴

一个简单的方法是使用第二个条件: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 + pathreturn URL_PREFIX + "/libs-releases-local/org/region/%s/%s/%s-%s" % (artifact, version, artifact, version)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python