def parse_table_name(name, project_id=None, dataset_id=None):
  """Parses a table name into its individual parts.

  Args:
    name: the name to parse, or a tuple, dictionary or array containing the parts.
    project_id: the expected project ID. If the name does not contain a project ID,
        this will be used; if the name does contain a project ID and it does not match
        this, an exception will be thrown.
    dataset_id: the expected dataset ID. If the name does not contain a dataset ID,
        this will be used; if the name does contain a dataset ID and it does not match
        this, an exception will be thrown.
  Returns:
    A TableName named tuple consisting of the full name and individual name parts.
  Raises:
    Exception: raised if the name doesn't match the expected formats, or a project_id and/or
        dataset_id was provided that does not match that in the name.
  """
  _project_id = _dataset_id = _table_id = _decorator = None
  if isinstance(name, basestring):
    # Try to parse as absolute name first.
    m = re.match(_ABS_TABLE_NAME_PATTERN, name, re.IGNORECASE)
    if m is not None:
      _project_id, _dataset_id, _table_id, _decorator = m.groups()
    else:
      # Next try to match as a relative name implicitly scoped within current project.
      m = re.match(_REL_TABLE_NAME_PATTERN, name)
      if m is not None:
        groups = m.groups()
        _project_id, _dataset_id, _table_id, _decorator =\
            project_id, groups[0], groups[1], groups[2]
      else:
        # Finally try to match as a table name only.
        m = re.match(_TABLE_NAME_PATTERN, name)
        if m is not None:
          groups = m.groups()
          _project_id, _dataset_id, _table_id, _decorator =\
              project_id, dataset_id, groups[0], groups[1]
  elif isinstance(name, dict):
    try:
      _table_id = name['table_id']
      _dataset_id = name['dataset_id']
      _project_id = name['project_id']
    except KeyError:
      pass
  else:
    # Try treat as an array or tuple
    if len(name) == 4:
      _project_id, _dataset_id, _table_id, _decorator = name
    elif len(name) == 3:
      _project_id, _dataset_id, _table_id = name
    elif len(name) == 2:
      _dataset_id, _table_id = name
  if not _table_id:
    raise Exception('Invalid table name: ' + str(name))
  if not _project_id:
    _project_id = project_id
  if not _dataset_id:
    _dataset_id = dataset_id
  if not _decorator:
    _decorator = ''

  return TableName(_project_id, _dataset_id, _table_id, _decorator)
Parses a table name into its individual parts.

  Args:
    name: the name to parse, or a tuple, dictionary or array containing the parts.
    project_id: the expected project ID. If the name does not contain a project ID,
        this will be used; if the name does contain a project ID and it does not match
        this, an exception will be thrown.
    dataset_id: the expected dataset ID. If the name does not contain a dataset ID,
        this will be used; if the name does contain a dataset ID and it does not match
        this, an exception will be thrown.
  Returns:
    A TableName named tuple consisting of the full name and individual name parts.
  Raises:
    Exception: raised if the name doesn't match the expected formats, or a project_id and/or
        dataset_id was provided that does not match that in the name.