Beispiel #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)
Beispiel #2
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
Beispiel #3
0
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)
Beispiel #4
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)
Beispiel #5
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
Beispiel #6
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
Beispiel #7
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())
Beispiel #8
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