Ejemplo n.º 1
0
def test_FromString_DecodeError_unknown_field():
  """Test that DecodeError is raised if string contains unknown field."""
  with pytest.raises(pbutil.DecodeError) as e_info:
    proto = pbutil.FromString('foo: "bar"',
                              test_protos_pb2.AnotherTestMessage())
  assert ('1:1 : Message type "labm8.AnotherTestMessage" '
          'has no field named "foo".') == str(e_info.value)
Ejemplo n.º 2
0
def ExtractJavaMethods(text: str,
                       static_only: bool = True) -> typing.List[str]:
    """Extract Java methods from a file.

  Args:
    text: The text of the target file.
    static_only: If true, only static methods are returned.

  Returns:
    A list of method implementations.

  Raises:
    ValueError: In case method extraction fails.
  """
    logging.debug('$ %s', JAVA_METHODS_EXTRACTOR)
    process = subprocess.Popen([JAVA_METHODS_EXTRACTOR],
                               stdin=subprocess.PIPE,
                               stdout=subprocess.PIPE,
                               stderr=subprocess.PIPE,
                               universal_newlines=True,
                               env=STATIC_ONLY_ENV if static_only else None)
    stdout, stderr = process.communicate(text)
    if process.returncode:
        raise ValueError("JavaMethodsExtractor exited with non-zero "
                         f"status {process.returncode}")
    methods_list = pbutil.FromString(stdout, scrape_repos_pb2.MethodsList())
    return list(methods_list.method)
Ejemplo n.º 3
0
def EnumerateModels() -> typing.List[model_pb2.Model]:
    """Enumerate the model configurations."""
    models = []
    base_model = pbutil.FromString(BASE_MODEL, model_pb2.Model())
    for num_neurons, num_layers in itertools.product(NUM_NEURONS, NUM_LAYERS):
        model = model_pb2.Model()
        model.CopyFrom(base_model)
        model.architecture.neurons_per_layer = num_neurons
        model.architecture.num_layers = num_layers
        models.append(model)
    return models
Ejemplo n.º 4
0
def GetGlobalConfig() -> config_pb2.GlobalConfig:
    """Read and return the GlobalConfig proto.

  Returns:
    A GlobalConfig instance.

  Raises:
    CorruptConfig: In case the configuration file could not be parsed.
  """
    try:
        config = pbutil.FromString(config_pbtxt_py.STRING,
                                   config_pb2.GlobalConfig())
    except pbutil.DecodeError as e:
        raise CorruptConfig(config_pbtxt_py.STRING, e)
    return config
Ejemplo n.º 5
0
Archivo: env.py Proyecto: SpringRi/phd
def all_envs() -> Iterator[OpenCLEnvironment]:
    """
  Iterate over all available OpenCL environments on a system.

  Parameters
  ----------
  devtype : str, optional
      OpenCL device type to filter by, one of: {all,cpu,gpu}.

  Returns
  -------
  Iterator[OpenCLEnvironment]
      An iterator over all available OpenCL environments.
  """
    stdout = subprocess.check_output([str(CLINFO)])
    devices = pbutil.FromString(stdout, clinfo_pb2.OpenClDevices())
    for device in devices.device:
        yield OpenCLEnvironment(device)
Ejemplo n.º 6
0
def test_FromString_valid_input():
  """Test that an valid input is decoded."""
  proto = pbutil.FromString('number: 5', test_protos_pb2.AnotherTestMessage())
  assert 5 == proto.number
Ejemplo n.º 7
0
def test_FromString_empty_string_required_fields_uninitialized_okay():
  """Test that an empty string raises DecodeError if uninitialized fields."""
  proto = pbutil.FromString(
      '', test_protos_pb2.TestMessage(), uninitialized_okay=True)
  assert not proto.string
Ejemplo n.º 8
0
def test_FromString_empty_string_required_fields():
  """Test that an empty string raises DecodeError if uninitialized fields."""
  with pytest.raises(pbutil.DecodeError):
    pbutil.FromString('', test_protos_pb2.TestMessage())
Ejemplo n.º 9
0
def test_FromString_empty_string():
  """Test that an empty string can be parsed as a proto."""
  proto = pbutil.FromString('', test_protos_pb2.AnotherTestMessage())
  assert not proto.number
Ejemplo n.º 10
0
def GetBuildInfo() -> config_pb2.BuildInfo:
    """Return the build state."""
    return pbutil.FromString(build_info_pbtxt_py.STRING,
                             config_pb2.BuildInfo(),
                             uninitialized_okay=False)