def _construct_version(major, minor, patch, level, pre_identifier, dev_identifier, post_identifier):
    """Construct a PEP0440 compatible version number to be set to __version__"""
    assert level in ["alpha", "beta", "candidate", "final"]

    version = "{0}.{1}".format(major, minor)
    if patch:
        version += ".{0}".format(patch)

    if level == "final":
        if post_identifier:
            version += ".{0}{1}".format("post", post_identifier)
        if dev_identifier:
            version += ".{0}{1}".format("dev", dev_identifier)
    else:
        level_short = {"alpha": "a", "beta": "b", "candidate": "rc"}[level]
        version += "{0}{1}".format(level_short, pre_identifier)
        if post_identifier:
            version += ".{0}{1}".format("post", post_identifier)
        if dev_identifier:
            version += ".{0}{1}".format("dev", dev_identifier)
    return version