def _serialize(self, version, serialize_format, context, raise_if_incomplete=False):
values = context.copy()<EOL>for k in version:<EOL><INDENT>values[k] = version[k]<EOL><DEDENT>try:<EOL><INDENT>serialized = serialize_format.format(**values)<EOL><DEDENT>except KeyError as e:<EOL><INDENT>missing_key = getattr(e, "<STR_LIT:message>", e.args[<NUM_LIT:0>])<EOL>raise MissingValueForSerializationException(<EOL>"<STR_LIT>".format(<EOL>repr(missing_key), repr(version)<EOL>)<EOL>)<EOL><DEDENT>keys_needing_representation = set()<EOL>found_required = False<EOL>for k in self.order():<EOL><INDENT>v = values[k]<EOL>if not isinstance(v, VersionPart):<EOL><INDENT>continue<EOL><DEDENT>if not v.is_optional():<EOL><INDENT>found_required = True<EOL>keys_needing_representation.add(k)<EOL><DEDENT>elif not found_required:<EOL><INDENT>keys_needing_representation.add(k)<EOL><DEDENT><DEDENT>required_by_format = set(self._labels_for_format(serialize_format))<EOL>if raise_if_incomplete:<EOL><INDENT>if not (keys_needing_representation <= required_by_format):<EOL><INDENT>raise IncompleteVersionRepresentationException(<EOL>"<STR_LIT>".format(<EOL>"<STR_LIT>".join(keys_needing_representation ^ required_by_format),<EOL>serialize_format,<EOL>)<EOL>)<EOL><DEDENT><DEDENT>return serialized<EOL>
Attempts to serialize a version with the given serialization format.

Raises MissingValueForSerializationException if not serializable