I want to construct a string from several substrings, each one of those with a different delimiter. If the string is present, the delimiter and the string should be added, if not, none of them should be added.
This is my current code that looks a bit dirty to me:
def my_join(major, minor, patch=None, pre_release=None, build=None, revision=None): version = '.'.join(filter(None,(major, minor, patch))) if pre_release: version += '-%s' % pre_release if build: version += '+b%s' % build if revision: version += '.%s' % revision return version
my_join('0', '1',pre_release='alpha') '0.1-alpha' my_join('2','3','4', build='567', revision='aabbcc440') '2.3.4+b567.aabbcc440'
My problem is basically an extension of join 4 strings to the one if they are not empty in python with the addition of using different delimiter strings.