code: def _finalize_namespaces(self, ns_dict=None):
          if ns_dict:
              for ns, alias in six.iteritems(ns_dict):
                  self._collected_namespaces.add_namespace_uri(ns, alias)
          self._collected_namespaces.add_namespace_uri(
              ns_uri=idgen.get_id_namespace(),
              prefix=idgen.get_id_namespace_alias()
          )
          self._fix_example_namespace()
          for prefix, uri in six.iteritems(self._input_namespaces):
              self._collected_namespaces.add_namespace_uri(uri, prefix)
          self._collected_namespaces.import_from(namespaces.XML_NAMESPACES)
          for ns_uri in self._collected_namespaces.namespace_uris:
              preferred_prefix = self._collected_namespaces.preferred_prefix_for_namespace(ns_uri)
              if preferred_prefix:
                  continue
              prefixes = self._collected_namespaces.get_prefixes(ns_uri)
              if prefixes:
                  prefix = next(iter(prefixes))
              else:
                  prefix = namespaces.lookup_name(ns_uri)
              if prefix is None:
                  raise namespaces.NoPrefixesError(ns_uri)
              self._collected_namespaces.set_preferred_prefix_for_namespace(
                  ns_uri=ns_uri,
                  prefix=prefix,
                  add_if_not_exist=True
              )