这是当前的代码,我正在尝试把头围起来:
目前我们有:
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)
我想做的是通过URL的URL_PREFIX值将另一个名为“ dockerservice”的类型并入其中。
URL_PREFIX + "/docker-dev-local/%s-%s" % (artifact, artifact, version)
用if ngwebui|nodeservice URL_PREFIX, dockerservice URL_PREFIX, else URL_PREFIX:
保留最后一个渔获物的最简单方法是什么?
答案 0 :(得分: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 + path
return URL_PREFIX + "/libs-releases-local/org/region/%s/%s/%s-%s" % (artifact, version, artifact, version)