Python absl.flags.DEFINE_list() Examples

The following are 14 code examples of absl.flags.DEFINE_list(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module absl.flags , or try the search function .
Example #1
Source File: flags.py    From benchmarks with Apache License 2.0 7 votes vote down vote up
def define_flags(specs=None):
  """Define a command line flag for each ParamSpec in flags.param_specs."""
  specs = specs or param_specs
  define_flag = {
      'boolean': absl_flags.DEFINE_boolean,
      'float': absl_flags.DEFINE_float,
      'integer': absl_flags.DEFINE_integer,
      'string': absl_flags.DEFINE_string,
      'enum': absl_flags.DEFINE_enum,
      'list': absl_flags.DEFINE_list
  }
  for name, param_spec in six.iteritems(specs):
    if param_spec.flag_type not in define_flag:
      raise ValueError('Unknown flag_type %s' % param_spec.flag_type)
    else:
      define_flag[param_spec.flag_type](name, param_spec.default_value,
                                        help=param_spec.description,
                                        **param_spec.kwargs) 
Example #2
Source File: flags_test.py    From abseil-py with Apache License 2.0 6 votes vote down vote up
def test_list_flag_format(self):
    """Tests for correctly-formatted list flags."""
    flags.DEFINE_list('listflag', '', 'A list of arguments')

    def _check_parsing(listval):
      """Parse a particular value for our test flag, --listflag."""
      argv = FLAGS(['./program', '--listflag=' + listval, 'plain-arg'])
      self.assertEqual(['./program', 'plain-arg'], argv)
      return FLAGS.listflag

    # Basic success case
    self.assertEqual(_check_parsing('foo,bar'), ['foo', 'bar'])
    # Success case: newline in argument is quoted.
    self.assertEqual(_check_parsing('"foo","bar\nbar"'), ['foo', 'bar\nbar'])
    # Failure case: newline in argument is unquoted.
    self.assertRaises(
        flags.IllegalFlagValueError, _check_parsing, '"foo",bar\nbar')
    # Failure case: unmatched ".
    self.assertRaises(
        flags.IllegalFlagValueError, _check_parsing, '"foo,barbar') 
Example #3
Source File: flags.py    From dlcookbook-dlbs with Apache License 2.0 6 votes vote down vote up
def define_flags():
  """Define a command line flag for each ParamSpec in flags.param_specs."""
  define_flag = {
      'boolean': absl_flags.DEFINE_boolean,
      'float': absl_flags.DEFINE_float,
      'integer': absl_flags.DEFINE_integer,
      'string': absl_flags.DEFINE_string,
      'enum': absl_flags.DEFINE_enum,
      'list': absl_flags.DEFINE_list
  }
  for name, param_spec in six.iteritems(param_specs):
    if param_spec.flag_type not in define_flag:
      raise ValueError('Unknown flag_type %s' % param_spec.flag_type)
    else:
      define_flag[param_spec.flag_type](name, param_spec.default_value,
                                        help=param_spec.description,
                                        **param_spec.kwargs) 
Example #4
Source File: app.py    From clgen with GNU General Public License v3.0 6 votes vote down vote up
def DEFINE_list(
  name: str,
  default: Optional[List[Any]],
  help: str,
  required: bool = False,
  validator: Callable[[List[Any]], bool] = None,
):
  """Registers a flag whose value must be a list."""
  absl_flags.DEFINE_list(
    name, default, help, module_name=get_calling_module_name(),
  )
  if required:
    absl_flags.mark_flag_as_required(name)
  if validator:
    RegisterFlagValidator(name, validator)


# My custom flag types. 
Example #5
Source File: flags.py    From benchmarks with Apache License 2.0 5 votes vote down vote up
def DEFINE_list(name, default, help):  # pylint: disable=invalid-name,redefined-builtin
  param_specs[name] = ParamSpec('list', default, help, {}) 
Example #6
Source File: flags_test.py    From abseil-py with Apache License 2.0 5 votes vote down vote up
def test_unicode_in_list(self):
    flags.DEFINE_list('unicode_list', ['abc', b'\xC3\x80'.decode('utf-8'),
                                       b'\xC3\xBD'.decode('utf-8')],
                      b'help:\xC3\xAB'.decode('utf-8'))
    argv = ('./program',)
    FLAGS(argv)   # should not raise any exceptions

    argv = ('./program', '--unicode_list=hello,there')
    FLAGS(argv)   # should not raise any exceptions 
Example #7
Source File: flags_test.py    From abseil-py with Apache License 2.0 5 votes vote down vote up
def test_xmloutput(self):
    flags.DEFINE_string('unicode1', b'\xC3\x80\xC3\xBD'.decode('utf-8'),
                        b'help:\xC3\xAC'.decode('utf-8'))
    flags.DEFINE_list('unicode2', ['abc', b'\xC3\x80'.decode('utf-8'),
                                   b'\xC3\xBD'.decode('utf-8')],
                      b'help:\xC3\xAD'.decode('utf-8'))
    flags.DEFINE_list('non_unicode', ['abc', 'def', 'ghi'],
                      b'help:\xC3\xAD'.decode('utf-8'))

    outfile = io.StringIO() if six.PY3 else io.BytesIO()
    FLAGS.write_help_in_xml_format(outfile)
    actual_output = outfile.getvalue()
    if six.PY2:
      actual_output = actual_output.decode('utf-8')

    # The xml output is large, so we just check parts of it.
    self.assertIn(b'<name>unicode1</name>\n'
                  b'    <meaning>help:\xc3\xac</meaning>\n'
                  b'    <default>\xc3\x80\xc3\xbd</default>\n'
                  b'    <current>\xc3\x80\xc3\xbd</current>'.decode('utf-8'),
                  actual_output)
    if six.PY2:
      self.assertIn(b'<name>unicode2</name>\n'
                    b'    <meaning>help:\xc3\xad</meaning>\n'
                    b'    <default>abc,\xc3\x80,\xc3\xbd</default>\n'
                    b"    <current>['abc', u'\\xc0', u'\\xfd']"
                    b'</current>'.decode('utf-8'),
                    actual_output)
    else:
      self.assertIn(b'<name>unicode2</name>\n'
                    b'    <meaning>help:\xc3\xad</meaning>\n'
                    b'    <default>abc,\xc3\x80,\xc3\xbd</default>\n'
                    b"    <current>['abc', '\xc3\x80', '\xc3\xbd']"
                    b'</current>'.decode('utf-8'),
                    actual_output)
    self.assertIn(b'<name>non_unicode</name>\n'
                  b'    <meaning>help:\xc3\xad</meaning>\n'
                  b'    <default>abc,def,ghi</default>\n'
                  b"    <current>['abc', 'def', 'ghi']"
                  b'</current>'.decode('utf-8'),
                  actual_output) 
Example #8
Source File: flags_test.py    From abseil-py with Apache License 2.0 5 votes vote down vote up
def setUp(self):
    self.flag_values = flags.FlagValues()
    flags.DEFINE_string('unittest_message1', 'Foo!', 'You Add Here.',
                        flag_values=self.flag_values)
    flags.DEFINE_string('unittest_message2', 'Bar!', 'Hello, Sailor!',
                        flag_values=self.flag_values)
    flags.DEFINE_boolean('unittest_boolflag', 0, 'Some Boolean thing',
                         flag_values=self.flag_values)
    flags.DEFINE_integer('unittest_number', 12345, 'Some integer',
                         lower_bound=0, flag_values=self.flag_values)
    flags.DEFINE_list('UnitTestList', '1,2,3', 'Some list',
                      flag_values=self.flag_values)
    self.tmp_path = None
    self.flag_values.mark_as_parsed() 
Example #9
Source File: flags_helpxml_test.py    From abseil-py with Apache License 2.0 5 votes vote down vote up
def test_flag_help_in_xml_comma_separated_list(self):
    flags.DEFINE_list('files', 'a.cc,a.h,archive/old.zip',
                      'Files to process.', flag_values=self.fv)
    expected_output = (
        '<flag>\n'
        '  <file>tool</file>\n'
        '  <name>files</name>\n'
        '  <meaning>Files to process.</meaning>\n'
        '  <default>a.cc,a.h,archive/old.zip</default>\n'
        '  <current>[\'a.cc\', \'a.h\', \'archive/old.zip\']</current>\n'
        '  <type>comma separated list of strings</type>\n'
        '  <list_separator>\',\'</list_separator>\n'
        '</flag>\n')
    self._check_flag_help_in_xml('files', 'tool', expected_output) 
Example #10
Source File: flags_helpxml_test.py    From abseil-py with Apache License 2.0 5 votes vote down vote up
def test_none_as_default_arguments_comma_separated_list(self):
    flags.DEFINE_list('allow_users', None,
                      'Users with access.', flag_values=self.fv)
    expected_output = (
        '<flag>\n'
        '  <file>tool</file>\n'
        '  <name>allow_users</name>\n'
        '  <meaning>Users with access.</meaning>\n'
        '  <default></default>\n'
        '  <current>None</current>\n'
        '  <type>comma separated list of strings</type>\n'
        '  <list_separator>\',\'</list_separator>\n'
        '</flag>\n')
    self._check_flag_help_in_xml('allow_users', 'tool', expected_output) 
Example #11
Source File: flags.py    From dlcookbook-dlbs with Apache License 2.0 5 votes vote down vote up
def DEFINE_list(name, default, help):  # pylint: disable=invalid-name,redefined-builtin
  param_specs[name] = ParamSpec('list', default, help, {}) 
Example #12
Source File: benchmark_main.py    From g-tensorflow-models with Apache License 2.0 4 votes vote down vote up
def define_keras_benchmark_flags():
  """Add flags for keras built-in application models."""
  flags_core.define_base(hooks=False)
  flags_core.define_performance()
  flags_core.define_image()
  flags_core.define_benchmark()
  flags.adopt_module_key_flags(flags_core)

  flags_core.set_defaults(
      data_format="channels_last",
      use_synthetic_data=True,
      batch_size=32,
      train_epochs=2)

  flags.DEFINE_enum(
      name="model", default=None,
      enum_values=MODELS.keys(), case_sensitive=False,
      help=flags_core.help_wrap(
          "Model to be benchmarked."))

  flags.DEFINE_integer(
      name="num_train_images", default=1000,
      help=flags_core.help_wrap(
          "The number of synthetic images for training. The default value is "
          "1000."))

  flags.DEFINE_integer(
      name="num_eval_images", default=50,
      help=flags_core.help_wrap(
          "The number of synthetic images for evaluation. The default value is "
          "50."))

  flags.DEFINE_boolean(
      name="eager", default=False, help=flags_core.help_wrap(
          "To enable eager execution. Note that if eager execution is enabled, "
          "only one GPU is utilized even if multiple GPUs are provided and "
          "multi_gpu_model is used."))

  flags.DEFINE_boolean(
      name="dist_strat", default=False, help=flags_core.help_wrap(
          "To enable distribution strategy for model training and evaluation. "
          "Number of GPUs used for distribution strategy can be set by the "
          "argument --num_gpus."))

  flags.DEFINE_list(
      name="callbacks",
      default=["ExamplesPerSecondCallback", "LoggingMetricCallback"],
      help=flags_core.help_wrap(
          "A list of (case insensitive) strings to specify the names of "
          "callbacks. For example: `--callbacks ExamplesPerSecondCallback,"
          "LoggingMetricCallback`"))

  @flags.multi_flags_validator(
      ["eager", "dist_strat"],
      message="Both --eager and --dist_strat were set. Only one can be "
              "defined, as DistributionStrategy is not supported in Eager "
              "execution currently.")
  # pylint: disable=unused-variable
  def _check_eager_dist_strat(flag_dict):
    return not(flag_dict["eager"] and flag_dict["dist_strat"]) 
Example #13
Source File: aclgen.py    From capirca with Apache License 2.0 4 votes vote down vote up
def SetupFlags():
  flags.DEFINE_string(
      'base_directory',
      './policies',
      'The base directory to look for acls; '
      'typically where you\'d find ./corp and ./prod')
  flags.DEFINE_string(
      'definitions_directory',
      './def',
      'Directory where the definitions can be found.')
  flags.DEFINE_string(
      'policy_file',
      None,
      'Individual policy file to generate.')
  flags.DEFINE_string(
      'output_directory',
      './',
      'Directory to output the rendered acls.')
  flags.DEFINE_boolean(
      'optimize',
      False,
      'Turn on optimization.',
      short_name='o')
  flags.DEFINE_boolean(
      'recursive',
      True,
      'Descend recursively from the base directory rendering acls')
  flags.DEFINE_boolean(
      'debug',
      False,
      'Debug messages')
  flags.DEFINE_boolean(
      'verbose',
      False,
      'Verbose messages')
  flags.DEFINE_list(
      'ignore_directories',
      'DEPRECATED, def',
      'Don\'t descend into directories that look like this string')
  flags.DEFINE_integer(
      'max_renderers',
      10,
      'Max number of rendering processes to use.')
  flags.DEFINE_boolean(
      'shade_check',
      False,
      'Raise an error when a term is completely shaded by a prior term.')
  flags.DEFINE_integer(
      'exp_info',
      2,
      'Print a info message when a term is set to expire in that many weeks.') 
Example #14
Source File: benchmark_main.py    From multilabel-image-classification-tensorflow with MIT License 4 votes vote down vote up
def define_keras_benchmark_flags():
  """Add flags for keras built-in application models."""
  flags_core.define_base(hooks=False)
  flags_core.define_performance()
  flags_core.define_image()
  flags_core.define_benchmark()
  flags.adopt_module_key_flags(flags_core)

  flags_core.set_defaults(
      data_format="channels_last",
      use_synthetic_data=True,
      batch_size=32,
      train_epochs=2)

  flags.DEFINE_enum(
      name="model", default=None,
      enum_values=MODELS.keys(), case_sensitive=False,
      help=flags_core.help_wrap(
          "Model to be benchmarked."))

  flags.DEFINE_integer(
      name="num_train_images", default=1000,
      help=flags_core.help_wrap(
          "The number of synthetic images for training. The default value is "
          "1000."))

  flags.DEFINE_integer(
      name="num_eval_images", default=50,
      help=flags_core.help_wrap(
          "The number of synthetic images for evaluation. The default value is "
          "50."))

  flags.DEFINE_boolean(
      name="eager", default=False, help=flags_core.help_wrap(
          "To enable eager execution. Note that if eager execution is enabled, "
          "only one GPU is utilized even if multiple GPUs are provided and "
          "multi_gpu_model is used."))

  flags.DEFINE_boolean(
      name="dist_strat", default=False, help=flags_core.help_wrap(
          "To enable distribution strategy for model training and evaluation. "
          "Number of GPUs used for distribution strategy can be set by the "
          "argument --num_gpus."))

  flags.DEFINE_list(
      name="callbacks",
      default=["ExamplesPerSecondCallback", "LoggingMetricCallback"],
      help=flags_core.help_wrap(
          "A list of (case insensitive) strings to specify the names of "
          "callbacks. For example: `--callbacks ExamplesPerSecondCallback,"
          "LoggingMetricCallback`"))

  @flags.multi_flags_validator(
      ["eager", "dist_strat"],
      message="Both --eager and --dist_strat were set. Only one can be "
              "defined, as DistributionStrategy is not supported in Eager "
              "execution currently.")
  # pylint: disable=unused-variable
  def _check_eager_dist_strat(flag_dict):
    return not(flag_dict["eager"] and flag_dict["dist_strat"])