@staticmethod
  def _parse_from_args(pairs=None, flat=None, app=None, namespace=None,
                       parent=None):
    if flat:
      if pairs is not None:
        raise TypeError('Key() cannot accept both flat and pairs arguments.')
      if len(flat) % 2:
        raise ValueError('Key() must have an even number of positional '
                         'arguments.')
      pairs = [(flat[i], flat[i + 1]) for i in xrange(0, len(flat), 2)]
    else:
      pairs = list(pairs)
    if not pairs:
      raise TypeError('Key must consist of at least one pair.')
    for i, (kind, id) in enumerate(pairs):
      if isinstance(id, unicode):
        id = id.encode('utf8')
      elif id is None:
        if i + 1 < len(pairs):
          raise datastore_errors.BadArgumentError(
              'Incomplete Key entry must be last')
      else:
        if not isinstance(id, (int, long, str)):
          raise TypeError('Key id must be a string or a number; received %r' %
                          id)
      if isinstance(kind, type):
        kind = kind._get_kind()
      if isinstance(kind, unicode):
        kind = kind.encode('utf8')
      if not isinstance(kind, str):
        raise TypeError('Key kind must be a string or Model class; '
                        'received %r' % kind)
      if not id:
        id = None
      pairs[i] = (kind, id)
    if parent is not None:
      if not isinstance(parent, Key):
        raise datastore_errors.BadValueError(
            'Expected Key instance, got %r' % parent)
      if not parent.id():
        raise datastore_errors.BadArgumentError(
            'Parent cannot have incomplete key')
      pairs[:0] = parent.pairs()
      if app:
        if app != parent.app():
          raise ValueError('Cannot specify a different app %r '
                           'than the parent app %r' %
                           (app, parent.app()))
      else:
        app = parent.app()
      if namespace is not None:
        if namespace != parent.namespace():
          raise ValueError('Cannot specify a different namespace %r '
                           'than the parent namespace %r' %
                           (namespace, parent.namespace()))
      else:
        namespace = parent.namespace()
    if not app:
      app = _DefaultAppId()
    if namespace is None:
      namespace = _DefaultNamespace()
    return tuple(pairs), app, namespace
dataset/ETHPy150Open GoogleCloudPlatform/datastore-ndb-python/ndb/key.py/Key._parse_from_args/original