Python absl.flags.register_validator() Examples

The following are 4 code examples of absl.flags.register_validator(). 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: flagsaver_test.py    From abseil-py with Apache License 2.0 6 votes vote down vote up
def test_validator_list_is_restored(self):

    self.assertLen(FLAGS['flagsaver_test_flag0'].validators, 1)
    original_validators = list(FLAGS['flagsaver_test_flag0'].validators)

    @flagsaver.flagsaver
    def modify_validators():

      def no_space(value):
        return ' ' not in value

      flags.register_validator('flagsaver_test_flag0', no_space)
      self.assertLen(FLAGS['flagsaver_test_flag0'].validators, 2)

    modify_validators()
    self.assertEqual(
        original_validators, FLAGS['flagsaver_test_flag0'].validators) 
Example #2
Source File: app.py    From clgen with GNU General Public License v3.0 5 votes vote down vote up
def RegisterFlagValidator(
  flag_name: str,
  checker: Callable[[Any], bool],
  message: str = "Flag validation failed",
):
  """Adds a constraint, which will be enforced during program execution.

  The constraint is validated when flags are initially parsed, and after each
  change of the corresponding flag's value.

  Args:
    flag_name: str, name of the flag to be checked.
    checker: callable, a function to validate the flag.
        input - A single positional argument: The value of the corresponding
            flag (string, boolean, etc.  This value will be passed to checker
            by the library).
        output - bool, True if validator constraint is satisfied.
            If constraint is not satisfied, it should either return False or
            raise flags.ValidationError(desired_error_message).
    message: str, error text to be shown to the user if checker returns False.
        If checker raises flags.ValidationError, message from the raised
        error will be shown.

  Raises:
    AttributeError: Raised when flag_name is not registered as a valid flag
        name.
  """
  absl_flags.register_validator(flag_name, checker, message) 
Example #3
Source File: inference_demo.py    From g-tensorflow-models with Apache License 2.0 5 votes vote down vote up
def _validate_flags():
  flags.register_validator('checkpoint_path', bool,
                           'Must provide `checkpoint_path`.')
  flags.register_validator(
      'generated_x_dir',
      lambda x: False if (FLAGS.image_set_y_glob and not x) else True,
      'Must provide `generated_x_dir`.')
  flags.register_validator(
      'generated_y_dir',
      lambda x: False if (FLAGS.image_set_x_glob and not x) else True,
      'Must provide `generated_y_dir`.') 
Example #4
Source File: inference_demo.py    From multilabel-image-classification-tensorflow with MIT License 5 votes vote down vote up
def _validate_flags():
  flags.register_validator('checkpoint_path', bool,
                           'Must provide `checkpoint_path`.')
  flags.register_validator(
      'generated_x_dir',
      lambda x: False if (FLAGS.image_set_y_glob and not x) else True,
      'Must provide `generated_x_dir`.')
  flags.register_validator(
      'generated_y_dir',
      lambda x: False if (FLAGS.image_set_x_glob and not x) else True,
      'Must provide `generated_y_dir`.')