示例#1
0
 def repr_fn_with_debug(val):  # Wrap repr_fn to add debug info
     try:
         return repr_fn(val)
     except Exception as e:  # pylint: disable=broad-except
         err_msg = (f'HTML formatting of column {name} failed:\n'
                    f' * feature: {feature}\n'
                    f' * input: {val!r}\n')
         py_utils.reraise(e, prefix=err_msg)
示例#2
0
def _try_import(module_name):
  """Try importing a module, with an informative error message on failure."""
  try:
    mod = importlib.import_module(module_name)
    return mod
  except ImportError:
    err_msg = ("Tried importing %s but failed. See setup.py extras_require. "
               "The dataset you are trying to use may have additional "
               "dependencies.")
    utils.reraise(err_msg)
示例#3
0
def _try_import(module_name):
  """Try importing a module, with an informative error message on failure."""
  try:
    mod = importlib.import_module(module_name)
    return mod
  except ImportError as e:
    err_msg = ("Failed importing {name}. This likely means that the dataset "
               "requires additional dependencies that have to be "
               "manually installed (usually with `pip install {name}`). See "
               "setup.py extras_require.").format(name=module_name)
    utils.reraise(e, suffix=err_msg)
示例#4
0
def _serialize_example(
        example: Any, features: features_lib.FeaturesDict,
        serializer: example_serializer.ExampleSerializer) -> str:
    try:
        encoded_example = features.encode_example(example)
    except Exception as e:  # pylint: disable=broad-except
        py_utils.reraise(e,
                         prefix='Failed to encode example:\n',
                         suffix=f'{example}\n')

    return serializer.serialize_example(encoded_example)
示例#5
0
def _reraise_with_list_builders(
    e: Exception,
    ns_name: Optional[str],
    builder_name: str,
) -> NoReturn:
    """Add the list of available builders to the DatasetNotFoundError."""
    # Should optimize to only filter through given namespace
    all_datasets = list_builders()
    all_datasets_str = '\n\t- '.join([''] + all_datasets)
    error_string = f'Available datasets:{all_datasets_str}\n'
    error_string += textwrap.dedent("""
      Check that:
          - if dataset was added recently, it may only be available
            in `tfds-nightly`
          - the dataset name is spelled correctly
          - dataset class defines all base class abstract methods
          - the module defining the dataset class is imported
      """)

    # Add close matches
    name = f'{ns_name}:{builder_name}' if ns_name else builder_name
    close_matches = difflib.get_close_matches(name, all_datasets, n=1)
    if close_matches:
        error_string += f'\nDid you meant: {close_matches[0]}'

    raise py_utils.reraise(e, suffix=error_string)
示例#6
0
def _dataset_name_and_kwargs_from_name_str(
    name_str: str,
) -> Tuple[str, Dict[str, Value]]:
  """Extract kwargs from name str."""
  err_msg = textwrap.dedent(
      f"""\
      Parsing builder name string {name_str} failed.
      The builder name string must be of the following format:
        dataset_name[/config_name][:version][/kwargs]

        Where:

          * dataset_name and config_name are string following python variable naming.
          * version is of the form x.y.z where {{x,y,z}} can be any digit or *.
          * kwargs is a comma list separated of arguments and values to pass to
            builder.

        Examples:
          my_dataset
          my_dataset:1.2.*
          my_dataset/config1
          my_dataset/config1:1.*.*
          my_dataset/config1/arg1=val1,arg2=val2
          my_dataset/config1:1.2.3/right=True,foo=bar,rate=1.2
      """
  )

  res = _NAME_REG.match(name_str)
  if not res:
    raise ValueError(err_msg)
  name = res.group('dataset_name')
  # Normalize the name to accept CamelCase
  name = camelcase_to_snakecase(name)
  kwargs = _kwargs_str_to_kwargs(res.group('kwargs'))
  try:
    for attr in ['config', 'version']:
      val = res.group(attr)
      if val is None:
        continue
      if attr in kwargs:
        raise ValueError('Dataset %s: cannot pass %s twice.' % (name, attr))
      kwargs[attr] = val
    return name, kwargs
  except Exception as e:  # pylint: disable=broad-except
    py_utils.reraise(e, prefix=err_msg)  # pytype: disable=bad-return-type