Python absl.flags.DEFINE_multi_integer() Examples

The following are 6 code examples of absl.flags.DEFINE_multi_integer(). 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_test.py    From abseil-py with Apache License 2.0 6 votes vote down vote up
def test_multi_numerical_flags(self):
    """Test multi_int and multi_float flags."""

    int_defaults = [77, 88]
    flags.DEFINE_multi_integer('m_int', int_defaults,
                               'integer option that can occur multiple times',
                               short_name='mi')
    self.assertListEqual(FLAGS.get_flag_value('m_int', None), int_defaults)
    argv = ('./program', '--m_int=-99', '--mi=101')
    FLAGS(argv)
    self.assertListEqual(FLAGS.get_flag_value('m_int', None), [-99, 101])

    float_defaults = [2.2, 3]
    flags.DEFINE_multi_float('m_float', float_defaults,
                             'float option that can occur multiple times',
                             short_name='mf')
    for (expected, actual) in zip(
        float_defaults, FLAGS.get_flag_value('m_float', None)):
      self.assertAlmostEqual(expected, actual)
    argv = ('./program', '--m_float=-17', '--mf=2.78e9')
    FLAGS(argv)
    expected_floats = [-17.0, 2.78e9]
    for (expected, actual) in zip(
        expected_floats, FLAGS.get_flag_value('m_float', None)):
      self.assertAlmostEqual(expected, actual) 
Example #2
Source File: flags_test.py    From abseil-py with Apache License 2.0 6 votes vote down vote up
def test_multi_numerical_with_tuples(self):
    """Verify multi_int/float accept tuples as default values."""
    flags.DEFINE_multi_integer(
        'm_int_tuple',
        (77, 88),
        'integer option that can occur multiple times',
        short_name='mi_tuple')
    self.assertListEqual(FLAGS.get_flag_value('m_int_tuple', None), [77, 88])

    dict_with_float_keys = {2.2: 'hello', 3: 'happy'}
    float_defaults = dict_with_float_keys.keys()
    flags.DEFINE_multi_float(
        'm_float_tuple',
        float_defaults,
        'float option that can occur multiple times',
        short_name='mf_tuple')
    for (expected, actual) in zip(float_defaults,
                                  FLAGS.get_flag_value('m_float_tuple', None)):
      self.assertAlmostEqual(expected, actual) 
Example #3
Source File: tpu_executor.py    From tpu_models with Apache License 2.0 5 votes vote down vote up
def define_tpu_flags():
  """Define common flags for TPU."""
  flags.DEFINE_string(
      'tpu',
      default=None,
      help='The Cloud TPU to use for training. This should be either the name '
      'used when creating the Cloud TPU, or a grpc://ip.address.of.tpu:8470 '
      'url.')
  flags.DEFINE_string(
      'gcp_project',
      default=None,
      help='Project name for the Cloud TPU-enabled project. If not specified, we '
      'will attempt to automatically detect the GCE project from metadata.')
  flags.DEFINE_string(
      'tpu_zone',
      default=None,
      help='GCE zone where the Cloud TPU is located in. If not specified, we '
      'will attempt to automatically detect the GCE project from metadata.')
  flags.DEFINE_integer(
      'num_cores', default=8, help='Number of TPU cores for training')
  flags.DEFINE_string(
      'eval_master',
      default='',
      help='GRPC URL of the eval master. Set to an appropiate value when running '
      'on CPU/GPU')
  flags.DEFINE_bool('use_tpu', True, 'Use TPUs rather than CPUs')
  flags.DEFINE_multi_integer(
      'input_partition_dims', [1],
      'A list that describes the partition dims for all the tensors.')
  flags.DEFINE_integer('iterations_per_loop', 8,
                       'Number of iterations per TPU training loop') 
Example #4
Source File: flags_test.py    From abseil-py with Apache License 2.0 5 votes vote down vote up
def test_single_value_default(self):
    """Test multi_int and multi_float flags with a single default value."""
    int_default = 77
    flags.DEFINE_multi_integer('m_int1', int_default,
                               'integer option that can occur multiple times')
    self.assertListEqual(FLAGS.get_flag_value('m_int1', None), [int_default])

    float_default = 2.2
    flags.DEFINE_multi_float('m_float1', float_default,
                             'float option that can occur multiple times')
    actual = FLAGS.get_flag_value('m_float1', None)
    self.assertEqual(1, len(actual))
    self.assertAlmostEqual(actual[0], float_default) 
Example #5
Source File: flags_test.py    From abseil-py with Apache License 2.0 5 votes vote down vote up
def test_bad_multi_numerical_flags(self):
    """Test multi_int and multi_float flags with non-parseable values."""

    # Test non-parseable defaults.
    self.assertRaisesRegex(
        flags.IllegalFlagValueError,
        r"flag --m_int2=abc: invalid literal for int\(\) with base 10: 'abc'",
        flags.DEFINE_multi_integer, 'm_int2', ['abc'], 'desc')

    self.assertRaisesRegex(
        flags.IllegalFlagValueError,
        r'flag --m_float2=abc: '
        r'(invalid literal for float\(\)|could not convert string to float): '
        r"'?abc'?",
        flags.DEFINE_multi_float, 'm_float2', ['abc'], 'desc')

    # Test non-parseable command line values.
    flags.DEFINE_multi_integer('m_int2', '77',
                               'integer option that can occur multiple times')
    argv = ('./program', '--m_int2=def')
    self.assertRaisesRegex(
        flags.IllegalFlagValueError,
        r"flag --m_int2=def: invalid literal for int\(\) with base 10: 'def'",
        FLAGS, argv)

    flags.DEFINE_multi_float('m_float2', 2.2,
                             'float option that can occur multiple times')
    argv = ('./program', '--m_float2=def')
    self.assertRaisesRegex(
        flags.IllegalFlagValueError,
        r'flag --m_float2=def: '
        r'(invalid literal for float\(\)|could not convert string to float): '
        r"'?def'?",
        FLAGS, argv) 
Example #6
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_multi_int(self):
    flags.DEFINE_multi_integer('cols', [5, 7, 23],
                               'Columns to select', flag_values=self.fv)
    expected_output = (
        '<flag>\n'
        '  <file>tool</file>\n'
        '  <name>cols</name>\n'
        '  <meaning>Columns to select;\n    '
        'repeat this option to specify a list of values</meaning>\n'
        '  <default>[5, 7, 23]</default>\n'
        '  <current>[5, 7, 23]</current>\n'
        '  <type>multi int</type>\n'
        '</flag>\n')
    self._check_flag_help_in_xml('cols', 'tool', expected_output)