ortools.linear_solver.linear_solver_pb2

Generated protocol buffer code.

  1# -*- coding: utf-8 -*-
  2# Generated by the protocol buffer compiler.  DO NOT EDIT!
  3# source: ortools/linear_solver/linear_solver.proto
  4# Protobuf Python Version: 5.26.1
  5"""Generated protocol buffer code."""
  6from google.protobuf import descriptor as _descriptor
  7from google.protobuf import descriptor_pool as _descriptor_pool
  8from google.protobuf import symbol_database as _symbol_database
  9from google.protobuf.internal import builder as _builder
 10# @@protoc_insertion_point(imports)
 11
 12_sym_db = _symbol_database.Default()
 13
 14
 15from ortools.util import optional_boolean_pb2 as ortools_dot_util_dot_optional__boolean__pb2
 16
 17
 18DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile(b'\n)ortools/linear_solver/linear_solver.proto\x12\x13operations_research\x1a#ortools/util/optional_boolean.proto\"\xb2\x01\n\x0fMPVariableProto\x12\x19\n\x0blower_bound\x18\x01 \x01(\x01:\x04-inf\x12\x18\n\x0bupper_bound\x18\x02 \x01(\x01:\x03inf\x12 \n\x15objective_coefficient\x18\x03 \x01(\x01:\x01\x30\x12\x19\n\nis_integer\x18\x04 \x01(\x08:\x05\x66\x61lse\x12\x0e\n\x04name\x18\x05 \x01(\t:\x00\x12\x1d\n\x12\x62ranching_priority\x18\x06 \x01(\x05:\x01\x30\"\xa0\x01\n\x11MPConstraintProto\x12\x15\n\tvar_index\x18\x06 \x03(\x05\x42\x02\x10\x01\x12\x17\n\x0b\x63oefficient\x18\x07 \x03(\x01\x42\x02\x10\x01\x12\x19\n\x0blower_bound\x18\x02 \x01(\x01:\x04-inf\x12\x18\n\x0bupper_bound\x18\x03 \x01(\x01:\x03inf\x12\x0e\n\x04name\x18\x04 \x01(\t:\x00\x12\x16\n\x07is_lazy\x18\x05 \x01(\x08:\x05\x66\x61lse\"\xf7\x04\n\x18MPGeneralConstraintProto\x12\x0e\n\x04name\x18\x01 \x01(\t:\x00\x12J\n\x14indicator_constraint\x18\x02 \x01(\x0b\x32*.operations_research.MPIndicatorConstraintH\x00\x12>\n\x0esos_constraint\x18\x03 \x01(\x0b\x32$.operations_research.MPSosConstraintH\x00\x12J\n\x14quadratic_constraint\x18\x04 \x01(\x0b\x32*.operations_research.MPQuadraticConstraintH\x00\x12>\n\x0e\x61\x62s_constraint\x18\x05 \x01(\x0b\x32$.operations_research.MPAbsConstraintH\x00\x12@\n\x0e\x61nd_constraint\x18\x06 \x01(\x0b\x32&.operations_research.MPArrayConstraintH\x00\x12?\n\ror_constraint\x18\x07 \x01(\x0b\x32&.operations_research.MPArrayConstraintH\x00\x12L\n\x0emin_constraint\x18\x08 \x01(\x0b\x32\x32.operations_research.MPArrayWithConstantConstraintH\x00\x12L\n\x0emax_constraint\x18\t \x01(\x0b\x32\x32.operations_research.MPArrayWithConstantConstraintH\x00\x42\x14\n\x12general_constraint\"y\n\x15MPIndicatorConstraint\x12\x11\n\tvar_index\x18\x01 \x01(\x05\x12\x11\n\tvar_value\x18\x02 \x01(\x05\x12:\n\nconstraint\x18\x03 \x01(\x0b\x32&.operations_research.MPConstraintProto\"\x9f\x01\n\x0fMPSosConstraint\x12\x45\n\x04type\x18\x01 \x01(\x0e\x32).operations_research.MPSosConstraint.Type:\x0cSOS1_DEFAULT\x12\x11\n\tvar_index\x18\x02 \x03(\x05\x12\x0e\n\x06weight\x18\x03 \x03(\x01\"\"\n\x04Type\x12\x10\n\x0cSOS1_DEFAULT\x10\x00\x12\x08\n\x04SOS2\x10\x01\"\xb4\x01\n\x15MPQuadraticConstraint\x12\x11\n\tvar_index\x18\x01 \x03(\x05\x12\x13\n\x0b\x63oefficient\x18\x02 \x03(\x01\x12\x13\n\x0bqvar1_index\x18\x03 \x03(\x05\x12\x13\n\x0bqvar2_index\x18\x04 \x03(\x05\x12\x14\n\x0cqcoefficient\x18\x05 \x03(\x01\x12\x19\n\x0blower_bound\x18\x06 \x01(\x01:\x04-inf\x12\x18\n\x0bupper_bound\x18\x07 \x01(\x01:\x03inf\"A\n\x0fMPAbsConstraint\x12\x11\n\tvar_index\x18\x01 \x01(\x05\x12\x1b\n\x13resultant_var_index\x18\x02 \x01(\x05\"C\n\x11MPArrayConstraint\x12\x11\n\tvar_index\x18\x01 \x03(\x05\x12\x1b\n\x13resultant_var_index\x18\x02 \x01(\x05\"a\n\x1dMPArrayWithConstantConstraint\x12\x11\n\tvar_index\x18\x01 \x03(\x05\x12\x10\n\x08\x63onstant\x18\x02 \x01(\x01\x12\x1b\n\x13resultant_var_index\x18\x03 \x01(\x05\"U\n\x14MPQuadraticObjective\x12\x13\n\x0bqvar1_index\x18\x01 \x03(\x05\x12\x13\n\x0bqvar2_index\x18\x02 \x03(\x05\x12\x13\n\x0b\x63oefficient\x18\x03 \x03(\x01\"I\n\x19PartialVariableAssignment\x12\x15\n\tvar_index\x18\x01 \x03(\x05\x42\x02\x10\x01\x12\x15\n\tvar_value\x18\x02 \x03(\x01\x42\x02\x10\x01\"\xe4\x05\n\x0cMPModelProto\x12\x36\n\x08variable\x18\x03 \x03(\x0b\x32$.operations_research.MPVariableProto\x12:\n\nconstraint\x18\x04 \x03(\x0b\x32&.operations_research.MPConstraintProto\x12I\n\x12general_constraint\x18\x07 \x03(\x0b\x32-.operations_research.MPGeneralConstraintProto\x12\x17\n\x08maximize\x18\x01 \x01(\x08:\x05\x66\x61lse\x12\x1b\n\x10objective_offset\x18\x02 \x01(\x01:\x01\x30\x12\x46\n\x13quadratic_objective\x18\x08 \x01(\x0b\x32).operations_research.MPQuadraticObjective\x12\x0e\n\x04name\x18\x05 \x01(\t:\x00\x12\x45\n\rsolution_hint\x18\x06 \x01(\x0b\x32..operations_research.PartialVariableAssignment\x12@\n\nannotation\x18\t \x03(\x0b\x32,.operations_research.MPModelProto.Annotation\x1a\xfd\x01\n\nAnnotation\x12L\n\x0btarget_type\x18\x01 \x01(\x0e\x32\x37.operations_research.MPModelProto.Annotation.TargetType\x12\x14\n\x0ctarget_index\x18\x02 \x01(\x05\x12\x13\n\x0btarget_name\x18\x03 \x01(\t\x12\x13\n\x0bpayload_key\x18\x04 \x01(\t\x12\x15\n\rpayload_value\x18\x05 \x01(\t\"J\n\nTargetType\x12\x14\n\x10VARIABLE_DEFAULT\x10\x00\x12\x0e\n\nCONSTRAINT\x10\x01\x12\x16\n\x12GENERAL_CONSTRAINT\x10\x02\"\x1f\n\x0eOptionalDouble\x12\r\n\x05value\x18\x01 \x01(\x01\"\xbd\x04\n\x18MPSolverCommonParameters\x12=\n\x10relative_mip_gap\x18\x01 \x01(\x0b\x32#.operations_research.OptionalDouble\x12=\n\x10primal_tolerance\x18\x02 \x01(\x0b\x32#.operations_research.OptionalDouble\x12;\n\x0e\x64ual_tolerance\x18\x03 \x01(\x0b\x32#.operations_research.OptionalDouble\x12j\n\x0clp_algorithm\x18\x04 \x01(\x0e\x32?.operations_research.MPSolverCommonParameters.LPAlgorithmValues:\x13LP_ALGO_UNSPECIFIED\x12H\n\x08presolve\x18\x05 \x01(\x0e\x32$.operations_research.OptionalBoolean:\x10\x42OOL_UNSPECIFIED\x12G\n\x07scaling\x18\x07 \x01(\x0e\x32$.operations_research.OptionalBoolean:\x10\x42OOL_UNSPECIFIED\"g\n\x11LPAlgorithmValues\x12\x17\n\x13LP_ALGO_UNSPECIFIED\x10\x00\x12\x10\n\x0cLP_ALGO_DUAL\x10\x01\x12\x12\n\x0eLP_ALGO_PRIMAL\x10\x02\x12\x13\n\x0fLP_ALGO_BARRIER\x10\x03\"\xb3\x03\n\x11MPModelDeltaProto\x12 \n\x18\x62\x61seline_model_file_path\x18\x01 \x01(\t\x12Y\n\x12variable_overrides\x18\x02 \x03(\x0b\x32=.operations_research.MPModelDeltaProto.VariableOverridesEntry\x12]\n\x14\x63onstraint_overrides\x18\x03 \x03(\x0b\x32?.operations_research.MPModelDeltaProto.ConstraintOverridesEntry\x1a^\n\x16VariableOverridesEntry\x12\x0b\n\x03key\x18\x01 \x01(\x05\x12\x33\n\x05value\x18\x02 \x01(\x0b\x32$.operations_research.MPVariableProto:\x02\x38\x01\x1a\x62\n\x18\x43onstraintOverridesEntry\x12\x0b\n\x03key\x18\x01 \x01(\x05\x12\x35\n\x05value\x18\x02 \x01(\x0b\x32&.operations_research.MPConstraintProto:\x02\x38\x01\"\x9b\x08\n\x0eMPModelRequest\x12\x30\n\x05model\x18\x01 \x01(\x0b\x32!.operations_research.MPModelProto\x12\\\n\x0bsolver_type\x18\x02 \x01(\x0e\x32..operations_research.MPModelRequest.SolverType:\x17GLOP_LINEAR_PROGRAMMING\x12!\n\x19solver_time_limit_seconds\x18\x03 \x01(\x01\x12,\n\x1d\x65nable_internal_solver_output\x18\x04 \x01(\x08:\x05\x66\x61lse\x12\"\n\x1asolver_specific_parameters\x18\x05 \x01(\t\x12\x38\n)ignore_solver_specific_parameters_failure\x18\t \x01(\x08:\x05\x66\x61lse\x12;\n\x0bmodel_delta\x18\x08 \x01(\x0b\x32&.operations_research.MPModelDeltaProto\x12.\n#populate_additional_solutions_up_to\x18\x0b \x01(\x05:\x01\x30\"\xdc\x04\n\nSolverType\x12\x1a\n\x16\x43LP_LINEAR_PROGRAMMING\x10\x00\x12\x1b\n\x17GLOP_LINEAR_PROGRAMMING\x10\x02\x12\x1b\n\x17GLPK_LINEAR_PROGRAMMING\x10\x01\x12\x1d\n\x19GUROBI_LINEAR_PROGRAMMING\x10\x06\x12\x1d\n\x19XPRESS_LINEAR_PROGRAMMING\x10\x65\x12\x1c\n\x18\x43PLEX_LINEAR_PROGRAMMING\x10\n\x12\x1c\n\x18HIGHS_LINEAR_PROGRAMMING\x10\x0f\x12\"\n\x1eSCIP_MIXED_INTEGER_PROGRAMMING\x10\x03\x12\"\n\x1eGLPK_MIXED_INTEGER_PROGRAMMING\x10\x04\x12!\n\x1d\x43\x42\x43_MIXED_INTEGER_PROGRAMMING\x10\x05\x12$\n GUROBI_MIXED_INTEGER_PROGRAMMING\x10\x07\x12$\n XPRESS_MIXED_INTEGER_PROGRAMMING\x10\x66\x12#\n\x1f\x43PLEX_MIXED_INTEGER_PROGRAMMING\x10\x0b\x12#\n\x1fHIGHS_MIXED_INTEGER_PROGRAMMING\x10\x10\x12\x1b\n\x17\x42OP_INTEGER_PROGRAMMING\x10\x0c\x12\x1b\n\x17SAT_INTEGER_PROGRAMMING\x10\x0e\x12\x1b\n\x17PDLP_LINEAR_PROGRAMMING\x10\x08\x12&\n\"KNAPSACK_MIXED_INTEGER_PROGRAMMING\x10\r\"A\n\nMPSolution\x12\x17\n\x0fobjective_value\x18\x01 \x01(\x01\x12\x1a\n\x0evariable_value\x18\x02 \x03(\x01\x42\x02\x10\x01\"O\n\x0bMPSolveInfo\x12\x1f\n\x17solve_wall_time_seconds\x18\x01 \x01(\x01\x12\x1f\n\x17solve_user_time_seconds\x18\x02 \x01(\x01\"\x96\x03\n\x12MPSolutionResponse\x12T\n\x06status\x18\x01 \x01(\x0e\x32+.operations_research.MPSolverResponseStatus:\x17MPSOLVER_UNKNOWN_STATUS\x12\x12\n\nstatus_str\x18\x07 \x01(\t\x12\x17\n\x0fobjective_value\x18\x02 \x01(\x01\x12\x1c\n\x14\x62\x65st_objective_bound\x18\x05 \x01(\x01\x12\x1a\n\x0evariable_value\x18\x03 \x03(\x01\x42\x02\x10\x01\x12\x34\n\nsolve_info\x18\n \x01(\x0b\x32 .operations_research.MPSolveInfo\x12\x1c\n\x14solver_specific_info\x18\x0b \x01(\x0c\x12\x16\n\ndual_value\x18\x04 \x03(\x01\x42\x02\x10\x01\x12\x18\n\x0creduced_cost\x18\x06 \x03(\x01\x42\x02\x10\x01\x12=\n\x14\x61\x64\x64itional_solutions\x18\x08 \x03(\x0b\x32\x1f.operations_research.MPSolution*\xbd\x03\n\x16MPSolverResponseStatus\x12\x14\n\x10MPSOLVER_OPTIMAL\x10\x00\x12\x15\n\x11MPSOLVER_FEASIBLE\x10\x01\x12\x17\n\x13MPSOLVER_INFEASIBLE\x10\x02\x12\x16\n\x12MPSOLVER_UNBOUNDED\x10\x03\x12\x15\n\x11MPSOLVER_ABNORMAL\x10\x04\x12\x17\n\x13MPSOLVER_NOT_SOLVED\x10\x06\x12\x1b\n\x17MPSOLVER_MODEL_IS_VALID\x10\x61\x12\x1e\n\x1aMPSOLVER_CANCELLED_BY_USER\x10\x62\x12\x1b\n\x17MPSOLVER_UNKNOWN_STATUS\x10\x63\x12\x1a\n\x16MPSOLVER_MODEL_INVALID\x10\x05\x12(\n$MPSOLVER_MODEL_INVALID_SOLUTION_HINT\x10T\x12,\n(MPSOLVER_MODEL_INVALID_SOLVER_PARAMETERS\x10U\x12$\n MPSOLVER_SOLVER_TYPE_UNAVAILABLE\x10\x07\x12!\n\x1dMPSOLVER_INCOMPATIBLE_OPTIONS\x10qB#\n\x1f\x63om.google.ortools.linearsolverP\x01')
 19
 20_globals = globals()
 21_builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, _globals)
 22_builder.BuildTopDescriptorsAndMessages(DESCRIPTOR, 'ortools.linear_solver.linear_solver_pb2', _globals)
 23if not _descriptor._USE_C_DESCRIPTORS:
 24  _globals['DESCRIPTOR']._loaded_options = None
 25  _globals['DESCRIPTOR']._serialized_options = b'\n\037com.google.ortools.linearsolverP\001'
 26  _globals['_MPCONSTRAINTPROTO'].fields_by_name['var_index']._loaded_options = None
 27  _globals['_MPCONSTRAINTPROTO'].fields_by_name['var_index']._serialized_options = b'\020\001'
 28  _globals['_MPCONSTRAINTPROTO'].fields_by_name['coefficient']._loaded_options = None
 29  _globals['_MPCONSTRAINTPROTO'].fields_by_name['coefficient']._serialized_options = b'\020\001'
 30  _globals['_PARTIALVARIABLEASSIGNMENT'].fields_by_name['var_index']._loaded_options = None
 31  _globals['_PARTIALVARIABLEASSIGNMENT'].fields_by_name['var_index']._serialized_options = b'\020\001'
 32  _globals['_PARTIALVARIABLEASSIGNMENT'].fields_by_name['var_value']._loaded_options = None
 33  _globals['_PARTIALVARIABLEASSIGNMENT'].fields_by_name['var_value']._serialized_options = b'\020\001'
 34  _globals['_MPMODELDELTAPROTO_VARIABLEOVERRIDESENTRY']._loaded_options = None
 35  _globals['_MPMODELDELTAPROTO_VARIABLEOVERRIDESENTRY']._serialized_options = b'8\001'
 36  _globals['_MPMODELDELTAPROTO_CONSTRAINTOVERRIDESENTRY']._loaded_options = None
 37  _globals['_MPMODELDELTAPROTO_CONSTRAINTOVERRIDESENTRY']._serialized_options = b'8\001'
 38  _globals['_MPSOLUTION'].fields_by_name['variable_value']._loaded_options = None
 39  _globals['_MPSOLUTION'].fields_by_name['variable_value']._serialized_options = b'\020\001'
 40  _globals['_MPSOLUTIONRESPONSE'].fields_by_name['variable_value']._loaded_options = None
 41  _globals['_MPSOLUTIONRESPONSE'].fields_by_name['variable_value']._serialized_options = b'\020\001'
 42  _globals['_MPSOLUTIONRESPONSE'].fields_by_name['dual_value']._loaded_options = None
 43  _globals['_MPSOLUTIONRESPONSE'].fields_by_name['dual_value']._serialized_options = b'\020\001'
 44  _globals['_MPSOLUTIONRESPONSE'].fields_by_name['reduced_cost']._loaded_options = None
 45  _globals['_MPSOLUTIONRESPONSE'].fields_by_name['reduced_cost']._serialized_options = b'\020\001'
 46  _globals['_MPSOLVERRESPONSESTATUS']._serialized_start=5348
 47  _globals['_MPSOLVERRESPONSESTATUS']._serialized_end=5793
 48  _globals['_MPVARIABLEPROTO']._serialized_start=104
 49  _globals['_MPVARIABLEPROTO']._serialized_end=282
 50  _globals['_MPCONSTRAINTPROTO']._serialized_start=285
 51  _globals['_MPCONSTRAINTPROTO']._serialized_end=445
 52  _globals['_MPGENERALCONSTRAINTPROTO']._serialized_start=448
 53  _globals['_MPGENERALCONSTRAINTPROTO']._serialized_end=1079
 54  _globals['_MPINDICATORCONSTRAINT']._serialized_start=1081
 55  _globals['_MPINDICATORCONSTRAINT']._serialized_end=1202
 56  _globals['_MPSOSCONSTRAINT']._serialized_start=1205
 57  _globals['_MPSOSCONSTRAINT']._serialized_end=1364
 58  _globals['_MPSOSCONSTRAINT_TYPE']._serialized_start=1330
 59  _globals['_MPSOSCONSTRAINT_TYPE']._serialized_end=1364
 60  _globals['_MPQUADRATICCONSTRAINT']._serialized_start=1367
 61  _globals['_MPQUADRATICCONSTRAINT']._serialized_end=1547
 62  _globals['_MPABSCONSTRAINT']._serialized_start=1549
 63  _globals['_MPABSCONSTRAINT']._serialized_end=1614
 64  _globals['_MPARRAYCONSTRAINT']._serialized_start=1616
 65  _globals['_MPARRAYCONSTRAINT']._serialized_end=1683
 66  _globals['_MPARRAYWITHCONSTANTCONSTRAINT']._serialized_start=1685
 67  _globals['_MPARRAYWITHCONSTANTCONSTRAINT']._serialized_end=1782
 68  _globals['_MPQUADRATICOBJECTIVE']._serialized_start=1784
 69  _globals['_MPQUADRATICOBJECTIVE']._serialized_end=1869
 70  _globals['_PARTIALVARIABLEASSIGNMENT']._serialized_start=1871
 71  _globals['_PARTIALVARIABLEASSIGNMENT']._serialized_end=1944
 72  _globals['_MPMODELPROTO']._serialized_start=1947
 73  _globals['_MPMODELPROTO']._serialized_end=2687
 74  _globals['_MPMODELPROTO_ANNOTATION']._serialized_start=2434
 75  _globals['_MPMODELPROTO_ANNOTATION']._serialized_end=2687
 76  _globals['_MPMODELPROTO_ANNOTATION_TARGETTYPE']._serialized_start=2613
 77  _globals['_MPMODELPROTO_ANNOTATION_TARGETTYPE']._serialized_end=2687
 78  _globals['_OPTIONALDOUBLE']._serialized_start=2689
 79  _globals['_OPTIONALDOUBLE']._serialized_end=2720
 80  _globals['_MPSOLVERCOMMONPARAMETERS']._serialized_start=2723
 81  _globals['_MPSOLVERCOMMONPARAMETERS']._serialized_end=3296
 82  _globals['_MPSOLVERCOMMONPARAMETERS_LPALGORITHMVALUES']._serialized_start=3193
 83  _globals['_MPSOLVERCOMMONPARAMETERS_LPALGORITHMVALUES']._serialized_end=3296
 84  _globals['_MPMODELDELTAPROTO']._serialized_start=3299
 85  _globals['_MPMODELDELTAPROTO']._serialized_end=3734
 86  _globals['_MPMODELDELTAPROTO_VARIABLEOVERRIDESENTRY']._serialized_start=3540
 87  _globals['_MPMODELDELTAPROTO_VARIABLEOVERRIDESENTRY']._serialized_end=3634
 88  _globals['_MPMODELDELTAPROTO_CONSTRAINTOVERRIDESENTRY']._serialized_start=3636
 89  _globals['_MPMODELDELTAPROTO_CONSTRAINTOVERRIDESENTRY']._serialized_end=3734
 90  _globals['_MPMODELREQUEST']._serialized_start=3737
 91  _globals['_MPMODELREQUEST']._serialized_end=4788
 92  _globals['_MPMODELREQUEST_SOLVERTYPE']._serialized_start=4184
 93  _globals['_MPMODELREQUEST_SOLVERTYPE']._serialized_end=4788
 94  _globals['_MPSOLUTION']._serialized_start=4790
 95  _globals['_MPSOLUTION']._serialized_end=4855
 96  _globals['_MPSOLVEINFO']._serialized_start=4857
 97  _globals['_MPSOLVEINFO']._serialized_end=4936
 98  _globals['_MPSOLUTIONRESPONSE']._serialized_start=4939
 99  _globals['_MPSOLUTIONRESPONSE']._serialized_end=5345
100# @@protoc_insertion_point(module_scope)
DESCRIPTOR = <google.protobuf.descriptor.FileDescriptor object>
class MPVariableProto(google.protobuf.message.Message):

Abstract base class for protocol messages.

Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.

MPVariableProto(**kwargs)
498  def init(self, **kwargs):
499    self._cached_byte_size = 0
500    self._cached_byte_size_dirty = len(kwargs) > 0
501    self._fields = {}
502    # Contains a mapping from oneof field descriptors to the descriptor
503    # of the currently set field in that oneof field.
504    self._oneofs = {}
505
506    # _unknown_fields is () when empty for efficiency, and will be turned into
507    # a list if fields are added.
508    self._unknown_fields = ()
509    self._is_present_in_parent = False
510    self._listener = message_listener_mod.NullMessageListener()
511    self._listener_for_children = _Listener(self)
512    for field_name, field_value in kwargs.items():
513      field = _GetFieldByName(message_descriptor, field_name)
514      if field is None:
515        raise TypeError('%s() got an unexpected keyword argument "%s"' %
516                        (message_descriptor.name, field_name))
517      if field_value is None:
518        # field=None is the same as no field at all.
519        continue
520      if field.label == _FieldDescriptor.LABEL_REPEATED:
521        field_copy = field._default_constructor(self)
522        if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:  # Composite
523          if _IsMapField(field):
524            if _IsMessageMapField(field):
525              for key in field_value:
526                field_copy[key].MergeFrom(field_value[key])
527            else:
528              field_copy.update(field_value)
529          else:
530            for val in field_value:
531              if isinstance(val, dict):
532                field_copy.add(**val)
533              else:
534                field_copy.add().MergeFrom(val)
535        else:  # Scalar
536          if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM:
537            field_value = [_GetIntegerEnumValue(field.enum_type, val)
538                           for val in field_value]
539          field_copy.extend(field_value)
540        self._fields[field] = field_copy
541      elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
542        field_copy = field._default_constructor(self)
543        new_val = None
544        if isinstance(field_value, message_mod.Message):
545          new_val = field_value
546        elif isinstance(field_value, dict):
547          if field.message_type.full_name == _StructFullTypeName:
548            field_copy.Clear()
549            if len(field_value) == 1 and 'fields' in field_value:
550              try:
551                field_copy.update(field_value)
552              except:
553                # Fall back to init normal message field
554                field_copy.Clear()
555                new_val = field.message_type._concrete_class(**field_value)
556            else:
557              field_copy.update(field_value)
558          else:
559            new_val = field.message_type._concrete_class(**field_value)
560        elif hasattr(field_copy, '_internal_assign'):
561          field_copy._internal_assign(field_value)
562        else:
563          raise TypeError(
564              'Message field {0}.{1} must be initialized with a '
565              'dict or instance of same class, got {2}.'.format(
566                  message_descriptor.name,
567                  field_name,
568                  type(field_value).__name__,
569              )
570          )
571
572        if new_val:
573          try:
574            field_copy.MergeFrom(new_val)
575          except TypeError:
576            _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
577        self._fields[field] = field_copy
578      else:
579        if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM:
580          field_value = _GetIntegerEnumValue(field.enum_type, field_value)
581        try:
582          setattr(self, field_name, field_value)
583        except TypeError:
584          _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
DESCRIPTOR = <google.protobuf.descriptor.Descriptor object>
def MergeFrom(self, msg):
1361  def MergeFrom(self, msg):
1362    if not isinstance(msg, cls):
1363      raise TypeError(
1364          'Parameter to MergeFrom() must be instance of same class: '
1365          'expected %s got %s.' % (_FullyQualifiedClassName(cls),
1366                                   _FullyQualifiedClassName(msg.__class__)))
1367
1368    assert msg is not self
1369    self._Modified()
1370
1371    fields = self._fields
1372
1373    for field, value in msg._fields.items():
1374      if field.label == LABEL_REPEATED:
1375        field_value = fields.get(field)
1376        if field_value is None:
1377          # Construct a new object to represent this field.
1378          field_value = field._default_constructor(self)
1379          fields[field] = field_value
1380        field_value.MergeFrom(value)
1381      elif field.cpp_type == CPPTYPE_MESSAGE:
1382        if value._is_present_in_parent:
1383          field_value = fields.get(field)
1384          if field_value is None:
1385            # Construct a new object to represent this field.
1386            field_value = field._default_constructor(self)
1387            fields[field] = field_value
1388          field_value.MergeFrom(value)
1389      else:
1390        self._fields[field] = value
1391        if field.containing_oneof:
1392          self._UpdateOneofState(field)
1393
1394    if msg._unknown_fields:
1395      if not self._unknown_fields:
1396        self._unknown_fields = []
1397      self._unknown_fields.extend(msg._unknown_fields)

Merges the contents of the specified message into current message.

This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.

Arguments:
  • other_msg (Message): A message to merge into the current message.
def Clear(self):
1420def _Clear(self):
1421  # Clear fields.
1422  self._fields = {}
1423  self._unknown_fields = ()
1424
1425  self._oneofs = {}
1426  self._Modified()

Clears all data that was set in the message.

def SetInParent(self):
1486  def Modified(self):
1487    """Sets the _cached_byte_size_dirty bit to true,
1488    and propagates this to our listener iff this was a state change.
1489    """
1490
1491    # Note:  Some callers check _cached_byte_size_dirty before calling
1492    #   _Modified() as an extra optimization.  So, if this method is ever
1493    #   changed such that it does stuff even when _cached_byte_size_dirty is
1494    #   already true, the callers need to be updated.
1495    if not self._cached_byte_size_dirty:
1496      self._cached_byte_size_dirty = True
1497      self._listener_for_children.dirty = True
1498      self._is_present_in_parent = True
1499      self._listener.Modified()

Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.

def IsInitialized(self, errors=None):
1262  def IsInitialized(self, errors=None):
1263    """Checks if all required fields of a message are set.
1264
1265    Args:
1266      errors:  A list which, if provided, will be populated with the field
1267               paths of all missing required fields.
1268
1269    Returns:
1270      True iff the specified message has all required fields set.
1271    """
1272
1273    # Performance is critical so we avoid HasField() and ListFields().
1274
1275    for field in required_fields:
1276      if (field not in self._fields or
1277          (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and
1278           not self._fields[field]._is_present_in_parent)):
1279        if errors is not None:
1280          errors.extend(self.FindInitializationErrors())
1281        return False
1282
1283    for field, value in list(self._fields.items()):  # dict can change size!
1284      if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
1285        if field.label == _FieldDescriptor.LABEL_REPEATED:
1286          if (field.message_type._is_map_entry):
1287            continue
1288          for element in value:
1289            if not element.IsInitialized():
1290              if errors is not None:
1291                errors.extend(self.FindInitializationErrors())
1292              return False
1293        elif value._is_present_in_parent and not value.IsInitialized():
1294          if errors is not None:
1295            errors.extend(self.FindInitializationErrors())
1296          return False
1297
1298    return True

Checks if all required fields of a message are set.

Arguments:
  • errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:

True iff the specified message has all required fields set.

def MergeFromString(self, serialized):
1177  def MergeFromString(self, serialized):
1178    serialized = memoryview(serialized)
1179    length = len(serialized)
1180    try:
1181      if self._InternalParse(serialized, 0, length) != length:
1182        # The only reason _InternalParse would return early is if it
1183        # encountered an end-group tag.
1184        raise message_mod.DecodeError('Unexpected end-group tag.')
1185    except (IndexError, TypeError):
1186      # Now ord(buf[p:p+1]) == ord('') gets TypeError.
1187      raise message_mod.DecodeError('Truncated message.')
1188    except struct.error as e:
1189      raise message_mod.DecodeError(e)
1190    return length   # Return this for legacy reasons.

Merges serialized protocol buffer data into this message.

When we find a field in serialized that is already present in this message:

  • If it's a "repeated" field, we append to the end of our list.
  • Else, if it's a scalar, we overwrite our field.
  • Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
  • serialized (bytes): Any object that allows us to call memoryview(serialized) to access a string of bytes using the buffer interface.
Returns:

int: The number of bytes read from serialized. For non-group messages, this will always be len(serialized), but for messages which are actually groups, this will generally be less than len(serialized), since we must stop when we reach an END_GROUP tag. Note that if we do stop because of an END_GROUP tag, the number of bytes returned does not include the bytes for the END_GROUP tag information.

Raises:
  • DecodeError: if the input cannot be parsed.
def SerializeToString(self, **kwargs):
1130  def SerializeToString(self, **kwargs):
1131    # Check if the message has all of its required fields set.
1132    if not self.IsInitialized():
1133      raise message_mod.EncodeError(
1134          'Message %s is missing required fields: %s' % (
1135          self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors())))
1136    return self.SerializePartialToString(**kwargs)

Serializes the protocol message to a binary string.

Keyword Args:

deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.

Returns:

A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).

Raises:
def SerializePartialToString(self, **kwargs):
1143  def SerializePartialToString(self, **kwargs):
1144    out = BytesIO()
1145    self._InternalSerialize(out.write, **kwargs)
1146    return out.getvalue()

Serializes the protocol message to a binary string.

This method is similar to SerializeToString but doesn't check if the message is initialized.

Keyword Args:

deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.

Returns:

bytes: A serialized representation of the partial message.

def ListFields(self):
848  def ListFields(self):
849    all_fields = [item for item in self._fields.items() if _IsPresent(item)]
850    all_fields.sort(key = lambda item: item[0].number)
851    return all_fields

Returns a list of (FieldDescriptor, value) tuples for present fields.

A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.

Returns:

list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.

def HasField(self, field_name):
872  def HasField(self, field_name):
873    try:
874      field = hassable_fields[field_name]
875    except KeyError as exc:
876      raise ValueError('Protocol message %s has no non-repeated field "%s" '
877                       'nor has presence is not available for this field.' % (
878                           message_descriptor.full_name, field_name)) from exc
879
880    if isinstance(field, descriptor_mod.OneofDescriptor):
881      try:
882        return HasField(self, self._oneofs[field].name)
883      except KeyError:
884        return False
885    else:
886      if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
887        value = self._fields.get(field)
888        return value is not None and value._is_present_in_parent
889      else:
890        return field in self._fields

Checks if a certain field is set for the message.

For a oneof group, checks if any field inside is set. Note that if the field_name is not defined in the message descriptor, ValueError will be raised.

Arguments:
  • field_name (str): The name of the field to check for presence.
Returns:

bool: Whether a value has been set for the named field.

Raises:
  • ValueError: if the field_name is not a member of this message.
def ClearField(self, field_name):
897  def ClearField(self, field_name):
898    try:
899      field = message_descriptor.fields_by_name[field_name]
900    except KeyError:
901      try:
902        field = message_descriptor.oneofs_by_name[field_name]
903        if field in self._oneofs:
904          field = self._oneofs[field]
905        else:
906          return
907      except KeyError:
908        raise ValueError('Protocol message %s has no "%s" field.' %
909                         (message_descriptor.name, field_name))
910
911    if field in self._fields:
912      # To match the C++ implementation, we need to invalidate iterators
913      # for map fields when ClearField() happens.
914      if hasattr(self._fields[field], 'InvalidateIterators'):
915        self._fields[field].InvalidateIterators()
916
917      # Note:  If the field is a sub-message, its listener will still point
918      #   at us.  That's fine, because the worst than can happen is that it
919      #   will call _Modified() and invalidate our byte size.  Big deal.
920      del self._fields[field]
921
922      if self._oneofs.get(field.containing_oneof, None) is field:
923        del self._oneofs[field.containing_oneof]
924
925    # Always call _Modified() -- even if nothing was changed, this is
926    # a mutating method, and thus calling it should cause the field to become
927    # present in the parent message.
928    self._Modified()

Clears the contents of a given field.

Inside a oneof group, clears the field set. If the name neither refers to a defined field or oneof group, ValueError is raised.

Arguments:
  • field_name (str): The name of the field to check for presence.
Raises:
  • ValueError: if the field_name is not a member of this message.
def WhichOneof(self, oneof_name):
1403  def WhichOneof(self, oneof_name):
1404    """Returns the name of the currently set field inside a oneof, or None."""
1405    try:
1406      field = message_descriptor.oneofs_by_name[oneof_name]
1407    except KeyError:
1408      raise ValueError(
1409          'Protocol message has no oneof "%s" field.' % oneof_name)
1410
1411    nested_field = self._oneofs.get(field, None)
1412    if nested_field is not None and self.HasField(nested_field.name):
1413      return nested_field.name
1414    else:
1415      return None

Returns the name of the currently set field inside a oneof, or None.

def DiscardUnknownFields(self):
1435def _DiscardUnknownFields(self):
1436  self._unknown_fields = []
1437  for field, value in self.ListFields():
1438    if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
1439      if _IsMapField(field):
1440        if _IsMessageMapField(field):
1441          for key in value:
1442            value[key].DiscardUnknownFields()
1443      elif field.label == _FieldDescriptor.LABEL_REPEATED:
1444        for sub_message in value:
1445          sub_message.DiscardUnknownFields()
1446      else:
1447        value.DiscardUnknownFields()

Clears all fields in the UnknownFieldSet.

This operation is recursive for nested message.

def ByteSize(self):
1098  def ByteSize(self):
1099    if not self._cached_byte_size_dirty:
1100      return self._cached_byte_size
1101
1102    size = 0
1103    descriptor = self.DESCRIPTOR
1104    if descriptor._is_map_entry:
1105      # Fields of map entry should always be serialized.
1106      key_field = descriptor.fields_by_name['key']
1107      _MaybeAddEncoder(cls, key_field)
1108      size = key_field._sizer(self.key)
1109      value_field = descriptor.fields_by_name['value']
1110      _MaybeAddEncoder(cls, value_field)
1111      size += value_field._sizer(self.value)
1112    else:
1113      for field_descriptor, field_value in self.ListFields():
1114        _MaybeAddEncoder(cls, field_descriptor)
1115        size += field_descriptor._sizer(field_value)
1116      for tag_bytes, value_bytes in self._unknown_fields:
1117        size += len(tag_bytes) + len(value_bytes)
1118
1119    self._cached_byte_size = size
1120    self._cached_byte_size_dirty = False
1121    self._listener_for_children.dirty = False
1122    return size

Returns the serialized size of this message.

Recursively calls ByteSize() on all contained messages.

Returns:

int: The number of bytes required to serialize this message.

def FromString(s):
826  def FromString(s):
827    message = cls()
828    message.MergeFromString(s)
829    return message
LOWER_BOUND_FIELD_NUMBER = 1
lower_bound
711  def getter(self):
712    # TODO: This may be broken since there may not be
713    # default_value.  Combine with has_default_value somehow.
714    return self._fields.get(field, default_value)

Getter for lower_bound.

UPPER_BOUND_FIELD_NUMBER = 2
upper_bound
711  def getter(self):
712    # TODO: This may be broken since there may not be
713    # default_value.  Combine with has_default_value somehow.
714    return self._fields.get(field, default_value)

Getter for upper_bound.

OBJECTIVE_COEFFICIENT_FIELD_NUMBER = 3
objective_coefficient
711  def getter(self):
712    # TODO: This may be broken since there may not be
713    # default_value.  Combine with has_default_value somehow.
714    return self._fields.get(field, default_value)

Getter for objective_coefficient.

IS_INTEGER_FIELD_NUMBER = 4
is_integer
711  def getter(self):
712    # TODO: This may be broken since there may not be
713    # default_value.  Combine with has_default_value somehow.
714    return self._fields.get(field, default_value)

Getter for is_integer.

NAME_FIELD_NUMBER = 5
name
711  def getter(self):
712    # TODO: This may be broken since there may not be
713    # default_value.  Combine with has_default_value somehow.
714    return self._fields.get(field, default_value)

Getter for name.

BRANCHING_PRIORITY_FIELD_NUMBER = 6
branching_priority
711  def getter(self):
712    # TODO: This may be broken since there may not be
713    # default_value.  Combine with has_default_value somehow.
714    return self._fields.get(field, default_value)

Getter for branching_priority.

def FindInitializationErrors(self):
1302  def FindInitializationErrors(self):
1303    """Finds required fields which are not initialized.
1304
1305    Returns:
1306      A list of strings.  Each string is a path to an uninitialized field from
1307      the top-level message, e.g. "foo.bar[5].baz".
1308    """
1309
1310    errors = []  # simplify things
1311
1312    for field in required_fields:
1313      if not self.HasField(field.name):
1314        errors.append(field.name)
1315
1316    for field, value in self.ListFields():
1317      if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
1318        if field.is_extension:
1319          name = '(%s)' % field.full_name
1320        else:
1321          name = field.name
1322
1323        if _IsMapField(field):
1324          if _IsMessageMapField(field):
1325            for key in value:
1326              element = value[key]
1327              prefix = '%s[%s].' % (name, key)
1328              sub_errors = element.FindInitializationErrors()
1329              errors += [prefix + error for error in sub_errors]
1330          else:
1331            # ScalarMaps can't have any initialization errors.
1332            pass
1333        elif field.label == _FieldDescriptor.LABEL_REPEATED:
1334          for i in range(len(value)):
1335            element = value[i]
1336            prefix = '%s[%d].' % (name, i)
1337            sub_errors = element.FindInitializationErrors()
1338            errors += [prefix + error for error in sub_errors]
1339        else:
1340          prefix = name + '.'
1341          sub_errors = value.FindInitializationErrors()
1342          errors += [prefix + error for error in sub_errors]
1343
1344    return errors

Finds required fields which are not initialized.

Returns:

A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".

Inherited Members
google.protobuf.message.Message
CopyFrom
ParseFromString
HasExtension
ClearExtension
UnknownFields
class MPConstraintProto(google.protobuf.message.Message):

Abstract base class for protocol messages.

Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.

MPConstraintProto(**kwargs)
498  def init(self, **kwargs):
499    self._cached_byte_size = 0
500    self._cached_byte_size_dirty = len(kwargs) > 0
501    self._fields = {}
502    # Contains a mapping from oneof field descriptors to the descriptor
503    # of the currently set field in that oneof field.
504    self._oneofs = {}
505
506    # _unknown_fields is () when empty for efficiency, and will be turned into
507    # a list if fields are added.
508    self._unknown_fields = ()
509    self._is_present_in_parent = False
510    self._listener = message_listener_mod.NullMessageListener()
511    self._listener_for_children = _Listener(self)
512    for field_name, field_value in kwargs.items():
513      field = _GetFieldByName(message_descriptor, field_name)
514      if field is None:
515        raise TypeError('%s() got an unexpected keyword argument "%s"' %
516                        (message_descriptor.name, field_name))
517      if field_value is None:
518        # field=None is the same as no field at all.
519        continue
520      if field.label == _FieldDescriptor.LABEL_REPEATED:
521        field_copy = field._default_constructor(self)
522        if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:  # Composite
523          if _IsMapField(field):
524            if _IsMessageMapField(field):
525              for key in field_value:
526                field_copy[key].MergeFrom(field_value[key])
527            else:
528              field_copy.update(field_value)
529          else:
530            for val in field_value:
531              if isinstance(val, dict):
532                field_copy.add(**val)
533              else:
534                field_copy.add().MergeFrom(val)
535        else:  # Scalar
536          if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM:
537            field_value = [_GetIntegerEnumValue(field.enum_type, val)
538                           for val in field_value]
539          field_copy.extend(field_value)
540        self._fields[field] = field_copy
541      elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
542        field_copy = field._default_constructor(self)
543        new_val = None
544        if isinstance(field_value, message_mod.Message):
545          new_val = field_value
546        elif isinstance(field_value, dict):
547          if field.message_type.full_name == _StructFullTypeName:
548            field_copy.Clear()
549            if len(field_value) == 1 and 'fields' in field_value:
550              try:
551                field_copy.update(field_value)
552              except:
553                # Fall back to init normal message field
554                field_copy.Clear()
555                new_val = field.message_type._concrete_class(**field_value)
556            else:
557              field_copy.update(field_value)
558          else:
559            new_val = field.message_type._concrete_class(**field_value)
560        elif hasattr(field_copy, '_internal_assign'):
561          field_copy._internal_assign(field_value)
562        else:
563          raise TypeError(
564              'Message field {0}.{1} must be initialized with a '
565              'dict or instance of same class, got {2}.'.format(
566                  message_descriptor.name,
567                  field_name,
568                  type(field_value).__name__,
569              )
570          )
571
572        if new_val:
573          try:
574            field_copy.MergeFrom(new_val)
575          except TypeError:
576            _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
577        self._fields[field] = field_copy
578      else:
579        if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM:
580          field_value = _GetIntegerEnumValue(field.enum_type, field_value)
581        try:
582          setattr(self, field_name, field_value)
583        except TypeError:
584          _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
DESCRIPTOR = <google.protobuf.descriptor.Descriptor object>
def MergeFrom(self, msg):
1361  def MergeFrom(self, msg):
1362    if not isinstance(msg, cls):
1363      raise TypeError(
1364          'Parameter to MergeFrom() must be instance of same class: '
1365          'expected %s got %s.' % (_FullyQualifiedClassName(cls),
1366                                   _FullyQualifiedClassName(msg.__class__)))
1367
1368    assert msg is not self
1369    self._Modified()
1370
1371    fields = self._fields
1372
1373    for field, value in msg._fields.items():
1374      if field.label == LABEL_REPEATED:
1375        field_value = fields.get(field)
1376        if field_value is None:
1377          # Construct a new object to represent this field.
1378          field_value = field._default_constructor(self)
1379          fields[field] = field_value
1380        field_value.MergeFrom(value)
1381      elif field.cpp_type == CPPTYPE_MESSAGE:
1382        if value._is_present_in_parent:
1383          field_value = fields.get(field)
1384          if field_value is None:
1385            # Construct a new object to represent this field.
1386            field_value = field._default_constructor(self)
1387            fields[field] = field_value
1388          field_value.MergeFrom(value)
1389      else:
1390        self._fields[field] = value
1391        if field.containing_oneof:
1392          self._UpdateOneofState(field)
1393
1394    if msg._unknown_fields:
1395      if not self._unknown_fields:
1396        self._unknown_fields = []
1397      self._unknown_fields.extend(msg._unknown_fields)

Merges the contents of the specified message into current message.

This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.

Arguments:
  • other_msg (Message): A message to merge into the current message.
def Clear(self):
1420def _Clear(self):
1421  # Clear fields.
1422  self._fields = {}
1423  self._unknown_fields = ()
1424
1425  self._oneofs = {}
1426  self._Modified()

Clears all data that was set in the message.

def SetInParent(self):
1486  def Modified(self):
1487    """Sets the _cached_byte_size_dirty bit to true,
1488    and propagates this to our listener iff this was a state change.
1489    """
1490
1491    # Note:  Some callers check _cached_byte_size_dirty before calling
1492    #   _Modified() as an extra optimization.  So, if this method is ever
1493    #   changed such that it does stuff even when _cached_byte_size_dirty is
1494    #   already true, the callers need to be updated.
1495    if not self._cached_byte_size_dirty:
1496      self._cached_byte_size_dirty = True
1497      self._listener_for_children.dirty = True
1498      self._is_present_in_parent = True
1499      self._listener.Modified()

Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.

def IsInitialized(self, errors=None):
1262  def IsInitialized(self, errors=None):
1263    """Checks if all required fields of a message are set.
1264
1265    Args:
1266      errors:  A list which, if provided, will be populated with the field
1267               paths of all missing required fields.
1268
1269    Returns:
1270      True iff the specified message has all required fields set.
1271    """
1272
1273    # Performance is critical so we avoid HasField() and ListFields().
1274
1275    for field in required_fields:
1276      if (field not in self._fields or
1277          (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and
1278           not self._fields[field]._is_present_in_parent)):
1279        if errors is not None:
1280          errors.extend(self.FindInitializationErrors())
1281        return False
1282
1283    for field, value in list(self._fields.items()):  # dict can change size!
1284      if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
1285        if field.label == _FieldDescriptor.LABEL_REPEATED:
1286          if (field.message_type._is_map_entry):
1287            continue
1288          for element in value:
1289            if not element.IsInitialized():
1290              if errors is not None:
1291                errors.extend(self.FindInitializationErrors())
1292              return False
1293        elif value._is_present_in_parent and not value.IsInitialized():
1294          if errors is not None:
1295            errors.extend(self.FindInitializationErrors())
1296          return False
1297
1298    return True

Checks if all required fields of a message are set.

Arguments:
  • errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:

True iff the specified message has all required fields set.

def MergeFromString(self, serialized):
1177  def MergeFromString(self, serialized):
1178    serialized = memoryview(serialized)
1179    length = len(serialized)
1180    try:
1181      if self._InternalParse(serialized, 0, length) != length:
1182        # The only reason _InternalParse would return early is if it
1183        # encountered an end-group tag.
1184        raise message_mod.DecodeError('Unexpected end-group tag.')
1185    except (IndexError, TypeError):
1186      # Now ord(buf[p:p+1]) == ord('') gets TypeError.
1187      raise message_mod.DecodeError('Truncated message.')
1188    except struct.error as e:
1189      raise message_mod.DecodeError(e)
1190    return length   # Return this for legacy reasons.

Merges serialized protocol buffer data into this message.

When we find a field in serialized that is already present in this message:

  • If it's a "repeated" field, we append to the end of our list.
  • Else, if it's a scalar, we overwrite our field.
  • Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
  • serialized (bytes): Any object that allows us to call memoryview(serialized) to access a string of bytes using the buffer interface.
Returns:

int: The number of bytes read from serialized. For non-group messages, this will always be len(serialized), but for messages which are actually groups, this will generally be less than len(serialized), since we must stop when we reach an END_GROUP tag. Note that if we do stop because of an END_GROUP tag, the number of bytes returned does not include the bytes for the END_GROUP tag information.

Raises:
  • DecodeError: if the input cannot be parsed.
def SerializeToString(self, **kwargs):
1130  def SerializeToString(self, **kwargs):
1131    # Check if the message has all of its required fields set.
1132    if not self.IsInitialized():
1133      raise message_mod.EncodeError(
1134          'Message %s is missing required fields: %s' % (
1135          self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors())))
1136    return self.SerializePartialToString(**kwargs)

Serializes the protocol message to a binary string.

Keyword Args:

deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.

Returns:

A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).

Raises:
def SerializePartialToString(self, **kwargs):
1143  def SerializePartialToString(self, **kwargs):
1144    out = BytesIO()
1145    self._InternalSerialize(out.write, **kwargs)
1146    return out.getvalue()

Serializes the protocol message to a binary string.

This method is similar to SerializeToString but doesn't check if the message is initialized.

Keyword Args:

deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.

Returns:

bytes: A serialized representation of the partial message.

def ListFields(self):
848  def ListFields(self):
849    all_fields = [item for item in self._fields.items() if _IsPresent(item)]
850    all_fields.sort(key = lambda item: item[0].number)
851    return all_fields

Returns a list of (FieldDescriptor, value) tuples for present fields.

A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.

Returns:

list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.

def HasField(self, field_name):
872  def HasField(self, field_name):
873    try:
874      field = hassable_fields[field_name]
875    except KeyError as exc:
876      raise ValueError('Protocol message %s has no non-repeated field "%s" '
877                       'nor has presence is not available for this field.' % (
878                           message_descriptor.full_name, field_name)) from exc
879
880    if isinstance(field, descriptor_mod.OneofDescriptor):
881      try:
882        return HasField(self, self._oneofs[field].name)
883      except KeyError:
884        return False
885    else:
886      if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
887        value = self._fields.get(field)
888        return value is not None and value._is_present_in_parent
889      else:
890        return field in self._fields

Checks if a certain field is set for the message.

For a oneof group, checks if any field inside is set. Note that if the field_name is not defined in the message descriptor, ValueError will be raised.

Arguments:
  • field_name (str): The name of the field to check for presence.
Returns:

bool: Whether a value has been set for the named field.

Raises:
  • ValueError: if the field_name is not a member of this message.
def ClearField(self, field_name):
897  def ClearField(self, field_name):
898    try:
899      field = message_descriptor.fields_by_name[field_name]
900    except KeyError:
901      try:
902        field = message_descriptor.oneofs_by_name[field_name]
903        if field in self._oneofs:
904          field = self._oneofs[field]
905        else:
906          return
907      except KeyError:
908        raise ValueError('Protocol message %s has no "%s" field.' %
909                         (message_descriptor.name, field_name))
910
911    if field in self._fields:
912      # To match the C++ implementation, we need to invalidate iterators
913      # for map fields when ClearField() happens.
914      if hasattr(self._fields[field], 'InvalidateIterators'):
915        self._fields[field].InvalidateIterators()
916
917      # Note:  If the field is a sub-message, its listener will still point
918      #   at us.  That's fine, because the worst than can happen is that it
919      #   will call _Modified() and invalidate our byte size.  Big deal.
920      del self._fields[field]
921
922      if self._oneofs.get(field.containing_oneof, None) is field:
923        del self._oneofs[field.containing_oneof]
924
925    # Always call _Modified() -- even if nothing was changed, this is
926    # a mutating method, and thus calling it should cause the field to become
927    # present in the parent message.
928    self._Modified()

Clears the contents of a given field.

Inside a oneof group, clears the field set. If the name neither refers to a defined field or oneof group, ValueError is raised.

Arguments:
  • field_name (str): The name of the field to check for presence.
Raises:
  • ValueError: if the field_name is not a member of this message.
def WhichOneof(self, oneof_name):
1403  def WhichOneof(self, oneof_name):
1404    """Returns the name of the currently set field inside a oneof, or None."""
1405    try:
1406      field = message_descriptor.oneofs_by_name[oneof_name]
1407    except KeyError:
1408      raise ValueError(
1409          'Protocol message has no oneof "%s" field.' % oneof_name)
1410
1411    nested_field = self._oneofs.get(field, None)
1412    if nested_field is not None and self.HasField(nested_field.name):
1413      return nested_field.name
1414    else:
1415      return None

Returns the name of the currently set field inside a oneof, or None.

def DiscardUnknownFields(self):
1435def _DiscardUnknownFields(self):
1436  self._unknown_fields = []
1437  for field, value in self.ListFields():
1438    if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
1439      if _IsMapField(field):
1440        if _IsMessageMapField(field):
1441          for key in value:
1442            value[key].DiscardUnknownFields()
1443      elif field.label == _FieldDescriptor.LABEL_REPEATED:
1444        for sub_message in value:
1445          sub_message.DiscardUnknownFields()
1446      else:
1447        value.DiscardUnknownFields()

Clears all fields in the UnknownFieldSet.

This operation is recursive for nested message.

def ByteSize(self):
1098  def ByteSize(self):
1099    if not self._cached_byte_size_dirty:
1100      return self._cached_byte_size
1101
1102    size = 0
1103    descriptor = self.DESCRIPTOR
1104    if descriptor._is_map_entry:
1105      # Fields of map entry should always be serialized.
1106      key_field = descriptor.fields_by_name['key']
1107      _MaybeAddEncoder(cls, key_field)
1108      size = key_field._sizer(self.key)
1109      value_field = descriptor.fields_by_name['value']
1110      _MaybeAddEncoder(cls, value_field)
1111      size += value_field._sizer(self.value)
1112    else:
1113      for field_descriptor, field_value in self.ListFields():
1114        _MaybeAddEncoder(cls, field_descriptor)
1115        size += field_descriptor._sizer(field_value)
1116      for tag_bytes, value_bytes in self._unknown_fields:
1117        size += len(tag_bytes) + len(value_bytes)
1118
1119    self._cached_byte_size = size
1120    self._cached_byte_size_dirty = False
1121    self._listener_for_children.dirty = False
1122    return size

Returns the serialized size of this message.

Recursively calls ByteSize() on all contained messages.

Returns:

int: The number of bytes required to serialize this message.

def FromString(s):
826  def FromString(s):
827    message = cls()
828    message.MergeFromString(s)
829    return message
VAR_INDEX_FIELD_NUMBER = 6
var_index
668  def getter(self):
669    field_value = self._fields.get(field)
670    if field_value is None:
671      # Construct a new object to represent this field.
672      field_value = field._default_constructor(self)
673
674      # Atomically check if another thread has preempted us and, if not, swap
675      # in the new object we just created.  If someone has preempted us, we
676      # take that object and discard ours.
677      # WARNING:  We are relying on setdefault() being atomic.  This is true
678      #   in CPython but we haven't investigated others.  This warning appears
679      #   in several other locations in this file.
680      field_value = self._fields.setdefault(field, field_value)
681    return field_value

Getter for var_index.

COEFFICIENT_FIELD_NUMBER = 7
coefficient
668  def getter(self):
669    field_value = self._fields.get(field)
670    if field_value is None:
671      # Construct a new object to represent this field.
672      field_value = field._default_constructor(self)
673
674      # Atomically check if another thread has preempted us and, if not, swap
675      # in the new object we just created.  If someone has preempted us, we
676      # take that object and discard ours.
677      # WARNING:  We are relying on setdefault() being atomic.  This is true
678      #   in CPython but we haven't investigated others.  This warning appears
679      #   in several other locations in this file.
680      field_value = self._fields.setdefault(field, field_value)
681    return field_value

Getter for coefficient.

LOWER_BOUND_FIELD_NUMBER = 2
lower_bound
711  def getter(self):
712    # TODO: This may be broken since there may not be
713    # default_value.  Combine with has_default_value somehow.
714    return self._fields.get(field, default_value)

Getter for lower_bound.

UPPER_BOUND_FIELD_NUMBER = 3
upper_bound
711  def getter(self):
712    # TODO: This may be broken since there may not be
713    # default_value.  Combine with has_default_value somehow.
714    return self._fields.get(field, default_value)

Getter for upper_bound.

NAME_FIELD_NUMBER = 4
name
711  def getter(self):
712    # TODO: This may be broken since there may not be
713    # default_value.  Combine with has_default_value somehow.
714    return self._fields.get(field, default_value)

Getter for name.

IS_LAZY_FIELD_NUMBER = 5
is_lazy
711  def getter(self):
712    # TODO: This may be broken since there may not be
713    # default_value.  Combine with has_default_value somehow.
714    return self._fields.get(field, default_value)

Getter for is_lazy.

def FindInitializationErrors(self):
1302  def FindInitializationErrors(self):
1303    """Finds required fields which are not initialized.
1304
1305    Returns:
1306      A list of strings.  Each string is a path to an uninitialized field from
1307      the top-level message, e.g. "foo.bar[5].baz".
1308    """
1309
1310    errors = []  # simplify things
1311
1312    for field in required_fields:
1313      if not self.HasField(field.name):
1314        errors.append(field.name)
1315
1316    for field, value in self.ListFields():
1317      if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
1318        if field.is_extension:
1319          name = '(%s)' % field.full_name
1320        else:
1321          name = field.name
1322
1323        if _IsMapField(field):
1324          if _IsMessageMapField(field):
1325            for key in value:
1326              element = value[key]
1327              prefix = '%s[%s].' % (name, key)
1328              sub_errors = element.FindInitializationErrors()
1329              errors += [prefix + error for error in sub_errors]
1330          else:
1331            # ScalarMaps can't have any initialization errors.
1332            pass
1333        elif field.label == _FieldDescriptor.LABEL_REPEATED:
1334          for i in range(len(value)):
1335            element = value[i]
1336            prefix = '%s[%d].' % (name, i)
1337            sub_errors = element.FindInitializationErrors()
1338            errors += [prefix + error for error in sub_errors]
1339        else:
1340          prefix = name + '.'
1341          sub_errors = value.FindInitializationErrors()
1342          errors += [prefix + error for error in sub_errors]
1343
1344    return errors

Finds required fields which are not initialized.

Returns:

A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".

Inherited Members
google.protobuf.message.Message
CopyFrom
ParseFromString
HasExtension
ClearExtension
UnknownFields
class MPGeneralConstraintProto(google.protobuf.message.Message):

Abstract base class for protocol messages.

Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.

MPGeneralConstraintProto(**kwargs)
498  def init(self, **kwargs):
499    self._cached_byte_size = 0
500    self._cached_byte_size_dirty = len(kwargs) > 0
501    self._fields = {}
502    # Contains a mapping from oneof field descriptors to the descriptor
503    # of the currently set field in that oneof field.
504    self._oneofs = {}
505
506    # _unknown_fields is () when empty for efficiency, and will be turned into
507    # a list if fields are added.
508    self._unknown_fields = ()
509    self._is_present_in_parent = False
510    self._listener = message_listener_mod.NullMessageListener()
511    self._listener_for_children = _Listener(self)
512    for field_name, field_value in kwargs.items():
513      field = _GetFieldByName(message_descriptor, field_name)
514      if field is None:
515        raise TypeError('%s() got an unexpected keyword argument "%s"' %
516                        (message_descriptor.name, field_name))
517      if field_value is None:
518        # field=None is the same as no field at all.
519        continue
520      if field.label == _FieldDescriptor.LABEL_REPEATED:
521        field_copy = field._default_constructor(self)
522        if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:  # Composite
523          if _IsMapField(field):
524            if _IsMessageMapField(field):
525              for key in field_value:
526                field_copy[key].MergeFrom(field_value[key])
527            else:
528              field_copy.update(field_value)
529          else:
530            for val in field_value:
531              if isinstance(val, dict):
532                field_copy.add(**val)
533              else:
534                field_copy.add().MergeFrom(val)
535        else:  # Scalar
536          if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM:
537            field_value = [_GetIntegerEnumValue(field.enum_type, val)
538                           for val in field_value]
539          field_copy.extend(field_value)
540        self._fields[field] = field_copy
541      elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
542        field_copy = field._default_constructor(self)
543        new_val = None
544        if isinstance(field_value, message_mod.Message):
545          new_val = field_value
546        elif isinstance(field_value, dict):
547          if field.message_type.full_name == _StructFullTypeName:
548            field_copy.Clear()
549            if len(field_value) == 1 and 'fields' in field_value:
550              try:
551                field_copy.update(field_value)
552              except:
553                # Fall back to init normal message field
554                field_copy.Clear()
555                new_val = field.message_type._concrete_class(**field_value)
556            else:
557              field_copy.update(field_value)
558          else:
559            new_val = field.message_type._concrete_class(**field_value)
560        elif hasattr(field_copy, '_internal_assign'):
561          field_copy._internal_assign(field_value)
562        else:
563          raise TypeError(
564              'Message field {0}.{1} must be initialized with a '
565              'dict or instance of same class, got {2}.'.format(
566                  message_descriptor.name,
567                  field_name,
568                  type(field_value).__name__,
569              )
570          )
571
572        if new_val:
573          try:
574            field_copy.MergeFrom(new_val)
575          except TypeError:
576            _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
577        self._fields[field] = field_copy
578      else:
579        if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM:
580          field_value = _GetIntegerEnumValue(field.enum_type, field_value)
581        try:
582          setattr(self, field_name, field_value)
583        except TypeError:
584          _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
DESCRIPTOR = <google.protobuf.descriptor.Descriptor object>
def MergeFrom(self, msg):
1361  def MergeFrom(self, msg):
1362    if not isinstance(msg, cls):
1363      raise TypeError(
1364          'Parameter to MergeFrom() must be instance of same class: '
1365          'expected %s got %s.' % (_FullyQualifiedClassName(cls),
1366                                   _FullyQualifiedClassName(msg.__class__)))
1367
1368    assert msg is not self
1369    self._Modified()
1370
1371    fields = self._fields
1372
1373    for field, value in msg._fields.items():
1374      if field.label == LABEL_REPEATED:
1375        field_value = fields.get(field)
1376        if field_value is None:
1377          # Construct a new object to represent this field.
1378          field_value = field._default_constructor(self)
1379          fields[field] = field_value
1380        field_value.MergeFrom(value)
1381      elif field.cpp_type == CPPTYPE_MESSAGE:
1382        if value._is_present_in_parent:
1383          field_value = fields.get(field)
1384          if field_value is None:
1385            # Construct a new object to represent this field.
1386            field_value = field._default_constructor(self)
1387            fields[field] = field_value
1388          field_value.MergeFrom(value)
1389      else:
1390        self._fields[field] = value
1391        if field.containing_oneof:
1392          self._UpdateOneofState(field)
1393
1394    if msg._unknown_fields:
1395      if not self._unknown_fields:
1396        self._unknown_fields = []
1397      self._unknown_fields.extend(msg._unknown_fields)

Merges the contents of the specified message into current message.

This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.

Arguments:
  • other_msg (Message): A message to merge into the current message.
def Clear(self):
1420def _Clear(self):
1421  # Clear fields.
1422  self._fields = {}
1423  self._unknown_fields = ()
1424
1425  self._oneofs = {}
1426  self._Modified()

Clears all data that was set in the message.

def SetInParent(self):
1486  def Modified(self):
1487    """Sets the _cached_byte_size_dirty bit to true,
1488    and propagates this to our listener iff this was a state change.
1489    """
1490
1491    # Note:  Some callers check _cached_byte_size_dirty before calling
1492    #   _Modified() as an extra optimization.  So, if this method is ever
1493    #   changed such that it does stuff even when _cached_byte_size_dirty is
1494    #   already true, the callers need to be updated.
1495    if not self._cached_byte_size_dirty:
1496      self._cached_byte_size_dirty = True
1497      self._listener_for_children.dirty = True
1498      self._is_present_in_parent = True
1499      self._listener.Modified()

Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.

def IsInitialized(self, errors=None):
1262  def IsInitialized(self, errors=None):
1263    """Checks if all required fields of a message are set.
1264
1265    Args:
1266      errors:  A list which, if provided, will be populated with the field
1267               paths of all missing required fields.
1268
1269    Returns:
1270      True iff the specified message has all required fields set.
1271    """
1272
1273    # Performance is critical so we avoid HasField() and ListFields().
1274
1275    for field in required_fields:
1276      if (field not in self._fields or
1277          (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and
1278           not self._fields[field]._is_present_in_parent)):
1279        if errors is not None:
1280          errors.extend(self.FindInitializationErrors())
1281        return False
1282
1283    for field, value in list(self._fields.items()):  # dict can change size!
1284      if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
1285        if field.label == _FieldDescriptor.LABEL_REPEATED:
1286          if (field.message_type._is_map_entry):
1287            continue
1288          for element in value:
1289            if not element.IsInitialized():
1290              if errors is not None:
1291                errors.extend(self.FindInitializationErrors())
1292              return False
1293        elif value._is_present_in_parent and not value.IsInitialized():
1294          if errors is not None:
1295            errors.extend(self.FindInitializationErrors())
1296          return False
1297
1298    return True

Checks if all required fields of a message are set.

Arguments:
  • errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:

True iff the specified message has all required fields set.

def MergeFromString(self, serialized):
1177  def MergeFromString(self, serialized):
1178    serialized = memoryview(serialized)
1179    length = len(serialized)
1180    try:
1181      if self._InternalParse(serialized, 0, length) != length:
1182        # The only reason _InternalParse would return early is if it
1183        # encountered an end-group tag.
1184        raise message_mod.DecodeError('Unexpected end-group tag.')
1185    except (IndexError, TypeError):
1186      # Now ord(buf[p:p+1]) == ord('') gets TypeError.
1187      raise message_mod.DecodeError('Truncated message.')
1188    except struct.error as e:
1189      raise message_mod.DecodeError(e)
1190    return length   # Return this for legacy reasons.

Merges serialized protocol buffer data into this message.

When we find a field in serialized that is already present in this message:

  • If it's a "repeated" field, we append to the end of our list.
  • Else, if it's a scalar, we overwrite our field.
  • Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
  • serialized (bytes): Any object that allows us to call memoryview(serialized) to access a string of bytes using the buffer interface.
Returns:

int: The number of bytes read from serialized. For non-group messages, this will always be len(serialized), but for messages which are actually groups, this will generally be less than len(serialized), since we must stop when we reach an END_GROUP tag. Note that if we do stop because of an END_GROUP tag, the number of bytes returned does not include the bytes for the END_GROUP tag information.

Raises:
  • DecodeError: if the input cannot be parsed.
def SerializeToString(self, **kwargs):
1130  def SerializeToString(self, **kwargs):
1131    # Check if the message has all of its required fields set.
1132    if not self.IsInitialized():
1133      raise message_mod.EncodeError(
1134          'Message %s is missing required fields: %s' % (
1135          self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors())))
1136    return self.SerializePartialToString(**kwargs)

Serializes the protocol message to a binary string.

Keyword Args:

deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.

Returns:

A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).

Raises:
def SerializePartialToString(self, **kwargs):
1143  def SerializePartialToString(self, **kwargs):
1144    out = BytesIO()
1145    self._InternalSerialize(out.write, **kwargs)
1146    return out.getvalue()

Serializes the protocol message to a binary string.

This method is similar to SerializeToString but doesn't check if the message is initialized.

Keyword Args:

deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.

Returns:

bytes: A serialized representation of the partial message.

def ListFields(self):
848  def ListFields(self):
849    all_fields = [item for item in self._fields.items() if _IsPresent(item)]
850    all_fields.sort(key = lambda item: item[0].number)
851    return all_fields

Returns a list of (FieldDescriptor, value) tuples for present fields.

A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.

Returns:

list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.

def HasField(self, field_name):
872  def HasField(self, field_name):
873    try:
874      field = hassable_fields[field_name]
875    except KeyError as exc:
876      raise ValueError('Protocol message %s has no non-repeated field "%s" '
877                       'nor has presence is not available for this field.' % (
878                           message_descriptor.full_name, field_name)) from exc
879
880    if isinstance(field, descriptor_mod.OneofDescriptor):
881      try:
882        return HasField(self, self._oneofs[field].name)
883      except KeyError:
884        return False
885    else:
886      if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
887        value = self._fields.get(field)
888        return value is not None and value._is_present_in_parent
889      else:
890        return field in self._fields

Checks if a certain field is set for the message.

For a oneof group, checks if any field inside is set. Note that if the field_name is not defined in the message descriptor, ValueError will be raised.

Arguments:
  • field_name (str): The name of the field to check for presence.
Returns:

bool: Whether a value has been set for the named field.

Raises:
  • ValueError: if the field_name is not a member of this message.
def ClearField(self, field_name):
897  def ClearField(self, field_name):
898    try:
899      field = message_descriptor.fields_by_name[field_name]
900    except KeyError:
901      try:
902        field = message_descriptor.oneofs_by_name[field_name]
903        if field in self._oneofs:
904          field = self._oneofs[field]
905        else:
906          return
907      except KeyError:
908        raise ValueError('Protocol message %s has no "%s" field.' %
909                         (message_descriptor.name, field_name))
910
911    if field in self._fields:
912      # To match the C++ implementation, we need to invalidate iterators
913      # for map fields when ClearField() happens.
914      if hasattr(self._fields[field], 'InvalidateIterators'):
915        self._fields[field].InvalidateIterators()
916
917      # Note:  If the field is a sub-message, its listener will still point
918      #   at us.  That's fine, because the worst than can happen is that it
919      #   will call _Modified() and invalidate our byte size.  Big deal.
920      del self._fields[field]
921
922      if self._oneofs.get(field.containing_oneof, None) is field:
923        del self._oneofs[field.containing_oneof]
924
925    # Always call _Modified() -- even if nothing was changed, this is
926    # a mutating method, and thus calling it should cause the field to become
927    # present in the parent message.
928    self._Modified()

Clears the contents of a given field.

Inside a oneof group, clears the field set. If the name neither refers to a defined field or oneof group, ValueError is raised.

Arguments:
  • field_name (str): The name of the field to check for presence.
Raises:
  • ValueError: if the field_name is not a member of this message.
def WhichOneof(self, oneof_name):
1403  def WhichOneof(self, oneof_name):
1404    """Returns the name of the currently set field inside a oneof, or None."""
1405    try:
1406      field = message_descriptor.oneofs_by_name[oneof_name]
1407    except KeyError:
1408      raise ValueError(
1409          'Protocol message has no oneof "%s" field.' % oneof_name)
1410
1411    nested_field = self._oneofs.get(field, None)
1412    if nested_field is not None and self.HasField(nested_field.name):
1413      return nested_field.name
1414    else:
1415      return None

Returns the name of the currently set field inside a oneof, or None.

def DiscardUnknownFields(self):
1435def _DiscardUnknownFields(self):
1436  self._unknown_fields = []
1437  for field, value in self.ListFields():
1438    if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
1439      if _IsMapField(field):
1440        if _IsMessageMapField(field):
1441          for key in value:
1442            value[key].DiscardUnknownFields()
1443      elif field.label == _FieldDescriptor.LABEL_REPEATED:
1444        for sub_message in value:
1445          sub_message.DiscardUnknownFields()
1446      else:
1447        value.DiscardUnknownFields()

Clears all fields in the UnknownFieldSet.

This operation is recursive for nested message.

def ByteSize(self):
1098  def ByteSize(self):
1099    if not self._cached_byte_size_dirty:
1100      return self._cached_byte_size
1101
1102    size = 0
1103    descriptor = self.DESCRIPTOR
1104    if descriptor._is_map_entry:
1105      # Fields of map entry should always be serialized.
1106      key_field = descriptor.fields_by_name['key']
1107      _MaybeAddEncoder(cls, key_field)
1108      size = key_field._sizer(self.key)
1109      value_field = descriptor.fields_by_name['value']
1110      _MaybeAddEncoder(cls, value_field)
1111      size += value_field._sizer(self.value)
1112    else:
1113      for field_descriptor, field_value in self.ListFields():
1114        _MaybeAddEncoder(cls, field_descriptor)
1115        size += field_descriptor._sizer(field_value)
1116      for tag_bytes, value_bytes in self._unknown_fields:
1117        size += len(tag_bytes) + len(value_bytes)
1118
1119    self._cached_byte_size = size
1120    self._cached_byte_size_dirty = False
1121    self._listener_for_children.dirty = False
1122    return size

Returns the serialized size of this message.

Recursively calls ByteSize() on all contained messages.

Returns:

int: The number of bytes required to serialize this message.

def FromString(s):
826  def FromString(s):
827    message = cls()
828    message.MergeFromString(s)
829    return message
NAME_FIELD_NUMBER = 1
name
711  def getter(self):
712    # TODO: This may be broken since there may not be
713    # default_value.  Combine with has_default_value somehow.
714    return self._fields.get(field, default_value)

Getter for name.

INDICATOR_CONSTRAINT_FIELD_NUMBER = 2
indicator_constraint
767  def getter(self):
768    field_value = self._fields.get(field)
769    if field_value is None:
770      # Construct a new object to represent this field.
771      field_value = field._default_constructor(self)
772
773      # Atomically check if another thread has preempted us and, if not, swap
774      # in the new object we just created.  If someone has preempted us, we
775      # take that object and discard ours.
776      # WARNING:  We are relying on setdefault() being atomic.  This is true
777      #   in CPython but we haven't investigated others.  This warning appears
778      #   in several other locations in this file.
779      field_value = self._fields.setdefault(field, field_value)
780    return field_value

Getter for indicator_constraint.

SOS_CONSTRAINT_FIELD_NUMBER = 3
sos_constraint
767  def getter(self):
768    field_value = self._fields.get(field)
769    if field_value is None:
770      # Construct a new object to represent this field.
771      field_value = field._default_constructor(self)
772
773      # Atomically check if another thread has preempted us and, if not, swap
774      # in the new object we just created.  If someone has preempted us, we
775      # take that object and discard ours.
776      # WARNING:  We are relying on setdefault() being atomic.  This is true
777      #   in CPython but we haven't investigated others.  This warning appears
778      #   in several other locations in this file.
779      field_value = self._fields.setdefault(field, field_value)
780    return field_value

Getter for sos_constraint.

QUADRATIC_CONSTRAINT_FIELD_NUMBER = 4
quadratic_constraint
767  def getter(self):
768    field_value = self._fields.get(field)
769    if field_value is None:
770      # Construct a new object to represent this field.
771      field_value = field._default_constructor(self)
772
773      # Atomically check if another thread has preempted us and, if not, swap
774      # in the new object we just created.  If someone has preempted us, we
775      # take that object and discard ours.
776      # WARNING:  We are relying on setdefault() being atomic.  This is true
777      #   in CPython but we haven't investigated others.  This warning appears
778      #   in several other locations in this file.
779      field_value = self._fields.setdefault(field, field_value)
780    return field_value

Getter for quadratic_constraint.

ABS_CONSTRAINT_FIELD_NUMBER = 5
abs_constraint
767  def getter(self):
768    field_value = self._fields.get(field)
769    if field_value is None:
770      # Construct a new object to represent this field.
771      field_value = field._default_constructor(self)
772
773      # Atomically check if another thread has preempted us and, if not, swap
774      # in the new object we just created.  If someone has preempted us, we
775      # take that object and discard ours.
776      # WARNING:  We are relying on setdefault() being atomic.  This is true
777      #   in CPython but we haven't investigated others.  This warning appears
778      #   in several other locations in this file.
779      field_value = self._fields.setdefault(field, field_value)
780    return field_value

Getter for abs_constraint.

AND_CONSTRAINT_FIELD_NUMBER = 6
and_constraint
767  def getter(self):
768    field_value = self._fields.get(field)
769    if field_value is None:
770      # Construct a new object to represent this field.
771      field_value = field._default_constructor(self)
772
773      # Atomically check if another thread has preempted us and, if not, swap
774      # in the new object we just created.  If someone has preempted us, we
775      # take that object and discard ours.
776      # WARNING:  We are relying on setdefault() being atomic.  This is true
777      #   in CPython but we haven't investigated others.  This warning appears
778      #   in several other locations in this file.
779      field_value = self._fields.setdefault(field, field_value)
780    return field_value

Getter for and_constraint.

OR_CONSTRAINT_FIELD_NUMBER = 7
or_constraint
767  def getter(self):
768    field_value = self._fields.get(field)
769    if field_value is None:
770      # Construct a new object to represent this field.
771      field_value = field._default_constructor(self)
772
773      # Atomically check if another thread has preempted us and, if not, swap
774      # in the new object we just created.  If someone has preempted us, we
775      # take that object and discard ours.
776      # WARNING:  We are relying on setdefault() being atomic.  This is true
777      #   in CPython but we haven't investigated others.  This warning appears
778      #   in several other locations in this file.
779      field_value = self._fields.setdefault(field, field_value)
780    return field_value

Getter for or_constraint.

MIN_CONSTRAINT_FIELD_NUMBER = 8
min_constraint
767  def getter(self):
768    field_value = self._fields.get(field)
769    if field_value is None:
770      # Construct a new object to represent this field.
771      field_value = field._default_constructor(self)
772
773      # Atomically check if another thread has preempted us and, if not, swap
774      # in the new object we just created.  If someone has preempted us, we
775      # take that object and discard ours.
776      # WARNING:  We are relying on setdefault() being atomic.  This is true
777      #   in CPython but we haven't investigated others.  This warning appears
778      #   in several other locations in this file.
779      field_value = self._fields.setdefault(field, field_value)
780    return field_value

Getter for min_constraint.

MAX_CONSTRAINT_FIELD_NUMBER = 9
max_constraint
767  def getter(self):
768    field_value = self._fields.get(field)
769    if field_value is None:
770      # Construct a new object to represent this field.
771      field_value = field._default_constructor(self)
772
773      # Atomically check if another thread has preempted us and, if not, swap
774      # in the new object we just created.  If someone has preempted us, we
775      # take that object and discard ours.
776      # WARNING:  We are relying on setdefault() being atomic.  This is true
777      #   in CPython but we haven't investigated others.  This warning appears
778      #   in several other locations in this file.
779      field_value = self._fields.setdefault(field, field_value)
780    return field_value

Getter for max_constraint.

def FindInitializationErrors(self):
1302  def FindInitializationErrors(self):
1303    """Finds required fields which are not initialized.
1304
1305    Returns:
1306      A list of strings.  Each string is a path to an uninitialized field from
1307      the top-level message, e.g. "foo.bar[5].baz".
1308    """
1309
1310    errors = []  # simplify things
1311
1312    for field in required_fields:
1313      if not self.HasField(field.name):
1314        errors.append(field.name)
1315
1316    for field, value in self.ListFields():
1317      if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
1318        if field.is_extension:
1319          name = '(%s)' % field.full_name
1320        else:
1321          name = field.name
1322
1323        if _IsMapField(field):
1324          if _IsMessageMapField(field):
1325            for key in value:
1326              element = value[key]
1327              prefix = '%s[%s].' % (name, key)
1328              sub_errors = element.FindInitializationErrors()
1329              errors += [prefix + error for error in sub_errors]
1330          else:
1331            # ScalarMaps can't have any initialization errors.
1332            pass
1333        elif field.label == _FieldDescriptor.LABEL_REPEATED:
1334          for i in range(len(value)):
1335            element = value[i]
1336            prefix = '%s[%d].' % (name, i)
1337            sub_errors = element.FindInitializationErrors()
1338            errors += [prefix + error for error in sub_errors]
1339        else:
1340          prefix = name + '.'
1341          sub_errors = value.FindInitializationErrors()
1342          errors += [prefix + error for error in sub_errors]
1343
1344    return errors

Finds required fields which are not initialized.

Returns:

A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".

Inherited Members
google.protobuf.message.Message
CopyFrom
ParseFromString
HasExtension
ClearExtension
UnknownFields
class MPIndicatorConstraint(google.protobuf.message.Message):

Abstract base class for protocol messages.

Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.

MPIndicatorConstraint(**kwargs)
498  def init(self, **kwargs):
499    self._cached_byte_size = 0
500    self._cached_byte_size_dirty = len(kwargs) > 0
501    self._fields = {}
502    # Contains a mapping from oneof field descriptors to the descriptor
503    # of the currently set field in that oneof field.
504    self._oneofs = {}
505
506    # _unknown_fields is () when empty for efficiency, and will be turned into
507    # a list if fields are added.
508    self._unknown_fields = ()
509    self._is_present_in_parent = False
510    self._listener = message_listener_mod.NullMessageListener()
511    self._listener_for_children = _Listener(self)
512    for field_name, field_value in kwargs.items():
513      field = _GetFieldByName(message_descriptor, field_name)
514      if field is None:
515        raise TypeError('%s() got an unexpected keyword argument "%s"' %
516                        (message_descriptor.name, field_name))
517      if field_value is None:
518        # field=None is the same as no field at all.
519        continue
520      if field.label == _FieldDescriptor.LABEL_REPEATED:
521        field_copy = field._default_constructor(self)
522        if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:  # Composite
523          if _IsMapField(field):
524            if _IsMessageMapField(field):
525              for key in field_value:
526                field_copy[key].MergeFrom(field_value[key])
527            else:
528              field_copy.update(field_value)
529          else:
530            for val in field_value:
531              if isinstance(val, dict):
532                field_copy.add(**val)
533              else:
534                field_copy.add().MergeFrom(val)
535        else:  # Scalar
536          if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM:
537            field_value = [_GetIntegerEnumValue(field.enum_type, val)
538                           for val in field_value]
539          field_copy.extend(field_value)
540        self._fields[field] = field_copy
541      elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
542        field_copy = field._default_constructor(self)
543        new_val = None
544        if isinstance(field_value, message_mod.Message):
545          new_val = field_value
546        elif isinstance(field_value, dict):
547          if field.message_type.full_name == _StructFullTypeName:
548            field_copy.Clear()
549            if len(field_value) == 1 and 'fields' in field_value:
550              try:
551                field_copy.update(field_value)
552              except:
553                # Fall back to init normal message field
554                field_copy.Clear()
555                new_val = field.message_type._concrete_class(**field_value)
556            else:
557              field_copy.update(field_value)
558          else:
559            new_val = field.message_type._concrete_class(**field_value)
560        elif hasattr(field_copy, '_internal_assign'):
561          field_copy._internal_assign(field_value)
562        else:
563          raise TypeError(
564              'Message field {0}.{1} must be initialized with a '
565              'dict or instance of same class, got {2}.'.format(
566                  message_descriptor.name,
567                  field_name,
568                  type(field_value).__name__,
569              )
570          )
571
572        if new_val:
573          try:
574            field_copy.MergeFrom(new_val)
575          except TypeError:
576            _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
577        self._fields[field] = field_copy
578      else:
579        if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM:
580          field_value = _GetIntegerEnumValue(field.enum_type, field_value)
581        try:
582          setattr(self, field_name, field_value)
583        except TypeError:
584          _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
DESCRIPTOR = <google.protobuf.descriptor.Descriptor object>
def MergeFrom(self, msg):
1361  def MergeFrom(self, msg):
1362    if not isinstance(msg, cls):
1363      raise TypeError(
1364          'Parameter to MergeFrom() must be instance of same class: '
1365          'expected %s got %s.' % (_FullyQualifiedClassName(cls),
1366                                   _FullyQualifiedClassName(msg.__class__)))
1367
1368    assert msg is not self
1369    self._Modified()
1370
1371    fields = self._fields
1372
1373    for field, value in msg._fields.items():
1374      if field.label == LABEL_REPEATED:
1375        field_value = fields.get(field)
1376        if field_value is None:
1377          # Construct a new object to represent this field.
1378          field_value = field._default_constructor(self)
1379          fields[field] = field_value
1380        field_value.MergeFrom(value)
1381      elif field.cpp_type == CPPTYPE_MESSAGE:
1382        if value._is_present_in_parent:
1383          field_value = fields.get(field)
1384          if field_value is None:
1385            # Construct a new object to represent this field.
1386            field_value = field._default_constructor(self)
1387            fields[field] = field_value
1388          field_value.MergeFrom(value)
1389      else:
1390        self._fields[field] = value
1391        if field.containing_oneof:
1392          self._UpdateOneofState(field)
1393
1394    if msg._unknown_fields:
1395      if not self._unknown_fields:
1396        self._unknown_fields = []
1397      self._unknown_fields.extend(msg._unknown_fields)

Merges the contents of the specified message into current message.

This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.

Arguments:
  • other_msg (Message): A message to merge into the current message.
def Clear(self):
1420def _Clear(self):
1421  # Clear fields.
1422  self._fields = {}
1423  self._unknown_fields = ()
1424
1425  self._oneofs = {}
1426  self._Modified()

Clears all data that was set in the message.

def SetInParent(self):
1486  def Modified(self):
1487    """Sets the _cached_byte_size_dirty bit to true,
1488    and propagates this to our listener iff this was a state change.
1489    """
1490
1491    # Note:  Some callers check _cached_byte_size_dirty before calling
1492    #   _Modified() as an extra optimization.  So, if this method is ever
1493    #   changed such that it does stuff even when _cached_byte_size_dirty is
1494    #   already true, the callers need to be updated.
1495    if not self._cached_byte_size_dirty:
1496      self._cached_byte_size_dirty = True
1497      self._listener_for_children.dirty = True
1498      self._is_present_in_parent = True
1499      self._listener.Modified()

Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.

def IsInitialized(self, errors=None):
1262  def IsInitialized(self, errors=None):
1263    """Checks if all required fields of a message are set.
1264
1265    Args:
1266      errors:  A list which, if provided, will be populated with the field
1267               paths of all missing required fields.
1268
1269    Returns:
1270      True iff the specified message has all required fields set.
1271    """
1272
1273    # Performance is critical so we avoid HasField() and ListFields().
1274
1275    for field in required_fields:
1276      if (field not in self._fields or
1277          (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and
1278           not self._fields[field]._is_present_in_parent)):
1279        if errors is not None:
1280          errors.extend(self.FindInitializationErrors())
1281        return False
1282
1283    for field, value in list(self._fields.items()):  # dict can change size!
1284      if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
1285        if field.label == _FieldDescriptor.LABEL_REPEATED:
1286          if (field.message_type._is_map_entry):
1287            continue
1288          for element in value:
1289            if not element.IsInitialized():
1290              if errors is not None:
1291                errors.extend(self.FindInitializationErrors())
1292              return False
1293        elif value._is_present_in_parent and not value.IsInitialized():
1294          if errors is not None:
1295            errors.extend(self.FindInitializationErrors())
1296          return False
1297
1298    return True

Checks if all required fields of a message are set.

Arguments:
  • errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:

True iff the specified message has all required fields set.

def MergeFromString(self, serialized):
1177  def MergeFromString(self, serialized):
1178    serialized = memoryview(serialized)
1179    length = len(serialized)
1180    try:
1181      if self._InternalParse(serialized, 0, length) != length:
1182        # The only reason _InternalParse would return early is if it
1183        # encountered an end-group tag.
1184        raise message_mod.DecodeError('Unexpected end-group tag.')
1185    except (IndexError, TypeError):
1186      # Now ord(buf[p:p+1]) == ord('') gets TypeError.
1187      raise message_mod.DecodeError('Truncated message.')
1188    except struct.error as e:
1189      raise message_mod.DecodeError(e)
1190    return length   # Return this for legacy reasons.

Merges serialized protocol buffer data into this message.

When we find a field in serialized that is already present in this message:

  • If it's a "repeated" field, we append to the end of our list.
  • Else, if it's a scalar, we overwrite our field.
  • Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
  • serialized (bytes): Any object that allows us to call memoryview(serialized) to access a string of bytes using the buffer interface.
Returns:

int: The number of bytes read from serialized. For non-group messages, this will always be len(serialized), but for messages which are actually groups, this will generally be less than len(serialized), since we must stop when we reach an END_GROUP tag. Note that if we do stop because of an END_GROUP tag, the number of bytes returned does not include the bytes for the END_GROUP tag information.

Raises:
  • DecodeError: if the input cannot be parsed.
def SerializeToString(self, **kwargs):
1130  def SerializeToString(self, **kwargs):
1131    # Check if the message has all of its required fields set.
1132    if not self.IsInitialized():
1133      raise message_mod.EncodeError(
1134          'Message %s is missing required fields: %s' % (
1135          self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors())))
1136    return self.SerializePartialToString(**kwargs)

Serializes the protocol message to a binary string.

Keyword Args:

deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.

Returns:

A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).

Raises:
def SerializePartialToString(self, **kwargs):
1143  def SerializePartialToString(self, **kwargs):
1144    out = BytesIO()
1145    self._InternalSerialize(out.write, **kwargs)
1146    return out.getvalue()

Serializes the protocol message to a binary string.

This method is similar to SerializeToString but doesn't check if the message is initialized.

Keyword Args:

deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.

Returns:

bytes: A serialized representation of the partial message.

def ListFields(self):
848  def ListFields(self):
849    all_fields = [item for item in self._fields.items() if _IsPresent(item)]
850    all_fields.sort(key = lambda item: item[0].number)
851    return all_fields

Returns a list of (FieldDescriptor, value) tuples for present fields.

A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.

Returns:

list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.

def HasField(self, field_name):
872  def HasField(self, field_name):
873    try:
874      field = hassable_fields[field_name]
875    except KeyError as exc:
876      raise ValueError('Protocol message %s has no non-repeated field "%s" '
877                       'nor has presence is not available for this field.' % (
878                           message_descriptor.full_name, field_name)) from exc
879
880    if isinstance(field, descriptor_mod.OneofDescriptor):
881      try:
882        return HasField(self, self._oneofs[field].name)
883      except KeyError:
884        return False
885    else:
886      if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
887        value = self._fields.get(field)
888        return value is not None and value._is_present_in_parent
889      else:
890        return field in self._fields

Checks if a certain field is set for the message.

For a oneof group, checks if any field inside is set. Note that if the field_name is not defined in the message descriptor, ValueError will be raised.

Arguments:
  • field_name (str): The name of the field to check for presence.
Returns:

bool: Whether a value has been set for the named field.

Raises:
  • ValueError: if the field_name is not a member of this message.
def ClearField(self, field_name):
897  def ClearField(self, field_name):
898    try:
899      field = message_descriptor.fields_by_name[field_name]
900    except KeyError:
901      try:
902        field = message_descriptor.oneofs_by_name[field_name]
903        if field in self._oneofs:
904          field = self._oneofs[field]
905        else:
906          return
907      except KeyError:
908        raise ValueError('Protocol message %s has no "%s" field.' %
909                         (message_descriptor.name, field_name))
910
911    if field in self._fields:
912      # To match the C++ implementation, we need to invalidate iterators
913      # for map fields when ClearField() happens.
914      if hasattr(self._fields[field], 'InvalidateIterators'):
915        self._fields[field].InvalidateIterators()
916
917      # Note:  If the field is a sub-message, its listener will still point
918      #   at us.  That's fine, because the worst than can happen is that it
919      #   will call _Modified() and invalidate our byte size.  Big deal.
920      del self._fields[field]
921
922      if self._oneofs.get(field.containing_oneof, None) is field:
923        del self._oneofs[field.containing_oneof]
924
925    # Always call _Modified() -- even if nothing was changed, this is
926    # a mutating method, and thus calling it should cause the field to become
927    # present in the parent message.
928    self._Modified()

Clears the contents of a given field.

Inside a oneof group, clears the field set. If the name neither refers to a defined field or oneof group, ValueError is raised.

Arguments:
  • field_name (str): The name of the field to check for presence.
Raises:
  • ValueError: if the field_name is not a member of this message.
def WhichOneof(self, oneof_name):
1403  def WhichOneof(self, oneof_name):
1404    """Returns the name of the currently set field inside a oneof, or None."""
1405    try:
1406      field = message_descriptor.oneofs_by_name[oneof_name]
1407    except KeyError:
1408      raise ValueError(
1409          'Protocol message has no oneof "%s" field.' % oneof_name)
1410
1411    nested_field = self._oneofs.get(field, None)
1412    if nested_field is not None and self.HasField(nested_field.name):
1413      return nested_field.name
1414    else:
1415      return None

Returns the name of the currently set field inside a oneof, or None.

def DiscardUnknownFields(self):
1435def _DiscardUnknownFields(self):
1436  self._unknown_fields = []
1437  for field, value in self.ListFields():
1438    if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
1439      if _IsMapField(field):
1440        if _IsMessageMapField(field):
1441          for key in value:
1442            value[key].DiscardUnknownFields()
1443      elif field.label == _FieldDescriptor.LABEL_REPEATED:
1444        for sub_message in value:
1445          sub_message.DiscardUnknownFields()
1446      else:
1447        value.DiscardUnknownFields()

Clears all fields in the UnknownFieldSet.

This operation is recursive for nested message.

def ByteSize(self):
1098  def ByteSize(self):
1099    if not self._cached_byte_size_dirty:
1100      return self._cached_byte_size
1101
1102    size = 0
1103    descriptor = self.DESCRIPTOR
1104    if descriptor._is_map_entry:
1105      # Fields of map entry should always be serialized.
1106      key_field = descriptor.fields_by_name['key']
1107      _MaybeAddEncoder(cls, key_field)
1108      size = key_field._sizer(self.key)
1109      value_field = descriptor.fields_by_name['value']
1110      _MaybeAddEncoder(cls, value_field)
1111      size += value_field._sizer(self.value)
1112    else:
1113      for field_descriptor, field_value in self.ListFields():
1114        _MaybeAddEncoder(cls, field_descriptor)
1115        size += field_descriptor._sizer(field_value)
1116      for tag_bytes, value_bytes in self._unknown_fields:
1117        size += len(tag_bytes) + len(value_bytes)
1118
1119    self._cached_byte_size = size
1120    self._cached_byte_size_dirty = False
1121    self._listener_for_children.dirty = False
1122    return size

Returns the serialized size of this message.

Recursively calls ByteSize() on all contained messages.

Returns:

int: The number of bytes required to serialize this message.

def FromString(s):
826  def FromString(s):
827    message = cls()
828    message.MergeFromString(s)
829    return message
VAR_INDEX_FIELD_NUMBER = 1
var_index
711  def getter(self):
712    # TODO: This may be broken since there may not be
713    # default_value.  Combine with has_default_value somehow.
714    return self._fields.get(field, default_value)

Getter for var_index.

VAR_VALUE_FIELD_NUMBER = 2
var_value
711  def getter(self):
712    # TODO: This may be broken since there may not be
713    # default_value.  Combine with has_default_value somehow.
714    return self._fields.get(field, default_value)

Getter for var_value.

CONSTRAINT_FIELD_NUMBER = 3
constraint
767  def getter(self):
768    field_value = self._fields.get(field)
769    if field_value is None:
770      # Construct a new object to represent this field.
771      field_value = field._default_constructor(self)
772
773      # Atomically check if another thread has preempted us and, if not, swap
774      # in the new object we just created.  If someone has preempted us, we
775      # take that object and discard ours.
776      # WARNING:  We are relying on setdefault() being atomic.  This is true
777      #   in CPython but we haven't investigated others.  This warning appears
778      #   in several other locations in this file.
779      field_value = self._fields.setdefault(field, field_value)
780    return field_value

Getter for constraint.

def FindInitializationErrors(self):
1302  def FindInitializationErrors(self):
1303    """Finds required fields which are not initialized.
1304
1305    Returns:
1306      A list of strings.  Each string is a path to an uninitialized field from
1307      the top-level message, e.g. "foo.bar[5].baz".
1308    """
1309
1310    errors = []  # simplify things
1311
1312    for field in required_fields:
1313      if not self.HasField(field.name):
1314        errors.append(field.name)
1315
1316    for field, value in self.ListFields():
1317      if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
1318        if field.is_extension:
1319          name = '(%s)' % field.full_name
1320        else:
1321          name = field.name
1322
1323        if _IsMapField(field):
1324          if _IsMessageMapField(field):
1325            for key in value:
1326              element = value[key]
1327              prefix = '%s[%s].' % (name, key)
1328              sub_errors = element.FindInitializationErrors()
1329              errors += [prefix + error for error in sub_errors]
1330          else:
1331            # ScalarMaps can't have any initialization errors.
1332            pass
1333        elif field.label == _FieldDescriptor.LABEL_REPEATED:
1334          for i in range(len(value)):
1335            element = value[i]
1336            prefix = '%s[%d].' % (name, i)
1337            sub_errors = element.FindInitializationErrors()
1338            errors += [prefix + error for error in sub_errors]
1339        else:
1340          prefix = name + '.'
1341          sub_errors = value.FindInitializationErrors()
1342          errors += [prefix + error for error in sub_errors]
1343
1344    return errors

Finds required fields which are not initialized.

Returns:

A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".

Inherited Members
google.protobuf.message.Message
CopyFrom
ParseFromString
HasExtension
ClearExtension
UnknownFields
class MPSosConstraint(google.protobuf.message.Message):

Abstract base class for protocol messages.

Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.

MPSosConstraint(**kwargs)
498  def init(self, **kwargs):
499    self._cached_byte_size = 0
500    self._cached_byte_size_dirty = len(kwargs) > 0
501    self._fields = {}
502    # Contains a mapping from oneof field descriptors to the descriptor
503    # of the currently set field in that oneof field.
504    self._oneofs = {}
505
506    # _unknown_fields is () when empty for efficiency, and will be turned into
507    # a list if fields are added.
508    self._unknown_fields = ()
509    self._is_present_in_parent = False
510    self._listener = message_listener_mod.NullMessageListener()
511    self._listener_for_children = _Listener(self)
512    for field_name, field_value in kwargs.items():
513      field = _GetFieldByName(message_descriptor, field_name)
514      if field is None:
515        raise TypeError('%s() got an unexpected keyword argument "%s"' %
516                        (message_descriptor.name, field_name))
517      if field_value is None:
518        # field=None is the same as no field at all.
519        continue
520      if field.label == _FieldDescriptor.LABEL_REPEATED:
521        field_copy = field._default_constructor(self)
522        if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:  # Composite
523          if _IsMapField(field):
524            if _IsMessageMapField(field):
525              for key in field_value:
526                field_copy[key].MergeFrom(field_value[key])
527            else:
528              field_copy.update(field_value)
529          else:
530            for val in field_value:
531              if isinstance(val, dict):
532                field_copy.add(**val)
533              else:
534                field_copy.add().MergeFrom(val)
535        else:  # Scalar
536          if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM:
537            field_value = [_GetIntegerEnumValue(field.enum_type, val)
538                           for val in field_value]
539          field_copy.extend(field_value)
540        self._fields[field] = field_copy
541      elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
542        field_copy = field._default_constructor(self)
543        new_val = None
544        if isinstance(field_value, message_mod.Message):
545          new_val = field_value
546        elif isinstance(field_value, dict):
547          if field.message_type.full_name == _StructFullTypeName:
548            field_copy.Clear()
549            if len(field_value) == 1 and 'fields' in field_value:
550              try:
551                field_copy.update(field_value)
552              except:
553                # Fall back to init normal message field
554                field_copy.Clear()
555                new_val = field.message_type._concrete_class(**field_value)
556            else:
557              field_copy.update(field_value)
558          else:
559            new_val = field.message_type._concrete_class(**field_value)
560        elif hasattr(field_copy, '_internal_assign'):
561          field_copy._internal_assign(field_value)
562        else:
563          raise TypeError(
564              'Message field {0}.{1} must be initialized with a '
565              'dict or instance of same class, got {2}.'.format(
566                  message_descriptor.name,
567                  field_name,
568                  type(field_value).__name__,
569              )
570          )
571
572        if new_val:
573          try:
574            field_copy.MergeFrom(new_val)
575          except TypeError:
576            _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
577        self._fields[field] = field_copy
578      else:
579        if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM:
580          field_value = _GetIntegerEnumValue(field.enum_type, field_value)
581        try:
582          setattr(self, field_name, field_value)
583        except TypeError:
584          _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
DESCRIPTOR = <google.protobuf.descriptor.Descriptor object>
def MergeFrom(self, msg):
1361  def MergeFrom(self, msg):
1362    if not isinstance(msg, cls):
1363      raise TypeError(
1364          'Parameter to MergeFrom() must be instance of same class: '
1365          'expected %s got %s.' % (_FullyQualifiedClassName(cls),
1366                                   _FullyQualifiedClassName(msg.__class__)))
1367
1368    assert msg is not self
1369    self._Modified()
1370
1371    fields = self._fields
1372
1373    for field, value in msg._fields.items():
1374      if field.label == LABEL_REPEATED:
1375        field_value = fields.get(field)
1376        if field_value is None:
1377          # Construct a new object to represent this field.
1378          field_value = field._default_constructor(self)
1379          fields[field] = field_value
1380        field_value.MergeFrom(value)
1381      elif field.cpp_type == CPPTYPE_MESSAGE:
1382        if value._is_present_in_parent:
1383          field_value = fields.get(field)
1384          if field_value is None:
1385            # Construct a new object to represent this field.
1386            field_value = field._default_constructor(self)
1387            fields[field] = field_value
1388          field_value.MergeFrom(value)
1389      else:
1390        self._fields[field] = value
1391        if field.containing_oneof:
1392          self._UpdateOneofState(field)
1393
1394    if msg._unknown_fields:
1395      if not self._unknown_fields:
1396        self._unknown_fields = []
1397      self._unknown_fields.extend(msg._unknown_fields)

Merges the contents of the specified message into current message.

This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.

Arguments:
  • other_msg (Message): A message to merge into the current message.
def Clear(self):
1420def _Clear(self):
1421  # Clear fields.
1422  self._fields = {}
1423  self._unknown_fields = ()
1424
1425  self._oneofs = {}
1426  self._Modified()

Clears all data that was set in the message.

def SetInParent(self):
1486  def Modified(self):
1487    """Sets the _cached_byte_size_dirty bit to true,
1488    and propagates this to our listener iff this was a state change.
1489    """
1490
1491    # Note:  Some callers check _cached_byte_size_dirty before calling
1492    #   _Modified() as an extra optimization.  So, if this method is ever
1493    #   changed such that it does stuff even when _cached_byte_size_dirty is
1494    #   already true, the callers need to be updated.
1495    if not self._cached_byte_size_dirty:
1496      self._cached_byte_size_dirty = True
1497      self._listener_for_children.dirty = True
1498      self._is_present_in_parent = True
1499      self._listener.Modified()

Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.

def IsInitialized(self, errors=None):
1262  def IsInitialized(self, errors=None):
1263    """Checks if all required fields of a message are set.
1264
1265    Args:
1266      errors:  A list which, if provided, will be populated with the field
1267               paths of all missing required fields.
1268
1269    Returns:
1270      True iff the specified message has all required fields set.
1271    """
1272
1273    # Performance is critical so we avoid HasField() and ListFields().
1274
1275    for field in required_fields:
1276      if (field not in self._fields or
1277          (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and
1278           not self._fields[field]._is_present_in_parent)):
1279        if errors is not None:
1280          errors.extend(self.FindInitializationErrors())
1281        return False
1282
1283    for field, value in list(self._fields.items()):  # dict can change size!
1284      if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
1285        if field.label == _FieldDescriptor.LABEL_REPEATED:
1286          if (field.message_type._is_map_entry):
1287            continue
1288          for element in value:
1289            if not element.IsInitialized():
1290              if errors is not None:
1291                errors.extend(self.FindInitializationErrors())
1292              return False
1293        elif value._is_present_in_parent and not value.IsInitialized():
1294          if errors is not None:
1295            errors.extend(self.FindInitializationErrors())
1296          return False
1297
1298    return True

Checks if all required fields of a message are set.

Arguments:
  • errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:

True iff the specified message has all required fields set.

def MergeFromString(self, serialized):
1177  def MergeFromString(self, serialized):
1178    serialized = memoryview(serialized)
1179    length = len(serialized)
1180    try:
1181      if self._InternalParse(serialized, 0, length) != length:
1182        # The only reason _InternalParse would return early is if it
1183        # encountered an end-group tag.
1184        raise message_mod.DecodeError('Unexpected end-group tag.')
1185    except (IndexError, TypeError):
1186      # Now ord(buf[p:p+1]) == ord('') gets TypeError.
1187      raise message_mod.DecodeError('Truncated message.')
1188    except struct.error as e:
1189      raise message_mod.DecodeError(e)
1190    return length   # Return this for legacy reasons.

Merges serialized protocol buffer data into this message.

When we find a field in serialized that is already present in this message:

  • If it's a "repeated" field, we append to the end of our list.
  • Else, if it's a scalar, we overwrite our field.
  • Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
  • serialized (bytes): Any object that allows us to call memoryview(serialized) to access a string of bytes using the buffer interface.
Returns:

int: The number of bytes read from serialized. For non-group messages, this will always be len(serialized), but for messages which are actually groups, this will generally be less than len(serialized), since we must stop when we reach an END_GROUP tag. Note that if we do stop because of an END_GROUP tag, the number of bytes returned does not include the bytes for the END_GROUP tag information.

Raises:
  • DecodeError: if the input cannot be parsed.
def SerializeToString(self, **kwargs):
1130  def SerializeToString(self, **kwargs):
1131    # Check if the message has all of its required fields set.
1132    if not self.IsInitialized():
1133      raise message_mod.EncodeError(
1134          'Message %s is missing required fields: %s' % (
1135          self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors())))
1136    return self.SerializePartialToString(**kwargs)

Serializes the protocol message to a binary string.

Keyword Args:

deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.

Returns:

A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).

Raises:
def SerializePartialToString(self, **kwargs):
1143  def SerializePartialToString(self, **kwargs):
1144    out = BytesIO()
1145    self._InternalSerialize(out.write, **kwargs)
1146    return out.getvalue()

Serializes the protocol message to a binary string.

This method is similar to SerializeToString but doesn't check if the message is initialized.

Keyword Args:

deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.

Returns:

bytes: A serialized representation of the partial message.

def ListFields(self):
848  def ListFields(self):
849    all_fields = [item for item in self._fields.items() if _IsPresent(item)]
850    all_fields.sort(key = lambda item: item[0].number)
851    return all_fields

Returns a list of (FieldDescriptor, value) tuples for present fields.

A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.

Returns:

list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.

def HasField(self, field_name):
872  def HasField(self, field_name):
873    try:
874      field = hassable_fields[field_name]
875    except KeyError as exc:
876      raise ValueError('Protocol message %s has no non-repeated field "%s" '
877                       'nor has presence is not available for this field.' % (
878                           message_descriptor.full_name, field_name)) from exc
879
880    if isinstance(field, descriptor_mod.OneofDescriptor):
881      try:
882        return HasField(self, self._oneofs[field].name)
883      except KeyError:
884        return False
885    else:
886      if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
887        value = self._fields.get(field)
888        return value is not None and value._is_present_in_parent
889      else:
890        return field in self._fields

Checks if a certain field is set for the message.

For a oneof group, checks if any field inside is set. Note that if the field_name is not defined in the message descriptor, ValueError will be raised.

Arguments:
  • field_name (str): The name of the field to check for presence.
Returns:

bool: Whether a value has been set for the named field.

Raises:
  • ValueError: if the field_name is not a member of this message.
def ClearField(self, field_name):
897  def ClearField(self, field_name):
898    try:
899      field = message_descriptor.fields_by_name[field_name]
900    except KeyError:
901      try:
902        field = message_descriptor.oneofs_by_name[field_name]
903        if field in self._oneofs:
904          field = self._oneofs[field]
905        else:
906          return
907      except KeyError:
908        raise ValueError('Protocol message %s has no "%s" field.' %
909                         (message_descriptor.name, field_name))
910
911    if field in self._fields:
912      # To match the C++ implementation, we need to invalidate iterators
913      # for map fields when ClearField() happens.
914      if hasattr(self._fields[field], 'InvalidateIterators'):
915        self._fields[field].InvalidateIterators()
916
917      # Note:  If the field is a sub-message, its listener will still point
918      #   at us.  That's fine, because the worst than can happen is that it
919      #   will call _Modified() and invalidate our byte size.  Big deal.
920      del self._fields[field]
921
922      if self._oneofs.get(field.containing_oneof, None) is field:
923        del self._oneofs[field.containing_oneof]
924
925    # Always call _Modified() -- even if nothing was changed, this is
926    # a mutating method, and thus calling it should cause the field to become
927    # present in the parent message.
928    self._Modified()

Clears the contents of a given field.

Inside a oneof group, clears the field set. If the name neither refers to a defined field or oneof group, ValueError is raised.

Arguments:
  • field_name (str): The name of the field to check for presence.
Raises:
  • ValueError: if the field_name is not a member of this message.
def WhichOneof(self, oneof_name):
1403  def WhichOneof(self, oneof_name):
1404    """Returns the name of the currently set field inside a oneof, or None."""
1405    try:
1406      field = message_descriptor.oneofs_by_name[oneof_name]
1407    except KeyError:
1408      raise ValueError(
1409          'Protocol message has no oneof "%s" field.' % oneof_name)
1410
1411    nested_field = self._oneofs.get(field, None)
1412    if nested_field is not None and self.HasField(nested_field.name):
1413      return nested_field.name
1414    else:
1415      return None

Returns the name of the currently set field inside a oneof, or None.

def DiscardUnknownFields(self):
1435def _DiscardUnknownFields(self):
1436  self._unknown_fields = []
1437  for field, value in self.ListFields():
1438    if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
1439      if _IsMapField(field):
1440        if _IsMessageMapField(field):
1441          for key in value:
1442            value[key].DiscardUnknownFields()
1443      elif field.label == _FieldDescriptor.LABEL_REPEATED:
1444        for sub_message in value:
1445          sub_message.DiscardUnknownFields()
1446      else:
1447        value.DiscardUnknownFields()

Clears all fields in the UnknownFieldSet.

This operation is recursive for nested message.

def ByteSize(self):
1098  def ByteSize(self):
1099    if not self._cached_byte_size_dirty:
1100      return self._cached_byte_size
1101
1102    size = 0
1103    descriptor = self.DESCRIPTOR
1104    if descriptor._is_map_entry:
1105      # Fields of map entry should always be serialized.
1106      key_field = descriptor.fields_by_name['key']
1107      _MaybeAddEncoder(cls, key_field)
1108      size = key_field._sizer(self.key)
1109      value_field = descriptor.fields_by_name['value']
1110      _MaybeAddEncoder(cls, value_field)
1111      size += value_field._sizer(self.value)
1112    else:
1113      for field_descriptor, field_value in self.ListFields():
1114        _MaybeAddEncoder(cls, field_descriptor)
1115        size += field_descriptor._sizer(field_value)
1116      for tag_bytes, value_bytes in self._unknown_fields:
1117        size += len(tag_bytes) + len(value_bytes)
1118
1119    self._cached_byte_size = size
1120    self._cached_byte_size_dirty = False
1121    self._listener_for_children.dirty = False
1122    return size

Returns the serialized size of this message.

Recursively calls ByteSize() on all contained messages.

Returns:

int: The number of bytes required to serialize this message.

def FromString(s):
826  def FromString(s):
827    message = cls()
828    message.MergeFromString(s)
829    return message
Type = <google.protobuf.internal.enum_type_wrapper.EnumTypeWrapper object>
SOS1_DEFAULT = 0
SOS2 = 1
TYPE_FIELD_NUMBER = 1
type
711  def getter(self):
712    # TODO: This may be broken since there may not be
713    # default_value.  Combine with has_default_value somehow.
714    return self._fields.get(field, default_value)

Getter for type.

VAR_INDEX_FIELD_NUMBER = 2
var_index
668  def getter(self):
669    field_value = self._fields.get(field)
670    if field_value is None:
671      # Construct a new object to represent this field.
672      field_value = field._default_constructor(self)
673
674      # Atomically check if another thread has preempted us and, if not, swap
675      # in the new object we just created.  If someone has preempted us, we
676      # take that object and discard ours.
677      # WARNING:  We are relying on setdefault() being atomic.  This is true
678      #   in CPython but we haven't investigated others.  This warning appears
679      #   in several other locations in this file.
680      field_value = self._fields.setdefault(field, field_value)
681    return field_value

Getter for var_index.

WEIGHT_FIELD_NUMBER = 3
weight
668  def getter(self):
669    field_value = self._fields.get(field)
670    if field_value is None:
671      # Construct a new object to represent this field.
672      field_value = field._default_constructor(self)
673
674      # Atomically check if another thread has preempted us and, if not, swap
675      # in the new object we just created.  If someone has preempted us, we
676      # take that object and discard ours.
677      # WARNING:  We are relying on setdefault() being atomic.  This is true
678      #   in CPython but we haven't investigated others.  This warning appears
679      #   in several other locations in this file.
680      field_value = self._fields.setdefault(field, field_value)
681    return field_value

Getter for weight.

def FindInitializationErrors(self):
1302  def FindInitializationErrors(self):
1303    """Finds required fields which are not initialized.
1304
1305    Returns:
1306      A list of strings.  Each string is a path to an uninitialized field from
1307      the top-level message, e.g. "foo.bar[5].baz".
1308    """
1309
1310    errors = []  # simplify things
1311
1312    for field in required_fields:
1313      if not self.HasField(field.name):
1314        errors.append(field.name)
1315
1316    for field, value in self.ListFields():
1317      if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
1318        if field.is_extension:
1319          name = '(%s)' % field.full_name
1320        else:
1321          name = field.name
1322
1323        if _IsMapField(field):
1324          if _IsMessageMapField(field):
1325            for key in value:
1326              element = value[key]
1327              prefix = '%s[%s].' % (name, key)
1328              sub_errors = element.FindInitializationErrors()
1329              errors += [prefix + error for error in sub_errors]
1330          else:
1331            # ScalarMaps can't have any initialization errors.
1332            pass
1333        elif field.label == _FieldDescriptor.LABEL_REPEATED:
1334          for i in range(len(value)):
1335            element = value[i]
1336            prefix = '%s[%d].' % (name, i)
1337            sub_errors = element.FindInitializationErrors()
1338            errors += [prefix + error for error in sub_errors]
1339        else:
1340          prefix = name + '.'
1341          sub_errors = value.FindInitializationErrors()
1342          errors += [prefix + error for error in sub_errors]
1343
1344    return errors

Finds required fields which are not initialized.

Returns:

A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".

Inherited Members
google.protobuf.message.Message
CopyFrom
ParseFromString
HasExtension
ClearExtension
UnknownFields
class MPQuadraticConstraint(google.protobuf.message.Message):

Abstract base class for protocol messages.

Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.

MPQuadraticConstraint(**kwargs)
498  def init(self, **kwargs):
499    self._cached_byte_size = 0
500    self._cached_byte_size_dirty = len(kwargs) > 0
501    self._fields = {}
502    # Contains a mapping from oneof field descriptors to the descriptor
503    # of the currently set field in that oneof field.
504    self._oneofs = {}
505
506    # _unknown_fields is () when empty for efficiency, and will be turned into
507    # a list if fields are added.
508    self._unknown_fields = ()
509    self._is_present_in_parent = False
510    self._listener = message_listener_mod.NullMessageListener()
511    self._listener_for_children = _Listener(self)
512    for field_name, field_value in kwargs.items():
513      field = _GetFieldByName(message_descriptor, field_name)
514      if field is None:
515        raise TypeError('%s() got an unexpected keyword argument "%s"' %
516                        (message_descriptor.name, field_name))
517      if field_value is None:
518        # field=None is the same as no field at all.
519        continue
520      if field.label == _FieldDescriptor.LABEL_REPEATED:
521        field_copy = field._default_constructor(self)
522        if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:  # Composite
523          if _IsMapField(field):
524            if _IsMessageMapField(field):
525              for key in field_value:
526                field_copy[key].MergeFrom(field_value[key])
527            else:
528              field_copy.update(field_value)
529          else:
530            for val in field_value:
531              if isinstance(val, dict):
532                field_copy.add(**val)
533              else:
534                field_copy.add().MergeFrom(val)
535        else:  # Scalar
536          if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM:
537            field_value = [_GetIntegerEnumValue(field.enum_type, val)
538                           for val in field_value]
539          field_copy.extend(field_value)
540        self._fields[field] = field_copy
541      elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
542        field_copy = field._default_constructor(self)
543        new_val = None
544        if isinstance(field_value, message_mod.Message):
545          new_val = field_value
546        elif isinstance(field_value, dict):
547          if field.message_type.full_name == _StructFullTypeName:
548            field_copy.Clear()
549            if len(field_value) == 1 and 'fields' in field_value:
550              try:
551                field_copy.update(field_value)
552              except:
553                # Fall back to init normal message field
554                field_copy.Clear()
555                new_val = field.message_type._concrete_class(**field_value)
556            else:
557              field_copy.update(field_value)
558          else:
559            new_val = field.message_type._concrete_class(**field_value)
560        elif hasattr(field_copy, '_internal_assign'):
561          field_copy._internal_assign(field_value)
562        else:
563          raise TypeError(
564              'Message field {0}.{1} must be initialized with a '
565              'dict or instance of same class, got {2}.'.format(
566                  message_descriptor.name,
567                  field_name,
568                  type(field_value).__name__,
569              )
570          )
571
572        if new_val:
573          try:
574            field_copy.MergeFrom(new_val)
575          except TypeError:
576            _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
577        self._fields[field] = field_copy
578      else:
579        if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM:
580          field_value = _GetIntegerEnumValue(field.enum_type, field_value)
581        try:
582          setattr(self, field_name, field_value)
583        except TypeError:
584          _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
DESCRIPTOR = <google.protobuf.descriptor.Descriptor object>
def MergeFrom(self, msg):
1361  def MergeFrom(self, msg):
1362    if not isinstance(msg, cls):
1363      raise TypeError(
1364          'Parameter to MergeFrom() must be instance of same class: '
1365          'expected %s got %s.' % (_FullyQualifiedClassName(cls),
1366                                   _FullyQualifiedClassName(msg.__class__)))
1367
1368    assert msg is not self
1369    self._Modified()
1370
1371    fields = self._fields
1372
1373    for field, value in msg._fields.items():
1374      if field.label == LABEL_REPEATED:
1375        field_value = fields.get(field)
1376        if field_value is None:
1377          # Construct a new object to represent this field.
1378          field_value = field._default_constructor(self)
1379          fields[field] = field_value
1380        field_value.MergeFrom(value)
1381      elif field.cpp_type == CPPTYPE_MESSAGE:
1382        if value._is_present_in_parent:
1383          field_value = fields.get(field)
1384          if field_value is None:
1385            # Construct a new object to represent this field.
1386            field_value = field._default_constructor(self)
1387            fields[field] = field_value
1388          field_value.MergeFrom(value)
1389      else:
1390        self._fields[field] = value
1391        if field.containing_oneof:
1392          self._UpdateOneofState(field)
1393
1394    if msg._unknown_fields:
1395      if not self._unknown_fields:
1396        self._unknown_fields = []
1397      self._unknown_fields.extend(msg._unknown_fields)

Merges the contents of the specified message into current message.

This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.

Arguments:
  • other_msg (Message): A message to merge into the current message.
def Clear(self):
1420def _Clear(self):
1421  # Clear fields.
1422  self._fields = {}
1423  self._unknown_fields = ()
1424
1425  self._oneofs = {}
1426  self._Modified()

Clears all data that was set in the message.

def SetInParent(self):
1486  def Modified(self):
1487    """Sets the _cached_byte_size_dirty bit to true,
1488    and propagates this to our listener iff this was a state change.
1489    """
1490
1491    # Note:  Some callers check _cached_byte_size_dirty before calling
1492    #   _Modified() as an extra optimization.  So, if this method is ever
1493    #   changed such that it does stuff even when _cached_byte_size_dirty is
1494    #   already true, the callers need to be updated.
1495    if not self._cached_byte_size_dirty:
1496      self._cached_byte_size_dirty = True
1497      self._listener_for_children.dirty = True
1498      self._is_present_in_parent = True
1499      self._listener.Modified()

Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.

def IsInitialized(self, errors=None):
1262  def IsInitialized(self, errors=None):
1263    """Checks if all required fields of a message are set.
1264
1265    Args:
1266      errors:  A list which, if provided, will be populated with the field
1267               paths of all missing required fields.
1268
1269    Returns:
1270      True iff the specified message has all required fields set.
1271    """
1272
1273    # Performance is critical so we avoid HasField() and ListFields().
1274
1275    for field in required_fields:
1276      if (field not in self._fields or
1277          (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and
1278           not self._fields[field]._is_present_in_parent)):
1279        if errors is not None:
1280          errors.extend(self.FindInitializationErrors())
1281        return False
1282
1283    for field, value in list(self._fields.items()):  # dict can change size!
1284      if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
1285        if field.label == _FieldDescriptor.LABEL_REPEATED:
1286          if (field.message_type._is_map_entry):
1287            continue
1288          for element in value:
1289            if not element.IsInitialized():
1290              if errors is not None:
1291                errors.extend(self.FindInitializationErrors())
1292              return False
1293        elif value._is_present_in_parent and not value.IsInitialized():
1294          if errors is not None:
1295            errors.extend(self.FindInitializationErrors())
1296          return False
1297
1298    return True

Checks if all required fields of a message are set.

Arguments:
  • errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:

True iff the specified message has all required fields set.

def MergeFromString(self, serialized):
1177  def MergeFromString(self, serialized):
1178    serialized = memoryview(serialized)
1179    length = len(serialized)
1180    try:
1181      if self._InternalParse(serialized, 0, length) != length:
1182        # The only reason _InternalParse would return early is if it
1183        # encountered an end-group tag.
1184        raise message_mod.DecodeError('Unexpected end-group tag.')
1185    except (IndexError, TypeError):
1186      # Now ord(buf[p:p+1]) == ord('') gets TypeError.
1187      raise message_mod.DecodeError('Truncated message.')
1188    except struct.error as e:
1189      raise message_mod.DecodeError(e)
1190    return length   # Return this for legacy reasons.

Merges serialized protocol buffer data into this message.

When we find a field in serialized that is already present in this message:

  • If it's a "repeated" field, we append to the end of our list.
  • Else, if it's a scalar, we overwrite our field.
  • Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
  • serialized (bytes): Any object that allows us to call memoryview(serialized) to access a string of bytes using the buffer interface.
Returns:

int: The number of bytes read from serialized. For non-group messages, this will always be len(serialized), but for messages which are actually groups, this will generally be less than len(serialized), since we must stop when we reach an END_GROUP tag. Note that if we do stop because of an END_GROUP tag, the number of bytes returned does not include the bytes for the END_GROUP tag information.

Raises:
  • DecodeError: if the input cannot be parsed.
def SerializeToString(self, **kwargs):
1130  def SerializeToString(self, **kwargs):
1131    # Check if the message has all of its required fields set.
1132    if not self.IsInitialized():
1133      raise message_mod.EncodeError(
1134          'Message %s is missing required fields: %s' % (
1135          self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors())))
1136    return self.SerializePartialToString(**kwargs)

Serializes the protocol message to a binary string.

Keyword Args:

deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.

Returns:

A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).

Raises:
def SerializePartialToString(self, **kwargs):
1143  def SerializePartialToString(self, **kwargs):
1144    out = BytesIO()
1145    self._InternalSerialize(out.write, **kwargs)
1146    return out.getvalue()

Serializes the protocol message to a binary string.

This method is similar to SerializeToString but doesn't check if the message is initialized.

Keyword Args:

deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.

Returns:

bytes: A serialized representation of the partial message.

def ListFields(self):
848  def ListFields(self):
849    all_fields = [item for item in self._fields.items() if _IsPresent(item)]
850    all_fields.sort(key = lambda item: item[0].number)
851    return all_fields

Returns a list of (FieldDescriptor, value) tuples for present fields.

A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.

Returns:

list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.

def HasField(self, field_name):
872  def HasField(self, field_name):
873    try:
874      field = hassable_fields[field_name]
875    except KeyError as exc:
876      raise ValueError('Protocol message %s has no non-repeated field "%s" '
877                       'nor has presence is not available for this field.' % (
878                           message_descriptor.full_name, field_name)) from exc
879
880    if isinstance(field, descriptor_mod.OneofDescriptor):
881      try:
882        return HasField(self, self._oneofs[field].name)
883      except KeyError:
884        return False
885    else:
886      if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
887        value = self._fields.get(field)
888        return value is not None and value._is_present_in_parent
889      else:
890        return field in self._fields

Checks if a certain field is set for the message.

For a oneof group, checks if any field inside is set. Note that if the field_name is not defined in the message descriptor, ValueError will be raised.

Arguments:
  • field_name (str): The name of the field to check for presence.
Returns:

bool: Whether a value has been set for the named field.

Raises:
  • ValueError: if the field_name is not a member of this message.
def ClearField(self, field_name):
897  def ClearField(self, field_name):
898    try:
899      field = message_descriptor.fields_by_name[field_name]
900    except KeyError:
901      try:
902        field = message_descriptor.oneofs_by_name[field_name]
903        if field in self._oneofs:
904          field = self._oneofs[field]
905        else:
906          return
907      except KeyError:
908        raise ValueError('Protocol message %s has no "%s" field.' %
909                         (message_descriptor.name, field_name))
910
911    if field in self._fields:
912      # To match the C++ implementation, we need to invalidate iterators
913      # for map fields when ClearField() happens.
914      if hasattr(self._fields[field], 'InvalidateIterators'):
915        self._fields[field].InvalidateIterators()
916
917      # Note:  If the field is a sub-message, its listener will still point
918      #   at us.  That's fine, because the worst than can happen is that it
919      #   will call _Modified() and invalidate our byte size.  Big deal.
920      del self._fields[field]
921
922      if self._oneofs.get(field.containing_oneof, None) is field:
923        del self._oneofs[field.containing_oneof]
924
925    # Always call _Modified() -- even if nothing was changed, this is
926    # a mutating method, and thus calling it should cause the field to become
927    # present in the parent message.
928    self._Modified()

Clears the contents of a given field.

Inside a oneof group, clears the field set. If the name neither refers to a defined field or oneof group, ValueError is raised.

Arguments:
  • field_name (str): The name of the field to check for presence.
Raises:
  • ValueError: if the field_name is not a member of this message.
def WhichOneof(self, oneof_name):
1403  def WhichOneof(self, oneof_name):
1404    """Returns the name of the currently set field inside a oneof, or None."""
1405    try:
1406      field = message_descriptor.oneofs_by_name[oneof_name]
1407    except KeyError:
1408      raise ValueError(
1409          'Protocol message has no oneof "%s" field.' % oneof_name)
1410
1411    nested_field = self._oneofs.get(field, None)
1412    if nested_field is not None and self.HasField(nested_field.name):
1413      return nested_field.name
1414    else:
1415      return None

Returns the name of the currently set field inside a oneof, or None.

def DiscardUnknownFields(self):
1435def _DiscardUnknownFields(self):
1436  self._unknown_fields = []
1437  for field, value in self.ListFields():
1438    if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
1439      if _IsMapField(field):
1440        if _IsMessageMapField(field):
1441          for key in value:
1442            value[key].DiscardUnknownFields()
1443      elif field.label == _FieldDescriptor.LABEL_REPEATED:
1444        for sub_message in value:
1445          sub_message.DiscardUnknownFields()
1446      else:
1447        value.DiscardUnknownFields()

Clears all fields in the UnknownFieldSet.

This operation is recursive for nested message.

def ByteSize(self):
1098  def ByteSize(self):
1099    if not self._cached_byte_size_dirty:
1100      return self._cached_byte_size
1101
1102    size = 0
1103    descriptor = self.DESCRIPTOR
1104    if descriptor._is_map_entry:
1105      # Fields of map entry should always be serialized.
1106      key_field = descriptor.fields_by_name['key']
1107      _MaybeAddEncoder(cls, key_field)
1108      size = key_field._sizer(self.key)
1109      value_field = descriptor.fields_by_name['value']
1110      _MaybeAddEncoder(cls, value_field)
1111      size += value_field._sizer(self.value)
1112    else:
1113      for field_descriptor, field_value in self.ListFields():
1114        _MaybeAddEncoder(cls, field_descriptor)
1115        size += field_descriptor._sizer(field_value)
1116      for tag_bytes, value_bytes in self._unknown_fields:
1117        size += len(tag_bytes) + len(value_bytes)
1118
1119    self._cached_byte_size = size
1120    self._cached_byte_size_dirty = False
1121    self._listener_for_children.dirty = False
1122    return size

Returns the serialized size of this message.

Recursively calls ByteSize() on all contained messages.

Returns:

int: The number of bytes required to serialize this message.

def FromString(s):
826  def FromString(s):
827    message = cls()
828    message.MergeFromString(s)
829    return message
VAR_INDEX_FIELD_NUMBER = 1
var_index
668  def getter(self):
669    field_value = self._fields.get(field)
670    if field_value is None:
671      # Construct a new object to represent this field.
672      field_value = field._default_constructor(self)
673
674      # Atomically check if another thread has preempted us and, if not, swap
675      # in the new object we just created.  If someone has preempted us, we
676      # take that object and discard ours.
677      # WARNING:  We are relying on setdefault() being atomic.  This is true
678      #   in CPython but we haven't investigated others.  This warning appears
679      #   in several other locations in this file.
680      field_value = self._fields.setdefault(field, field_value)
681    return field_value

Getter for var_index.

COEFFICIENT_FIELD_NUMBER = 2
coefficient
668  def getter(self):
669    field_value = self._fields.get(field)
670    if field_value is None:
671      # Construct a new object to represent this field.
672      field_value = field._default_constructor(self)
673
674      # Atomically check if another thread has preempted us and, if not, swap
675      # in the new object we just created.  If someone has preempted us, we
676      # take that object and discard ours.
677      # WARNING:  We are relying on setdefault() being atomic.  This is true
678      #   in CPython but we haven't investigated others.  This warning appears
679      #   in several other locations in this file.
680      field_value = self._fields.setdefault(field, field_value)
681    return field_value

Getter for coefficient.

QVAR1_INDEX_FIELD_NUMBER = 3
qvar1_index
668  def getter(self):
669    field_value = self._fields.get(field)
670    if field_value is None:
671      # Construct a new object to represent this field.
672      field_value = field._default_constructor(self)
673
674      # Atomically check if another thread has preempted us and, if not, swap
675      # in the new object we just created.  If someone has preempted us, we
676      # take that object and discard ours.
677      # WARNING:  We are relying on setdefault() being atomic.  This is true
678      #   in CPython but we haven't investigated others.  This warning appears
679      #   in several other locations in this file.
680      field_value = self._fields.setdefault(field, field_value)
681    return field_value

Getter for qvar1_index.

QVAR2_INDEX_FIELD_NUMBER = 4
qvar2_index
668  def getter(self):
669    field_value = self._fields.get(field)
670    if field_value is None:
671      # Construct a new object to represent this field.
672      field_value = field._default_constructor(self)
673
674      # Atomically check if another thread has preempted us and, if not, swap
675      # in the new object we just created.  If someone has preempted us, we
676      # take that object and discard ours.
677      # WARNING:  We are relying on setdefault() being atomic.  This is true
678      #   in CPython but we haven't investigated others.  This warning appears
679      #   in several other locations in this file.
680      field_value = self._fields.setdefault(field, field_value)
681    return field_value

Getter for qvar2_index.

QCOEFFICIENT_FIELD_NUMBER = 5
qcoefficient
668  def getter(self):
669    field_value = self._fields.get(field)
670    if field_value is None:
671      # Construct a new object to represent this field.
672      field_value = field._default_constructor(self)
673
674      # Atomically check if another thread has preempted us and, if not, swap
675      # in the new object we just created.  If someone has preempted us, we
676      # take that object and discard ours.
677      # WARNING:  We are relying on setdefault() being atomic.  This is true
678      #   in CPython but we haven't investigated others.  This warning appears
679      #   in several other locations in this file.
680      field_value = self._fields.setdefault(field, field_value)
681    return field_value

Getter for qcoefficient.

LOWER_BOUND_FIELD_NUMBER = 6
lower_bound
711  def getter(self):
712    # TODO: This may be broken since there may not be
713    # default_value.  Combine with has_default_value somehow.
714    return self._fields.get(field, default_value)

Getter for lower_bound.

UPPER_BOUND_FIELD_NUMBER = 7
upper_bound
711  def getter(self):
712    # TODO: This may be broken since there may not be
713    # default_value.  Combine with has_default_value somehow.
714    return self._fields.get(field, default_value)

Getter for upper_bound.

def FindInitializationErrors(self):
1302  def FindInitializationErrors(self):
1303    """Finds required fields which are not initialized.
1304
1305    Returns:
1306      A list of strings.  Each string is a path to an uninitialized field from
1307      the top-level message, e.g. "foo.bar[5].baz".
1308    """
1309
1310    errors = []  # simplify things
1311
1312    for field in required_fields:
1313      if not self.HasField(field.name):
1314        errors.append(field.name)
1315
1316    for field, value in self.ListFields():
1317      if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
1318        if field.is_extension:
1319          name = '(%s)' % field.full_name
1320        else:
1321          name = field.name
1322
1323        if _IsMapField(field):
1324          if _IsMessageMapField(field):
1325            for key in value:
1326              element = value[key]
1327              prefix = '%s[%s].' % (name, key)
1328              sub_errors = element.FindInitializationErrors()
1329              errors += [prefix + error for error in sub_errors]
1330          else:
1331            # ScalarMaps can't have any initialization errors.
1332            pass
1333        elif field.label == _FieldDescriptor.LABEL_REPEATED:
1334          for i in range(len(value)):
1335            element = value[i]
1336            prefix = '%s[%d].' % (name, i)
1337            sub_errors = element.FindInitializationErrors()
1338            errors += [prefix + error for error in sub_errors]
1339        else:
1340          prefix = name + '.'
1341          sub_errors = value.FindInitializationErrors()
1342          errors += [prefix + error for error in sub_errors]
1343
1344    return errors

Finds required fields which are not initialized.

Returns:

A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".

Inherited Members
google.protobuf.message.Message
CopyFrom
ParseFromString
HasExtension
ClearExtension
UnknownFields
class MPAbsConstraint(google.protobuf.message.Message):

Abstract base class for protocol messages.

Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.

MPAbsConstraint(**kwargs)
498  def init(self, **kwargs):
499    self._cached_byte_size = 0
500    self._cached_byte_size_dirty = len(kwargs) > 0
501    self._fields = {}
502    # Contains a mapping from oneof field descriptors to the descriptor
503    # of the currently set field in that oneof field.
504    self._oneofs = {}
505
506    # _unknown_fields is () when empty for efficiency, and will be turned into
507    # a list if fields are added.
508    self._unknown_fields = ()
509    self._is_present_in_parent = False
510    self._listener = message_listener_mod.NullMessageListener()
511    self._listener_for_children = _Listener(self)
512    for field_name, field_value in kwargs.items():
513      field = _GetFieldByName(message_descriptor, field_name)
514      if field is None:
515        raise TypeError('%s() got an unexpected keyword argument "%s"' %
516                        (message_descriptor.name, field_name))
517      if field_value is None:
518        # field=None is the same as no field at all.
519        continue
520      if field.label == _FieldDescriptor.LABEL_REPEATED:
521        field_copy = field._default_constructor(self)
522        if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:  # Composite
523          if _IsMapField(field):
524            if _IsMessageMapField(field):
525              for key in field_value:
526                field_copy[key].MergeFrom(field_value[key])
527            else:
528              field_copy.update(field_value)
529          else:
530            for val in field_value:
531              if isinstance(val, dict):
532                field_copy.add(**val)
533              else:
534                field_copy.add().MergeFrom(val)
535        else:  # Scalar
536          if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM:
537            field_value = [_GetIntegerEnumValue(field.enum_type, val)
538                           for val in field_value]
539          field_copy.extend(field_value)
540        self._fields[field] = field_copy
541      elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
542        field_copy = field._default_constructor(self)
543        new_val = None
544        if isinstance(field_value, message_mod.Message):
545          new_val = field_value
546        elif isinstance(field_value, dict):
547          if field.message_type.full_name == _StructFullTypeName:
548            field_copy.Clear()
549            if len(field_value) == 1 and 'fields' in field_value:
550              try:
551                field_copy.update(field_value)
552              except:
553                # Fall back to init normal message field
554                field_copy.Clear()
555                new_val = field.message_type._concrete_class(**field_value)
556            else:
557              field_copy.update(field_value)
558          else:
559            new_val = field.message_type._concrete_class(**field_value)
560        elif hasattr(field_copy, '_internal_assign'):
561          field_copy._internal_assign(field_value)
562        else:
563          raise TypeError(
564              'Message field {0}.{1} must be initialized with a '
565              'dict or instance of same class, got {2}.'.format(
566                  message_descriptor.name,
567                  field_name,
568                  type(field_value).__name__,
569              )
570          )
571
572        if new_val:
573          try:
574            field_copy.MergeFrom(new_val)
575          except TypeError:
576            _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
577        self._fields[field] = field_copy
578      else:
579        if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM:
580          field_value = _GetIntegerEnumValue(field.enum_type, field_value)
581        try:
582          setattr(self, field_name, field_value)
583        except TypeError:
584          _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
DESCRIPTOR = <google.protobuf.descriptor.Descriptor object>
def MergeFrom(self, msg):
1361  def MergeFrom(self, msg):
1362    if not isinstance(msg, cls):
1363      raise TypeError(
1364          'Parameter to MergeFrom() must be instance of same class: '
1365          'expected %s got %s.' % (_FullyQualifiedClassName(cls),
1366                                   _FullyQualifiedClassName(msg.__class__)))
1367
1368    assert msg is not self
1369    self._Modified()
1370
1371    fields = self._fields
1372
1373    for field, value in msg._fields.items():
1374      if field.label == LABEL_REPEATED:
1375        field_value = fields.get(field)
1376        if field_value is None:
1377          # Construct a new object to represent this field.
1378          field_value = field._default_constructor(self)
1379          fields[field] = field_value
1380        field_value.MergeFrom(value)
1381      elif field.cpp_type == CPPTYPE_MESSAGE:
1382        if value._is_present_in_parent:
1383          field_value = fields.get(field)
1384          if field_value is None:
1385            # Construct a new object to represent this field.
1386            field_value = field._default_constructor(self)
1387            fields[field] = field_value
1388          field_value.MergeFrom(value)
1389      else:
1390        self._fields[field] = value
1391        if field.containing_oneof:
1392          self._UpdateOneofState(field)
1393
1394    if msg._unknown_fields:
1395      if not self._unknown_fields:
1396        self._unknown_fields = []
1397      self._unknown_fields.extend(msg._unknown_fields)

Merges the contents of the specified message into current message.

This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.

Arguments:
  • other_msg (Message): A message to merge into the current message.
def Clear(self):
1420def _Clear(self):
1421  # Clear fields.
1422  self._fields = {}
1423  self._unknown_fields = ()
1424
1425  self._oneofs = {}
1426  self._Modified()

Clears all data that was set in the message.

def SetInParent(self):
1486  def Modified(self):
1487    """Sets the _cached_byte_size_dirty bit to true,
1488    and propagates this to our listener iff this was a state change.
1489    """
1490
1491    # Note:  Some callers check _cached_byte_size_dirty before calling
1492    #   _Modified() as an extra optimization.  So, if this method is ever
1493    #   changed such that it does stuff even when _cached_byte_size_dirty is
1494    #   already true, the callers need to be updated.
1495    if not self._cached_byte_size_dirty:
1496      self._cached_byte_size_dirty = True
1497      self._listener_for_children.dirty = True
1498      self._is_present_in_parent = True
1499      self._listener.Modified()

Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.

def IsInitialized(self, errors=None):
1262  def IsInitialized(self, errors=None):
1263    """Checks if all required fields of a message are set.
1264
1265    Args:
1266      errors:  A list which, if provided, will be populated with the field
1267               paths of all missing required fields.
1268
1269    Returns:
1270      True iff the specified message has all required fields set.
1271    """
1272
1273    # Performance is critical so we avoid HasField() and ListFields().
1274
1275    for field in required_fields:
1276      if (field not in self._fields or
1277          (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and
1278           not self._fields[field]._is_present_in_parent)):
1279        if errors is not None:
1280          errors.extend(self.FindInitializationErrors())
1281        return False
1282
1283    for field, value in list(self._fields.items()):  # dict can change size!
1284      if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
1285        if field.label == _FieldDescriptor.LABEL_REPEATED:
1286          if (field.message_type._is_map_entry):
1287            continue
1288          for element in value:
1289            if not element.IsInitialized():
1290              if errors is not None:
1291                errors.extend(self.FindInitializationErrors())
1292              return False
1293        elif value._is_present_in_parent and not value.IsInitialized():
1294          if errors is not None:
1295            errors.extend(self.FindInitializationErrors())
1296          return False
1297
1298    return True

Checks if all required fields of a message are set.

Arguments:
  • errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:

True iff the specified message has all required fields set.

def MergeFromString(self, serialized):
1177  def MergeFromString(self, serialized):
1178    serialized = memoryview(serialized)
1179    length = len(serialized)
1180    try:
1181      if self._InternalParse(serialized, 0, length) != length:
1182        # The only reason _InternalParse would return early is if it
1183        # encountered an end-group tag.
1184        raise message_mod.DecodeError('Unexpected end-group tag.')
1185    except (IndexError, TypeError):
1186      # Now ord(buf[p:p+1]) == ord('') gets TypeError.
1187      raise message_mod.DecodeError('Truncated message.')
1188    except struct.error as e:
1189      raise message_mod.DecodeError(e)
1190    return length   # Return this for legacy reasons.

Merges serialized protocol buffer data into this message.

When we find a field in serialized that is already present in this message:

  • If it's a "repeated" field, we append to the end of our list.
  • Else, if it's a scalar, we overwrite our field.
  • Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
  • serialized (bytes): Any object that allows us to call memoryview(serialized) to access a string of bytes using the buffer interface.
Returns:

int: The number of bytes read from serialized. For non-group messages, this will always be len(serialized), but for messages which are actually groups, this will generally be less than len(serialized), since we must stop when we reach an END_GROUP tag. Note that if we do stop because of an END_GROUP tag, the number of bytes returned does not include the bytes for the END_GROUP tag information.

Raises:
  • DecodeError: if the input cannot be parsed.
def SerializeToString(self, **kwargs):
1130  def SerializeToString(self, **kwargs):
1131    # Check if the message has all of its required fields set.
1132    if not self.IsInitialized():
1133      raise message_mod.EncodeError(
1134          'Message %s is missing required fields: %s' % (
1135          self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors())))
1136    return self.SerializePartialToString(**kwargs)

Serializes the protocol message to a binary string.

Keyword Args:

deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.

Returns:

A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).

Raises:
def SerializePartialToString(self, **kwargs):
1143  def SerializePartialToString(self, **kwargs):
1144    out = BytesIO()
1145    self._InternalSerialize(out.write, **kwargs)
1146    return out.getvalue()

Serializes the protocol message to a binary string.

This method is similar to SerializeToString but doesn't check if the message is initialized.

Keyword Args:

deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.

Returns:

bytes: A serialized representation of the partial message.

def ListFields(self):
848  def ListFields(self):
849    all_fields = [item for item in self._fields.items() if _IsPresent(item)]
850    all_fields.sort(key = lambda item: item[0].number)
851    return all_fields

Returns a list of (FieldDescriptor, value) tuples for present fields.

A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.

Returns:

list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.

def HasField(self, field_name):
872  def HasField(self, field_name):
873    try:
874      field = hassable_fields[field_name]
875    except KeyError as exc:
876      raise ValueError('Protocol message %s has no non-repeated field "%s" '
877                       'nor has presence is not available for this field.' % (
878                           message_descriptor.full_name, field_name)) from exc
879
880    if isinstance(field, descriptor_mod.OneofDescriptor):
881      try:
882        return HasField(self, self._oneofs[field].name)
883      except KeyError:
884        return False
885    else:
886      if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
887        value = self._fields.get(field)
888        return value is not None and value._is_present_in_parent
889      else:
890        return field in self._fields

Checks if a certain field is set for the message.

For a oneof group, checks if any field inside is set. Note that if the field_name is not defined in the message descriptor, ValueError will be raised.

Arguments:
  • field_name (str): The name of the field to check for presence.
Returns:

bool: Whether a value has been set for the named field.

Raises:
  • ValueError: if the field_name is not a member of this message.
def ClearField(self, field_name):
897  def ClearField(self, field_name):
898    try:
899      field = message_descriptor.fields_by_name[field_name]
900    except KeyError:
901      try:
902        field = message_descriptor.oneofs_by_name[field_name]
903        if field in self._oneofs:
904          field = self._oneofs[field]
905        else:
906          return
907      except KeyError:
908        raise ValueError('Protocol message %s has no "%s" field.' %
909                         (message_descriptor.name, field_name))
910
911    if field in self._fields:
912      # To match the C++ implementation, we need to invalidate iterators
913      # for map fields when ClearField() happens.
914      if hasattr(self._fields[field], 'InvalidateIterators'):
915        self._fields[field].InvalidateIterators()
916
917      # Note:  If the field is a sub-message, its listener will still point
918      #   at us.  That's fine, because the worst than can happen is that it
919      #   will call _Modified() and invalidate our byte size.  Big deal.
920      del self._fields[field]
921
922      if self._oneofs.get(field.containing_oneof, None) is field:
923        del self._oneofs[field.containing_oneof]
924
925    # Always call _Modified() -- even if nothing was changed, this is
926    # a mutating method, and thus calling it should cause the field to become
927    # present in the parent message.
928    self._Modified()

Clears the contents of a given field.

Inside a oneof group, clears the field set. If the name neither refers to a defined field or oneof group, ValueError is raised.

Arguments:
  • field_name (str): The name of the field to check for presence.
Raises:
  • ValueError: if the field_name is not a member of this message.
def WhichOneof(self, oneof_name):
1403  def WhichOneof(self, oneof_name):
1404    """Returns the name of the currently set field inside a oneof, or None."""
1405    try:
1406      field = message_descriptor.oneofs_by_name[oneof_name]
1407    except KeyError:
1408      raise ValueError(
1409          'Protocol message has no oneof "%s" field.' % oneof_name)
1410
1411    nested_field = self._oneofs.get(field, None)
1412    if nested_field is not None and self.HasField(nested_field.name):
1413      return nested_field.name
1414    else:
1415      return None

Returns the name of the currently set field inside a oneof, or None.

def DiscardUnknownFields(self):
1435def _DiscardUnknownFields(self):
1436  self._unknown_fields = []
1437  for field, value in self.ListFields():
1438    if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
1439      if _IsMapField(field):
1440        if _IsMessageMapField(field):
1441          for key in value:
1442            value[key].DiscardUnknownFields()
1443      elif field.label == _FieldDescriptor.LABEL_REPEATED:
1444        for sub_message in value:
1445          sub_message.DiscardUnknownFields()
1446      else:
1447        value.DiscardUnknownFields()

Clears all fields in the UnknownFieldSet.

This operation is recursive for nested message.

def ByteSize(self):
1098  def ByteSize(self):
1099    if not self._cached_byte_size_dirty:
1100      return self._cached_byte_size
1101
1102    size = 0
1103    descriptor = self.DESCRIPTOR
1104    if descriptor._is_map_entry:
1105      # Fields of map entry should always be serialized.
1106      key_field = descriptor.fields_by_name['key']
1107      _MaybeAddEncoder(cls, key_field)
1108      size = key_field._sizer(self.key)
1109      value_field = descriptor.fields_by_name['value']
1110      _MaybeAddEncoder(cls, value_field)
1111      size += value_field._sizer(self.value)
1112    else:
1113      for field_descriptor, field_value in self.ListFields():
1114        _MaybeAddEncoder(cls, field_descriptor)
1115        size += field_descriptor._sizer(field_value)
1116      for tag_bytes, value_bytes in self._unknown_fields:
1117        size += len(tag_bytes) + len(value_bytes)
1118
1119    self._cached_byte_size = size
1120    self._cached_byte_size_dirty = False
1121    self._listener_for_children.dirty = False
1122    return size

Returns the serialized size of this message.

Recursively calls ByteSize() on all contained messages.

Returns:

int: The number of bytes required to serialize this message.

def FromString(s):
826  def FromString(s):
827    message = cls()
828    message.MergeFromString(s)
829    return message
VAR_INDEX_FIELD_NUMBER = 1
var_index
711  def getter(self):
712    # TODO: This may be broken since there may not be
713    # default_value.  Combine with has_default_value somehow.
714    return self._fields.get(field, default_value)

Getter for var_index.

RESULTANT_VAR_INDEX_FIELD_NUMBER = 2
resultant_var_index
711  def getter(self):
712    # TODO: This may be broken since there may not be
713    # default_value.  Combine with has_default_value somehow.
714    return self._fields.get(field, default_value)

Getter for resultant_var_index.

def FindInitializationErrors(self):
1302  def FindInitializationErrors(self):
1303    """Finds required fields which are not initialized.
1304
1305    Returns:
1306      A list of strings.  Each string is a path to an uninitialized field from
1307      the top-level message, e.g. "foo.bar[5].baz".
1308    """
1309
1310    errors = []  # simplify things
1311
1312    for field in required_fields:
1313      if not self.HasField(field.name):
1314        errors.append(field.name)
1315
1316    for field, value in self.ListFields():
1317      if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
1318        if field.is_extension:
1319          name = '(%s)' % field.full_name
1320        else:
1321          name = field.name
1322
1323        if _IsMapField(field):
1324          if _IsMessageMapField(field):
1325            for key in value:
1326              element = value[key]
1327              prefix = '%s[%s].' % (name, key)
1328              sub_errors = element.FindInitializationErrors()
1329              errors += [prefix + error for error in sub_errors]
1330          else:
1331            # ScalarMaps can't have any initialization errors.
1332            pass
1333        elif field.label == _FieldDescriptor.LABEL_REPEATED:
1334          for i in range(len(value)):
1335            element = value[i]
1336            prefix = '%s[%d].' % (name, i)
1337            sub_errors = element.FindInitializationErrors()
1338            errors += [prefix + error for error in sub_errors]
1339        else:
1340          prefix = name + '.'
1341          sub_errors = value.FindInitializationErrors()
1342          errors += [prefix + error for error in sub_errors]
1343
1344    return errors

Finds required fields which are not initialized.

Returns:

A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".

Inherited Members
google.protobuf.message.Message
CopyFrom
ParseFromString
HasExtension
ClearExtension
UnknownFields
class MPArrayConstraint(google.protobuf.message.Message):

Abstract base class for protocol messages.

Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.

MPArrayConstraint(**kwargs)
498  def init(self, **kwargs):
499    self._cached_byte_size = 0
500    self._cached_byte_size_dirty = len(kwargs) > 0
501    self._fields = {}
502    # Contains a mapping from oneof field descriptors to the descriptor
503    # of the currently set field in that oneof field.
504    self._oneofs = {}
505
506    # _unknown_fields is () when empty for efficiency, and will be turned into
507    # a list if fields are added.
508    self._unknown_fields = ()
509    self._is_present_in_parent = False
510    self._listener = message_listener_mod.NullMessageListener()
511    self._listener_for_children = _Listener(self)
512    for field_name, field_value in kwargs.items():
513      field = _GetFieldByName(message_descriptor, field_name)
514      if field is None:
515        raise TypeError('%s() got an unexpected keyword argument "%s"' %
516                        (message_descriptor.name, field_name))
517      if field_value is None:
518        # field=None is the same as no field at all.
519        continue
520      if field.label == _FieldDescriptor.LABEL_REPEATED:
521        field_copy = field._default_constructor(self)
522        if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:  # Composite
523          if _IsMapField(field):
524            if _IsMessageMapField(field):
525              for key in field_value:
526                field_copy[key].MergeFrom(field_value[key])
527            else:
528              field_copy.update(field_value)
529          else:
530            for val in field_value:
531              if isinstance(val, dict):
532                field_copy.add(**val)
533              else:
534                field_copy.add().MergeFrom(val)
535        else:  # Scalar
536          if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM:
537            field_value = [_GetIntegerEnumValue(field.enum_type, val)
538                           for val in field_value]
539          field_copy.extend(field_value)
540        self._fields[field] = field_copy
541      elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
542        field_copy = field._default_constructor(self)
543        new_val = None
544        if isinstance(field_value, message_mod.Message):
545          new_val = field_value
546        elif isinstance(field_value, dict):
547          if field.message_type.full_name == _StructFullTypeName:
548            field_copy.Clear()
549            if len(field_value) == 1 and 'fields' in field_value:
550              try:
551                field_copy.update(field_value)
552              except:
553                # Fall back to init normal message field
554                field_copy.Clear()
555                new_val = field.message_type._concrete_class(**field_value)
556            else:
557              field_copy.update(field_value)
558          else:
559            new_val = field.message_type._concrete_class(**field_value)
560        elif hasattr(field_copy, '_internal_assign'):
561          field_copy._internal_assign(field_value)
562        else:
563          raise TypeError(
564              'Message field {0}.{1} must be initialized with a '
565              'dict or instance of same class, got {2}.'.format(
566                  message_descriptor.name,
567                  field_name,
568                  type(field_value).__name__,
569              )
570          )
571
572        if new_val:
573          try:
574            field_copy.MergeFrom(new_val)
575          except TypeError:
576            _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
577        self._fields[field] = field_copy
578      else:
579        if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM:
580          field_value = _GetIntegerEnumValue(field.enum_type, field_value)
581        try:
582          setattr(self, field_name, field_value)
583        except TypeError:
584          _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
DESCRIPTOR = <google.protobuf.descriptor.Descriptor object>
def MergeFrom(self, msg):
1361  def MergeFrom(self, msg):
1362    if not isinstance(msg, cls):
1363      raise TypeError(
1364          'Parameter to MergeFrom() must be instance of same class: '
1365          'expected %s got %s.' % (_FullyQualifiedClassName(cls),
1366                                   _FullyQualifiedClassName(msg.__class__)))
1367
1368    assert msg is not self
1369    self._Modified()
1370
1371    fields = self._fields
1372
1373    for field, value in msg._fields.items():
1374      if field.label == LABEL_REPEATED:
1375        field_value = fields.get(field)
1376        if field_value is None:
1377          # Construct a new object to represent this field.
1378          field_value = field._default_constructor(self)
1379          fields[field] = field_value
1380        field_value.MergeFrom(value)
1381      elif field.cpp_type == CPPTYPE_MESSAGE:
1382        if value._is_present_in_parent:
1383          field_value = fields.get(field)
1384          if field_value is None:
1385            # Construct a new object to represent this field.
1386            field_value = field._default_constructor(self)
1387            fields[field] = field_value
1388          field_value.MergeFrom(value)
1389      else:
1390        self._fields[field] = value
1391        if field.containing_oneof:
1392          self._UpdateOneofState(field)
1393
1394    if msg._unknown_fields:
1395      if not self._unknown_fields:
1396        self._unknown_fields = []
1397      self._unknown_fields.extend(msg._unknown_fields)

Merges the contents of the specified message into current message.

This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.

Arguments:
  • other_msg (Message): A message to merge into the current message.
def Clear(self):
1420def _Clear(self):
1421  # Clear fields.
1422  self._fields = {}
1423  self._unknown_fields = ()
1424
1425  self._oneofs = {}
1426  self._Modified()

Clears all data that was set in the message.

def SetInParent(self):
1486  def Modified(self):
1487    """Sets the _cached_byte_size_dirty bit to true,
1488    and propagates this to our listener iff this was a state change.
1489    """
1490
1491    # Note:  Some callers check _cached_byte_size_dirty before calling
1492    #   _Modified() as an extra optimization.  So, if this method is ever
1493    #   changed such that it does stuff even when _cached_byte_size_dirty is
1494    #   already true, the callers need to be updated.
1495    if not self._cached_byte_size_dirty:
1496      self._cached_byte_size_dirty = True
1497      self._listener_for_children.dirty = True
1498      self._is_present_in_parent = True
1499      self._listener.Modified()

Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.

def IsInitialized(self, errors=None):
1262  def IsInitialized(self, errors=None):
1263    """Checks if all required fields of a message are set.
1264
1265    Args:
1266      errors:  A list which, if provided, will be populated with the field
1267               paths of all missing required fields.
1268
1269    Returns:
1270      True iff the specified message has all required fields set.
1271    """
1272
1273    # Performance is critical so we avoid HasField() and ListFields().
1274
1275    for field in required_fields:
1276      if (field not in self._fields or
1277          (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and
1278           not self._fields[field]._is_present_in_parent)):
1279        if errors is not None:
1280          errors.extend(self.FindInitializationErrors())
1281        return False
1282
1283    for field, value in list(self._fields.items()):  # dict can change size!
1284      if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
1285        if field.label == _FieldDescriptor.LABEL_REPEATED:
1286          if (field.message_type._is_map_entry):
1287            continue
1288          for element in value:
1289            if not element.IsInitialized():
1290              if errors is not None:
1291                errors.extend(self.FindInitializationErrors())
1292              return False
1293        elif value._is_present_in_parent and not value.IsInitialized():
1294          if errors is not None:
1295            errors.extend(self.FindInitializationErrors())
1296          return False
1297
1298    return True

Checks if all required fields of a message are set.

Arguments:
  • errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:

True iff the specified message has all required fields set.

def MergeFromString(self, serialized):
1177  def MergeFromString(self, serialized):
1178    serialized = memoryview(serialized)
1179    length = len(serialized)
1180    try:
1181      if self._InternalParse(serialized, 0, length) != length:
1182        # The only reason _InternalParse would return early is if it
1183        # encountered an end-group tag.
1184        raise message_mod.DecodeError('Unexpected end-group tag.')
1185    except (IndexError, TypeError):
1186      # Now ord(buf[p:p+1]) == ord('') gets TypeError.
1187      raise message_mod.DecodeError('Truncated message.')
1188    except struct.error as e:
1189      raise message_mod.DecodeError(e)
1190    return length   # Return this for legacy reasons.

Merges serialized protocol buffer data into this message.

When we find a field in serialized that is already present in this message:

  • If it's a "repeated" field, we append to the end of our list.
  • Else, if it's a scalar, we overwrite our field.
  • Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
  • serialized (bytes): Any object that allows us to call memoryview(serialized) to access a string of bytes using the buffer interface.
Returns:

int: The number of bytes read from serialized. For non-group messages, this will always be len(serialized), but for messages which are actually groups, this will generally be less than len(serialized), since we must stop when we reach an END_GROUP tag. Note that if we do stop because of an END_GROUP tag, the number of bytes returned does not include the bytes for the END_GROUP tag information.

Raises:
  • DecodeError: if the input cannot be parsed.
def SerializeToString(self, **kwargs):
1130  def SerializeToString(self, **kwargs):
1131    # Check if the message has all of its required fields set.
1132    if not self.IsInitialized():
1133      raise message_mod.EncodeError(
1134          'Message %s is missing required fields: %s' % (
1135          self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors())))
1136    return self.SerializePartialToString(**kwargs)

Serializes the protocol message to a binary string.

Keyword Args:

deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.

Returns:

A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).

Raises:
def SerializePartialToString(self, **kwargs):
1143  def SerializePartialToString(self, **kwargs):
1144    out = BytesIO()
1145    self._InternalSerialize(out.write, **kwargs)
1146    return out.getvalue()

Serializes the protocol message to a binary string.

This method is similar to SerializeToString but doesn't check if the message is initialized.

Keyword Args:

deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.

Returns:

bytes: A serialized representation of the partial message.

def ListFields(self):
848  def ListFields(self):
849    all_fields = [item for item in self._fields.items() if _IsPresent(item)]
850    all_fields.sort(key = lambda item: item[0].number)
851    return all_fields

Returns a list of (FieldDescriptor, value) tuples for present fields.

A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.

Returns:

list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.

def HasField(self, field_name):
872  def HasField(self, field_name):
873    try:
874      field = hassable_fields[field_name]
875    except KeyError as exc:
876      raise ValueError('Protocol message %s has no non-repeated field "%s" '
877                       'nor has presence is not available for this field.' % (
878                           message_descriptor.full_name, field_name)) from exc
879
880    if isinstance(field, descriptor_mod.OneofDescriptor):
881      try:
882        return HasField(self, self._oneofs[field].name)
883      except KeyError:
884        return False
885    else:
886      if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
887        value = self._fields.get(field)
888        return value is not None and value._is_present_in_parent
889      else:
890        return field in self._fields

Checks if a certain field is set for the message.

For a oneof group, checks if any field inside is set. Note that if the field_name is not defined in the message descriptor, ValueError will be raised.

Arguments:
  • field_name (str): The name of the field to check for presence.
Returns:

bool: Whether a value has been set for the named field.

Raises:
  • ValueError: if the field_name is not a member of this message.
def ClearField(self, field_name):
897  def ClearField(self, field_name):
898    try:
899      field = message_descriptor.fields_by_name[field_name]
900    except KeyError:
901      try:
902        field = message_descriptor.oneofs_by_name[field_name]
903        if field in self._oneofs:
904          field = self._oneofs[field]
905        else:
906          return
907      except KeyError:
908        raise ValueError('Protocol message %s has no "%s" field.' %
909                         (message_descriptor.name, field_name))
910
911    if field in self._fields:
912      # To match the C++ implementation, we need to invalidate iterators
913      # for map fields when ClearField() happens.
914      if hasattr(self._fields[field], 'InvalidateIterators'):
915        self._fields[field].InvalidateIterators()
916
917      # Note:  If the field is a sub-message, its listener will still point
918      #   at us.  That's fine, because the worst than can happen is that it
919      #   will call _Modified() and invalidate our byte size.  Big deal.
920      del self._fields[field]
921
922      if self._oneofs.get(field.containing_oneof, None) is field:
923        del self._oneofs[field.containing_oneof]
924
925    # Always call _Modified() -- even if nothing was changed, this is
926    # a mutating method, and thus calling it should cause the field to become
927    # present in the parent message.
928    self._Modified()

Clears the contents of a given field.

Inside a oneof group, clears the field set. If the name neither refers to a defined field or oneof group, ValueError is raised.

Arguments:
  • field_name (str): The name of the field to check for presence.
Raises:
  • ValueError: if the field_name is not a member of this message.
def WhichOneof(self, oneof_name):
1403  def WhichOneof(self, oneof_name):
1404    """Returns the name of the currently set field inside a oneof, or None."""
1405    try:
1406      field = message_descriptor.oneofs_by_name[oneof_name]
1407    except KeyError:
1408      raise ValueError(
1409          'Protocol message has no oneof "%s" field.' % oneof_name)
1410
1411    nested_field = self._oneofs.get(field, None)
1412    if nested_field is not None and self.HasField(nested_field.name):
1413      return nested_field.name
1414    else:
1415      return None

Returns the name of the currently set field inside a oneof, or None.

def DiscardUnknownFields(self):
1435def _DiscardUnknownFields(self):
1436  self._unknown_fields = []
1437  for field, value in self.ListFields():
1438    if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
1439      if _IsMapField(field):
1440        if _IsMessageMapField(field):
1441          for key in value:
1442            value[key].DiscardUnknownFields()
1443      elif field.label == _FieldDescriptor.LABEL_REPEATED:
1444        for sub_message in value:
1445          sub_message.DiscardUnknownFields()
1446      else:
1447        value.DiscardUnknownFields()

Clears all fields in the UnknownFieldSet.

This operation is recursive for nested message.

def ByteSize(self):
1098  def ByteSize(self):
1099    if not self._cached_byte_size_dirty:
1100      return self._cached_byte_size
1101
1102    size = 0
1103    descriptor = self.DESCRIPTOR
1104    if descriptor._is_map_entry:
1105      # Fields of map entry should always be serialized.
1106      key_field = descriptor.fields_by_name['key']
1107      _MaybeAddEncoder(cls, key_field)
1108      size = key_field._sizer(self.key)
1109      value_field = descriptor.fields_by_name['value']
1110      _MaybeAddEncoder(cls, value_field)
1111      size += value_field._sizer(self.value)
1112    else:
1113      for field_descriptor, field_value in self.ListFields():
1114        _MaybeAddEncoder(cls, field_descriptor)
1115        size += field_descriptor._sizer(field_value)
1116      for tag_bytes, value_bytes in self._unknown_fields:
1117        size += len(tag_bytes) + len(value_bytes)
1118
1119    self._cached_byte_size = size
1120    self._cached_byte_size_dirty = False
1121    self._listener_for_children.dirty = False
1122    return size

Returns the serialized size of this message.

Recursively calls ByteSize() on all contained messages.

Returns:

int: The number of bytes required to serialize this message.

def FromString(s):
826  def FromString(s):
827    message = cls()
828    message.MergeFromString(s)
829    return message
VAR_INDEX_FIELD_NUMBER = 1
var_index
668  def getter(self):
669    field_value = self._fields.get(field)
670    if field_value is None:
671      # Construct a new object to represent this field.
672      field_value = field._default_constructor(self)
673
674      # Atomically check if another thread has preempted us and, if not, swap
675      # in the new object we just created.  If someone has preempted us, we
676      # take that object and discard ours.
677      # WARNING:  We are relying on setdefault() being atomic.  This is true
678      #   in CPython but we haven't investigated others.  This warning appears
679      #   in several other locations in this file.
680      field_value = self._fields.setdefault(field, field_value)
681    return field_value

Getter for var_index.

RESULTANT_VAR_INDEX_FIELD_NUMBER = 2
resultant_var_index
711  def getter(self):
712    # TODO: This may be broken since there may not be
713    # default_value.  Combine with has_default_value somehow.
714    return self._fields.get(field, default_value)

Getter for resultant_var_index.

def FindInitializationErrors(self):
1302  def FindInitializationErrors(self):
1303    """Finds required fields which are not initialized.
1304
1305    Returns:
1306      A list of strings.  Each string is a path to an uninitialized field from
1307      the top-level message, e.g. "foo.bar[5].baz".
1308    """
1309
1310    errors = []  # simplify things
1311
1312    for field in required_fields:
1313      if not self.HasField(field.name):
1314        errors.append(field.name)
1315
1316    for field, value in self.ListFields():
1317      if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
1318        if field.is_extension:
1319          name = '(%s)' % field.full_name
1320        else:
1321          name = field.name
1322
1323        if _IsMapField(field):
1324          if _IsMessageMapField(field):
1325            for key in value:
1326              element = value[key]
1327              prefix = '%s[%s].' % (name, key)
1328              sub_errors = element.FindInitializationErrors()
1329              errors += [prefix + error for error in sub_errors]
1330          else:
1331            # ScalarMaps can't have any initialization errors.
1332            pass
1333        elif field.label == _FieldDescriptor.LABEL_REPEATED:
1334          for i in range(len(value)):
1335            element = value[i]
1336            prefix = '%s[%d].' % (name, i)
1337            sub_errors = element.FindInitializationErrors()
1338            errors += [prefix + error for error in sub_errors]
1339        else:
1340          prefix = name + '.'
1341          sub_errors = value.FindInitializationErrors()
1342          errors += [prefix + error for error in sub_errors]
1343
1344    return errors

Finds required fields which are not initialized.

Returns:

A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".

Inherited Members
google.protobuf.message.Message
CopyFrom
ParseFromString
HasExtension
ClearExtension
UnknownFields
class MPArrayWithConstantConstraint(google.protobuf.message.Message):

Abstract base class for protocol messages.

Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.

MPArrayWithConstantConstraint(**kwargs)
498  def init(self, **kwargs):
499    self._cached_byte_size = 0
500    self._cached_byte_size_dirty = len(kwargs) > 0
501    self._fields = {}
502    # Contains a mapping from oneof field descriptors to the descriptor
503    # of the currently set field in that oneof field.
504    self._oneofs = {}
505
506    # _unknown_fields is () when empty for efficiency, and will be turned into
507    # a list if fields are added.
508    self._unknown_fields = ()
509    self._is_present_in_parent = False
510    self._listener = message_listener_mod.NullMessageListener()
511    self._listener_for_children = _Listener(self)
512    for field_name, field_value in kwargs.items():
513      field = _GetFieldByName(message_descriptor, field_name)
514      if field is None:
515        raise TypeError('%s() got an unexpected keyword argument "%s"' %
516                        (message_descriptor.name, field_name))
517      if field_value is None:
518        # field=None is the same as no field at all.
519        continue
520      if field.label == _FieldDescriptor.LABEL_REPEATED:
521        field_copy = field._default_constructor(self)
522        if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:  # Composite
523          if _IsMapField(field):
524            if _IsMessageMapField(field):
525              for key in field_value:
526                field_copy[key].MergeFrom(field_value[key])
527            else:
528              field_copy.update(field_value)
529          else:
530            for val in field_value:
531              if isinstance(val, dict):
532                field_copy.add(**val)
533              else:
534                field_copy.add().MergeFrom(val)
535        else:  # Scalar
536          if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM:
537            field_value = [_GetIntegerEnumValue(field.enum_type, val)
538                           for val in field_value]
539          field_copy.extend(field_value)
540        self._fields[field] = field_copy
541      elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
542        field_copy = field._default_constructor(self)
543        new_val = None
544        if isinstance(field_value, message_mod.Message):
545          new_val = field_value
546        elif isinstance(field_value, dict):
547          if field.message_type.full_name == _StructFullTypeName:
548            field_copy.Clear()
549            if len(field_value) == 1 and 'fields' in field_value:
550              try:
551                field_copy.update(field_value)
552              except:
553                # Fall back to init normal message field
554                field_copy.Clear()
555                new_val = field.message_type._concrete_class(**field_value)
556            else:
557              field_copy.update(field_value)
558          else:
559            new_val = field.message_type._concrete_class(**field_value)
560        elif hasattr(field_copy, '_internal_assign'):
561          field_copy._internal_assign(field_value)
562        else:
563          raise TypeError(
564              'Message field {0}.{1} must be initialized with a '
565              'dict or instance of same class, got {2}.'.format(
566                  message_descriptor.name,
567                  field_name,
568                  type(field_value).__name__,
569              )
570          )
571
572        if new_val:
573          try:
574            field_copy.MergeFrom(new_val)
575          except TypeError:
576            _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
577        self._fields[field] = field_copy
578      else:
579        if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM:
580          field_value = _GetIntegerEnumValue(field.enum_type, field_value)
581        try:
582          setattr(self, field_name, field_value)
583        except TypeError:
584          _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
DESCRIPTOR = <google.protobuf.descriptor.Descriptor object>
def MergeFrom(self, msg):
1361  def MergeFrom(self, msg):
1362    if not isinstance(msg, cls):
1363      raise TypeError(
1364          'Parameter to MergeFrom() must be instance of same class: '
1365          'expected %s got %s.' % (_FullyQualifiedClassName(cls),
1366                                   _FullyQualifiedClassName(msg.__class__)))
1367
1368    assert msg is not self
1369    self._Modified()
1370
1371    fields = self._fields
1372
1373    for field, value in msg._fields.items():
1374      if field.label == LABEL_REPEATED:
1375        field_value = fields.get(field)
1376        if field_value is None:
1377          # Construct a new object to represent this field.
1378          field_value = field._default_constructor(self)
1379          fields[field] = field_value
1380        field_value.MergeFrom(value)
1381      elif field.cpp_type == CPPTYPE_MESSAGE:
1382        if value._is_present_in_parent:
1383          field_value = fields.get(field)
1384          if field_value is None:
1385            # Construct a new object to represent this field.
1386            field_value = field._default_constructor(self)
1387            fields[field] = field_value
1388          field_value.MergeFrom(value)
1389      else:
1390        self._fields[field] = value
1391        if field.containing_oneof:
1392          self._UpdateOneofState(field)
1393
1394    if msg._unknown_fields:
1395      if not self._unknown_fields:
1396        self._unknown_fields = []
1397      self._unknown_fields.extend(msg._unknown_fields)

Merges the contents of the specified message into current message.

This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.

Arguments:
  • other_msg (Message): A message to merge into the current message.
def Clear(self):
1420def _Clear(self):
1421  # Clear fields.
1422  self._fields = {}
1423  self._unknown_fields = ()
1424
1425  self._oneofs = {}
1426  self._Modified()

Clears all data that was set in the message.

def SetInParent(self):
1486  def Modified(self):
1487    """Sets the _cached_byte_size_dirty bit to true,
1488    and propagates this to our listener iff this was a state change.
1489    """
1490
1491    # Note:  Some callers check _cached_byte_size_dirty before calling
1492    #   _Modified() as an extra optimization.  So, if this method is ever
1493    #   changed such that it does stuff even when _cached_byte_size_dirty is
1494    #   already true, the callers need to be updated.
1495    if not self._cached_byte_size_dirty:
1496      self._cached_byte_size_dirty = True
1497      self._listener_for_children.dirty = True
1498      self._is_present_in_parent = True
1499      self._listener.Modified()

Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.

def IsInitialized(self, errors=None):
1262  def IsInitialized(self, errors=None):
1263    """Checks if all required fields of a message are set.
1264
1265    Args:
1266      errors:  A list which, if provided, will be populated with the field
1267               paths of all missing required fields.
1268
1269    Returns:
1270      True iff the specified message has all required fields set.
1271    """
1272
1273    # Performance is critical so we avoid HasField() and ListFields().
1274
1275    for field in required_fields:
1276      if (field not in self._fields or
1277          (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and
1278           not self._fields[field]._is_present_in_parent)):
1279        if errors is not None:
1280          errors.extend(self.FindInitializationErrors())
1281        return False
1282
1283    for field, value in list(self._fields.items()):  # dict can change size!
1284      if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
1285        if field.label == _FieldDescriptor.LABEL_REPEATED:
1286          if (field.message_type._is_map_entry):
1287            continue
1288          for element in value:
1289            if not element.IsInitialized():
1290              if errors is not None:
1291                errors.extend(self.FindInitializationErrors())
1292              return False
1293        elif value._is_present_in_parent and not value.IsInitialized():
1294          if errors is not None:
1295            errors.extend(self.FindInitializationErrors())
1296          return False
1297
1298    return True

Checks if all required fields of a message are set.

Arguments:
  • errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:

True iff the specified message has all required fields set.

def MergeFromString(self, serialized):
1177  def MergeFromString(self, serialized):
1178    serialized = memoryview(serialized)
1179    length = len(serialized)
1180    try:
1181      if self._InternalParse(serialized, 0, length) != length:
1182        # The only reason _InternalParse would return early is if it
1183        # encountered an end-group tag.
1184        raise message_mod.DecodeError('Unexpected end-group tag.')
1185    except (IndexError, TypeError):
1186      # Now ord(buf[p:p+1]) == ord('') gets TypeError.
1187      raise message_mod.DecodeError('Truncated message.')
1188    except struct.error as e:
1189      raise message_mod.DecodeError(e)
1190    return length   # Return this for legacy reasons.

Merges serialized protocol buffer data into this message.

When we find a field in serialized that is already present in this message:

  • If it's a "repeated" field, we append to the end of our list.
  • Else, if it's a scalar, we overwrite our field.
  • Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
  • serialized (bytes): Any object that allows us to call memoryview(serialized) to access a string of bytes using the buffer interface.
Returns:

int: The number of bytes read from serialized. For non-group messages, this will always be len(serialized), but for messages which are actually groups, this will generally be less than len(serialized), since we must stop when we reach an END_GROUP tag. Note that if we do stop because of an END_GROUP tag, the number of bytes returned does not include the bytes for the END_GROUP tag information.

Raises:
  • DecodeError: if the input cannot be parsed.
def SerializeToString(self, **kwargs):
1130  def SerializeToString(self, **kwargs):
1131    # Check if the message has all of its required fields set.
1132    if not self.IsInitialized():
1133      raise message_mod.EncodeError(
1134          'Message %s is missing required fields: %s' % (
1135          self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors())))
1136    return self.SerializePartialToString(**kwargs)

Serializes the protocol message to a binary string.

Keyword Args:

deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.

Returns:

A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).

Raises:
def SerializePartialToString(self, **kwargs):
1143  def SerializePartialToString(self, **kwargs):
1144    out = BytesIO()
1145    self._InternalSerialize(out.write, **kwargs)
1146    return out.getvalue()

Serializes the protocol message to a binary string.

This method is similar to SerializeToString but doesn't check if the message is initialized.

Keyword Args:

deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.

Returns:

bytes: A serialized representation of the partial message.

def ListFields(self):
848  def ListFields(self):
849    all_fields = [item for item in self._fields.items() if _IsPresent(item)]
850    all_fields.sort(key = lambda item: item[0].number)
851    return all_fields

Returns a list of (FieldDescriptor, value) tuples for present fields.

A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.

Returns:

list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.

def HasField(self, field_name):
872  def HasField(self, field_name):
873    try:
874      field = hassable_fields[field_name]
875    except KeyError as exc:
876      raise ValueError('Protocol message %s has no non-repeated field "%s" '
877                       'nor has presence is not available for this field.' % (
878                           message_descriptor.full_name, field_name)) from exc
879
880    if isinstance(field, descriptor_mod.OneofDescriptor):
881      try:
882        return HasField(self, self._oneofs[field].name)
883      except KeyError:
884        return False
885    else:
886      if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
887        value = self._fields.get(field)
888        return value is not None and value._is_present_in_parent
889      else:
890        return field in self._fields

Checks if a certain field is set for the message.

For a oneof group, checks if any field inside is set. Note that if the field_name is not defined in the message descriptor, ValueError will be raised.

Arguments:
  • field_name (str): The name of the field to check for presence.
Returns:

bool: Whether a value has been set for the named field.

Raises:
  • ValueError: if the field_name is not a member of this message.
def ClearField(self, field_name):
897  def ClearField(self, field_name):
898    try:
899      field = message_descriptor.fields_by_name[field_name]
900    except KeyError:
901      try:
902        field = message_descriptor.oneofs_by_name[field_name]
903        if field in self._oneofs:
904          field = self._oneofs[field]
905        else:
906          return
907      except KeyError:
908        raise ValueError('Protocol message %s has no "%s" field.' %
909                         (message_descriptor.name, field_name))
910
911    if field in self._fields:
912      # To match the C++ implementation, we need to invalidate iterators
913      # for map fields when ClearField() happens.
914      if hasattr(self._fields[field], 'InvalidateIterators'):
915        self._fields[field].InvalidateIterators()
916
917      # Note:  If the field is a sub-message, its listener will still point
918      #   at us.  That's fine, because the worst than can happen is that it
919      #   will call _Modified() and invalidate our byte size.  Big deal.
920      del self._fields[field]
921
922      if self._oneofs.get(field.containing_oneof, None) is field:
923        del self._oneofs[field.containing_oneof]
924
925    # Always call _Modified() -- even if nothing was changed, this is
926    # a mutating method, and thus calling it should cause the field to become
927    # present in the parent message.
928    self._Modified()

Clears the contents of a given field.

Inside a oneof group, clears the field set. If the name neither refers to a defined field or oneof group, ValueError is raised.

Arguments:
  • field_name (str): The name of the field to check for presence.
Raises:
  • ValueError: if the field_name is not a member of this message.
def WhichOneof(self, oneof_name):
1403  def WhichOneof(self, oneof_name):
1404    """Returns the name of the currently set field inside a oneof, or None."""
1405    try:
1406      field = message_descriptor.oneofs_by_name[oneof_name]
1407    except KeyError:
1408      raise ValueError(
1409          'Protocol message has no oneof "%s" field.' % oneof_name)
1410
1411    nested_field = self._oneofs.get(field, None)
1412    if nested_field is not None and self.HasField(nested_field.name):
1413      return nested_field.name
1414    else:
1415      return None

Returns the name of the currently set field inside a oneof, or None.

def DiscardUnknownFields(self):
1435def _DiscardUnknownFields(self):
1436  self._unknown_fields = []
1437  for field, value in self.ListFields():
1438    if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
1439      if _IsMapField(field):
1440        if _IsMessageMapField(field):
1441          for key in value:
1442            value[key].DiscardUnknownFields()
1443      elif field.label == _FieldDescriptor.LABEL_REPEATED:
1444        for sub_message in value:
1445          sub_message.DiscardUnknownFields()
1446      else:
1447        value.DiscardUnknownFields()

Clears all fields in the UnknownFieldSet.

This operation is recursive for nested message.

def ByteSize(self):
1098  def ByteSize(self):
1099    if not self._cached_byte_size_dirty:
1100      return self._cached_byte_size
1101
1102    size = 0
1103    descriptor = self.DESCRIPTOR
1104    if descriptor._is_map_entry:
1105      # Fields of map entry should always be serialized.
1106      key_field = descriptor.fields_by_name['key']
1107      _MaybeAddEncoder(cls, key_field)
1108      size = key_field._sizer(self.key)
1109      value_field = descriptor.fields_by_name['value']
1110      _MaybeAddEncoder(cls, value_field)
1111      size += value_field._sizer(self.value)
1112    else:
1113      for field_descriptor, field_value in self.ListFields():
1114        _MaybeAddEncoder(cls, field_descriptor)
1115        size += field_descriptor._sizer(field_value)
1116      for tag_bytes, value_bytes in self._unknown_fields:
1117        size += len(tag_bytes) + len(value_bytes)
1118
1119    self._cached_byte_size = size
1120    self._cached_byte_size_dirty = False
1121    self._listener_for_children.dirty = False
1122    return size

Returns the serialized size of this message.

Recursively calls ByteSize() on all contained messages.

Returns:

int: The number of bytes required to serialize this message.

def FromString(s):
826  def FromString(s):
827    message = cls()
828    message.MergeFromString(s)
829    return message
VAR_INDEX_FIELD_NUMBER = 1
var_index
668  def getter(self):
669    field_value = self._fields.get(field)
670    if field_value is None:
671      # Construct a new object to represent this field.
672      field_value = field._default_constructor(self)
673
674      # Atomically check if another thread has preempted us and, if not, swap
675      # in the new object we just created.  If someone has preempted us, we
676      # take that object and discard ours.
677      # WARNING:  We are relying on setdefault() being atomic.  This is true
678      #   in CPython but we haven't investigated others.  This warning appears
679      #   in several other locations in this file.
680      field_value = self._fields.setdefault(field, field_value)
681    return field_value

Getter for var_index.

CONSTANT_FIELD_NUMBER = 2
constant
711  def getter(self):
712    # TODO: This may be broken since there may not be
713    # default_value.  Combine with has_default_value somehow.
714    return self._fields.get(field, default_value)

Getter for constant.

RESULTANT_VAR_INDEX_FIELD_NUMBER = 3
resultant_var_index
711  def getter(self):
712    # TODO: This may be broken since there may not be
713    # default_value.  Combine with has_default_value somehow.
714    return self._fields.get(field, default_value)

Getter for resultant_var_index.

def FindInitializationErrors(self):
1302  def FindInitializationErrors(self):
1303    """Finds required fields which are not initialized.
1304
1305    Returns:
1306      A list of strings.  Each string is a path to an uninitialized field from
1307      the top-level message, e.g. "foo.bar[5].baz".
1308    """
1309
1310    errors = []  # simplify things
1311
1312    for field in required_fields:
1313      if not self.HasField(field.name):
1314        errors.append(field.name)
1315
1316    for field, value in self.ListFields():
1317      if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
1318        if field.is_extension:
1319          name = '(%s)' % field.full_name
1320        else:
1321          name = field.name
1322
1323        if _IsMapField(field):
1324          if _IsMessageMapField(field):
1325            for key in value:
1326              element = value[key]
1327              prefix = '%s[%s].' % (name, key)
1328              sub_errors = element.FindInitializationErrors()
1329              errors += [prefix + error for error in sub_errors]
1330          else:
1331            # ScalarMaps can't have any initialization errors.
1332            pass
1333        elif field.label == _FieldDescriptor.LABEL_REPEATED:
1334          for i in range(len(value)):
1335            element = value[i]
1336            prefix = '%s[%d].' % (name, i)
1337            sub_errors = element.FindInitializationErrors()
1338            errors += [prefix + error for error in sub_errors]
1339        else:
1340          prefix = name + '.'
1341          sub_errors = value.FindInitializationErrors()
1342          errors += [prefix + error for error in sub_errors]
1343
1344    return errors

Finds required fields which are not initialized.

Returns:

A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".

Inherited Members
google.protobuf.message.Message
CopyFrom
ParseFromString
HasExtension
ClearExtension
UnknownFields
class MPQuadraticObjective(google.protobuf.message.Message):

Abstract base class for protocol messages.

Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.

MPQuadraticObjective(**kwargs)
498  def init(self, **kwargs):
499    self._cached_byte_size = 0
500    self._cached_byte_size_dirty = len(kwargs) > 0
501    self._fields = {}
502    # Contains a mapping from oneof field descriptors to the descriptor
503    # of the currently set field in that oneof field.
504    self._oneofs = {}
505
506    # _unknown_fields is () when empty for efficiency, and will be turned into
507    # a list if fields are added.
508    self._unknown_fields = ()
509    self._is_present_in_parent = False
510    self._listener = message_listener_mod.NullMessageListener()
511    self._listener_for_children = _Listener(self)
512    for field_name, field_value in kwargs.items():
513      field = _GetFieldByName(message_descriptor, field_name)
514      if field is None:
515        raise TypeError('%s() got an unexpected keyword argument "%s"' %
516                        (message_descriptor.name, field_name))
517      if field_value is None:
518        # field=None is the same as no field at all.
519        continue
520      if field.label == _FieldDescriptor.LABEL_REPEATED:
521        field_copy = field._default_constructor(self)
522        if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:  # Composite
523          if _IsMapField(field):
524            if _IsMessageMapField(field):
525              for key in field_value:
526                field_copy[key].MergeFrom(field_value[key])
527            else:
528              field_copy.update(field_value)
529          else:
530            for val in field_value:
531              if isinstance(val, dict):
532                field_copy.add(**val)
533              else:
534                field_copy.add().MergeFrom(val)
535        else:  # Scalar
536          if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM:
537            field_value = [_GetIntegerEnumValue(field.enum_type, val)
538                           for val in field_value]
539          field_copy.extend(field_value)
540        self._fields[field] = field_copy
541      elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
542        field_copy = field._default_constructor(self)
543        new_val = None
544        if isinstance(field_value, message_mod.Message):
545          new_val = field_value
546        elif isinstance(field_value, dict):
547          if field.message_type.full_name == _StructFullTypeName:
548            field_copy.Clear()
549            if len(field_value) == 1 and 'fields' in field_value:
550              try:
551                field_copy.update(field_value)
552              except:
553                # Fall back to init normal message field
554                field_copy.Clear()
555                new_val = field.message_type._concrete_class(**field_value)
556            else:
557              field_copy.update(field_value)
558          else:
559            new_val = field.message_type._concrete_class(**field_value)
560        elif hasattr(field_copy, '_internal_assign'):
561          field_copy._internal_assign(field_value)
562        else:
563          raise TypeError(
564              'Message field {0}.{1} must be initialized with a '
565              'dict or instance of same class, got {2}.'.format(
566                  message_descriptor.name,
567                  field_name,
568                  type(field_value).__name__,
569              )
570          )
571
572        if new_val:
573          try:
574            field_copy.MergeFrom(new_val)
575          except TypeError:
576            _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
577        self._fields[field] = field_copy
578      else:
579        if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM:
580          field_value = _GetIntegerEnumValue(field.enum_type, field_value)
581        try:
582          setattr(self, field_name, field_value)
583        except TypeError:
584          _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
DESCRIPTOR = <google.protobuf.descriptor.Descriptor object>
def MergeFrom(self, msg):
1361  def MergeFrom(self, msg):
1362    if not isinstance(msg, cls):
1363      raise TypeError(
1364          'Parameter to MergeFrom() must be instance of same class: '
1365          'expected %s got %s.' % (_FullyQualifiedClassName(cls),
1366                                   _FullyQualifiedClassName(msg.__class__)))
1367
1368    assert msg is not self
1369    self._Modified()
1370
1371    fields = self._fields
1372
1373    for field, value in msg._fields.items():
1374      if field.label == LABEL_REPEATED:
1375        field_value = fields.get(field)
1376        if field_value is None:
1377          # Construct a new object to represent this field.
1378          field_value = field._default_constructor(self)
1379          fields[field] = field_value
1380        field_value.MergeFrom(value)
1381      elif field.cpp_type == CPPTYPE_MESSAGE:
1382        if value._is_present_in_parent:
1383          field_value = fields.get(field)
1384          if field_value is None:
1385            # Construct a new object to represent this field.
1386            field_value = field._default_constructor(self)
1387            fields[field] = field_value
1388          field_value.MergeFrom(value)
1389      else:
1390        self._fields[field] = value
1391        if field.containing_oneof:
1392          self._UpdateOneofState(field)
1393
1394    if msg._unknown_fields:
1395      if not self._unknown_fields:
1396        self._unknown_fields = []
1397      self._unknown_fields.extend(msg._unknown_fields)

Merges the contents of the specified message into current message.

This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.

Arguments:
  • other_msg (Message): A message to merge into the current message.
def Clear(self):
1420def _Clear(self):
1421  # Clear fields.
1422  self._fields = {}
1423  self._unknown_fields = ()
1424
1425  self._oneofs = {}
1426  self._Modified()

Clears all data that was set in the message.

def SetInParent(self):
1486  def Modified(self):
1487    """Sets the _cached_byte_size_dirty bit to true,
1488    and propagates this to our listener iff this was a state change.
1489    """
1490
1491    # Note:  Some callers check _cached_byte_size_dirty before calling
1492    #   _Modified() as an extra optimization.  So, if this method is ever
1493    #   changed such that it does stuff even when _cached_byte_size_dirty is
1494    #   already true, the callers need to be updated.
1495    if not self._cached_byte_size_dirty:
1496      self._cached_byte_size_dirty = True
1497      self._listener_for_children.dirty = True
1498      self._is_present_in_parent = True
1499      self._listener.Modified()

Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.

def IsInitialized(self, errors=None):
1262  def IsInitialized(self, errors=None):
1263    """Checks if all required fields of a message are set.
1264
1265    Args:
1266      errors:  A list which, if provided, will be populated with the field
1267               paths of all missing required fields.
1268
1269    Returns:
1270      True iff the specified message has all required fields set.
1271    """
1272
1273    # Performance is critical so we avoid HasField() and ListFields().
1274
1275    for field in required_fields:
1276      if (field not in self._fields or
1277          (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and
1278           not self._fields[field]._is_present_in_parent)):
1279        if errors is not None:
1280          errors.extend(self.FindInitializationErrors())
1281        return False
1282
1283    for field, value in list(self._fields.items()):  # dict can change size!
1284      if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
1285        if field.label == _FieldDescriptor.LABEL_REPEATED:
1286          if (field.message_type._is_map_entry):
1287            continue
1288          for element in value:
1289            if not element.IsInitialized():
1290              if errors is not None:
1291                errors.extend(self.FindInitializationErrors())
1292              return False
1293        elif value._is_present_in_parent and not value.IsInitialized():
1294          if errors is not None:
1295            errors.extend(self.FindInitializationErrors())
1296          return False
1297
1298    return True

Checks if all required fields of a message are set.

Arguments:
  • errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:

True iff the specified message has all required fields set.

def MergeFromString(self, serialized):
1177  def MergeFromString(self, serialized):
1178    serialized = memoryview(serialized)
1179    length = len(serialized)
1180    try:
1181      if self._InternalParse(serialized, 0, length) != length:
1182        # The only reason _InternalParse would return early is if it
1183        # encountered an end-group tag.
1184        raise message_mod.DecodeError('Unexpected end-group tag.')
1185    except (IndexError, TypeError):
1186      # Now ord(buf[p:p+1]) == ord('') gets TypeError.
1187      raise message_mod.DecodeError('Truncated message.')
1188    except struct.error as e:
1189      raise message_mod.DecodeError(e)
1190    return length   # Return this for legacy reasons.

Merges serialized protocol buffer data into this message.

When we find a field in serialized that is already present in this message:

  • If it's a "repeated" field, we append to the end of our list.
  • Else, if it's a scalar, we overwrite our field.
  • Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
  • serialized (bytes): Any object that allows us to call memoryview(serialized) to access a string of bytes using the buffer interface.
Returns:

int: The number of bytes read from serialized. For non-group messages, this will always be len(serialized), but for messages which are actually groups, this will generally be less than len(serialized), since we must stop when we reach an END_GROUP tag. Note that if we do stop because of an END_GROUP tag, the number of bytes returned does not include the bytes for the END_GROUP tag information.

Raises:
  • DecodeError: if the input cannot be parsed.
def SerializeToString(self, **kwargs):
1130  def SerializeToString(self, **kwargs):
1131    # Check if the message has all of its required fields set.
1132    if not self.IsInitialized():
1133      raise message_mod.EncodeError(
1134          'Message %s is missing required fields: %s' % (
1135          self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors())))
1136    return self.SerializePartialToString(**kwargs)

Serializes the protocol message to a binary string.

Keyword Args:

deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.

Returns:

A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).

Raises:
def SerializePartialToString(self, **kwargs):
1143  def SerializePartialToString(self, **kwargs):
1144    out = BytesIO()
1145    self._InternalSerialize(out.write, **kwargs)
1146    return out.getvalue()

Serializes the protocol message to a binary string.

This method is similar to SerializeToString but doesn't check if the message is initialized.

Keyword Args:

deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.

Returns:

bytes: A serialized representation of the partial message.

def ListFields(self):
848  def ListFields(self):
849    all_fields = [item for item in self._fields.items() if _IsPresent(item)]
850    all_fields.sort(key = lambda item: item[0].number)
851    return all_fields

Returns a list of (FieldDescriptor, value) tuples for present fields.

A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.

Returns:

list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.

def HasField(self, field_name):
872  def HasField(self, field_name):
873    try:
874      field = hassable_fields[field_name]
875    except KeyError as exc:
876      raise ValueError('Protocol message %s has no non-repeated field "%s" '
877                       'nor has presence is not available for this field.' % (
878                           message_descriptor.full_name, field_name)) from exc
879
880    if isinstance(field, descriptor_mod.OneofDescriptor):
881      try:
882        return HasField(self, self._oneofs[field].name)
883      except KeyError:
884        return False
885    else:
886      if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
887        value = self._fields.get(field)
888        return value is not None and value._is_present_in_parent
889      else:
890        return field in self._fields

Checks if a certain field is set for the message.

For a oneof group, checks if any field inside is set. Note that if the field_name is not defined in the message descriptor, ValueError will be raised.

Arguments:
  • field_name (str): The name of the field to check for presence.
Returns:

bool: Whether a value has been set for the named field.

Raises:
  • ValueError: if the field_name is not a member of this message.
def ClearField(self, field_name):
897  def ClearField(self, field_name):
898    try:
899      field = message_descriptor.fields_by_name[field_name]
900    except KeyError:
901      try:
902        field = message_descriptor.oneofs_by_name[field_name]
903        if field in self._oneofs:
904          field = self._oneofs[field]
905        else:
906          return
907      except KeyError:
908        raise ValueError('Protocol message %s has no "%s" field.' %
909                         (message_descriptor.name, field_name))
910
911    if field in self._fields:
912      # To match the C++ implementation, we need to invalidate iterators
913      # for map fields when ClearField() happens.
914      if hasattr(self._fields[field], 'InvalidateIterators'):
915        self._fields[field].InvalidateIterators()
916
917      # Note:  If the field is a sub-message, its listener will still point
918      #   at us.  That's fine, because the worst than can happen is that it
919      #   will call _Modified() and invalidate our byte size.  Big deal.
920      del self._fields[field]
921
922      if self._oneofs.get(field.containing_oneof, None) is field:
923        del self._oneofs[field.containing_oneof]
924
925    # Always call _Modified() -- even if nothing was changed, this is
926    # a mutating method, and thus calling it should cause the field to become
927    # present in the parent message.
928    self._Modified()

Clears the contents of a given field.

Inside a oneof group, clears the field set. If the name neither refers to a defined field or oneof group, ValueError is raised.

Arguments:
  • field_name (str): The name of the field to check for presence.
Raises:
  • ValueError: if the field_name is not a member of this message.
def WhichOneof(self, oneof_name):
1403  def WhichOneof(self, oneof_name):
1404    """Returns the name of the currently set field inside a oneof, or None."""
1405    try:
1406      field = message_descriptor.oneofs_by_name[oneof_name]
1407    except KeyError:
1408      raise ValueError(
1409          'Protocol message has no oneof "%s" field.' % oneof_name)
1410
1411    nested_field = self._oneofs.get(field, None)
1412    if nested_field is not None and self.HasField(nested_field.name):
1413      return nested_field.name
1414    else:
1415      return None

Returns the name of the currently set field inside a oneof, or None.

def DiscardUnknownFields(self):
1435def _DiscardUnknownFields(self):
1436  self._unknown_fields = []
1437  for field, value in self.ListFields():
1438    if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
1439      if _IsMapField(field):
1440        if _IsMessageMapField(field):
1441          for key in value:
1442            value[key].DiscardUnknownFields()
1443      elif field.label == _FieldDescriptor.LABEL_REPEATED:
1444        for sub_message in value:
1445          sub_message.DiscardUnknownFields()
1446      else:
1447        value.DiscardUnknownFields()

Clears all fields in the UnknownFieldSet.

This operation is recursive for nested message.

def ByteSize(self):
1098  def ByteSize(self):
1099    if not self._cached_byte_size_dirty:
1100      return self._cached_byte_size
1101
1102    size = 0
1103    descriptor = self.DESCRIPTOR
1104    if descriptor._is_map_entry:
1105      # Fields of map entry should always be serialized.
1106      key_field = descriptor.fields_by_name['key']
1107      _MaybeAddEncoder(cls, key_field)
1108      size = key_field._sizer(self.key)
1109      value_field = descriptor.fields_by_name['value']
1110      _MaybeAddEncoder(cls, value_field)
1111      size += value_field._sizer(self.value)
1112    else:
1113      for field_descriptor, field_value in self.ListFields():
1114        _MaybeAddEncoder(cls, field_descriptor)
1115        size += field_descriptor._sizer(field_value)
1116      for tag_bytes, value_bytes in self._unknown_fields:
1117        size += len(tag_bytes) + len(value_bytes)
1118
1119    self._cached_byte_size = size
1120    self._cached_byte_size_dirty = False
1121    self._listener_for_children.dirty = False
1122    return size

Returns the serialized size of this message.

Recursively calls ByteSize() on all contained messages.

Returns:

int: The number of bytes required to serialize this message.

def FromString(s):
826  def FromString(s):
827    message = cls()
828    message.MergeFromString(s)
829    return message
QVAR1_INDEX_FIELD_NUMBER = 1
qvar1_index
668  def getter(self):
669    field_value = self._fields.get(field)
670    if field_value is None:
671      # Construct a new object to represent this field.
672      field_value = field._default_constructor(self)
673
674      # Atomically check if another thread has preempted us and, if not, swap
675      # in the new object we just created.  If someone has preempted us, we
676      # take that object and discard ours.
677      # WARNING:  We are relying on setdefault() being atomic.  This is true
678      #   in CPython but we haven't investigated others.  This warning appears
679      #   in several other locations in this file.
680      field_value = self._fields.setdefault(field, field_value)
681    return field_value

Getter for qvar1_index.

QVAR2_INDEX_FIELD_NUMBER = 2
qvar2_index
668  def getter(self):
669    field_value = self._fields.get(field)
670    if field_value is None:
671      # Construct a new object to represent this field.
672      field_value = field._default_constructor(self)
673
674      # Atomically check if another thread has preempted us and, if not, swap
675      # in the new object we just created.  If someone has preempted us, we
676      # take that object and discard ours.
677      # WARNING:  We are relying on setdefault() being atomic.  This is true
678      #   in CPython but we haven't investigated others.  This warning appears
679      #   in several other locations in this file.
680      field_value = self._fields.setdefault(field, field_value)
681    return field_value

Getter for qvar2_index.

COEFFICIENT_FIELD_NUMBER = 3
coefficient
668  def getter(self):
669    field_value = self._fields.get(field)
670    if field_value is None:
671      # Construct a new object to represent this field.
672      field_value = field._default_constructor(self)
673
674      # Atomically check if another thread has preempted us and, if not, swap
675      # in the new object we just created.  If someone has preempted us, we
676      # take that object and discard ours.
677      # WARNING:  We are relying on setdefault() being atomic.  This is true
678      #   in CPython but we haven't investigated others.  This warning appears
679      #   in several other locations in this file.
680      field_value = self._fields.setdefault(field, field_value)
681    return field_value

Getter for coefficient.

def FindInitializationErrors(self):
1302  def FindInitializationErrors(self):
1303    """Finds required fields which are not initialized.
1304
1305    Returns:
1306      A list of strings.  Each string is a path to an uninitialized field from
1307      the top-level message, e.g. "foo.bar[5].baz".
1308    """
1309
1310    errors = []  # simplify things
1311
1312    for field in required_fields:
1313      if not self.HasField(field.name):
1314        errors.append(field.name)
1315
1316    for field, value in self.ListFields():
1317      if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
1318        if field.is_extension:
1319          name = '(%s)' % field.full_name
1320        else:
1321          name = field.name
1322
1323        if _IsMapField(field):
1324          if _IsMessageMapField(field):
1325            for key in value:
1326              element = value[key]
1327              prefix = '%s[%s].' % (name, key)
1328              sub_errors = element.FindInitializationErrors()
1329              errors += [prefix + error for error in sub_errors]
1330          else:
1331            # ScalarMaps can't have any initialization errors.
1332            pass
1333        elif field.label == _FieldDescriptor.LABEL_REPEATED:
1334          for i in range(len(value)):
1335            element = value[i]
1336            prefix = '%s[%d].' % (name, i)
1337            sub_errors = element.FindInitializationErrors()
1338            errors += [prefix + error for error in sub_errors]
1339        else:
1340          prefix = name + '.'
1341          sub_errors = value.FindInitializationErrors()
1342          errors += [prefix + error for error in sub_errors]
1343
1344    return errors

Finds required fields which are not initialized.

Returns:

A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".

Inherited Members
google.protobuf.message.Message
CopyFrom
ParseFromString
HasExtension
ClearExtension
UnknownFields
class PartialVariableAssignment(google.protobuf.message.Message):

Abstract base class for protocol messages.

Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.

PartialVariableAssignment(**kwargs)
498  def init(self, **kwargs):
499    self._cached_byte_size = 0
500    self._cached_byte_size_dirty = len(kwargs) > 0
501    self._fields = {}
502    # Contains a mapping from oneof field descriptors to the descriptor
503    # of the currently set field in that oneof field.
504    self._oneofs = {}
505
506    # _unknown_fields is () when empty for efficiency, and will be turned into
507    # a list if fields are added.
508    self._unknown_fields = ()
509    self._is_present_in_parent = False
510    self._listener = message_listener_mod.NullMessageListener()
511    self._listener_for_children = _Listener(self)
512    for field_name, field_value in kwargs.items():
513      field = _GetFieldByName(message_descriptor, field_name)
514      if field is None:
515        raise TypeError('%s() got an unexpected keyword argument "%s"' %
516                        (message_descriptor.name, field_name))
517      if field_value is None:
518        # field=None is the same as no field at all.
519        continue
520      if field.label == _FieldDescriptor.LABEL_REPEATED:
521        field_copy = field._default_constructor(self)
522        if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:  # Composite
523          if _IsMapField(field):
524            if _IsMessageMapField(field):
525              for key in field_value:
526                field_copy[key].MergeFrom(field_value[key])
527            else:
528              field_copy.update(field_value)
529          else:
530            for val in field_value:
531              if isinstance(val, dict):
532                field_copy.add(**val)
533              else:
534                field_copy.add().MergeFrom(val)
535        else:  # Scalar
536          if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM:
537            field_value = [_GetIntegerEnumValue(field.enum_type, val)
538                           for val in field_value]
539          field_copy.extend(field_value)
540        self._fields[field] = field_copy
541      elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
542        field_copy = field._default_constructor(self)
543        new_val = None
544        if isinstance(field_value, message_mod.Message):
545          new_val = field_value
546        elif isinstance(field_value, dict):
547          if field.message_type.full_name == _StructFullTypeName:
548            field_copy.Clear()
549            if len(field_value) == 1 and 'fields' in field_value:
550              try:
551                field_copy.update(field_value)
552              except:
553                # Fall back to init normal message field
554                field_copy.Clear()
555                new_val = field.message_type._concrete_class(**field_value)
556            else:
557              field_copy.update(field_value)
558          else:
559            new_val = field.message_type._concrete_class(**field_value)
560        elif hasattr(field_copy, '_internal_assign'):
561          field_copy._internal_assign(field_value)
562        else:
563          raise TypeError(
564              'Message field {0}.{1} must be initialized with a '
565              'dict or instance of same class, got {2}.'.format(
566                  message_descriptor.name,
567                  field_name,
568                  type(field_value).__name__,
569              )
570          )
571
572        if new_val:
573          try:
574            field_copy.MergeFrom(new_val)
575          except TypeError:
576            _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
577        self._fields[field] = field_copy
578      else:
579        if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM:
580          field_value = _GetIntegerEnumValue(field.enum_type, field_value)
581        try:
582          setattr(self, field_name, field_value)
583        except TypeError:
584          _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
DESCRIPTOR = <google.protobuf.descriptor.Descriptor object>
def MergeFrom(self, msg):
1361  def MergeFrom(self, msg):
1362    if not isinstance(msg, cls):
1363      raise TypeError(
1364          'Parameter to MergeFrom() must be instance of same class: '
1365          'expected %s got %s.' % (_FullyQualifiedClassName(cls),
1366                                   _FullyQualifiedClassName(msg.__class__)))
1367
1368    assert msg is not self
1369    self._Modified()
1370
1371    fields = self._fields
1372
1373    for field, value in msg._fields.items():
1374      if field.label == LABEL_REPEATED:
1375        field_value = fields.get(field)
1376        if field_value is None:
1377          # Construct a new object to represent this field.
1378          field_value = field._default_constructor(self)
1379          fields[field] = field_value
1380        field_value.MergeFrom(value)
1381      elif field.cpp_type == CPPTYPE_MESSAGE:
1382        if value._is_present_in_parent:
1383          field_value = fields.get(field)
1384          if field_value is None:
1385            # Construct a new object to represent this field.
1386            field_value = field._default_constructor(self)
1387            fields[field] = field_value
1388          field_value.MergeFrom(value)
1389      else:
1390        self._fields[field] = value
1391        if field.containing_oneof:
1392          self._UpdateOneofState(field)
1393
1394    if msg._unknown_fields:
1395      if not self._unknown_fields:
1396        self._unknown_fields = []
1397      self._unknown_fields.extend(msg._unknown_fields)

Merges the contents of the specified message into current message.

This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.

Arguments:
  • other_msg (Message): A message to merge into the current message.
def Clear(self):
1420def _Clear(self):
1421  # Clear fields.
1422  self._fields = {}
1423  self._unknown_fields = ()
1424
1425  self._oneofs = {}
1426  self._Modified()

Clears all data that was set in the message.

def SetInParent(self):
1486  def Modified(self):
1487    """Sets the _cached_byte_size_dirty bit to true,
1488    and propagates this to our listener iff this was a state change.
1489    """
1490
1491    # Note:  Some callers check _cached_byte_size_dirty before calling
1492    #   _Modified() as an extra optimization.  So, if this method is ever
1493    #   changed such that it does stuff even when _cached_byte_size_dirty is
1494    #   already true, the callers need to be updated.
1495    if not self._cached_byte_size_dirty:
1496      self._cached_byte_size_dirty = True
1497      self._listener_for_children.dirty = True
1498      self._is_present_in_parent = True
1499      self._listener.Modified()

Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.

def IsInitialized(self, errors=None):
1262  def IsInitialized(self, errors=None):
1263    """Checks if all required fields of a message are set.
1264
1265    Args:
1266      errors:  A list which, if provided, will be populated with the field
1267               paths of all missing required fields.
1268
1269    Returns:
1270      True iff the specified message has all required fields set.
1271    """
1272
1273    # Performance is critical so we avoid HasField() and ListFields().
1274
1275    for field in required_fields:
1276      if (field not in self._fields or
1277          (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and
1278           not self._fields[field]._is_present_in_parent)):
1279        if errors is not None:
1280          errors.extend(self.FindInitializationErrors())
1281        return False
1282
1283    for field, value in list(self._fields.items()):  # dict can change size!
1284      if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
1285        if field.label == _FieldDescriptor.LABEL_REPEATED:
1286          if (field.message_type._is_map_entry):
1287            continue
1288          for element in value:
1289            if not element.IsInitialized():
1290              if errors is not None:
1291                errors.extend(self.FindInitializationErrors())
1292              return False
1293        elif value._is_present_in_parent and not value.IsInitialized():
1294          if errors is not None:
1295            errors.extend(self.FindInitializationErrors())
1296          return False
1297
1298    return True

Checks if all required fields of a message are set.

Arguments:
  • errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:

True iff the specified message has all required fields set.

def MergeFromString(self, serialized):
1177  def MergeFromString(self, serialized):
1178    serialized = memoryview(serialized)
1179    length = len(serialized)
1180    try:
1181      if self._InternalParse(serialized, 0, length) != length:
1182        # The only reason _InternalParse would return early is if it
1183        # encountered an end-group tag.
1184        raise message_mod.DecodeError('Unexpected end-group tag.')
1185    except (IndexError, TypeError):
1186      # Now ord(buf[p:p+1]) == ord('') gets TypeError.
1187      raise message_mod.DecodeError('Truncated message.')
1188    except struct.error as e:
1189      raise message_mod.DecodeError(e)
1190    return length   # Return this for legacy reasons.

Merges serialized protocol buffer data into this message.

When we find a field in serialized that is already present in this message:

  • If it's a "repeated" field, we append to the end of our list.
  • Else, if it's a scalar, we overwrite our field.
  • Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
  • serialized (bytes): Any object that allows us to call memoryview(serialized) to access a string of bytes using the buffer interface.
Returns:

int: The number of bytes read from serialized. For non-group messages, this will always be len(serialized), but for messages which are actually groups, this will generally be less than len(serialized), since we must stop when we reach an END_GROUP tag. Note that if we do stop because of an END_GROUP tag, the number of bytes returned does not include the bytes for the END_GROUP tag information.

Raises:
  • DecodeError: if the input cannot be parsed.
def SerializeToString(self, **kwargs):
1130  def SerializeToString(self, **kwargs):
1131    # Check if the message has all of its required fields set.
1132    if not self.IsInitialized():
1133      raise message_mod.EncodeError(
1134          'Message %s is missing required fields: %s' % (
1135          self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors())))
1136    return self.SerializePartialToString(**kwargs)

Serializes the protocol message to a binary string.

Keyword Args:

deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.

Returns:

A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).

Raises:
def SerializePartialToString(self, **kwargs):
1143  def SerializePartialToString(self, **kwargs):
1144    out = BytesIO()
1145    self._InternalSerialize(out.write, **kwargs)
1146    return out.getvalue()

Serializes the protocol message to a binary string.

This method is similar to SerializeToString but doesn't check if the message is initialized.

Keyword Args:

deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.

Returns:

bytes: A serialized representation of the partial message.

def ListFields(self):
848  def ListFields(self):
849    all_fields = [item for item in self._fields.items() if _IsPresent(item)]
850    all_fields.sort(key = lambda item: item[0].number)
851    return all_fields

Returns a list of (FieldDescriptor, value) tuples for present fields.

A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.

Returns:

list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.

def HasField(self, field_name):
872  def HasField(self, field_name):
873    try:
874      field = hassable_fields[field_name]
875    except KeyError as exc:
876      raise ValueError('Protocol message %s has no non-repeated field "%s" '
877                       'nor has presence is not available for this field.' % (
878                           message_descriptor.full_name, field_name)) from exc
879
880    if isinstance(field, descriptor_mod.OneofDescriptor):
881      try:
882        return HasField(self, self._oneofs[field].name)
883      except KeyError:
884        return False
885    else:
886      if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
887        value = self._fields.get(field)
888        return value is not None and value._is_present_in_parent
889      else:
890        return field in self._fields

Checks if a certain field is set for the message.

For a oneof group, checks if any field inside is set. Note that if the field_name is not defined in the message descriptor, ValueError will be raised.

Arguments:
  • field_name (str): The name of the field to check for presence.
Returns:

bool: Whether a value has been set for the named field.

Raises:
  • ValueError: if the field_name is not a member of this message.
def ClearField(self, field_name):
897  def ClearField(self, field_name):
898    try:
899      field = message_descriptor.fields_by_name[field_name]
900    except KeyError:
901      try:
902        field = message_descriptor.oneofs_by_name[field_name]
903        if field in self._oneofs:
904          field = self._oneofs[field]
905        else:
906          return
907      except KeyError:
908        raise ValueError('Protocol message %s has no "%s" field.' %
909                         (message_descriptor.name, field_name))
910
911    if field in self._fields:
912      # To match the C++ implementation, we need to invalidate iterators
913      # for map fields when ClearField() happens.
914      if hasattr(self._fields[field], 'InvalidateIterators'):
915        self._fields[field].InvalidateIterators()
916
917      # Note:  If the field is a sub-message, its listener will still point
918      #   at us.  That's fine, because the worst than can happen is that it
919      #   will call _Modified() and invalidate our byte size.  Big deal.
920      del self._fields[field]
921
922      if self._oneofs.get(field.containing_oneof, None) is field:
923        del self._oneofs[field.containing_oneof]
924
925    # Always call _Modified() -- even if nothing was changed, this is
926    # a mutating method, and thus calling it should cause the field to become
927    # present in the parent message.
928    self._Modified()

Clears the contents of a given field.

Inside a oneof group, clears the field set. If the name neither refers to a defined field or oneof group, ValueError is raised.

Arguments:
  • field_name (str): The name of the field to check for presence.
Raises:
  • ValueError: if the field_name is not a member of this message.
def WhichOneof(self, oneof_name):
1403  def WhichOneof(self, oneof_name):
1404    """Returns the name of the currently set field inside a oneof, or None."""
1405    try:
1406      field = message_descriptor.oneofs_by_name[oneof_name]
1407    except KeyError:
1408      raise ValueError(
1409          'Protocol message has no oneof "%s" field.' % oneof_name)
1410
1411    nested_field = self._oneofs.get(field, None)
1412    if nested_field is not None and self.HasField(nested_field.name):
1413      return nested_field.name
1414    else:
1415      return None

Returns the name of the currently set field inside a oneof, or None.

def DiscardUnknownFields(self):
1435def _DiscardUnknownFields(self):
1436  self._unknown_fields = []
1437  for field, value in self.ListFields():
1438    if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
1439      if _IsMapField(field):
1440        if _IsMessageMapField(field):
1441          for key in value:
1442            value[key].DiscardUnknownFields()
1443      elif field.label == _FieldDescriptor.LABEL_REPEATED:
1444        for sub_message in value:
1445          sub_message.DiscardUnknownFields()
1446      else:
1447        value.DiscardUnknownFields()

Clears all fields in the UnknownFieldSet.

This operation is recursive for nested message.

def ByteSize(self):
1098  def ByteSize(self):
1099    if not self._cached_byte_size_dirty:
1100      return self._cached_byte_size
1101
1102    size = 0
1103    descriptor = self.DESCRIPTOR
1104    if descriptor._is_map_entry:
1105      # Fields of map entry should always be serialized.
1106      key_field = descriptor.fields_by_name['key']
1107      _MaybeAddEncoder(cls, key_field)
1108      size = key_field._sizer(self.key)
1109      value_field = descriptor.fields_by_name['value']
1110      _MaybeAddEncoder(cls, value_field)
1111      size += value_field._sizer(self.value)
1112    else:
1113      for field_descriptor, field_value in self.ListFields():
1114        _MaybeAddEncoder(cls, field_descriptor)
1115        size += field_descriptor._sizer(field_value)
1116      for tag_bytes, value_bytes in self._unknown_fields:
1117        size += len(tag_bytes) + len(value_bytes)
1118
1119    self._cached_byte_size = size
1120    self._cached_byte_size_dirty = False
1121    self._listener_for_children.dirty = False
1122    return size

Returns the serialized size of this message.

Recursively calls ByteSize() on all contained messages.

Returns:

int: The number of bytes required to serialize this message.

def FromString(s):
826  def FromString(s):
827    message = cls()
828    message.MergeFromString(s)
829    return message
VAR_INDEX_FIELD_NUMBER = 1
var_index
668  def getter(self):
669    field_value = self._fields.get(field)
670    if field_value is None:
671      # Construct a new object to represent this field.
672      field_value = field._default_constructor(self)
673
674      # Atomically check if another thread has preempted us and, if not, swap
675      # in the new object we just created.  If someone has preempted us, we
676      # take that object and discard ours.
677      # WARNING:  We are relying on setdefault() being atomic.  This is true
678      #   in CPython but we haven't investigated others.  This warning appears
679      #   in several other locations in this file.
680      field_value = self._fields.setdefault(field, field_value)
681    return field_value

Getter for var_index.

VAR_VALUE_FIELD_NUMBER = 2
var_value
668  def getter(self):
669    field_value = self._fields.get(field)
670    if field_value is None:
671      # Construct a new object to represent this field.
672      field_value = field._default_constructor(self)
673
674      # Atomically check if another thread has preempted us and, if not, swap
675      # in the new object we just created.  If someone has preempted us, we
676      # take that object and discard ours.
677      # WARNING:  We are relying on setdefault() being atomic.  This is true
678      #   in CPython but we haven't investigated others.  This warning appears
679      #   in several other locations in this file.
680      field_value = self._fields.setdefault(field, field_value)
681    return field_value

Getter for var_value.

def FindInitializationErrors(self):
1302  def FindInitializationErrors(self):
1303    """Finds required fields which are not initialized.
1304
1305    Returns:
1306      A list of strings.  Each string is a path to an uninitialized field from
1307      the top-level message, e.g. "foo.bar[5].baz".
1308    """
1309
1310    errors = []  # simplify things
1311
1312    for field in required_fields:
1313      if not self.HasField(field.name):
1314        errors.append(field.name)
1315
1316    for field, value in self.ListFields():
1317      if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
1318        if field.is_extension:
1319          name = '(%s)' % field.full_name
1320        else:
1321          name = field.name
1322
1323        if _IsMapField(field):
1324          if _IsMessageMapField(field):
1325            for key in value:
1326              element = value[key]
1327              prefix = '%s[%s].' % (name, key)
1328              sub_errors = element.FindInitializationErrors()
1329              errors += [prefix + error for error in sub_errors]
1330          else:
1331            # ScalarMaps can't have any initialization errors.
1332            pass
1333        elif field.label == _FieldDescriptor.LABEL_REPEATED:
1334          for i in range(len(value)):
1335            element = value[i]
1336            prefix = '%s[%d].' % (name, i)
1337            sub_errors = element.FindInitializationErrors()
1338            errors += [prefix + error for error in sub_errors]
1339        else:
1340          prefix = name + '.'
1341          sub_errors = value.FindInitializationErrors()
1342          errors += [prefix + error for error in sub_errors]
1343
1344    return errors

Finds required fields which are not initialized.

Returns:

A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".

Inherited Members
google.protobuf.message.Message
CopyFrom
ParseFromString
HasExtension
ClearExtension
UnknownFields
class MPModelProto(google.protobuf.message.Message):

Abstract base class for protocol messages.

Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.

MPModelProto(**kwargs)
498  def init(self, **kwargs):
499    self._cached_byte_size = 0
500    self._cached_byte_size_dirty = len(kwargs) > 0
501    self._fields = {}
502    # Contains a mapping from oneof field descriptors to the descriptor
503    # of the currently set field in that oneof field.
504    self._oneofs = {}
505
506    # _unknown_fields is () when empty for efficiency, and will be turned into
507    # a list if fields are added.
508    self._unknown_fields = ()
509    self._is_present_in_parent = False
510    self._listener = message_listener_mod.NullMessageListener()
511    self._listener_for_children = _Listener(self)
512    for field_name, field_value in kwargs.items():
513      field = _GetFieldByName(message_descriptor, field_name)
514      if field is None:
515        raise TypeError('%s() got an unexpected keyword argument "%s"' %
516                        (message_descriptor.name, field_name))
517      if field_value is None:
518        # field=None is the same as no field at all.
519        continue
520      if field.label == _FieldDescriptor.LABEL_REPEATED:
521        field_copy = field._default_constructor(self)
522        if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:  # Composite
523          if _IsMapField(field):
524            if _IsMessageMapField(field):
525              for key in field_value:
526                field_copy[key].MergeFrom(field_value[key])
527            else:
528              field_copy.update(field_value)
529          else:
530            for val in field_value:
531              if isinstance(val, dict):
532                field_copy.add(**val)
533              else:
534                field_copy.add().MergeFrom(val)
535        else:  # Scalar
536          if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM:
537            field_value = [_GetIntegerEnumValue(field.enum_type, val)
538                           for val in field_value]
539          field_copy.extend(field_value)
540        self._fields[field] = field_copy
541      elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
542        field_copy = field._default_constructor(self)
543        new_val = None
544        if isinstance(field_value, message_mod.Message):
545          new_val = field_value
546        elif isinstance(field_value, dict):
547          if field.message_type.full_name == _StructFullTypeName:
548            field_copy.Clear()
549            if len(field_value) == 1 and 'fields' in field_value:
550              try:
551                field_copy.update(field_value)
552              except:
553                # Fall back to init normal message field
554                field_copy.Clear()
555                new_val = field.message_type._concrete_class(**field_value)
556            else:
557              field_copy.update(field_value)
558          else:
559            new_val = field.message_type._concrete_class(**field_value)
560        elif hasattr(field_copy, '_internal_assign'):
561          field_copy._internal_assign(field_value)
562        else:
563          raise TypeError(
564              'Message field {0}.{1} must be initialized with a '
565              'dict or instance of same class, got {2}.'.format(
566                  message_descriptor.name,
567                  field_name,
568                  type(field_value).__name__,
569              )
570          )
571
572        if new_val:
573          try:
574            field_copy.MergeFrom(new_val)
575          except TypeError:
576            _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
577        self._fields[field] = field_copy
578      else:
579        if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM:
580          field_value = _GetIntegerEnumValue(field.enum_type, field_value)
581        try:
582          setattr(self, field_name, field_value)
583        except TypeError:
584          _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
DESCRIPTOR = <google.protobuf.descriptor.Descriptor object>
def MergeFrom(self, msg):
1361  def MergeFrom(self, msg):
1362    if not isinstance(msg, cls):
1363      raise TypeError(
1364          'Parameter to MergeFrom() must be instance of same class: '
1365          'expected %s got %s.' % (_FullyQualifiedClassName(cls),
1366                                   _FullyQualifiedClassName(msg.__class__)))
1367
1368    assert msg is not self
1369    self._Modified()
1370
1371    fields = self._fields
1372
1373    for field, value in msg._fields.items():
1374      if field.label == LABEL_REPEATED:
1375        field_value = fields.get(field)
1376        if field_value is None:
1377          # Construct a new object to represent this field.
1378          field_value = field._default_constructor(self)
1379          fields[field] = field_value
1380        field_value.MergeFrom(value)
1381      elif field.cpp_type == CPPTYPE_MESSAGE:
1382        if value._is_present_in_parent:
1383          field_value = fields.get(field)
1384          if field_value is None:
1385            # Construct a new object to represent this field.
1386            field_value = field._default_constructor(self)
1387            fields[field] = field_value
1388          field_value.MergeFrom(value)
1389      else:
1390        self._fields[field] = value
1391        if field.containing_oneof:
1392          self._UpdateOneofState(field)
1393
1394    if msg._unknown_fields:
1395      if not self._unknown_fields:
1396        self._unknown_fields = []
1397      self._unknown_fields.extend(msg._unknown_fields)

Merges the contents of the specified message into current message.

This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.

Arguments:
  • other_msg (Message): A message to merge into the current message.
def Clear(self):
1420def _Clear(self):
1421  # Clear fields.
1422  self._fields = {}
1423  self._unknown_fields = ()
1424
1425  self._oneofs = {}
1426  self._Modified()

Clears all data that was set in the message.

def SetInParent(self):
1486  def Modified(self):
1487    """Sets the _cached_byte_size_dirty bit to true,
1488    and propagates this to our listener iff this was a state change.
1489    """
1490
1491    # Note:  Some callers check _cached_byte_size_dirty before calling
1492    #   _Modified() as an extra optimization.  So, if this method is ever
1493    #   changed such that it does stuff even when _cached_byte_size_dirty is
1494    #   already true, the callers need to be updated.
1495    if not self._cached_byte_size_dirty:
1496      self._cached_byte_size_dirty = True
1497      self._listener_for_children.dirty = True
1498      self._is_present_in_parent = True
1499      self._listener.Modified()

Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.

def IsInitialized(self, errors=None):
1262  def IsInitialized(self, errors=None):
1263    """Checks if all required fields of a message are set.
1264
1265    Args:
1266      errors:  A list which, if provided, will be populated with the field
1267               paths of all missing required fields.
1268
1269    Returns:
1270      True iff the specified message has all required fields set.
1271    """
1272
1273    # Performance is critical so we avoid HasField() and ListFields().
1274
1275    for field in required_fields:
1276      if (field not in self._fields or
1277          (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and
1278           not self._fields[field]._is_present_in_parent)):
1279        if errors is not None:
1280          errors.extend(self.FindInitializationErrors())
1281        return False
1282
1283    for field, value in list(self._fields.items()):  # dict can change size!
1284      if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
1285        if field.label == _FieldDescriptor.LABEL_REPEATED:
1286          if (field.message_type._is_map_entry):
1287            continue
1288          for element in value:
1289            if not element.IsInitialized():
1290              if errors is not None:
1291                errors.extend(self.FindInitializationErrors())
1292              return False
1293        elif value._is_present_in_parent and not value.IsInitialized():
1294          if errors is not None:
1295            errors.extend(self.FindInitializationErrors())
1296          return False
1297
1298    return True

Checks if all required fields of a message are set.

Arguments:
  • errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:

True iff the specified message has all required fields set.

def MergeFromString(self, serialized):
1177  def MergeFromString(self, serialized):
1178    serialized = memoryview(serialized)
1179    length = len(serialized)
1180    try:
1181      if self._InternalParse(serialized, 0, length) != length:
1182        # The only reason _InternalParse would return early is if it
1183        # encountered an end-group tag.
1184        raise message_mod.DecodeError('Unexpected end-group tag.')
1185    except (IndexError, TypeError):
1186      # Now ord(buf[p:p+1]) == ord('') gets TypeError.
1187      raise message_mod.DecodeError('Truncated message.')
1188    except struct.error as e:
1189      raise message_mod.DecodeError(e)
1190    return length   # Return this for legacy reasons.

Merges serialized protocol buffer data into this message.

When we find a field in serialized that is already present in this message:

  • If it's a "repeated" field, we append to the end of our list.
  • Else, if it's a scalar, we overwrite our field.
  • Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
  • serialized (bytes): Any object that allows us to call memoryview(serialized) to access a string of bytes using the buffer interface.
Returns:

int: The number of bytes read from serialized. For non-group messages, this will always be len(serialized), but for messages which are actually groups, this will generally be less than len(serialized), since we must stop when we reach an END_GROUP tag. Note that if we do stop because of an END_GROUP tag, the number of bytes returned does not include the bytes for the END_GROUP tag information.

Raises:
  • DecodeError: if the input cannot be parsed.
def SerializeToString(self, **kwargs):
1130  def SerializeToString(self, **kwargs):
1131    # Check if the message has all of its required fields set.
1132    if not self.IsInitialized():
1133      raise message_mod.EncodeError(
1134          'Message %s is missing required fields: %s' % (
1135          self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors())))
1136    return self.SerializePartialToString(**kwargs)

Serializes the protocol message to a binary string.

Keyword Args:

deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.

Returns:

A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).

Raises:
def SerializePartialToString(self, **kwargs):
1143  def SerializePartialToString(self, **kwargs):
1144    out = BytesIO()
1145    self._InternalSerialize(out.write, **kwargs)
1146    return out.getvalue()

Serializes the protocol message to a binary string.

This method is similar to SerializeToString but doesn't check if the message is initialized.

Keyword Args:

deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.

Returns:

bytes: A serialized representation of the partial message.

def ListFields(self):
848  def ListFields(self):
849    all_fields = [item for item in self._fields.items() if _IsPresent(item)]
850    all_fields.sort(key = lambda item: item[0].number)
851    return all_fields

Returns a list of (FieldDescriptor, value) tuples for present fields.

A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.

Returns:

list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.

def HasField(self, field_name):
872  def HasField(self, field_name):
873    try:
874      field = hassable_fields[field_name]
875    except KeyError as exc:
876      raise ValueError('Protocol message %s has no non-repeated field "%s" '
877                       'nor has presence is not available for this field.' % (
878                           message_descriptor.full_name, field_name)) from exc
879
880    if isinstance(field, descriptor_mod.OneofDescriptor):
881      try:
882        return HasField(self, self._oneofs[field].name)
883      except KeyError:
884        return False
885    else:
886      if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
887        value = self._fields.get(field)
888        return value is not None and value._is_present_in_parent
889      else:
890        return field in self._fields

Checks if a certain field is set for the message.

For a oneof group, checks if any field inside is set. Note that if the field_name is not defined in the message descriptor, ValueError will be raised.

Arguments:
  • field_name (str): The name of the field to check for presence.
Returns:

bool: Whether a value has been set for the named field.

Raises:
  • ValueError: if the field_name is not a member of this message.
def ClearField(self, field_name):
897  def ClearField(self, field_name):
898    try:
899      field = message_descriptor.fields_by_name[field_name]
900    except KeyError:
901      try:
902        field = message_descriptor.oneofs_by_name[field_name]
903        if field in self._oneofs:
904          field = self._oneofs[field]
905        else:
906          return
907      except KeyError:
908        raise ValueError('Protocol message %s has no "%s" field.' %
909                         (message_descriptor.name, field_name))
910
911    if field in self._fields:
912      # To match the C++ implementation, we need to invalidate iterators
913      # for map fields when ClearField() happens.
914      if hasattr(self._fields[field], 'InvalidateIterators'):
915        self._fields[field].InvalidateIterators()
916
917      # Note:  If the field is a sub-message, its listener will still point
918      #   at us.  That's fine, because the worst than can happen is that it
919      #   will call _Modified() and invalidate our byte size.  Big deal.
920      del self._fields[field]
921
922      if self._oneofs.get(field.containing_oneof, None) is field:
923        del self._oneofs[field.containing_oneof]
924
925    # Always call _Modified() -- even if nothing was changed, this is
926    # a mutating method, and thus calling it should cause the field to become
927    # present in the parent message.
928    self._Modified()

Clears the contents of a given field.

Inside a oneof group, clears the field set. If the name neither refers to a defined field or oneof group, ValueError is raised.

Arguments:
  • field_name (str): The name of the field to check for presence.
Raises:
  • ValueError: if the field_name is not a member of this message.
def WhichOneof(self, oneof_name):
1403  def WhichOneof(self, oneof_name):
1404    """Returns the name of the currently set field inside a oneof, or None."""
1405    try:
1406      field = message_descriptor.oneofs_by_name[oneof_name]
1407    except KeyError:
1408      raise ValueError(
1409          'Protocol message has no oneof "%s" field.' % oneof_name)
1410
1411    nested_field = self._oneofs.get(field, None)
1412    if nested_field is not None and self.HasField(nested_field.name):
1413      return nested_field.name
1414    else:
1415      return None

Returns the name of the currently set field inside a oneof, or None.

def DiscardUnknownFields(self):
1435def _DiscardUnknownFields(self):
1436  self._unknown_fields = []
1437  for field, value in self.ListFields():
1438    if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
1439      if _IsMapField(field):
1440        if _IsMessageMapField(field):
1441          for key in value:
1442            value[key].DiscardUnknownFields()
1443      elif field.label == _FieldDescriptor.LABEL_REPEATED:
1444        for sub_message in value:
1445          sub_message.DiscardUnknownFields()
1446      else:
1447        value.DiscardUnknownFields()

Clears all fields in the UnknownFieldSet.

This operation is recursive for nested message.

def ByteSize(self):
1098  def ByteSize(self):
1099    if not self._cached_byte_size_dirty:
1100      return self._cached_byte_size
1101
1102    size = 0
1103    descriptor = self.DESCRIPTOR
1104    if descriptor._is_map_entry:
1105      # Fields of map entry should always be serialized.
1106      key_field = descriptor.fields_by_name['key']
1107      _MaybeAddEncoder(cls, key_field)
1108      size = key_field._sizer(self.key)
1109      value_field = descriptor.fields_by_name['value']
1110      _MaybeAddEncoder(cls, value_field)
1111      size += value_field._sizer(self.value)
1112    else:
1113      for field_descriptor, field_value in self.ListFields():
1114        _MaybeAddEncoder(cls, field_descriptor)
1115        size += field_descriptor._sizer(field_value)
1116      for tag_bytes, value_bytes in self._unknown_fields:
1117        size += len(tag_bytes) + len(value_bytes)
1118
1119    self._cached_byte_size = size
1120    self._cached_byte_size_dirty = False
1121    self._listener_for_children.dirty = False
1122    return size

Returns the serialized size of this message.

Recursively calls ByteSize() on all contained messages.

Returns:

int: The number of bytes required to serialize this message.

def FromString(s):
826  def FromString(s):
827    message = cls()
828    message.MergeFromString(s)
829    return message
VARIABLE_FIELD_NUMBER = 3
variable
668  def getter(self):
669    field_value = self._fields.get(field)
670    if field_value is None:
671      # Construct a new object to represent this field.
672      field_value = field._default_constructor(self)
673
674      # Atomically check if another thread has preempted us and, if not, swap
675      # in the new object we just created.  If someone has preempted us, we
676      # take that object and discard ours.
677      # WARNING:  We are relying on setdefault() being atomic.  This is true
678      #   in CPython but we haven't investigated others.  This warning appears
679      #   in several other locations in this file.
680      field_value = self._fields.setdefault(field, field_value)
681    return field_value

Getter for variable.

CONSTRAINT_FIELD_NUMBER = 4
constraint
668  def getter(self):
669    field_value = self._fields.get(field)
670    if field_value is None:
671      # Construct a new object to represent this field.
672      field_value = field._default_constructor(self)
673
674      # Atomically check if another thread has preempted us and, if not, swap
675      # in the new object we just created.  If someone has preempted us, we
676      # take that object and discard ours.
677      # WARNING:  We are relying on setdefault() being atomic.  This is true
678      #   in CPython but we haven't investigated others.  This warning appears
679      #   in several other locations in this file.
680      field_value = self._fields.setdefault(field, field_value)
681    return field_value

Getter for constraint.

GENERAL_CONSTRAINT_FIELD_NUMBER = 7
general_constraint
668  def getter(self):
669    field_value = self._fields.get(field)
670    if field_value is None:
671      # Construct a new object to represent this field.
672      field_value = field._default_constructor(self)
673
674      # Atomically check if another thread has preempted us and, if not, swap
675      # in the new object we just created.  If someone has preempted us, we
676      # take that object and discard ours.
677      # WARNING:  We are relying on setdefault() being atomic.  This is true
678      #   in CPython but we haven't investigated others.  This warning appears
679      #   in several other locations in this file.
680      field_value = self._fields.setdefault(field, field_value)
681    return field_value

Getter for general_constraint.

MAXIMIZE_FIELD_NUMBER = 1
maximize
711  def getter(self):
712    # TODO: This may be broken since there may not be
713    # default_value.  Combine with has_default_value somehow.
714    return self._fields.get(field, default_value)

Getter for maximize.

OBJECTIVE_OFFSET_FIELD_NUMBER = 2
objective_offset
711  def getter(self):
712    # TODO: This may be broken since there may not be
713    # default_value.  Combine with has_default_value somehow.
714    return self._fields.get(field, default_value)

Getter for objective_offset.

QUADRATIC_OBJECTIVE_FIELD_NUMBER = 8
quadratic_objective
767  def getter(self):
768    field_value = self._fields.get(field)
769    if field_value is None:
770      # Construct a new object to represent this field.
771      field_value = field._default_constructor(self)
772
773      # Atomically check if another thread has preempted us and, if not, swap
774      # in the new object we just created.  If someone has preempted us, we
775      # take that object and discard ours.
776      # WARNING:  We are relying on setdefault() being atomic.  This is true
777      #   in CPython but we haven't investigated others.  This warning appears
778      #   in several other locations in this file.
779      field_value = self._fields.setdefault(field, field_value)
780    return field_value

Getter for quadratic_objective.

NAME_FIELD_NUMBER = 5
name
711  def getter(self):
712    # TODO: This may be broken since there may not be
713    # default_value.  Combine with has_default_value somehow.
714    return self._fields.get(field, default_value)

Getter for name.

SOLUTION_HINT_FIELD_NUMBER = 6
solution_hint
767  def getter(self):
768    field_value = self._fields.get(field)
769    if field_value is None:
770      # Construct a new object to represent this field.
771      field_value = field._default_constructor(self)
772
773      # Atomically check if another thread has preempted us and, if not, swap
774      # in the new object we just created.  If someone has preempted us, we
775      # take that object and discard ours.
776      # WARNING:  We are relying on setdefault() being atomic.  This is true
777      #   in CPython but we haven't investigated others.  This warning appears
778      #   in several other locations in this file.
779      field_value = self._fields.setdefault(field, field_value)
780    return field_value

Getter for solution_hint.

ANNOTATION_FIELD_NUMBER = 9
annotation
668  def getter(self):
669    field_value = self._fields.get(field)
670    if field_value is None:
671      # Construct a new object to represent this field.
672      field_value = field._default_constructor(self)
673
674      # Atomically check if another thread has preempted us and, if not, swap
675      # in the new object we just created.  If someone has preempted us, we
676      # take that object and discard ours.
677      # WARNING:  We are relying on setdefault() being atomic.  This is true
678      #   in CPython but we haven't investigated others.  This warning appears
679      #   in several other locations in this file.
680      field_value = self._fields.setdefault(field, field_value)
681    return field_value

Getter for annotation.

def FindInitializationErrors(self):
1302  def FindInitializationErrors(self):
1303    """Finds required fields which are not initialized.
1304
1305    Returns:
1306      A list of strings.  Each string is a path to an uninitialized field from
1307      the top-level message, e.g. "foo.bar[5].baz".
1308    """
1309
1310    errors = []  # simplify things
1311
1312    for field in required_fields:
1313      if not self.HasField(field.name):
1314        errors.append(field.name)
1315
1316    for field, value in self.ListFields():
1317      if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
1318        if field.is_extension:
1319          name = '(%s)' % field.full_name
1320        else:
1321          name = field.name
1322
1323        if _IsMapField(field):
1324          if _IsMessageMapField(field):
1325            for key in value:
1326              element = value[key]
1327              prefix = '%s[%s].' % (name, key)
1328              sub_errors = element.FindInitializationErrors()
1329              errors += [prefix + error for error in sub_errors]
1330          else:
1331            # ScalarMaps can't have any initialization errors.
1332            pass
1333        elif field.label == _FieldDescriptor.LABEL_REPEATED:
1334          for i in range(len(value)):
1335            element = value[i]
1336            prefix = '%s[%d].' % (name, i)
1337            sub_errors = element.FindInitializationErrors()
1338            errors += [prefix + error for error in sub_errors]
1339        else:
1340          prefix = name + '.'
1341          sub_errors = value.FindInitializationErrors()
1342          errors += [prefix + error for error in sub_errors]
1343
1344    return errors

Finds required fields which are not initialized.

Returns:

A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".

Inherited Members
google.protobuf.message.Message
CopyFrom
ParseFromString
HasExtension
ClearExtension
UnknownFields
class MPModelProto.Annotation(google.protobuf.message.Message):

Abstract base class for protocol messages.

Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.

class OptionalDouble(google.protobuf.message.Message):

Abstract base class for protocol messages.

Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.

OptionalDouble(**kwargs)
498  def init(self, **kwargs):
499    self._cached_byte_size = 0
500    self._cached_byte_size_dirty = len(kwargs) > 0
501    self._fields = {}
502    # Contains a mapping from oneof field descriptors to the descriptor
503    # of the currently set field in that oneof field.
504    self._oneofs = {}
505
506    # _unknown_fields is () when empty for efficiency, and will be turned into
507    # a list if fields are added.
508    self._unknown_fields = ()
509    self._is_present_in_parent = False
510    self._listener = message_listener_mod.NullMessageListener()
511    self._listener_for_children = _Listener(self)
512    for field_name, field_value in kwargs.items():
513      field = _GetFieldByName(message_descriptor, field_name)
514      if field is None:
515        raise TypeError('%s() got an unexpected keyword argument "%s"' %
516                        (message_descriptor.name, field_name))
517      if field_value is None:
518        # field=None is the same as no field at all.
519        continue
520      if field.label == _FieldDescriptor.LABEL_REPEATED:
521        field_copy = field._default_constructor(self)
522        if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:  # Composite
523          if _IsMapField(field):
524            if _IsMessageMapField(field):
525              for key in field_value:
526                field_copy[key].MergeFrom(field_value[key])
527            else:
528              field_copy.update(field_value)
529          else:
530            for val in field_value:
531              if isinstance(val, dict):
532                field_copy.add(**val)
533              else:
534                field_copy.add().MergeFrom(val)
535        else:  # Scalar
536          if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM:
537            field_value = [_GetIntegerEnumValue(field.enum_type, val)
538                           for val in field_value]
539          field_copy.extend(field_value)
540        self._fields[field] = field_copy
541      elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
542        field_copy = field._default_constructor(self)
543        new_val = None
544        if isinstance(field_value, message_mod.Message):
545          new_val = field_value
546        elif isinstance(field_value, dict):
547          if field.message_type.full_name == _StructFullTypeName:
548            field_copy.Clear()
549            if len(field_value) == 1 and 'fields' in field_value:
550              try:
551                field_copy.update(field_value)
552              except:
553                # Fall back to init normal message field
554                field_copy.Clear()
555                new_val = field.message_type._concrete_class(**field_value)
556            else:
557              field_copy.update(field_value)
558          else:
559            new_val = field.message_type._concrete_class(**field_value)
560        elif hasattr(field_copy, '_internal_assign'):
561          field_copy._internal_assign(field_value)
562        else:
563          raise TypeError(
564              'Message field {0}.{1} must be initialized with a '
565              'dict or instance of same class, got {2}.'.format(
566                  message_descriptor.name,
567                  field_name,
568                  type(field_value).__name__,
569              )
570          )
571
572        if new_val:
573          try:
574            field_copy.MergeFrom(new_val)
575          except TypeError:
576            _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
577        self._fields[field] = field_copy
578      else:
579        if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM:
580          field_value = _GetIntegerEnumValue(field.enum_type, field_value)
581        try:
582          setattr(self, field_name, field_value)
583        except TypeError:
584          _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
DESCRIPTOR = <google.protobuf.descriptor.Descriptor object>
def MergeFrom(self, msg):
1361  def MergeFrom(self, msg):
1362    if not isinstance(msg, cls):
1363      raise TypeError(
1364          'Parameter to MergeFrom() must be instance of same class: '
1365          'expected %s got %s.' % (_FullyQualifiedClassName(cls),
1366                                   _FullyQualifiedClassName(msg.__class__)))
1367
1368    assert msg is not self
1369    self._Modified()
1370
1371    fields = self._fields
1372
1373    for field, value in msg._fields.items():
1374      if field.label == LABEL_REPEATED:
1375        field_value = fields.get(field)
1376        if field_value is None:
1377          # Construct a new object to represent this field.
1378          field_value = field._default_constructor(self)
1379          fields[field] = field_value
1380        field_value.MergeFrom(value)
1381      elif field.cpp_type == CPPTYPE_MESSAGE:
1382        if value._is_present_in_parent:
1383          field_value = fields.get(field)
1384          if field_value is None:
1385            # Construct a new object to represent this field.
1386            field_value = field._default_constructor(self)
1387            fields[field] = field_value
1388          field_value.MergeFrom(value)
1389      else:
1390        self._fields[field] = value
1391        if field.containing_oneof:
1392          self._UpdateOneofState(field)
1393
1394    if msg._unknown_fields:
1395      if not self._unknown_fields:
1396        self._unknown_fields = []
1397      self._unknown_fields.extend(msg._unknown_fields)

Merges the contents of the specified message into current message.

This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.

Arguments:
  • other_msg (Message): A message to merge into the current message.
def Clear(self):
1420def _Clear(self):
1421  # Clear fields.
1422  self._fields = {}
1423  self._unknown_fields = ()
1424
1425  self._oneofs = {}
1426  self._Modified()

Clears all data that was set in the message.

def SetInParent(self):
1486  def Modified(self):
1487    """Sets the _cached_byte_size_dirty bit to true,
1488    and propagates this to our listener iff this was a state change.
1489    """
1490
1491    # Note:  Some callers check _cached_byte_size_dirty before calling
1492    #   _Modified() as an extra optimization.  So, if this method is ever
1493    #   changed such that it does stuff even when _cached_byte_size_dirty is
1494    #   already true, the callers need to be updated.
1495    if not self._cached_byte_size_dirty:
1496      self._cached_byte_size_dirty = True
1497      self._listener_for_children.dirty = True
1498      self._is_present_in_parent = True
1499      self._listener.Modified()

Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.

def IsInitialized(self, errors=None):
1262  def IsInitialized(self, errors=None):
1263    """Checks if all required fields of a message are set.
1264
1265    Args:
1266      errors:  A list which, if provided, will be populated with the field
1267               paths of all missing required fields.
1268
1269    Returns:
1270      True iff the specified message has all required fields set.
1271    """
1272
1273    # Performance is critical so we avoid HasField() and ListFields().
1274
1275    for field in required_fields:
1276      if (field not in self._fields or
1277          (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and
1278           not self._fields[field]._is_present_in_parent)):
1279        if errors is not None:
1280          errors.extend(self.FindInitializationErrors())
1281        return False
1282
1283    for field, value in list(self._fields.items()):  # dict can change size!
1284      if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
1285        if field.label == _FieldDescriptor.LABEL_REPEATED:
1286          if (field.message_type._is_map_entry):
1287            continue
1288          for element in value:
1289            if not element.IsInitialized():
1290              if errors is not None:
1291                errors.extend(self.FindInitializationErrors())
1292              return False
1293        elif value._is_present_in_parent and not value.IsInitialized():
1294          if errors is not None:
1295            errors.extend(self.FindInitializationErrors())
1296          return False
1297
1298    return True

Checks if all required fields of a message are set.

Arguments:
  • errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:

True iff the specified message has all required fields set.

def MergeFromString(self, serialized):
1177  def MergeFromString(self, serialized):
1178    serialized = memoryview(serialized)
1179    length = len(serialized)
1180    try:
1181      if self._InternalParse(serialized, 0, length) != length:
1182        # The only reason _InternalParse would return early is if it
1183        # encountered an end-group tag.
1184        raise message_mod.DecodeError('Unexpected end-group tag.')
1185    except (IndexError, TypeError):
1186      # Now ord(buf[p:p+1]) == ord('') gets TypeError.
1187      raise message_mod.DecodeError('Truncated message.')
1188    except struct.error as e:
1189      raise message_mod.DecodeError(e)
1190    return length   # Return this for legacy reasons.

Merges serialized protocol buffer data into this message.

When we find a field in serialized that is already present in this message:

  • If it's a "repeated" field, we append to the end of our list.
  • Else, if it's a scalar, we overwrite our field.
  • Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
  • serialized (bytes): Any object that allows us to call memoryview(serialized) to access a string of bytes using the buffer interface.
Returns:

int: The number of bytes read from serialized. For non-group messages, this will always be len(serialized), but for messages which are actually groups, this will generally be less than len(serialized), since we must stop when we reach an END_GROUP tag. Note that if we do stop because of an END_GROUP tag, the number of bytes returned does not include the bytes for the END_GROUP tag information.

Raises:
  • DecodeError: if the input cannot be parsed.
def SerializeToString(self, **kwargs):
1130  def SerializeToString(self, **kwargs):
1131    # Check if the message has all of its required fields set.
1132    if not self.IsInitialized():
1133      raise message_mod.EncodeError(
1134          'Message %s is missing required fields: %s' % (
1135          self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors())))
1136    return self.SerializePartialToString(**kwargs)

Serializes the protocol message to a binary string.

Keyword Args:

deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.

Returns:

A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).

Raises:
def SerializePartialToString(self, **kwargs):
1143  def SerializePartialToString(self, **kwargs):
1144    out = BytesIO()
1145    self._InternalSerialize(out.write, **kwargs)
1146    return out.getvalue()

Serializes the protocol message to a binary string.

This method is similar to SerializeToString but doesn't check if the message is initialized.

Keyword Args:

deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.

Returns:

bytes: A serialized representation of the partial message.

def ListFields(self):
848  def ListFields(self):
849    all_fields = [item for item in self._fields.items() if _IsPresent(item)]
850    all_fields.sort(key = lambda item: item[0].number)
851    return all_fields

Returns a list of (FieldDescriptor, value) tuples for present fields.

A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.

Returns:

list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.

def HasField(self, field_name):
872  def HasField(self, field_name):
873    try:
874      field = hassable_fields[field_name]
875    except KeyError as exc:
876      raise ValueError('Protocol message %s has no non-repeated field "%s" '
877                       'nor has presence is not available for this field.' % (
878                           message_descriptor.full_name, field_name)) from exc
879
880    if isinstance(field, descriptor_mod.OneofDescriptor):
881      try:
882        return HasField(self, self._oneofs[field].name)
883      except KeyError:
884        return False
885    else:
886      if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
887        value = self._fields.get(field)
888        return value is not None and value._is_present_in_parent
889      else:
890        return field in self._fields

Checks if a certain field is set for the message.

For a oneof group, checks if any field inside is set. Note that if the field_name is not defined in the message descriptor, ValueError will be raised.

Arguments:
  • field_name (str): The name of the field to check for presence.
Returns:

bool: Whether a value has been set for the named field.

Raises:
  • ValueError: if the field_name is not a member of this message.
def ClearField(self, field_name):
897  def ClearField(self, field_name):
898    try:
899      field = message_descriptor.fields_by_name[field_name]
900    except KeyError:
901      try:
902        field = message_descriptor.oneofs_by_name[field_name]
903        if field in self._oneofs:
904          field = self._oneofs[field]
905        else:
906          return
907      except KeyError:
908        raise ValueError('Protocol message %s has no "%s" field.' %
909                         (message_descriptor.name, field_name))
910
911    if field in self._fields:
912      # To match the C++ implementation, we need to invalidate iterators
913      # for map fields when ClearField() happens.
914      if hasattr(self._fields[field], 'InvalidateIterators'):
915        self._fields[field].InvalidateIterators()
916
917      # Note:  If the field is a sub-message, its listener will still point
918      #   at us.  That's fine, because the worst than can happen is that it
919      #   will call _Modified() and invalidate our byte size.  Big deal.
920      del self._fields[field]
921
922      if self._oneofs.get(field.containing_oneof, None) is field:
923        del self._oneofs[field.containing_oneof]
924
925    # Always call _Modified() -- even if nothing was changed, this is
926    # a mutating method, and thus calling it should cause the field to become
927    # present in the parent message.
928    self._Modified()

Clears the contents of a given field.

Inside a oneof group, clears the field set. If the name neither refers to a defined field or oneof group, ValueError is raised.

Arguments:
  • field_name (str): The name of the field to check for presence.
Raises:
  • ValueError: if the field_name is not a member of this message.
def WhichOneof(self, oneof_name):
1403  def WhichOneof(self, oneof_name):
1404    """Returns the name of the currently set field inside a oneof, or None."""
1405    try:
1406      field = message_descriptor.oneofs_by_name[oneof_name]
1407    except KeyError:
1408      raise ValueError(
1409          'Protocol message has no oneof "%s" field.' % oneof_name)
1410
1411    nested_field = self._oneofs.get(field, None)
1412    if nested_field is not None and self.HasField(nested_field.name):
1413      return nested_field.name
1414    else:
1415      return None

Returns the name of the currently set field inside a oneof, or None.

def DiscardUnknownFields(self):
1435def _DiscardUnknownFields(self):
1436  self._unknown_fields = []
1437  for field, value in self.ListFields():
1438    if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
1439      if _IsMapField(field):
1440        if _IsMessageMapField(field):
1441          for key in value:
1442            value[key].DiscardUnknownFields()
1443      elif field.label == _FieldDescriptor.LABEL_REPEATED:
1444        for sub_message in value:
1445          sub_message.DiscardUnknownFields()
1446      else:
1447        value.DiscardUnknownFields()

Clears all fields in the UnknownFieldSet.

This operation is recursive for nested message.

def ByteSize(self):
1098  def ByteSize(self):
1099    if not self._cached_byte_size_dirty:
1100      return self._cached_byte_size
1101
1102    size = 0
1103    descriptor = self.DESCRIPTOR
1104    if descriptor._is_map_entry:
1105      # Fields of map entry should always be serialized.
1106      key_field = descriptor.fields_by_name['key']
1107      _MaybeAddEncoder(cls, key_field)
1108      size = key_field._sizer(self.key)
1109      value_field = descriptor.fields_by_name['value']
1110      _MaybeAddEncoder(cls, value_field)
1111      size += value_field._sizer(self.value)
1112    else:
1113      for field_descriptor, field_value in self.ListFields():
1114        _MaybeAddEncoder(cls, field_descriptor)
1115        size += field_descriptor._sizer(field_value)
1116      for tag_bytes, value_bytes in self._unknown_fields:
1117        size += len(tag_bytes) + len(value_bytes)
1118
1119    self._cached_byte_size = size
1120    self._cached_byte_size_dirty = False
1121    self._listener_for_children.dirty = False
1122    return size

Returns the serialized size of this message.

Recursively calls ByteSize() on all contained messages.

Returns:

int: The number of bytes required to serialize this message.

def FromString(s):
826  def FromString(s):
827    message = cls()
828    message.MergeFromString(s)
829    return message
VALUE_FIELD_NUMBER = 1
value
711  def getter(self):
712    # TODO: This may be broken since there may not be
713    # default_value.  Combine with has_default_value somehow.
714    return self._fields.get(field, default_value)

Getter for value.

def FindInitializationErrors(self):
1302  def FindInitializationErrors(self):
1303    """Finds required fields which are not initialized.
1304
1305    Returns:
1306      A list of strings.  Each string is a path to an uninitialized field from
1307      the top-level message, e.g. "foo.bar[5].baz".
1308    """
1309
1310    errors = []  # simplify things
1311
1312    for field in required_fields:
1313      if not self.HasField(field.name):
1314        errors.append(field.name)
1315
1316    for field, value in self.ListFields():
1317      if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
1318        if field.is_extension:
1319          name = '(%s)' % field.full_name
1320        else:
1321          name = field.name
1322
1323        if _IsMapField(field):
1324          if _IsMessageMapField(field):
1325            for key in value:
1326              element = value[key]
1327              prefix = '%s[%s].' % (name, key)
1328              sub_errors = element.FindInitializationErrors()
1329              errors += [prefix + error for error in sub_errors]
1330          else:
1331            # ScalarMaps can't have any initialization errors.
1332            pass
1333        elif field.label == _FieldDescriptor.LABEL_REPEATED:
1334          for i in range(len(value)):
1335            element = value[i]
1336            prefix = '%s[%d].' % (name, i)
1337            sub_errors = element.FindInitializationErrors()
1338            errors += [prefix + error for error in sub_errors]
1339        else:
1340          prefix = name + '.'
1341          sub_errors = value.FindInitializationErrors()
1342          errors += [prefix + error for error in sub_errors]
1343
1344    return errors

Finds required fields which are not initialized.

Returns:

A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".

Inherited Members
google.protobuf.message.Message
CopyFrom
ParseFromString
HasExtension
ClearExtension
UnknownFields
class MPSolverCommonParameters(google.protobuf.message.Message):

Abstract base class for protocol messages.

Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.

MPSolverCommonParameters(**kwargs)
498  def init(self, **kwargs):
499    self._cached_byte_size = 0
500    self._cached_byte_size_dirty = len(kwargs) > 0
501    self._fields = {}
502    # Contains a mapping from oneof field descriptors to the descriptor
503    # of the currently set field in that oneof field.
504    self._oneofs = {}
505
506    # _unknown_fields is () when empty for efficiency, and will be turned into
507    # a list if fields are added.
508    self._unknown_fields = ()
509    self._is_present_in_parent = False
510    self._listener = message_listener_mod.NullMessageListener()
511    self._listener_for_children = _Listener(self)
512    for field_name, field_value in kwargs.items():
513      field = _GetFieldByName(message_descriptor, field_name)
514      if field is None:
515        raise TypeError('%s() got an unexpected keyword argument "%s"' %
516                        (message_descriptor.name, field_name))
517      if field_value is None:
518        # field=None is the same as no field at all.
519        continue
520      if field.label == _FieldDescriptor.LABEL_REPEATED:
521        field_copy = field._default_constructor(self)
522        if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:  # Composite
523          if _IsMapField(field):
524            if _IsMessageMapField(field):
525              for key in field_value:
526                field_copy[key].MergeFrom(field_value[key])
527            else:
528              field_copy.update(field_value)
529          else:
530            for val in field_value:
531              if isinstance(val, dict):
532                field_copy.add(**val)
533              else:
534                field_copy.add().MergeFrom(val)
535        else:  # Scalar
536          if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM:
537            field_value = [_GetIntegerEnumValue(field.enum_type, val)
538                           for val in field_value]
539          field_copy.extend(field_value)
540        self._fields[field] = field_copy
541      elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
542        field_copy = field._default_constructor(self)
543        new_val = None
544        if isinstance(field_value, message_mod.Message):
545          new_val = field_value
546        elif isinstance(field_value, dict):
547          if field.message_type.full_name == _StructFullTypeName:
548            field_copy.Clear()
549            if len(field_value) == 1 and 'fields' in field_value:
550              try:
551                field_copy.update(field_value)
552              except:
553                # Fall back to init normal message field
554                field_copy.Clear()
555                new_val = field.message_type._concrete_class(**field_value)
556            else:
557              field_copy.update(field_value)
558          else:
559            new_val = field.message_type._concrete_class(**field_value)
560        elif hasattr(field_copy, '_internal_assign'):
561          field_copy._internal_assign(field_value)
562        else:
563          raise TypeError(
564              'Message field {0}.{1} must be initialized with a '
565              'dict or instance of same class, got {2}.'.format(
566                  message_descriptor.name,
567                  field_name,
568                  type(field_value).__name__,
569              )
570          )
571
572        if new_val:
573          try:
574            field_copy.MergeFrom(new_val)
575          except TypeError:
576            _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
577        self._fields[field] = field_copy
578      else:
579        if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM:
580          field_value = _GetIntegerEnumValue(field.enum_type, field_value)
581        try:
582          setattr(self, field_name, field_value)
583        except TypeError:
584          _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
DESCRIPTOR = <google.protobuf.descriptor.Descriptor object>
def MergeFrom(self, msg):
1361  def MergeFrom(self, msg):
1362    if not isinstance(msg, cls):
1363      raise TypeError(
1364          'Parameter to MergeFrom() must be instance of same class: '
1365          'expected %s got %s.' % (_FullyQualifiedClassName(cls),
1366                                   _FullyQualifiedClassName(msg.__class__)))
1367
1368    assert msg is not self
1369    self._Modified()
1370
1371    fields = self._fields
1372
1373    for field, value in msg._fields.items():
1374      if field.label == LABEL_REPEATED:
1375        field_value = fields.get(field)
1376        if field_value is None:
1377          # Construct a new object to represent this field.
1378          field_value = field._default_constructor(self)
1379          fields[field] = field_value
1380        field_value.MergeFrom(value)
1381      elif field.cpp_type == CPPTYPE_MESSAGE:
1382        if value._is_present_in_parent:
1383          field_value = fields.get(field)
1384          if field_value is None:
1385            # Construct a new object to represent this field.
1386            field_value = field._default_constructor(self)
1387            fields[field] = field_value
1388          field_value.MergeFrom(value)
1389      else:
1390        self._fields[field] = value
1391        if field.containing_oneof:
1392          self._UpdateOneofState(field)
1393
1394    if msg._unknown_fields:
1395      if not self._unknown_fields:
1396        self._unknown_fields = []
1397      self._unknown_fields.extend(msg._unknown_fields)

Merges the contents of the specified message into current message.

This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.

Arguments:
  • other_msg (Message): A message to merge into the current message.
def Clear(self):
1420def _Clear(self):
1421  # Clear fields.
1422  self._fields = {}
1423  self._unknown_fields = ()
1424
1425  self._oneofs = {}
1426  self._Modified()

Clears all data that was set in the message.

def SetInParent(self):
1486  def Modified(self):
1487    """Sets the _cached_byte_size_dirty bit to true,
1488    and propagates this to our listener iff this was a state change.
1489    """
1490
1491    # Note:  Some callers check _cached_byte_size_dirty before calling
1492    #   _Modified() as an extra optimization.  So, if this method is ever
1493    #   changed such that it does stuff even when _cached_byte_size_dirty is
1494    #   already true, the callers need to be updated.
1495    if not self._cached_byte_size_dirty:
1496      self._cached_byte_size_dirty = True
1497      self._listener_for_children.dirty = True
1498      self._is_present_in_parent = True
1499      self._listener.Modified()

Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.

def IsInitialized(self, errors=None):
1262  def IsInitialized(self, errors=None):
1263    """Checks if all required fields of a message are set.
1264
1265    Args:
1266      errors:  A list which, if provided, will be populated with the field
1267               paths of all missing required fields.
1268
1269    Returns:
1270      True iff the specified message has all required fields set.
1271    """
1272
1273    # Performance is critical so we avoid HasField() and ListFields().
1274
1275    for field in required_fields:
1276      if (field not in self._fields or
1277          (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and
1278           not self._fields[field]._is_present_in_parent)):
1279        if errors is not None:
1280          errors.extend(self.FindInitializationErrors())
1281        return False
1282
1283    for field, value in list(self._fields.items()):  # dict can change size!
1284      if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
1285        if field.label == _FieldDescriptor.LABEL_REPEATED:
1286          if (field.message_type._is_map_entry):
1287            continue
1288          for element in value:
1289            if not element.IsInitialized():
1290              if errors is not None:
1291                errors.extend(self.FindInitializationErrors())
1292              return False
1293        elif value._is_present_in_parent and not value.IsInitialized():
1294          if errors is not None:
1295            errors.extend(self.FindInitializationErrors())
1296          return False
1297
1298    return True

Checks if all required fields of a message are set.

Arguments:
  • errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:

True iff the specified message has all required fields set.

def MergeFromString(self, serialized):
1177  def MergeFromString(self, serialized):
1178    serialized = memoryview(serialized)
1179    length = len(serialized)
1180    try:
1181      if self._InternalParse(serialized, 0, length) != length:
1182        # The only reason _InternalParse would return early is if it
1183        # encountered an end-group tag.
1184        raise message_mod.DecodeError('Unexpected end-group tag.')
1185    except (IndexError, TypeError):
1186      # Now ord(buf[p:p+1]) == ord('') gets TypeError.
1187      raise message_mod.DecodeError('Truncated message.')
1188    except struct.error as e:
1189      raise message_mod.DecodeError(e)
1190    return length   # Return this for legacy reasons.

Merges serialized protocol buffer data into this message.

When we find a field in serialized that is already present in this message:

  • If it's a "repeated" field, we append to the end of our list.
  • Else, if it's a scalar, we overwrite our field.
  • Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
  • serialized (bytes): Any object that allows us to call memoryview(serialized) to access a string of bytes using the buffer interface.
Returns:

int: The number of bytes read from serialized. For non-group messages, this will always be len(serialized), but for messages which are actually groups, this will generally be less than len(serialized), since we must stop when we reach an END_GROUP tag. Note that if we do stop because of an END_GROUP tag, the number of bytes returned does not include the bytes for the END_GROUP tag information.

Raises:
  • DecodeError: if the input cannot be parsed.
def SerializeToString(self, **kwargs):
1130  def SerializeToString(self, **kwargs):
1131    # Check if the message has all of its required fields set.
1132    if not self.IsInitialized():
1133      raise message_mod.EncodeError(
1134          'Message %s is missing required fields: %s' % (
1135          self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors())))
1136    return self.SerializePartialToString(**kwargs)

Serializes the protocol message to a binary string.

Keyword Args:

deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.

Returns:

A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).

Raises:
def SerializePartialToString(self, **kwargs):
1143  def SerializePartialToString(self, **kwargs):
1144    out = BytesIO()
1145    self._InternalSerialize(out.write, **kwargs)
1146    return out.getvalue()

Serializes the protocol message to a binary string.

This method is similar to SerializeToString but doesn't check if the message is initialized.

Keyword Args:

deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.

Returns:

bytes: A serialized representation of the partial message.

def ListFields(self):
848  def ListFields(self):
849    all_fields = [item for item in self._fields.items() if _IsPresent(item)]
850    all_fields.sort(key = lambda item: item[0].number)
851    return all_fields

Returns a list of (FieldDescriptor, value) tuples for present fields.

A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.

Returns:

list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.

def HasField(self, field_name):
872  def HasField(self, field_name):
873    try:
874      field = hassable_fields[field_name]
875    except KeyError as exc:
876      raise ValueError('Protocol message %s has no non-repeated field "%s" '
877                       'nor has presence is not available for this field.' % (
878                           message_descriptor.full_name, field_name)) from exc
879
880    if isinstance(field, descriptor_mod.OneofDescriptor):
881      try:
882        return HasField(self, self._oneofs[field].name)
883      except KeyError:
884        return False
885    else:
886      if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
887        value = self._fields.get(field)
888        return value is not None and value._is_present_in_parent
889      else:
890        return field in self._fields

Checks if a certain field is set for the message.

For a oneof group, checks if any field inside is set. Note that if the field_name is not defined in the message descriptor, ValueError will be raised.

Arguments:
  • field_name (str): The name of the field to check for presence.
Returns:

bool: Whether a value has been set for the named field.

Raises:
  • ValueError: if the field_name is not a member of this message.
def ClearField(self, field_name):
897  def ClearField(self, field_name):
898    try:
899      field = message_descriptor.fields_by_name[field_name]
900    except KeyError:
901      try:
902        field = message_descriptor.oneofs_by_name[field_name]
903        if field in self._oneofs:
904          field = self._oneofs[field]
905        else:
906          return
907      except KeyError:
908        raise ValueError('Protocol message %s has no "%s" field.' %
909                         (message_descriptor.name, field_name))
910
911    if field in self._fields:
912      # To match the C++ implementation, we need to invalidate iterators
913      # for map fields when ClearField() happens.
914      if hasattr(self._fields[field], 'InvalidateIterators'):
915        self._fields[field].InvalidateIterators()
916
917      # Note:  If the field is a sub-message, its listener will still point
918      #   at us.  That's fine, because the worst than can happen is that it
919      #   will call _Modified() and invalidate our byte size.  Big deal.
920      del self._fields[field]
921
922      if self._oneofs.get(field.containing_oneof, None) is field:
923        del self._oneofs[field.containing_oneof]
924
925    # Always call _Modified() -- even if nothing was changed, this is
926    # a mutating method, and thus calling it should cause the field to become
927    # present in the parent message.
928    self._Modified()

Clears the contents of a given field.

Inside a oneof group, clears the field set. If the name neither refers to a defined field or oneof group, ValueError is raised.

Arguments:
  • field_name (str): The name of the field to check for presence.
Raises:
  • ValueError: if the field_name is not a member of this message.
def WhichOneof(self, oneof_name):
1403  def WhichOneof(self, oneof_name):
1404    """Returns the name of the currently set field inside a oneof, or None."""
1405    try:
1406      field = message_descriptor.oneofs_by_name[oneof_name]
1407    except KeyError:
1408      raise ValueError(
1409          'Protocol message has no oneof "%s" field.' % oneof_name)
1410
1411    nested_field = self._oneofs.get(field, None)
1412    if nested_field is not None and self.HasField(nested_field.name):
1413      return nested_field.name
1414    else:
1415      return None

Returns the name of the currently set field inside a oneof, or None.

def DiscardUnknownFields(self):
1435def _DiscardUnknownFields(self):
1436  self._unknown_fields = []
1437  for field, value in self.ListFields():
1438    if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
1439      if _IsMapField(field):
1440        if _IsMessageMapField(field):
1441          for key in value:
1442            value[key].DiscardUnknownFields()
1443      elif field.label == _FieldDescriptor.LABEL_REPEATED:
1444        for sub_message in value:
1445          sub_message.DiscardUnknownFields()
1446      else:
1447        value.DiscardUnknownFields()

Clears all fields in the UnknownFieldSet.

This operation is recursive for nested message.

def ByteSize(self):
1098  def ByteSize(self):
1099    if not self._cached_byte_size_dirty:
1100      return self._cached_byte_size
1101
1102    size = 0
1103    descriptor = self.DESCRIPTOR
1104    if descriptor._is_map_entry:
1105      # Fields of map entry should always be serialized.
1106      key_field = descriptor.fields_by_name['key']
1107      _MaybeAddEncoder(cls, key_field)
1108      size = key_field._sizer(self.key)
1109      value_field = descriptor.fields_by_name['value']
1110      _MaybeAddEncoder(cls, value_field)
1111      size += value_field._sizer(self.value)
1112    else:
1113      for field_descriptor, field_value in self.ListFields():
1114        _MaybeAddEncoder(cls, field_descriptor)
1115        size += field_descriptor._sizer(field_value)
1116      for tag_bytes, value_bytes in self._unknown_fields:
1117        size += len(tag_bytes) + len(value_bytes)
1118
1119    self._cached_byte_size = size
1120    self._cached_byte_size_dirty = False
1121    self._listener_for_children.dirty = False
1122    return size

Returns the serialized size of this message.

Recursively calls ByteSize() on all contained messages.

Returns:

int: The number of bytes required to serialize this message.

def FromString(s):
826  def FromString(s):
827    message = cls()
828    message.MergeFromString(s)
829    return message
LPAlgorithmValues = <google.protobuf.internal.enum_type_wrapper.EnumTypeWrapper object>
LP_ALGO_UNSPECIFIED = 0
LP_ALGO_DUAL = 1
LP_ALGO_PRIMAL = 2
LP_ALGO_BARRIER = 3
RELATIVE_MIP_GAP_FIELD_NUMBER = 1
relative_mip_gap
767  def getter(self):
768    field_value = self._fields.get(field)
769    if field_value is None:
770      # Construct a new object to represent this field.
771      field_value = field._default_constructor(self)
772
773      # Atomically check if another thread has preempted us and, if not, swap
774      # in the new object we just created.  If someone has preempted us, we
775      # take that object and discard ours.
776      # WARNING:  We are relying on setdefault() being atomic.  This is true
777      #   in CPython but we haven't investigated others.  This warning appears
778      #   in several other locations in this file.
779      field_value = self._fields.setdefault(field, field_value)
780    return field_value

Getter for relative_mip_gap.

PRIMAL_TOLERANCE_FIELD_NUMBER = 2
primal_tolerance
767  def getter(self):
768    field_value = self._fields.get(field)
769    if field_value is None:
770      # Construct a new object to represent this field.
771      field_value = field._default_constructor(self)
772
773      # Atomically check if another thread has preempted us and, if not, swap
774      # in the new object we just created.  If someone has preempted us, we
775      # take that object and discard ours.
776      # WARNING:  We are relying on setdefault() being atomic.  This is true
777      #   in CPython but we haven't investigated others.  This warning appears
778      #   in several other locations in this file.
779      field_value = self._fields.setdefault(field, field_value)
780    return field_value

Getter for primal_tolerance.

DUAL_TOLERANCE_FIELD_NUMBER = 3
dual_tolerance
767  def getter(self):
768    field_value = self._fields.get(field)
769    if field_value is None:
770      # Construct a new object to represent this field.
771      field_value = field._default_constructor(self)
772
773      # Atomically check if another thread has preempted us and, if not, swap
774      # in the new object we just created.  If someone has preempted us, we
775      # take that object and discard ours.
776      # WARNING:  We are relying on setdefault() being atomic.  This is true
777      #   in CPython but we haven't investigated others.  This warning appears
778      #   in several other locations in this file.
779      field_value = self._fields.setdefault(field, field_value)
780    return field_value

Getter for dual_tolerance.

LP_ALGORITHM_FIELD_NUMBER = 4
lp_algorithm
711  def getter(self):
712    # TODO: This may be broken since there may not be
713    # default_value.  Combine with has_default_value somehow.
714    return self._fields.get(field, default_value)

Getter for lp_algorithm.

PRESOLVE_FIELD_NUMBER = 5
presolve
711  def getter(self):
712    # TODO: This may be broken since there may not be
713    # default_value.  Combine with has_default_value somehow.
714    return self._fields.get(field, default_value)

Getter for presolve.

SCALING_FIELD_NUMBER = 7
scaling
711  def getter(self):
712    # TODO: This may be broken since there may not be
713    # default_value.  Combine with has_default_value somehow.
714    return self._fields.get(field, default_value)

Getter for scaling.

def FindInitializationErrors(self):
1302  def FindInitializationErrors(self):
1303    """Finds required fields which are not initialized.
1304
1305    Returns:
1306      A list of strings.  Each string is a path to an uninitialized field from
1307      the top-level message, e.g. "foo.bar[5].baz".
1308    """
1309
1310    errors = []  # simplify things
1311
1312    for field in required_fields:
1313      if not self.HasField(field.name):
1314        errors.append(field.name)
1315
1316    for field, value in self.ListFields():
1317      if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
1318        if field.is_extension:
1319          name = '(%s)' % field.full_name
1320        else:
1321          name = field.name
1322
1323        if _IsMapField(field):
1324          if _IsMessageMapField(field):
1325            for key in value:
1326              element = value[key]
1327              prefix = '%s[%s].' % (name, key)
1328              sub_errors = element.FindInitializationErrors()
1329              errors += [prefix + error for error in sub_errors]
1330          else:
1331            # ScalarMaps can't have any initialization errors.
1332            pass
1333        elif field.label == _FieldDescriptor.LABEL_REPEATED:
1334          for i in range(len(value)):
1335            element = value[i]
1336            prefix = '%s[%d].' % (name, i)
1337            sub_errors = element.FindInitializationErrors()
1338            errors += [prefix + error for error in sub_errors]
1339        else:
1340          prefix = name + '.'
1341          sub_errors = value.FindInitializationErrors()
1342          errors += [prefix + error for error in sub_errors]
1343
1344    return errors

Finds required fields which are not initialized.

Returns:

A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".

Inherited Members
google.protobuf.message.Message
CopyFrom
ParseFromString
HasExtension
ClearExtension
UnknownFields
class MPModelDeltaProto(google.protobuf.message.Message):

Abstract base class for protocol messages.

Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.

MPModelDeltaProto(**kwargs)
498  def init(self, **kwargs):
499    self._cached_byte_size = 0
500    self._cached_byte_size_dirty = len(kwargs) > 0
501    self._fields = {}
502    # Contains a mapping from oneof field descriptors to the descriptor
503    # of the currently set field in that oneof field.
504    self._oneofs = {}
505
506    # _unknown_fields is () when empty for efficiency, and will be turned into
507    # a list if fields are added.
508    self._unknown_fields = ()
509    self._is_present_in_parent = False
510    self._listener = message_listener_mod.NullMessageListener()
511    self._listener_for_children = _Listener(self)
512    for field_name, field_value in kwargs.items():
513      field = _GetFieldByName(message_descriptor, field_name)
514      if field is None:
515        raise TypeError('%s() got an unexpected keyword argument "%s"' %
516                        (message_descriptor.name, field_name))
517      if field_value is None:
518        # field=None is the same as no field at all.
519        continue
520      if field.label == _FieldDescriptor.LABEL_REPEATED:
521        field_copy = field._default_constructor(self)
522        if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:  # Composite
523          if _IsMapField(field):
524            if _IsMessageMapField(field):
525              for key in field_value:
526                field_copy[key].MergeFrom(field_value[key])
527            else:
528              field_copy.update(field_value)
529          else:
530            for val in field_value:
531              if isinstance(val, dict):
532                field_copy.add(**val)
533              else:
534                field_copy.add().MergeFrom(val)
535        else:  # Scalar
536          if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM:
537            field_value = [_GetIntegerEnumValue(field.enum_type, val)
538                           for val in field_value]
539          field_copy.extend(field_value)
540        self._fields[field] = field_copy
541      elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
542        field_copy = field._default_constructor(self)
543        new_val = None
544        if isinstance(field_value, message_mod.Message):
545          new_val = field_value
546        elif isinstance(field_value, dict):
547          if field.message_type.full_name == _StructFullTypeName:
548            field_copy.Clear()
549            if len(field_value) == 1 and 'fields' in field_value:
550              try:
551                field_copy.update(field_value)
552              except:
553                # Fall back to init normal message field
554                field_copy.Clear()
555                new_val = field.message_type._concrete_class(**field_value)
556            else:
557              field_copy.update(field_value)
558          else:
559            new_val = field.message_type._concrete_class(**field_value)
560        elif hasattr(field_copy, '_internal_assign'):
561          field_copy._internal_assign(field_value)
562        else:
563          raise TypeError(
564              'Message field {0}.{1} must be initialized with a '
565              'dict or instance of same class, got {2}.'.format(
566                  message_descriptor.name,
567                  field_name,
568                  type(field_value).__name__,
569              )
570          )
571
572        if new_val:
573          try:
574            field_copy.MergeFrom(new_val)
575          except TypeError:
576            _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
577        self._fields[field] = field_copy
578      else:
579        if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM:
580          field_value = _GetIntegerEnumValue(field.enum_type, field_value)
581        try:
582          setattr(self, field_name, field_value)
583        except TypeError:
584          _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
DESCRIPTOR = <google.protobuf.descriptor.Descriptor object>
def MergeFrom(self, msg):
1361  def MergeFrom(self, msg):
1362    if not isinstance(msg, cls):
1363      raise TypeError(
1364          'Parameter to MergeFrom() must be instance of same class: '
1365          'expected %s got %s.' % (_FullyQualifiedClassName(cls),
1366                                   _FullyQualifiedClassName(msg.__class__)))
1367
1368    assert msg is not self
1369    self._Modified()
1370
1371    fields = self._fields
1372
1373    for field, value in msg._fields.items():
1374      if field.label == LABEL_REPEATED:
1375        field_value = fields.get(field)
1376        if field_value is None:
1377          # Construct a new object to represent this field.
1378          field_value = field._default_constructor(self)
1379          fields[field] = field_value
1380        field_value.MergeFrom(value)
1381      elif field.cpp_type == CPPTYPE_MESSAGE:
1382        if value._is_present_in_parent:
1383          field_value = fields.get(field)
1384          if field_value is None:
1385            # Construct a new object to represent this field.
1386            field_value = field._default_constructor(self)
1387            fields[field] = field_value
1388          field_value.MergeFrom(value)
1389      else:
1390        self._fields[field] = value
1391        if field.containing_oneof:
1392          self._UpdateOneofState(field)
1393
1394    if msg._unknown_fields:
1395      if not self._unknown_fields:
1396        self._unknown_fields = []
1397      self._unknown_fields.extend(msg._unknown_fields)

Merges the contents of the specified message into current message.

This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.

Arguments:
  • other_msg (Message): A message to merge into the current message.
def Clear(self):
1420def _Clear(self):
1421  # Clear fields.
1422  self._fields = {}
1423  self._unknown_fields = ()
1424
1425  self._oneofs = {}
1426  self._Modified()

Clears all data that was set in the message.

def SetInParent(self):
1486  def Modified(self):
1487    """Sets the _cached_byte_size_dirty bit to true,
1488    and propagates this to our listener iff this was a state change.
1489    """
1490
1491    # Note:  Some callers check _cached_byte_size_dirty before calling
1492    #   _Modified() as an extra optimization.  So, if this method is ever
1493    #   changed such that it does stuff even when _cached_byte_size_dirty is
1494    #   already true, the callers need to be updated.
1495    if not self._cached_byte_size_dirty:
1496      self._cached_byte_size_dirty = True
1497      self._listener_for_children.dirty = True
1498      self._is_present_in_parent = True
1499      self._listener.Modified()

Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.

def IsInitialized(self, errors=None):
1262  def IsInitialized(self, errors=None):
1263    """Checks if all required fields of a message are set.
1264
1265    Args:
1266      errors:  A list which, if provided, will be populated with the field
1267               paths of all missing required fields.
1268
1269    Returns:
1270      True iff the specified message has all required fields set.
1271    """
1272
1273    # Performance is critical so we avoid HasField() and ListFields().
1274
1275    for field in required_fields:
1276      if (field not in self._fields or
1277          (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and
1278           not self._fields[field]._is_present_in_parent)):
1279        if errors is not None:
1280          errors.extend(self.FindInitializationErrors())
1281        return False
1282
1283    for field, value in list(self._fields.items()):  # dict can change size!
1284      if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
1285        if field.label == _FieldDescriptor.LABEL_REPEATED:
1286          if (field.message_type._is_map_entry):
1287            continue
1288          for element in value:
1289            if not element.IsInitialized():
1290              if errors is not None:
1291                errors.extend(self.FindInitializationErrors())
1292              return False
1293        elif value._is_present_in_parent and not value.IsInitialized():
1294          if errors is not None:
1295            errors.extend(self.FindInitializationErrors())
1296          return False
1297
1298    return True

Checks if all required fields of a message are set.

Arguments:
  • errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:

True iff the specified message has all required fields set.

def MergeFromString(self, serialized):
1177  def MergeFromString(self, serialized):
1178    serialized = memoryview(serialized)
1179    length = len(serialized)
1180    try:
1181      if self._InternalParse(serialized, 0, length) != length:
1182        # The only reason _InternalParse would return early is if it
1183        # encountered an end-group tag.
1184        raise message_mod.DecodeError('Unexpected end-group tag.')
1185    except (IndexError, TypeError):
1186      # Now ord(buf[p:p+1]) == ord('') gets TypeError.
1187      raise message_mod.DecodeError('Truncated message.')
1188    except struct.error as e:
1189      raise message_mod.DecodeError(e)
1190    return length   # Return this for legacy reasons.

Merges serialized protocol buffer data into this message.

When we find a field in serialized that is already present in this message:

  • If it's a "repeated" field, we append to the end of our list.
  • Else, if it's a scalar, we overwrite our field.
  • Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
  • serialized (bytes): Any object that allows us to call memoryview(serialized) to access a string of bytes using the buffer interface.
Returns:

int: The number of bytes read from serialized. For non-group messages, this will always be len(serialized), but for messages which are actually groups, this will generally be less than len(serialized), since we must stop when we reach an END_GROUP tag. Note that if we do stop because of an END_GROUP tag, the number of bytes returned does not include the bytes for the END_GROUP tag information.

Raises:
  • DecodeError: if the input cannot be parsed.
def SerializeToString(self, **kwargs):
1130  def SerializeToString(self, **kwargs):
1131    # Check if the message has all of its required fields set.
1132    if not self.IsInitialized():
1133      raise message_mod.EncodeError(
1134          'Message %s is missing required fields: %s' % (
1135          self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors())))
1136    return self.SerializePartialToString(**kwargs)

Serializes the protocol message to a binary string.

Keyword Args:

deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.

Returns:

A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).

Raises:
def SerializePartialToString(self, **kwargs):
1143  def SerializePartialToString(self, **kwargs):
1144    out = BytesIO()
1145    self._InternalSerialize(out.write, **kwargs)
1146    return out.getvalue()

Serializes the protocol message to a binary string.

This method is similar to SerializeToString but doesn't check if the message is initialized.

Keyword Args:

deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.

Returns:

bytes: A serialized representation of the partial message.

def ListFields(self):
848  def ListFields(self):
849    all_fields = [item for item in self._fields.items() if _IsPresent(item)]
850    all_fields.sort(key = lambda item: item[0].number)
851    return all_fields

Returns a list of (FieldDescriptor, value) tuples for present fields.

A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.

Returns:

list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.

def HasField(self, field_name):
872  def HasField(self, field_name):
873    try:
874      field = hassable_fields[field_name]
875    except KeyError as exc:
876      raise ValueError('Protocol message %s has no non-repeated field "%s" '
877                       'nor has presence is not available for this field.' % (
878                           message_descriptor.full_name, field_name)) from exc
879
880    if isinstance(field, descriptor_mod.OneofDescriptor):
881      try:
882        return HasField(self, self._oneofs[field].name)
883      except KeyError:
884        return False
885    else:
886      if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
887        value = self._fields.get(field)
888        return value is not None and value._is_present_in_parent
889      else:
890        return field in self._fields

Checks if a certain field is set for the message.

For a oneof group, checks if any field inside is set. Note that if the field_name is not defined in the message descriptor, ValueError will be raised.

Arguments:
  • field_name (str): The name of the field to check for presence.
Returns:

bool: Whether a value has been set for the named field.

Raises:
  • ValueError: if the field_name is not a member of this message.
def ClearField(self, field_name):
897  def ClearField(self, field_name):
898    try:
899      field = message_descriptor.fields_by_name[field_name]
900    except KeyError:
901      try:
902        field = message_descriptor.oneofs_by_name[field_name]
903        if field in self._oneofs:
904          field = self._oneofs[field]
905        else:
906          return
907      except KeyError:
908        raise ValueError('Protocol message %s has no "%s" field.' %
909                         (message_descriptor.name, field_name))
910
911    if field in self._fields:
912      # To match the C++ implementation, we need to invalidate iterators
913      # for map fields when ClearField() happens.
914      if hasattr(self._fields[field], 'InvalidateIterators'):
915        self._fields[field].InvalidateIterators()
916
917      # Note:  If the field is a sub-message, its listener will still point
918      #   at us.  That's fine, because the worst than can happen is that it
919      #   will call _Modified() and invalidate our byte size.  Big deal.
920      del self._fields[field]
921
922      if self._oneofs.get(field.containing_oneof, None) is field:
923        del self._oneofs[field.containing_oneof]
924
925    # Always call _Modified() -- even if nothing was changed, this is
926    # a mutating method, and thus calling it should cause the field to become
927    # present in the parent message.
928    self._Modified()

Clears the contents of a given field.

Inside a oneof group, clears the field set. If the name neither refers to a defined field or oneof group, ValueError is raised.

Arguments:
  • field_name (str): The name of the field to check for presence.
Raises:
  • ValueError: if the field_name is not a member of this message.
def WhichOneof(self, oneof_name):
1403  def WhichOneof(self, oneof_name):
1404    """Returns the name of the currently set field inside a oneof, or None."""
1405    try:
1406      field = message_descriptor.oneofs_by_name[oneof_name]
1407    except KeyError:
1408      raise ValueError(
1409          'Protocol message has no oneof "%s" field.' % oneof_name)
1410
1411    nested_field = self._oneofs.get(field, None)
1412    if nested_field is not None and self.HasField(nested_field.name):
1413      return nested_field.name
1414    else:
1415      return None

Returns the name of the currently set field inside a oneof, or None.

def DiscardUnknownFields(self):
1435def _DiscardUnknownFields(self):
1436  self._unknown_fields = []
1437  for field, value in self.ListFields():
1438    if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
1439      if _IsMapField(field):
1440        if _IsMessageMapField(field):
1441          for key in value:
1442            value[key].DiscardUnknownFields()
1443      elif field.label == _FieldDescriptor.LABEL_REPEATED:
1444        for sub_message in value:
1445          sub_message.DiscardUnknownFields()
1446      else:
1447        value.DiscardUnknownFields()

Clears all fields in the UnknownFieldSet.

This operation is recursive for nested message.

def ByteSize(self):
1098  def ByteSize(self):
1099    if not self._cached_byte_size_dirty:
1100      return self._cached_byte_size
1101
1102    size = 0
1103    descriptor = self.DESCRIPTOR
1104    if descriptor._is_map_entry:
1105      # Fields of map entry should always be serialized.
1106      key_field = descriptor.fields_by_name['key']
1107      _MaybeAddEncoder(cls, key_field)
1108      size = key_field._sizer(self.key)
1109      value_field = descriptor.fields_by_name['value']
1110      _MaybeAddEncoder(cls, value_field)
1111      size += value_field._sizer(self.value)
1112    else:
1113      for field_descriptor, field_value in self.ListFields():
1114        _MaybeAddEncoder(cls, field_descriptor)
1115        size += field_descriptor._sizer(field_value)
1116      for tag_bytes, value_bytes in self._unknown_fields:
1117        size += len(tag_bytes) + len(value_bytes)
1118
1119    self._cached_byte_size = size
1120    self._cached_byte_size_dirty = False
1121    self._listener_for_children.dirty = False
1122    return size

Returns the serialized size of this message.

Recursively calls ByteSize() on all contained messages.

Returns:

int: The number of bytes required to serialize this message.

def FromString(s):
826  def FromString(s):
827    message = cls()
828    message.MergeFromString(s)
829    return message
BASELINE_MODEL_FILE_PATH_FIELD_NUMBER = 1
baseline_model_file_path
711  def getter(self):
712    # TODO: This may be broken since there may not be
713    # default_value.  Combine with has_default_value somehow.
714    return self._fields.get(field, default_value)

Getter for baseline_model_file_path.

VARIABLE_OVERRIDES_FIELD_NUMBER = 2
variable_overrides
668  def getter(self):
669    field_value = self._fields.get(field)
670    if field_value is None:
671      # Construct a new object to represent this field.
672      field_value = field._default_constructor(self)
673
674      # Atomically check if another thread has preempted us and, if not, swap
675      # in the new object we just created.  If someone has preempted us, we
676      # take that object and discard ours.
677      # WARNING:  We are relying on setdefault() being atomic.  This is true
678      #   in CPython but we haven't investigated others.  This warning appears
679      #   in several other locations in this file.
680      field_value = self._fields.setdefault(field, field_value)
681    return field_value

Getter for variable_overrides.

CONSTRAINT_OVERRIDES_FIELD_NUMBER = 3
constraint_overrides
668  def getter(self):
669    field_value = self._fields.get(field)
670    if field_value is None:
671      # Construct a new object to represent this field.
672      field_value = field._default_constructor(self)
673
674      # Atomically check if another thread has preempted us and, if not, swap
675      # in the new object we just created.  If someone has preempted us, we
676      # take that object and discard ours.
677      # WARNING:  We are relying on setdefault() being atomic.  This is true
678      #   in CPython but we haven't investigated others.  This warning appears
679      #   in several other locations in this file.
680      field_value = self._fields.setdefault(field, field_value)
681    return field_value

Getter for constraint_overrides.

def FindInitializationErrors(self):
1302  def FindInitializationErrors(self):
1303    """Finds required fields which are not initialized.
1304
1305    Returns:
1306      A list of strings.  Each string is a path to an uninitialized field from
1307      the top-level message, e.g. "foo.bar[5].baz".
1308    """
1309
1310    errors = []  # simplify things
1311
1312    for field in required_fields:
1313      if not self.HasField(field.name):
1314        errors.append(field.name)
1315
1316    for field, value in self.ListFields():
1317      if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
1318        if field.is_extension:
1319          name = '(%s)' % field.full_name
1320        else:
1321          name = field.name
1322
1323        if _IsMapField(field):
1324          if _IsMessageMapField(field):
1325            for key in value:
1326              element = value[key]
1327              prefix = '%s[%s].' % (name, key)
1328              sub_errors = element.FindInitializationErrors()
1329              errors += [prefix + error for error in sub_errors]
1330          else:
1331            # ScalarMaps can't have any initialization errors.
1332            pass
1333        elif field.label == _FieldDescriptor.LABEL_REPEATED:
1334          for i in range(len(value)):
1335            element = value[i]
1336            prefix = '%s[%d].' % (name, i)
1337            sub_errors = element.FindInitializationErrors()
1338            errors += [prefix + error for error in sub_errors]
1339        else:
1340          prefix = name + '.'
1341          sub_errors = value.FindInitializationErrors()
1342          errors += [prefix + error for error in sub_errors]
1343
1344    return errors

Finds required fields which are not initialized.

Returns:

A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".

Inherited Members
google.protobuf.message.Message
CopyFrom
ParseFromString
HasExtension
ClearExtension
UnknownFields
class MPModelDeltaProto.VariableOverridesEntry(google.protobuf.message.Message):

Abstract base class for protocol messages.

Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.

class MPModelDeltaProto.ConstraintOverridesEntry(google.protobuf.message.Message):

Abstract base class for protocol messages.

Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.

class MPModelRequest(google.protobuf.message.Message):

Abstract base class for protocol messages.

Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.

MPModelRequest(**kwargs)
498  def init(self, **kwargs):
499    self._cached_byte_size = 0
500    self._cached_byte_size_dirty = len(kwargs) > 0
501    self._fields = {}
502    # Contains a mapping from oneof field descriptors to the descriptor
503    # of the currently set field in that oneof field.
504    self._oneofs = {}
505
506    # _unknown_fields is () when empty for efficiency, and will be turned into
507    # a list if fields are added.
508    self._unknown_fields = ()
509    self._is_present_in_parent = False
510    self._listener = message_listener_mod.NullMessageListener()
511    self._listener_for_children = _Listener(self)
512    for field_name, field_value in kwargs.items():
513      field = _GetFieldByName(message_descriptor, field_name)
514      if field is None:
515        raise TypeError('%s() got an unexpected keyword argument "%s"' %
516                        (message_descriptor.name, field_name))
517      if field_value is None:
518        # field=None is the same as no field at all.
519        continue
520      if field.label == _FieldDescriptor.LABEL_REPEATED:
521        field_copy = field._default_constructor(self)
522        if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:  # Composite
523          if _IsMapField(field):
524            if _IsMessageMapField(field):
525              for key in field_value:
526                field_copy[key].MergeFrom(field_value[key])
527            else:
528              field_copy.update(field_value)
529          else:
530            for val in field_value:
531              if isinstance(val, dict):
532                field_copy.add(**val)
533              else:
534                field_copy.add().MergeFrom(val)
535        else:  # Scalar
536          if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM:
537            field_value = [_GetIntegerEnumValue(field.enum_type, val)
538                           for val in field_value]
539          field_copy.extend(field_value)
540        self._fields[field] = field_copy
541      elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
542        field_copy = field._default_constructor(self)
543        new_val = None
544        if isinstance(field_value, message_mod.Message):
545          new_val = field_value
546        elif isinstance(field_value, dict):
547          if field.message_type.full_name == _StructFullTypeName:
548            field_copy.Clear()
549            if len(field_value) == 1 and 'fields' in field_value:
550              try:
551                field_copy.update(field_value)
552              except:
553                # Fall back to init normal message field
554                field_copy.Clear()
555                new_val = field.message_type._concrete_class(**field_value)
556            else:
557              field_copy.update(field_value)
558          else:
559            new_val = field.message_type._concrete_class(**field_value)
560        elif hasattr(field_copy, '_internal_assign'):
561          field_copy._internal_assign(field_value)
562        else:
563          raise TypeError(
564              'Message field {0}.{1} must be initialized with a '
565              'dict or instance of same class, got {2}.'.format(
566                  message_descriptor.name,
567                  field_name,
568                  type(field_value).__name__,
569              )
570          )
571
572        if new_val:
573          try:
574            field_copy.MergeFrom(new_val)
575          except TypeError:
576            _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
577        self._fields[field] = field_copy
578      else:
579        if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM:
580          field_value = _GetIntegerEnumValue(field.enum_type, field_value)
581        try:
582          setattr(self, field_name, field_value)
583        except TypeError:
584          _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
DESCRIPTOR = <google.protobuf.descriptor.Descriptor object>
def MergeFrom(self, msg):
1361  def MergeFrom(self, msg):
1362    if not isinstance(msg, cls):
1363      raise TypeError(
1364          'Parameter to MergeFrom() must be instance of same class: '
1365          'expected %s got %s.' % (_FullyQualifiedClassName(cls),
1366                                   _FullyQualifiedClassName(msg.__class__)))
1367
1368    assert msg is not self
1369    self._Modified()
1370
1371    fields = self._fields
1372
1373    for field, value in msg._fields.items():
1374      if field.label == LABEL_REPEATED:
1375        field_value = fields.get(field)
1376        if field_value is None:
1377          # Construct a new object to represent this field.
1378          field_value = field._default_constructor(self)
1379          fields[field] = field_value
1380        field_value.MergeFrom(value)
1381      elif field.cpp_type == CPPTYPE_MESSAGE:
1382        if value._is_present_in_parent:
1383          field_value = fields.get(field)
1384          if field_value is None:
1385            # Construct a new object to represent this field.
1386            field_value = field._default_constructor(self)
1387            fields[field] = field_value
1388          field_value.MergeFrom(value)
1389      else:
1390        self._fields[field] = value
1391        if field.containing_oneof:
1392          self._UpdateOneofState(field)
1393
1394    if msg._unknown_fields:
1395      if not self._unknown_fields:
1396        self._unknown_fields = []
1397      self._unknown_fields.extend(msg._unknown_fields)

Merges the contents of the specified message into current message.

This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.

Arguments:
  • other_msg (Message): A message to merge into the current message.
def Clear(self):
1420def _Clear(self):
1421  # Clear fields.
1422  self._fields = {}
1423  self._unknown_fields = ()
1424
1425  self._oneofs = {}
1426  self._Modified()

Clears all data that was set in the message.

def SetInParent(self):
1486  def Modified(self):
1487    """Sets the _cached_byte_size_dirty bit to true,
1488    and propagates this to our listener iff this was a state change.
1489    """
1490
1491    # Note:  Some callers check _cached_byte_size_dirty before calling
1492    #   _Modified() as an extra optimization.  So, if this method is ever
1493    #   changed such that it does stuff even when _cached_byte_size_dirty is
1494    #   already true, the callers need to be updated.
1495    if not self._cached_byte_size_dirty:
1496      self._cached_byte_size_dirty = True
1497      self._listener_for_children.dirty = True
1498      self._is_present_in_parent = True
1499      self._listener.Modified()

Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.

def IsInitialized(self, errors=None):
1262  def IsInitialized(self, errors=None):
1263    """Checks if all required fields of a message are set.
1264
1265    Args:
1266      errors:  A list which, if provided, will be populated with the field
1267               paths of all missing required fields.
1268
1269    Returns:
1270      True iff the specified message has all required fields set.
1271    """
1272
1273    # Performance is critical so we avoid HasField() and ListFields().
1274
1275    for field in required_fields:
1276      if (field not in self._fields or
1277          (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and
1278           not self._fields[field]._is_present_in_parent)):
1279        if errors is not None:
1280          errors.extend(self.FindInitializationErrors())
1281        return False
1282
1283    for field, value in list(self._fields.items()):  # dict can change size!
1284      if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
1285        if field.label == _FieldDescriptor.LABEL_REPEATED:
1286          if (field.message_type._is_map_entry):
1287            continue
1288          for element in value:
1289            if not element.IsInitialized():
1290              if errors is not None:
1291                errors.extend(self.FindInitializationErrors())
1292              return False
1293        elif value._is_present_in_parent and not value.IsInitialized():
1294          if errors is not None:
1295            errors.extend(self.FindInitializationErrors())
1296          return False
1297
1298    return True

Checks if all required fields of a message are set.

Arguments:
  • errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:

True iff the specified message has all required fields set.

def MergeFromString(self, serialized):
1177  def MergeFromString(self, serialized):
1178    serialized = memoryview(serialized)
1179    length = len(serialized)
1180    try:
1181      if self._InternalParse(serialized, 0, length) != length:
1182        # The only reason _InternalParse would return early is if it
1183        # encountered an end-group tag.
1184        raise message_mod.DecodeError('Unexpected end-group tag.')
1185    except (IndexError, TypeError):
1186      # Now ord(buf[p:p+1]) == ord('') gets TypeError.
1187      raise message_mod.DecodeError('Truncated message.')
1188    except struct.error as e:
1189      raise message_mod.DecodeError(e)
1190    return length   # Return this for legacy reasons.

Merges serialized protocol buffer data into this message.

When we find a field in serialized that is already present in this message:

  • If it's a "repeated" field, we append to the end of our list.
  • Else, if it's a scalar, we overwrite our field.
  • Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
  • serialized (bytes): Any object that allows us to call memoryview(serialized) to access a string of bytes using the buffer interface.
Returns:

int: The number of bytes read from serialized. For non-group messages, this will always be len(serialized), but for messages which are actually groups, this will generally be less than len(serialized), since we must stop when we reach an END_GROUP tag. Note that if we do stop because of an END_GROUP tag, the number of bytes returned does not include the bytes for the END_GROUP tag information.

Raises:
  • DecodeError: if the input cannot be parsed.
def SerializeToString(self, **kwargs):
1130  def SerializeToString(self, **kwargs):
1131    # Check if the message has all of its required fields set.
1132    if not self.IsInitialized():
1133      raise message_mod.EncodeError(
1134          'Message %s is missing required fields: %s' % (
1135          self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors())))
1136    return self.SerializePartialToString(**kwargs)

Serializes the protocol message to a binary string.

Keyword Args:

deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.

Returns:

A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).

Raises:
def SerializePartialToString(self, **kwargs):
1143  def SerializePartialToString(self, **kwargs):
1144    out = BytesIO()
1145    self._InternalSerialize(out.write, **kwargs)
1146    return out.getvalue()

Serializes the protocol message to a binary string.

This method is similar to SerializeToString but doesn't check if the message is initialized.

Keyword Args:

deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.

Returns:

bytes: A serialized representation of the partial message.

def ListFields(self):
848  def ListFields(self):
849    all_fields = [item for item in self._fields.items() if _IsPresent(item)]
850    all_fields.sort(key = lambda item: item[0].number)
851    return all_fields

Returns a list of (FieldDescriptor, value) tuples for present fields.

A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.

Returns:

list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.

def HasField(self, field_name):
872  def HasField(self, field_name):
873    try:
874      field = hassable_fields[field_name]
875    except KeyError as exc:
876      raise ValueError('Protocol message %s has no non-repeated field "%s" '
877                       'nor has presence is not available for this field.' % (
878                           message_descriptor.full_name, field_name)) from exc
879
880    if isinstance(field, descriptor_mod.OneofDescriptor):
881      try:
882        return HasField(self, self._oneofs[field].name)
883      except KeyError:
884        return False
885    else:
886      if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
887        value = self._fields.get(field)
888        return value is not None and value._is_present_in_parent
889      else:
890        return field in self._fields

Checks if a certain field is set for the message.

For a oneof group, checks if any field inside is set. Note that if the field_name is not defined in the message descriptor, ValueError will be raised.

Arguments:
  • field_name (str): The name of the field to check for presence.
Returns:

bool: Whether a value has been set for the named field.

Raises:
  • ValueError: if the field_name is not a member of this message.
def ClearField(self, field_name):
897  def ClearField(self, field_name):
898    try:
899      field = message_descriptor.fields_by_name[field_name]
900    except KeyError:
901      try:
902        field = message_descriptor.oneofs_by_name[field_name]
903        if field in self._oneofs:
904          field = self._oneofs[field]
905        else:
906          return
907      except KeyError:
908        raise ValueError('Protocol message %s has no "%s" field.' %
909                         (message_descriptor.name, field_name))
910
911    if field in self._fields:
912      # To match the C++ implementation, we need to invalidate iterators
913      # for map fields when ClearField() happens.
914      if hasattr(self._fields[field], 'InvalidateIterators'):
915        self._fields[field].InvalidateIterators()
916
917      # Note:  If the field is a sub-message, its listener will still point
918      #   at us.  That's fine, because the worst than can happen is that it
919      #   will call _Modified() and invalidate our byte size.  Big deal.
920      del self._fields[field]
921
922      if self._oneofs.get(field.containing_oneof, None) is field:
923        del self._oneofs[field.containing_oneof]
924
925    # Always call _Modified() -- even if nothing was changed, this is
926    # a mutating method, and thus calling it should cause the field to become
927    # present in the parent message.
928    self._Modified()

Clears the contents of a given field.

Inside a oneof group, clears the field set. If the name neither refers to a defined field or oneof group, ValueError is raised.

Arguments:
  • field_name (str): The name of the field to check for presence.
Raises:
  • ValueError: if the field_name is not a member of this message.
def WhichOneof(self, oneof_name):
1403  def WhichOneof(self, oneof_name):
1404    """Returns the name of the currently set field inside a oneof, or None."""
1405    try:
1406      field = message_descriptor.oneofs_by_name[oneof_name]
1407    except KeyError:
1408      raise ValueError(
1409          'Protocol message has no oneof "%s" field.' % oneof_name)
1410
1411    nested_field = self._oneofs.get(field, None)
1412    if nested_field is not None and self.HasField(nested_field.name):
1413      return nested_field.name
1414    else:
1415      return None

Returns the name of the currently set field inside a oneof, or None.

def DiscardUnknownFields(self):
1435def _DiscardUnknownFields(self):
1436  self._unknown_fields = []
1437  for field, value in self.ListFields():
1438    if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
1439      if _IsMapField(field):
1440        if _IsMessageMapField(field):
1441          for key in value:
1442            value[key].DiscardUnknownFields()
1443      elif field.label == _FieldDescriptor.LABEL_REPEATED:
1444        for sub_message in value:
1445          sub_message.DiscardUnknownFields()
1446      else:
1447        value.DiscardUnknownFields()

Clears all fields in the UnknownFieldSet.

This operation is recursive for nested message.

def ByteSize(self):
1098  def ByteSize(self):
1099    if not self._cached_byte_size_dirty:
1100      return self._cached_byte_size
1101
1102    size = 0
1103    descriptor = self.DESCRIPTOR
1104    if descriptor._is_map_entry:
1105      # Fields of map entry should always be serialized.
1106      key_field = descriptor.fields_by_name['key']
1107      _MaybeAddEncoder(cls, key_field)
1108      size = key_field._sizer(self.key)
1109      value_field = descriptor.fields_by_name['value']
1110      _MaybeAddEncoder(cls, value_field)
1111      size += value_field._sizer(self.value)
1112    else:
1113      for field_descriptor, field_value in self.ListFields():
1114        _MaybeAddEncoder(cls, field_descriptor)
1115        size += field_descriptor._sizer(field_value)
1116      for tag_bytes, value_bytes in self._unknown_fields:
1117        size += len(tag_bytes) + len(value_bytes)
1118
1119    self._cached_byte_size = size
1120    self._cached_byte_size_dirty = False
1121    self._listener_for_children.dirty = False
1122    return size

Returns the serialized size of this message.

Recursively calls ByteSize() on all contained messages.

Returns:

int: The number of bytes required to serialize this message.

def FromString(s):
826  def FromString(s):
827    message = cls()
828    message.MergeFromString(s)
829    return message
SolverType = <google.protobuf.internal.enum_type_wrapper.EnumTypeWrapper object>
CLP_LINEAR_PROGRAMMING = 0
GLOP_LINEAR_PROGRAMMING = 2
GLPK_LINEAR_PROGRAMMING = 1
GUROBI_LINEAR_PROGRAMMING = 6
XPRESS_LINEAR_PROGRAMMING = 101
CPLEX_LINEAR_PROGRAMMING = 10
HIGHS_LINEAR_PROGRAMMING = 15
SCIP_MIXED_INTEGER_PROGRAMMING = 3
GLPK_MIXED_INTEGER_PROGRAMMING = 4
CBC_MIXED_INTEGER_PROGRAMMING = 5
GUROBI_MIXED_INTEGER_PROGRAMMING = 7
XPRESS_MIXED_INTEGER_PROGRAMMING = 102
CPLEX_MIXED_INTEGER_PROGRAMMING = 11
HIGHS_MIXED_INTEGER_PROGRAMMING = 16
BOP_INTEGER_PROGRAMMING = 12
SAT_INTEGER_PROGRAMMING = 14
PDLP_LINEAR_PROGRAMMING = 8
KNAPSACK_MIXED_INTEGER_PROGRAMMING = 13
MODEL_FIELD_NUMBER = 1
model
767  def getter(self):
768    field_value = self._fields.get(field)
769    if field_value is None:
770      # Construct a new object to represent this field.
771      field_value = field._default_constructor(self)
772
773      # Atomically check if another thread has preempted us and, if not, swap
774      # in the new object we just created.  If someone has preempted us, we
775      # take that object and discard ours.
776      # WARNING:  We are relying on setdefault() being atomic.  This is true
777      #   in CPython but we haven't investigated others.  This warning appears
778      #   in several other locations in this file.
779      field_value = self._fields.setdefault(field, field_value)
780    return field_value

Getter for model.

SOLVER_TYPE_FIELD_NUMBER = 2
solver_type
711  def getter(self):
712    # TODO: This may be broken since there may not be
713    # default_value.  Combine with has_default_value somehow.
714    return self._fields.get(field, default_value)

Getter for solver_type.

SOLVER_TIME_LIMIT_SECONDS_FIELD_NUMBER = 3
solver_time_limit_seconds
711  def getter(self):
712    # TODO: This may be broken since there may not be
713    # default_value.  Combine with has_default_value somehow.
714    return self._fields.get(field, default_value)

Getter for solver_time_limit_seconds.

ENABLE_INTERNAL_SOLVER_OUTPUT_FIELD_NUMBER = 4
enable_internal_solver_output
711  def getter(self):
712    # TODO: This may be broken since there may not be
713    # default_value.  Combine with has_default_value somehow.
714    return self._fields.get(field, default_value)

Getter for enable_internal_solver_output.

SOLVER_SPECIFIC_PARAMETERS_FIELD_NUMBER = 5
solver_specific_parameters
711  def getter(self):
712    # TODO: This may be broken since there may not be
713    # default_value.  Combine with has_default_value somehow.
714    return self._fields.get(field, default_value)

Getter for solver_specific_parameters.

IGNORE_SOLVER_SPECIFIC_PARAMETERS_FAILURE_FIELD_NUMBER = 9
ignore_solver_specific_parameters_failure
711  def getter(self):
712    # TODO: This may be broken since there may not be
713    # default_value.  Combine with has_default_value somehow.
714    return self._fields.get(field, default_value)

Getter for ignore_solver_specific_parameters_failure.

MODEL_DELTA_FIELD_NUMBER = 8
model_delta
767  def getter(self):
768    field_value = self._fields.get(field)
769    if field_value is None:
770      # Construct a new object to represent this field.
771      field_value = field._default_constructor(self)
772
773      # Atomically check if another thread has preempted us and, if not, swap
774      # in the new object we just created.  If someone has preempted us, we
775      # take that object and discard ours.
776      # WARNING:  We are relying on setdefault() being atomic.  This is true
777      #   in CPython but we haven't investigated others.  This warning appears
778      #   in several other locations in this file.
779      field_value = self._fields.setdefault(field, field_value)
780    return field_value

Getter for model_delta.

POPULATE_ADDITIONAL_SOLUTIONS_UP_TO_FIELD_NUMBER = 11
populate_additional_solutions_up_to
711  def getter(self):
712    # TODO: This may be broken since there may not be
713    # default_value.  Combine with has_default_value somehow.
714    return self._fields.get(field, default_value)

Getter for populate_additional_solutions_up_to.

def FindInitializationErrors(self):
1302  def FindInitializationErrors(self):
1303    """Finds required fields which are not initialized.
1304
1305    Returns:
1306      A list of strings.  Each string is a path to an uninitialized field from
1307      the top-level message, e.g. "foo.bar[5].baz".
1308    """
1309
1310    errors = []  # simplify things
1311
1312    for field in required_fields:
1313      if not self.HasField(field.name):
1314        errors.append(field.name)
1315
1316    for field, value in self.ListFields():
1317      if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
1318        if field.is_extension:
1319          name = '(%s)' % field.full_name
1320        else:
1321          name = field.name
1322
1323        if _IsMapField(field):
1324          if _IsMessageMapField(field):
1325            for key in value:
1326              element = value[key]
1327              prefix = '%s[%s].' % (name, key)
1328              sub_errors = element.FindInitializationErrors()
1329              errors += [prefix + error for error in sub_errors]
1330          else:
1331            # ScalarMaps can't have any initialization errors.
1332            pass
1333        elif field.label == _FieldDescriptor.LABEL_REPEATED:
1334          for i in range(len(value)):
1335            element = value[i]
1336            prefix = '%s[%d].' % (name, i)
1337            sub_errors = element.FindInitializationErrors()
1338            errors += [prefix + error for error in sub_errors]
1339        else:
1340          prefix = name + '.'
1341          sub_errors = value.FindInitializationErrors()
1342          errors += [prefix + error for error in sub_errors]
1343
1344    return errors

Finds required fields which are not initialized.

Returns:

A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".

Inherited Members
google.protobuf.message.Message
CopyFrom
ParseFromString
HasExtension
ClearExtension
UnknownFields
class MPSolution(google.protobuf.message.Message):

Abstract base class for protocol messages.

Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.

MPSolution(**kwargs)
498  def init(self, **kwargs):
499    self._cached_byte_size = 0
500    self._cached_byte_size_dirty = len(kwargs) > 0
501    self._fields = {}
502    # Contains a mapping from oneof field descriptors to the descriptor
503    # of the currently set field in that oneof field.
504    self._oneofs = {}
505
506    # _unknown_fields is () when empty for efficiency, and will be turned into
507    # a list if fields are added.
508    self._unknown_fields = ()
509    self._is_present_in_parent = False
510    self._listener = message_listener_mod.NullMessageListener()
511    self._listener_for_children = _Listener(self)
512    for field_name, field_value in kwargs.items():
513      field = _GetFieldByName(message_descriptor, field_name)
514      if field is None:
515        raise TypeError('%s() got an unexpected keyword argument "%s"' %
516                        (message_descriptor.name, field_name))
517      if field_value is None:
518        # field=None is the same as no field at all.
519        continue
520      if field.label == _FieldDescriptor.LABEL_REPEATED:
521        field_copy = field._default_constructor(self)
522        if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:  # Composite
523          if _IsMapField(field):
524            if _IsMessageMapField(field):
525              for key in field_value:
526                field_copy[key].MergeFrom(field_value[key])
527            else:
528              field_copy.update(field_value)
529          else:
530            for val in field_value:
531              if isinstance(val, dict):
532                field_copy.add(**val)
533              else:
534                field_copy.add().MergeFrom(val)
535        else:  # Scalar
536          if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM:
537            field_value = [_GetIntegerEnumValue(field.enum_type, val)
538                           for val in field_value]
539          field_copy.extend(field_value)
540        self._fields[field] = field_copy
541      elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
542        field_copy = field._default_constructor(self)
543        new_val = None
544        if isinstance(field_value, message_mod.Message):
545          new_val = field_value
546        elif isinstance(field_value, dict):
547          if field.message_type.full_name == _StructFullTypeName:
548            field_copy.Clear()
549            if len(field_value) == 1 and 'fields' in field_value:
550              try:
551                field_copy.update(field_value)
552              except:
553                # Fall back to init normal message field
554                field_copy.Clear()
555                new_val = field.message_type._concrete_class(**field_value)
556            else:
557              field_copy.update(field_value)
558          else:
559            new_val = field.message_type._concrete_class(**field_value)
560        elif hasattr(field_copy, '_internal_assign'):
561          field_copy._internal_assign(field_value)
562        else:
563          raise TypeError(
564              'Message field {0}.{1} must be initialized with a '
565              'dict or instance of same class, got {2}.'.format(
566                  message_descriptor.name,
567                  field_name,
568                  type(field_value).__name__,
569              )
570          )
571
572        if new_val:
573          try:
574            field_copy.MergeFrom(new_val)
575          except TypeError:
576            _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
577        self._fields[field] = field_copy
578      else:
579        if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM:
580          field_value = _GetIntegerEnumValue(field.enum_type, field_value)
581        try:
582          setattr(self, field_name, field_value)
583        except TypeError:
584          _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
DESCRIPTOR = <google.protobuf.descriptor.Descriptor object>
def MergeFrom(self, msg):
1361  def MergeFrom(self, msg):
1362    if not isinstance(msg, cls):
1363      raise TypeError(
1364          'Parameter to MergeFrom() must be instance of same class: '
1365          'expected %s got %s.' % (_FullyQualifiedClassName(cls),
1366                                   _FullyQualifiedClassName(msg.__class__)))
1367
1368    assert msg is not self
1369    self._Modified()
1370
1371    fields = self._fields
1372
1373    for field, value in msg._fields.items():
1374      if field.label == LABEL_REPEATED:
1375        field_value = fields.get(field)
1376        if field_value is None:
1377          # Construct a new object to represent this field.
1378          field_value = field._default_constructor(self)
1379          fields[field] = field_value
1380        field_value.MergeFrom(value)
1381      elif field.cpp_type == CPPTYPE_MESSAGE:
1382        if value._is_present_in_parent:
1383          field_value = fields.get(field)
1384          if field_value is None:
1385            # Construct a new object to represent this field.
1386            field_value = field._default_constructor(self)
1387            fields[field] = field_value
1388          field_value.MergeFrom(value)
1389      else:
1390        self._fields[field] = value
1391        if field.containing_oneof:
1392          self._UpdateOneofState(field)
1393
1394    if msg._unknown_fields:
1395      if not self._unknown_fields:
1396        self._unknown_fields = []
1397      self._unknown_fields.extend(msg._unknown_fields)

Merges the contents of the specified message into current message.

This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.

Arguments:
  • other_msg (Message): A message to merge into the current message.
def Clear(self):
1420def _Clear(self):
1421  # Clear fields.
1422  self._fields = {}
1423  self._unknown_fields = ()
1424
1425  self._oneofs = {}
1426  self._Modified()

Clears all data that was set in the message.

def SetInParent(self):
1486  def Modified(self):
1487    """Sets the _cached_byte_size_dirty bit to true,
1488    and propagates this to our listener iff this was a state change.
1489    """
1490
1491    # Note:  Some callers check _cached_byte_size_dirty before calling
1492    #   _Modified() as an extra optimization.  So, if this method is ever
1493    #   changed such that it does stuff even when _cached_byte_size_dirty is
1494    #   already true, the callers need to be updated.
1495    if not self._cached_byte_size_dirty:
1496      self._cached_byte_size_dirty = True
1497      self._listener_for_children.dirty = True
1498      self._is_present_in_parent = True
1499      self._listener.Modified()

Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.

def IsInitialized(self, errors=None):
1262  def IsInitialized(self, errors=None):
1263    """Checks if all required fields of a message are set.
1264
1265    Args:
1266      errors:  A list which, if provided, will be populated with the field
1267               paths of all missing required fields.
1268
1269    Returns:
1270      True iff the specified message has all required fields set.
1271    """
1272
1273    # Performance is critical so we avoid HasField() and ListFields().
1274
1275    for field in required_fields:
1276      if (field not in self._fields or
1277          (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and
1278           not self._fields[field]._is_present_in_parent)):
1279        if errors is not None:
1280          errors.extend(self.FindInitializationErrors())
1281        return False
1282
1283    for field, value in list(self._fields.items()):  # dict can change size!
1284      if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
1285        if field.label == _FieldDescriptor.LABEL_REPEATED:
1286          if (field.message_type._is_map_entry):
1287            continue
1288          for element in value:
1289            if not element.IsInitialized():
1290              if errors is not None:
1291                errors.extend(self.FindInitializationErrors())
1292              return False
1293        elif value._is_present_in_parent and not value.IsInitialized():
1294          if errors is not None:
1295            errors.extend(self.FindInitializationErrors())
1296          return False
1297
1298    return True

Checks if all required fields of a message are set.

Arguments:
  • errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:

True iff the specified message has all required fields set.

def MergeFromString(self, serialized):
1177  def MergeFromString(self, serialized):
1178    serialized = memoryview(serialized)
1179    length = len(serialized)
1180    try:
1181      if self._InternalParse(serialized, 0, length) != length:
1182        # The only reason _InternalParse would return early is if it
1183        # encountered an end-group tag.
1184        raise message_mod.DecodeError('Unexpected end-group tag.')
1185    except (IndexError, TypeError):
1186      # Now ord(buf[p:p+1]) == ord('') gets TypeError.
1187      raise message_mod.DecodeError('Truncated message.')
1188    except struct.error as e:
1189      raise message_mod.DecodeError(e)
1190    return length   # Return this for legacy reasons.

Merges serialized protocol buffer data into this message.

When we find a field in serialized that is already present in this message:

  • If it's a "repeated" field, we append to the end of our list.
  • Else, if it's a scalar, we overwrite our field.
  • Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
  • serialized (bytes): Any object that allows us to call memoryview(serialized) to access a string of bytes using the buffer interface.
Returns:

int: The number of bytes read from serialized. For non-group messages, this will always be len(serialized), but for messages which are actually groups, this will generally be less than len(serialized), since we must stop when we reach an END_GROUP tag. Note that if we do stop because of an END_GROUP tag, the number of bytes returned does not include the bytes for the END_GROUP tag information.

Raises:
  • DecodeError: if the input cannot be parsed.
def SerializeToString(self, **kwargs):
1130  def SerializeToString(self, **kwargs):
1131    # Check if the message has all of its required fields set.
1132    if not self.IsInitialized():
1133      raise message_mod.EncodeError(
1134          'Message %s is missing required fields: %s' % (
1135          self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors())))
1136    return self.SerializePartialToString(**kwargs)

Serializes the protocol message to a binary string.

Keyword Args:

deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.

Returns:

A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).

Raises:
def SerializePartialToString(self, **kwargs):
1143  def SerializePartialToString(self, **kwargs):
1144    out = BytesIO()
1145    self._InternalSerialize(out.write, **kwargs)
1146    return out.getvalue()

Serializes the protocol message to a binary string.

This method is similar to SerializeToString but doesn't check if the message is initialized.

Keyword Args:

deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.

Returns:

bytes: A serialized representation of the partial message.

def ListFields(self):
848  def ListFields(self):
849    all_fields = [item for item in self._fields.items() if _IsPresent(item)]
850    all_fields.sort(key = lambda item: item[0].number)
851    return all_fields

Returns a list of (FieldDescriptor, value) tuples for present fields.

A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.

Returns:

list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.

def HasField(self, field_name):
872  def HasField(self, field_name):
873    try:
874      field = hassable_fields[field_name]
875    except KeyError as exc:
876      raise ValueError('Protocol message %s has no non-repeated field "%s" '
877                       'nor has presence is not available for this field.' % (
878                           message_descriptor.full_name, field_name)) from exc
879
880    if isinstance(field, descriptor_mod.OneofDescriptor):
881      try:
882        return HasField(self, self._oneofs[field].name)
883      except KeyError:
884        return False
885    else:
886      if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
887        value = self._fields.get(field)
888        return value is not None and value._is_present_in_parent
889      else:
890        return field in self._fields

Checks if a certain field is set for the message.

For a oneof group, checks if any field inside is set. Note that if the field_name is not defined in the message descriptor, ValueError will be raised.

Arguments:
  • field_name (str): The name of the field to check for presence.
Returns:

bool: Whether a value has been set for the named field.

Raises:
  • ValueError: if the field_name is not a member of this message.
def ClearField(self, field_name):
897  def ClearField(self, field_name):
898    try:
899      field = message_descriptor.fields_by_name[field_name]
900    except KeyError:
901      try:
902        field = message_descriptor.oneofs_by_name[field_name]
903        if field in self._oneofs:
904          field = self._oneofs[field]
905        else:
906          return
907      except KeyError:
908        raise ValueError('Protocol message %s has no "%s" field.' %
909                         (message_descriptor.name, field_name))
910
911    if field in self._fields:
912      # To match the C++ implementation, we need to invalidate iterators
913      # for map fields when ClearField() happens.
914      if hasattr(self._fields[field], 'InvalidateIterators'):
915        self._fields[field].InvalidateIterators()
916
917      # Note:  If the field is a sub-message, its listener will still point
918      #   at us.  That's fine, because the worst than can happen is that it
919      #   will call _Modified() and invalidate our byte size.  Big deal.
920      del self._fields[field]
921
922      if self._oneofs.get(field.containing_oneof, None) is field:
923        del self._oneofs[field.containing_oneof]
924
925    # Always call _Modified() -- even if nothing was changed, this is
926    # a mutating method, and thus calling it should cause the field to become
927    # present in the parent message.
928    self._Modified()

Clears the contents of a given field.

Inside a oneof group, clears the field set. If the name neither refers to a defined field or oneof group, ValueError is raised.

Arguments:
  • field_name (str): The name of the field to check for presence.
Raises:
  • ValueError: if the field_name is not a member of this message.
def WhichOneof(self, oneof_name):
1403  def WhichOneof(self, oneof_name):
1404    """Returns the name of the currently set field inside a oneof, or None."""
1405    try:
1406      field = message_descriptor.oneofs_by_name[oneof_name]
1407    except KeyError:
1408      raise ValueError(
1409          'Protocol message has no oneof "%s" field.' % oneof_name)
1410
1411    nested_field = self._oneofs.get(field, None)
1412    if nested_field is not None and self.HasField(nested_field.name):
1413      return nested_field.name
1414    else:
1415      return None

Returns the name of the currently set field inside a oneof, or None.

def DiscardUnknownFields(self):
1435def _DiscardUnknownFields(self):
1436  self._unknown_fields = []
1437  for field, value in self.ListFields():
1438    if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
1439      if _IsMapField(field):
1440        if _IsMessageMapField(field):
1441          for key in value:
1442            value[key].DiscardUnknownFields()
1443      elif field.label == _FieldDescriptor.LABEL_REPEATED:
1444        for sub_message in value:
1445          sub_message.DiscardUnknownFields()
1446      else:
1447        value.DiscardUnknownFields()

Clears all fields in the UnknownFieldSet.

This operation is recursive for nested message.

def ByteSize(self):
1098  def ByteSize(self):
1099    if not self._cached_byte_size_dirty:
1100      return self._cached_byte_size
1101
1102    size = 0
1103    descriptor = self.DESCRIPTOR
1104    if descriptor._is_map_entry:
1105      # Fields of map entry should always be serialized.
1106      key_field = descriptor.fields_by_name['key']
1107      _MaybeAddEncoder(cls, key_field)
1108      size = key_field._sizer(self.key)
1109      value_field = descriptor.fields_by_name['value']
1110      _MaybeAddEncoder(cls, value_field)
1111      size += value_field._sizer(self.value)
1112    else:
1113      for field_descriptor, field_value in self.ListFields():
1114        _MaybeAddEncoder(cls, field_descriptor)
1115        size += field_descriptor._sizer(field_value)
1116      for tag_bytes, value_bytes in self._unknown_fields:
1117        size += len(tag_bytes) + len(value_bytes)
1118
1119    self._cached_byte_size = size
1120    self._cached_byte_size_dirty = False
1121    self._listener_for_children.dirty = False
1122    return size

Returns the serialized size of this message.

Recursively calls ByteSize() on all contained messages.

Returns:

int: The number of bytes required to serialize this message.

def FromString(s):
826  def FromString(s):
827    message = cls()
828    message.MergeFromString(s)
829    return message
OBJECTIVE_VALUE_FIELD_NUMBER = 1
objective_value
711  def getter(self):
712    # TODO: This may be broken since there may not be
713    # default_value.  Combine with has_default_value somehow.
714    return self._fields.get(field, default_value)

Getter for objective_value.

VARIABLE_VALUE_FIELD_NUMBER = 2
variable_value
668  def getter(self):
669    field_value = self._fields.get(field)
670    if field_value is None:
671      # Construct a new object to represent this field.
672      field_value = field._default_constructor(self)
673
674      # Atomically check if another thread has preempted us and, if not, swap
675      # in the new object we just created.  If someone has preempted us, we
676      # take that object and discard ours.
677      # WARNING:  We are relying on setdefault() being atomic.  This is true
678      #   in CPython but we haven't investigated others.  This warning appears
679      #   in several other locations in this file.
680      field_value = self._fields.setdefault(field, field_value)
681    return field_value

Getter for variable_value.

def FindInitializationErrors(self):
1302  def FindInitializationErrors(self):
1303    """Finds required fields which are not initialized.
1304
1305    Returns:
1306      A list of strings.  Each string is a path to an uninitialized field from
1307      the top-level message, e.g. "foo.bar[5].baz".
1308    """
1309
1310    errors = []  # simplify things
1311
1312    for field in required_fields:
1313      if not self.HasField(field.name):
1314        errors.append(field.name)
1315
1316    for field, value in self.ListFields():
1317      if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
1318        if field.is_extension:
1319          name = '(%s)' % field.full_name
1320        else:
1321          name = field.name
1322
1323        if _IsMapField(field):
1324          if _IsMessageMapField(field):
1325            for key in value:
1326              element = value[key]
1327              prefix = '%s[%s].' % (name, key)
1328              sub_errors = element.FindInitializationErrors()
1329              errors += [prefix + error for error in sub_errors]
1330          else:
1331            # ScalarMaps can't have any initialization errors.
1332            pass
1333        elif field.label == _FieldDescriptor.LABEL_REPEATED:
1334          for i in range(len(value)):
1335            element = value[i]
1336            prefix = '%s[%d].' % (name, i)
1337            sub_errors = element.FindInitializationErrors()
1338            errors += [prefix + error for error in sub_errors]
1339        else:
1340          prefix = name + '.'
1341          sub_errors = value.FindInitializationErrors()
1342          errors += [prefix + error for error in sub_errors]
1343
1344    return errors

Finds required fields which are not initialized.

Returns:

A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".

Inherited Members
google.protobuf.message.Message
CopyFrom
ParseFromString
HasExtension
ClearExtension
UnknownFields
class MPSolveInfo(google.protobuf.message.Message):

Abstract base class for protocol messages.

Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.

MPSolveInfo(**kwargs)
498  def init(self, **kwargs):
499    self._cached_byte_size = 0
500    self._cached_byte_size_dirty = len(kwargs) > 0
501    self._fields = {}
502    # Contains a mapping from oneof field descriptors to the descriptor
503    # of the currently set field in that oneof field.
504    self._oneofs = {}
505
506    # _unknown_fields is () when empty for efficiency, and will be turned into
507    # a list if fields are added.
508    self._unknown_fields = ()
509    self._is_present_in_parent = False
510    self._listener = message_listener_mod.NullMessageListener()
511    self._listener_for_children = _Listener(self)
512    for field_name, field_value in kwargs.items():
513      field = _GetFieldByName(message_descriptor, field_name)
514      if field is None:
515        raise TypeError('%s() got an unexpected keyword argument "%s"' %
516                        (message_descriptor.name, field_name))
517      if field_value is None:
518        # field=None is the same as no field at all.
519        continue
520      if field.label == _FieldDescriptor.LABEL_REPEATED:
521        field_copy = field._default_constructor(self)
522        if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:  # Composite
523          if _IsMapField(field):
524            if _IsMessageMapField(field):
525              for key in field_value:
526                field_copy[key].MergeFrom(field_value[key])
527            else:
528              field_copy.update(field_value)
529          else:
530            for val in field_value:
531              if isinstance(val, dict):
532                field_copy.add(**val)
533              else:
534                field_copy.add().MergeFrom(val)
535        else:  # Scalar
536          if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM:
537            field_value = [_GetIntegerEnumValue(field.enum_type, val)
538                           for val in field_value]
539          field_copy.extend(field_value)
540        self._fields[field] = field_copy
541      elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
542        field_copy = field._default_constructor(self)
543        new_val = None
544        if isinstance(field_value, message_mod.Message):
545          new_val = field_value
546        elif isinstance(field_value, dict):
547          if field.message_type.full_name == _StructFullTypeName:
548            field_copy.Clear()
549            if len(field_value) == 1 and 'fields' in field_value:
550              try:
551                field_copy.update(field_value)
552              except:
553                # Fall back to init normal message field
554                field_copy.Clear()
555                new_val = field.message_type._concrete_class(**field_value)
556            else:
557              field_copy.update(field_value)
558          else:
559            new_val = field.message_type._concrete_class(**field_value)
560        elif hasattr(field_copy, '_internal_assign'):
561          field_copy._internal_assign(field_value)
562        else:
563          raise TypeError(
564              'Message field {0}.{1} must be initialized with a '
565              'dict or instance of same class, got {2}.'.format(
566                  message_descriptor.name,
567                  field_name,
568                  type(field_value).__name__,
569              )
570          )
571
572        if new_val:
573          try:
574            field_copy.MergeFrom(new_val)
575          except TypeError:
576            _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
577        self._fields[field] = field_copy
578      else:
579        if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM:
580          field_value = _GetIntegerEnumValue(field.enum_type, field_value)
581        try:
582          setattr(self, field_name, field_value)
583        except TypeError:
584          _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
DESCRIPTOR = <google.protobuf.descriptor.Descriptor object>
def MergeFrom(self, msg):
1361  def MergeFrom(self, msg):
1362    if not isinstance(msg, cls):
1363      raise TypeError(
1364          'Parameter to MergeFrom() must be instance of same class: '
1365          'expected %s got %s.' % (_FullyQualifiedClassName(cls),
1366                                   _FullyQualifiedClassName(msg.__class__)))
1367
1368    assert msg is not self
1369    self._Modified()
1370
1371    fields = self._fields
1372
1373    for field, value in msg._fields.items():
1374      if field.label == LABEL_REPEATED:
1375        field_value = fields.get(field)
1376        if field_value is None:
1377          # Construct a new object to represent this field.
1378          field_value = field._default_constructor(self)
1379          fields[field] = field_value
1380        field_value.MergeFrom(value)
1381      elif field.cpp_type == CPPTYPE_MESSAGE:
1382        if value._is_present_in_parent:
1383          field_value = fields.get(field)
1384          if field_value is None:
1385            # Construct a new object to represent this field.
1386            field_value = field._default_constructor(self)
1387            fields[field] = field_value
1388          field_value.MergeFrom(value)
1389      else:
1390        self._fields[field] = value
1391        if field.containing_oneof:
1392          self._UpdateOneofState(field)
1393
1394    if msg._unknown_fields:
1395      if not self._unknown_fields:
1396        self._unknown_fields = []
1397      self._unknown_fields.extend(msg._unknown_fields)

Merges the contents of the specified message into current message.

This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.

Arguments:
  • other_msg (Message): A message to merge into the current message.
def Clear(self):
1420def _Clear(self):
1421  # Clear fields.
1422  self._fields = {}
1423  self._unknown_fields = ()
1424
1425  self._oneofs = {}
1426  self._Modified()

Clears all data that was set in the message.

def SetInParent(self):
1486  def Modified(self):
1487    """Sets the _cached_byte_size_dirty bit to true,
1488    and propagates this to our listener iff this was a state change.
1489    """
1490
1491    # Note:  Some callers check _cached_byte_size_dirty before calling
1492    #   _Modified() as an extra optimization.  So, if this method is ever
1493    #   changed such that it does stuff even when _cached_byte_size_dirty is
1494    #   already true, the callers need to be updated.
1495    if not self._cached_byte_size_dirty:
1496      self._cached_byte_size_dirty = True
1497      self._listener_for_children.dirty = True
1498      self._is_present_in_parent = True
1499      self._listener.Modified()

Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.

def IsInitialized(self, errors=None):
1262  def IsInitialized(self, errors=None):
1263    """Checks if all required fields of a message are set.
1264
1265    Args:
1266      errors:  A list which, if provided, will be populated with the field
1267               paths of all missing required fields.
1268
1269    Returns:
1270      True iff the specified message has all required fields set.
1271    """
1272
1273    # Performance is critical so we avoid HasField() and ListFields().
1274
1275    for field in required_fields:
1276      if (field not in self._fields or
1277          (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and
1278           not self._fields[field]._is_present_in_parent)):
1279        if errors is not None:
1280          errors.extend(self.FindInitializationErrors())
1281        return False
1282
1283    for field, value in list(self._fields.items()):  # dict can change size!
1284      if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
1285        if field.label == _FieldDescriptor.LABEL_REPEATED:
1286          if (field.message_type._is_map_entry):
1287            continue
1288          for element in value:
1289            if not element.IsInitialized():
1290              if errors is not None:
1291                errors.extend(self.FindInitializationErrors())
1292              return False
1293        elif value._is_present_in_parent and not value.IsInitialized():
1294          if errors is not None:
1295            errors.extend(self.FindInitializationErrors())
1296          return False
1297
1298    return True

Checks if all required fields of a message are set.

Arguments:
  • errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:

True iff the specified message has all required fields set.

def MergeFromString(self, serialized):
1177  def MergeFromString(self, serialized):
1178    serialized = memoryview(serialized)
1179    length = len(serialized)
1180    try:
1181      if self._InternalParse(serialized, 0, length) != length:
1182        # The only reason _InternalParse would return early is if it
1183        # encountered an end-group tag.
1184        raise message_mod.DecodeError('Unexpected end-group tag.')
1185    except (IndexError, TypeError):
1186      # Now ord(buf[p:p+1]) == ord('') gets TypeError.
1187      raise message_mod.DecodeError('Truncated message.')
1188    except struct.error as e:
1189      raise message_mod.DecodeError(e)
1190    return length   # Return this for legacy reasons.

Merges serialized protocol buffer data into this message.

When we find a field in serialized that is already present in this message:

  • If it's a "repeated" field, we append to the end of our list.
  • Else, if it's a scalar, we overwrite our field.
  • Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
  • serialized (bytes): Any object that allows us to call memoryview(serialized) to access a string of bytes using the buffer interface.
Returns:

int: The number of bytes read from serialized. For non-group messages, this will always be len(serialized), but for messages which are actually groups, this will generally be less than len(serialized), since we must stop when we reach an END_GROUP tag. Note that if we do stop because of an END_GROUP tag, the number of bytes returned does not include the bytes for the END_GROUP tag information.

Raises:
  • DecodeError: if the input cannot be parsed.
def SerializeToString(self, **kwargs):
1130  def SerializeToString(self, **kwargs):
1131    # Check if the message has all of its required fields set.
1132    if not self.IsInitialized():
1133      raise message_mod.EncodeError(
1134          'Message %s is missing required fields: %s' % (
1135          self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors())))
1136    return self.SerializePartialToString(**kwargs)

Serializes the protocol message to a binary string.

Keyword Args:

deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.

Returns:

A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).

Raises:
def SerializePartialToString(self, **kwargs):
1143  def SerializePartialToString(self, **kwargs):
1144    out = BytesIO()
1145    self._InternalSerialize(out.write, **kwargs)
1146    return out.getvalue()

Serializes the protocol message to a binary string.

This method is similar to SerializeToString but doesn't check if the message is initialized.

Keyword Args:

deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.

Returns:

bytes: A serialized representation of the partial message.

def ListFields(self):
848  def ListFields(self):
849    all_fields = [item for item in self._fields.items() if _IsPresent(item)]
850    all_fields.sort(key = lambda item: item[0].number)
851    return all_fields

Returns a list of (FieldDescriptor, value) tuples for present fields.

A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.

Returns:

list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.

def HasField(self, field_name):
872  def HasField(self, field_name):
873    try:
874      field = hassable_fields[field_name]
875    except KeyError as exc:
876      raise ValueError('Protocol message %s has no non-repeated field "%s" '
877                       'nor has presence is not available for this field.' % (
878                           message_descriptor.full_name, field_name)) from exc
879
880    if isinstance(field, descriptor_mod.OneofDescriptor):
881      try:
882        return HasField(self, self._oneofs[field].name)
883      except KeyError:
884        return False
885    else:
886      if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
887        value = self._fields.get(field)
888        return value is not None and value._is_present_in_parent
889      else:
890        return field in self._fields

Checks if a certain field is set for the message.

For a oneof group, checks if any field inside is set. Note that if the field_name is not defined in the message descriptor, ValueError will be raised.

Arguments:
  • field_name (str): The name of the field to check for presence.
Returns:

bool: Whether a value has been set for the named field.

Raises:
  • ValueError: if the field_name is not a member of this message.
def ClearField(self, field_name):
897  def ClearField(self, field_name):
898    try:
899      field = message_descriptor.fields_by_name[field_name]
900    except KeyError:
901      try:
902        field = message_descriptor.oneofs_by_name[field_name]
903        if field in self._oneofs:
904          field = self._oneofs[field]
905        else:
906          return
907      except KeyError:
908        raise ValueError('Protocol message %s has no "%s" field.' %
909                         (message_descriptor.name, field_name))
910
911    if field in self._fields:
912      # To match the C++ implementation, we need to invalidate iterators
913      # for map fields when ClearField() happens.
914      if hasattr(self._fields[field], 'InvalidateIterators'):
915        self._fields[field].InvalidateIterators()
916
917      # Note:  If the field is a sub-message, its listener will still point
918      #   at us.  That's fine, because the worst than can happen is that it
919      #   will call _Modified() and invalidate our byte size.  Big deal.
920      del self._fields[field]
921
922      if self._oneofs.get(field.containing_oneof, None) is field:
923        del self._oneofs[field.containing_oneof]
924
925    # Always call _Modified() -- even if nothing was changed, this is
926    # a mutating method, and thus calling it should cause the field to become
927    # present in the parent message.
928    self._Modified()

Clears the contents of a given field.

Inside a oneof group, clears the field set. If the name neither refers to a defined field or oneof group, ValueError is raised.

Arguments:
  • field_name (str): The name of the field to check for presence.
Raises:
  • ValueError: if the field_name is not a member of this message.
def WhichOneof(self, oneof_name):
1403  def WhichOneof(self, oneof_name):
1404    """Returns the name of the currently set field inside a oneof, or None."""
1405    try:
1406      field = message_descriptor.oneofs_by_name[oneof_name]
1407    except KeyError:
1408      raise ValueError(
1409          'Protocol message has no oneof "%s" field.' % oneof_name)
1410
1411    nested_field = self._oneofs.get(field, None)
1412    if nested_field is not None and self.HasField(nested_field.name):
1413      return nested_field.name
1414    else:
1415      return None

Returns the name of the currently set field inside a oneof, or None.

def DiscardUnknownFields(self):
1435def _DiscardUnknownFields(self):
1436  self._unknown_fields = []
1437  for field, value in self.ListFields():
1438    if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
1439      if _IsMapField(field):
1440        if _IsMessageMapField(field):
1441          for key in value:
1442            value[key].DiscardUnknownFields()
1443      elif field.label == _FieldDescriptor.LABEL_REPEATED:
1444        for sub_message in value:
1445          sub_message.DiscardUnknownFields()
1446      else:
1447        value.DiscardUnknownFields()

Clears all fields in the UnknownFieldSet.

This operation is recursive for nested message.

def ByteSize(self):
1098  def ByteSize(self):
1099    if not self._cached_byte_size_dirty:
1100      return self._cached_byte_size
1101
1102    size = 0
1103    descriptor = self.DESCRIPTOR
1104    if descriptor._is_map_entry:
1105      # Fields of map entry should always be serialized.
1106      key_field = descriptor.fields_by_name['key']
1107      _MaybeAddEncoder(cls, key_field)
1108      size = key_field._sizer(self.key)
1109      value_field = descriptor.fields_by_name['value']
1110      _MaybeAddEncoder(cls, value_field)
1111      size += value_field._sizer(self.value)
1112    else:
1113      for field_descriptor, field_value in self.ListFields():
1114        _MaybeAddEncoder(cls, field_descriptor)
1115        size += field_descriptor._sizer(field_value)
1116      for tag_bytes, value_bytes in self._unknown_fields:
1117        size += len(tag_bytes) + len(value_bytes)
1118
1119    self._cached_byte_size = size
1120    self._cached_byte_size_dirty = False
1121    self._listener_for_children.dirty = False
1122    return size

Returns the serialized size of this message.

Recursively calls ByteSize() on all contained messages.

Returns:

int: The number of bytes required to serialize this message.

def FromString(s):
826  def FromString(s):
827    message = cls()
828    message.MergeFromString(s)
829    return message
SOLVE_WALL_TIME_SECONDS_FIELD_NUMBER = 1
solve_wall_time_seconds
711  def getter(self):
712    # TODO: This may be broken since there may not be
713    # default_value.  Combine with has_default_value somehow.
714    return self._fields.get(field, default_value)

Getter for solve_wall_time_seconds.

SOLVE_USER_TIME_SECONDS_FIELD_NUMBER = 2
solve_user_time_seconds
711  def getter(self):
712    # TODO: This may be broken since there may not be
713    # default_value.  Combine with has_default_value somehow.
714    return self._fields.get(field, default_value)

Getter for solve_user_time_seconds.

def FindInitializationErrors(self):
1302  def FindInitializationErrors(self):
1303    """Finds required fields which are not initialized.
1304
1305    Returns:
1306      A list of strings.  Each string is a path to an uninitialized field from
1307      the top-level message, e.g. "foo.bar[5].baz".
1308    """
1309
1310    errors = []  # simplify things
1311
1312    for field in required_fields:
1313      if not self.HasField(field.name):
1314        errors.append(field.name)
1315
1316    for field, value in self.ListFields():
1317      if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
1318        if field.is_extension:
1319          name = '(%s)' % field.full_name
1320        else:
1321          name = field.name
1322
1323        if _IsMapField(field):
1324          if _IsMessageMapField(field):
1325            for key in value:
1326              element = value[key]
1327              prefix = '%s[%s].' % (name, key)
1328              sub_errors = element.FindInitializationErrors()
1329              errors += [prefix + error for error in sub_errors]
1330          else:
1331            # ScalarMaps can't have any initialization errors.
1332            pass
1333        elif field.label == _FieldDescriptor.LABEL_REPEATED:
1334          for i in range(len(value)):
1335            element = value[i]
1336            prefix = '%s[%d].' % (name, i)
1337            sub_errors = element.FindInitializationErrors()
1338            errors += [prefix + error for error in sub_errors]
1339        else:
1340          prefix = name + '.'
1341          sub_errors = value.FindInitializationErrors()
1342          errors += [prefix + error for error in sub_errors]
1343
1344    return errors

Finds required fields which are not initialized.

Returns:

A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".

Inherited Members
google.protobuf.message.Message
CopyFrom
ParseFromString
HasExtension
ClearExtension
UnknownFields
class MPSolutionResponse(google.protobuf.message.Message):

Abstract base class for protocol messages.

Protocol message classes are almost always generated by the protocol compiler. These generated types subclass Message and implement the methods shown below.

MPSolutionResponse(**kwargs)
498  def init(self, **kwargs):
499    self._cached_byte_size = 0
500    self._cached_byte_size_dirty = len(kwargs) > 0
501    self._fields = {}
502    # Contains a mapping from oneof field descriptors to the descriptor
503    # of the currently set field in that oneof field.
504    self._oneofs = {}
505
506    # _unknown_fields is () when empty for efficiency, and will be turned into
507    # a list if fields are added.
508    self._unknown_fields = ()
509    self._is_present_in_parent = False
510    self._listener = message_listener_mod.NullMessageListener()
511    self._listener_for_children = _Listener(self)
512    for field_name, field_value in kwargs.items():
513      field = _GetFieldByName(message_descriptor, field_name)
514      if field is None:
515        raise TypeError('%s() got an unexpected keyword argument "%s"' %
516                        (message_descriptor.name, field_name))
517      if field_value is None:
518        # field=None is the same as no field at all.
519        continue
520      if field.label == _FieldDescriptor.LABEL_REPEATED:
521        field_copy = field._default_constructor(self)
522        if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:  # Composite
523          if _IsMapField(field):
524            if _IsMessageMapField(field):
525              for key in field_value:
526                field_copy[key].MergeFrom(field_value[key])
527            else:
528              field_copy.update(field_value)
529          else:
530            for val in field_value:
531              if isinstance(val, dict):
532                field_copy.add(**val)
533              else:
534                field_copy.add().MergeFrom(val)
535        else:  # Scalar
536          if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM:
537            field_value = [_GetIntegerEnumValue(field.enum_type, val)
538                           for val in field_value]
539          field_copy.extend(field_value)
540        self._fields[field] = field_copy
541      elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
542        field_copy = field._default_constructor(self)
543        new_val = None
544        if isinstance(field_value, message_mod.Message):
545          new_val = field_value
546        elif isinstance(field_value, dict):
547          if field.message_type.full_name == _StructFullTypeName:
548            field_copy.Clear()
549            if len(field_value) == 1 and 'fields' in field_value:
550              try:
551                field_copy.update(field_value)
552              except:
553                # Fall back to init normal message field
554                field_copy.Clear()
555                new_val = field.message_type._concrete_class(**field_value)
556            else:
557              field_copy.update(field_value)
558          else:
559            new_val = field.message_type._concrete_class(**field_value)
560        elif hasattr(field_copy, '_internal_assign'):
561          field_copy._internal_assign(field_value)
562        else:
563          raise TypeError(
564              'Message field {0}.{1} must be initialized with a '
565              'dict or instance of same class, got {2}.'.format(
566                  message_descriptor.name,
567                  field_name,
568                  type(field_value).__name__,
569              )
570          )
571
572        if new_val:
573          try:
574            field_copy.MergeFrom(new_val)
575          except TypeError:
576            _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
577        self._fields[field] = field_copy
578      else:
579        if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM:
580          field_value = _GetIntegerEnumValue(field.enum_type, field_value)
581        try:
582          setattr(self, field_name, field_value)
583        except TypeError:
584          _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
DESCRIPTOR = <google.protobuf.descriptor.Descriptor object>
def MergeFrom(self, msg):
1361  def MergeFrom(self, msg):
1362    if not isinstance(msg, cls):
1363      raise TypeError(
1364          'Parameter to MergeFrom() must be instance of same class: '
1365          'expected %s got %s.' % (_FullyQualifiedClassName(cls),
1366                                   _FullyQualifiedClassName(msg.__class__)))
1367
1368    assert msg is not self
1369    self._Modified()
1370
1371    fields = self._fields
1372
1373    for field, value in msg._fields.items():
1374      if field.label == LABEL_REPEATED:
1375        field_value = fields.get(field)
1376        if field_value is None:
1377          # Construct a new object to represent this field.
1378          field_value = field._default_constructor(self)
1379          fields[field] = field_value
1380        field_value.MergeFrom(value)
1381      elif field.cpp_type == CPPTYPE_MESSAGE:
1382        if value._is_present_in_parent:
1383          field_value = fields.get(field)
1384          if field_value is None:
1385            # Construct a new object to represent this field.
1386            field_value = field._default_constructor(self)
1387            fields[field] = field_value
1388          field_value.MergeFrom(value)
1389      else:
1390        self._fields[field] = value
1391        if field.containing_oneof:
1392          self._UpdateOneofState(field)
1393
1394    if msg._unknown_fields:
1395      if not self._unknown_fields:
1396        self._unknown_fields = []
1397      self._unknown_fields.extend(msg._unknown_fields)

Merges the contents of the specified message into current message.

This method merges the contents of the specified message into the current message. Singular fields that are set in the specified message overwrite the corresponding fields in the current message. Repeated fields are appended. Singular sub-messages and groups are recursively merged.

Arguments:
  • other_msg (Message): A message to merge into the current message.
def Clear(self):
1420def _Clear(self):
1421  # Clear fields.
1422  self._fields = {}
1423  self._unknown_fields = ()
1424
1425  self._oneofs = {}
1426  self._Modified()

Clears all data that was set in the message.

def SetInParent(self):
1486  def Modified(self):
1487    """Sets the _cached_byte_size_dirty bit to true,
1488    and propagates this to our listener iff this was a state change.
1489    """
1490
1491    # Note:  Some callers check _cached_byte_size_dirty before calling
1492    #   _Modified() as an extra optimization.  So, if this method is ever
1493    #   changed such that it does stuff even when _cached_byte_size_dirty is
1494    #   already true, the callers need to be updated.
1495    if not self._cached_byte_size_dirty:
1496      self._cached_byte_size_dirty = True
1497      self._listener_for_children.dirty = True
1498      self._is_present_in_parent = True
1499      self._listener.Modified()

Sets the _cached_byte_size_dirty bit to true, and propagates this to our listener iff this was a state change.

def IsInitialized(self, errors=None):
1262  def IsInitialized(self, errors=None):
1263    """Checks if all required fields of a message are set.
1264
1265    Args:
1266      errors:  A list which, if provided, will be populated with the field
1267               paths of all missing required fields.
1268
1269    Returns:
1270      True iff the specified message has all required fields set.
1271    """
1272
1273    # Performance is critical so we avoid HasField() and ListFields().
1274
1275    for field in required_fields:
1276      if (field not in self._fields or
1277          (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE and
1278           not self._fields[field]._is_present_in_parent)):
1279        if errors is not None:
1280          errors.extend(self.FindInitializationErrors())
1281        return False
1282
1283    for field, value in list(self._fields.items()):  # dict can change size!
1284      if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
1285        if field.label == _FieldDescriptor.LABEL_REPEATED:
1286          if (field.message_type._is_map_entry):
1287            continue
1288          for element in value:
1289            if not element.IsInitialized():
1290              if errors is not None:
1291                errors.extend(self.FindInitializationErrors())
1292              return False
1293        elif value._is_present_in_parent and not value.IsInitialized():
1294          if errors is not None:
1295            errors.extend(self.FindInitializationErrors())
1296          return False
1297
1298    return True

Checks if all required fields of a message are set.

Arguments:
  • errors: A list which, if provided, will be populated with the field paths of all missing required fields.
Returns:

True iff the specified message has all required fields set.

def MergeFromString(self, serialized):
1177  def MergeFromString(self, serialized):
1178    serialized = memoryview(serialized)
1179    length = len(serialized)
1180    try:
1181      if self._InternalParse(serialized, 0, length) != length:
1182        # The only reason _InternalParse would return early is if it
1183        # encountered an end-group tag.
1184        raise message_mod.DecodeError('Unexpected end-group tag.')
1185    except (IndexError, TypeError):
1186      # Now ord(buf[p:p+1]) == ord('') gets TypeError.
1187      raise message_mod.DecodeError('Truncated message.')
1188    except struct.error as e:
1189      raise message_mod.DecodeError(e)
1190    return length   # Return this for legacy reasons.

Merges serialized protocol buffer data into this message.

When we find a field in serialized that is already present in this message:

  • If it's a "repeated" field, we append to the end of our list.
  • Else, if it's a scalar, we overwrite our field.
  • Else, (it's a nonrepeated composite), we recursively merge into the existing composite.
Arguments:
  • serialized (bytes): Any object that allows us to call memoryview(serialized) to access a string of bytes using the buffer interface.
Returns:

int: The number of bytes read from serialized. For non-group messages, this will always be len(serialized), but for messages which are actually groups, this will generally be less than len(serialized), since we must stop when we reach an END_GROUP tag. Note that if we do stop because of an END_GROUP tag, the number of bytes returned does not include the bytes for the END_GROUP tag information.

Raises:
  • DecodeError: if the input cannot be parsed.
def SerializeToString(self, **kwargs):
1130  def SerializeToString(self, **kwargs):
1131    # Check if the message has all of its required fields set.
1132    if not self.IsInitialized():
1133      raise message_mod.EncodeError(
1134          'Message %s is missing required fields: %s' % (
1135          self.DESCRIPTOR.full_name, ','.join(self.FindInitializationErrors())))
1136    return self.SerializePartialToString(**kwargs)

Serializes the protocol message to a binary string.

Keyword Args:

deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.

Returns:

A binary string representation of the message if all of the required fields in the message are set (i.e. the message is initialized).

Raises:
def SerializePartialToString(self, **kwargs):
1143  def SerializePartialToString(self, **kwargs):
1144    out = BytesIO()
1145    self._InternalSerialize(out.write, **kwargs)
1146    return out.getvalue()

Serializes the protocol message to a binary string.

This method is similar to SerializeToString but doesn't check if the message is initialized.

Keyword Args:

deterministic (bool): If true, requests deterministic serialization of the protobuf, with predictable ordering of map keys.

Returns:

bytes: A serialized representation of the partial message.

def ListFields(self):
848  def ListFields(self):
849    all_fields = [item for item in self._fields.items() if _IsPresent(item)]
850    all_fields.sort(key = lambda item: item[0].number)
851    return all_fields

Returns a list of (FieldDescriptor, value) tuples for present fields.

A message field is non-empty if HasField() would return true. A singular primitive field is non-empty if HasField() would return true in proto2 or it is non zero in proto3. A repeated field is non-empty if it contains at least one element. The fields are ordered by field number.

Returns:

list[tuple(FieldDescriptor, value)]: field descriptors and values for all fields in the message which are not empty. The values vary by field type.

def HasField(self, field_name):
872  def HasField(self, field_name):
873    try:
874      field = hassable_fields[field_name]
875    except KeyError as exc:
876      raise ValueError('Protocol message %s has no non-repeated field "%s" '
877                       'nor has presence is not available for this field.' % (
878                           message_descriptor.full_name, field_name)) from exc
879
880    if isinstance(field, descriptor_mod.OneofDescriptor):
881      try:
882        return HasField(self, self._oneofs[field].name)
883      except KeyError:
884        return False
885    else:
886      if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
887        value = self._fields.get(field)
888        return value is not None and value._is_present_in_parent
889      else:
890        return field in self._fields

Checks if a certain field is set for the message.

For a oneof group, checks if any field inside is set. Note that if the field_name is not defined in the message descriptor, ValueError will be raised.

Arguments:
  • field_name (str): The name of the field to check for presence.
Returns:

bool: Whether a value has been set for the named field.

Raises:
  • ValueError: if the field_name is not a member of this message.
def ClearField(self, field_name):
897  def ClearField(self, field_name):
898    try:
899      field = message_descriptor.fields_by_name[field_name]
900    except KeyError:
901      try:
902        field = message_descriptor.oneofs_by_name[field_name]
903        if field in self._oneofs:
904          field = self._oneofs[field]
905        else:
906          return
907      except KeyError:
908        raise ValueError('Protocol message %s has no "%s" field.' %
909                         (message_descriptor.name, field_name))
910
911    if field in self._fields:
912      # To match the C++ implementation, we need to invalidate iterators
913      # for map fields when ClearField() happens.
914      if hasattr(self._fields[field], 'InvalidateIterators'):
915        self._fields[field].InvalidateIterators()
916
917      # Note:  If the field is a sub-message, its listener will still point
918      #   at us.  That's fine, because the worst than can happen is that it
919      #   will call _Modified() and invalidate our byte size.  Big deal.
920      del self._fields[field]
921
922      if self._oneofs.get(field.containing_oneof, None) is field:
923        del self._oneofs[field.containing_oneof]
924
925    # Always call _Modified() -- even if nothing was changed, this is
926    # a mutating method, and thus calling it should cause the field to become
927    # present in the parent message.
928    self._Modified()

Clears the contents of a given field.

Inside a oneof group, clears the field set. If the name neither refers to a defined field or oneof group, ValueError is raised.

Arguments:
  • field_name (str): The name of the field to check for presence.
Raises:
  • ValueError: if the field_name is not a member of this message.
def WhichOneof(self, oneof_name):
1403  def WhichOneof(self, oneof_name):
1404    """Returns the name of the currently set field inside a oneof, or None."""
1405    try:
1406      field = message_descriptor.oneofs_by_name[oneof_name]
1407    except KeyError:
1408      raise ValueError(
1409          'Protocol message has no oneof "%s" field.' % oneof_name)
1410
1411    nested_field = self._oneofs.get(field, None)
1412    if nested_field is not None and self.HasField(nested_field.name):
1413      return nested_field.name
1414    else:
1415      return None

Returns the name of the currently set field inside a oneof, or None.

def DiscardUnknownFields(self):
1435def _DiscardUnknownFields(self):
1436  self._unknown_fields = []
1437  for field, value in self.ListFields():
1438    if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
1439      if _IsMapField(field):
1440        if _IsMessageMapField(field):
1441          for key in value:
1442            value[key].DiscardUnknownFields()
1443      elif field.label == _FieldDescriptor.LABEL_REPEATED:
1444        for sub_message in value:
1445          sub_message.DiscardUnknownFields()
1446      else:
1447        value.DiscardUnknownFields()

Clears all fields in the UnknownFieldSet.

This operation is recursive for nested message.

def ByteSize(self):
1098  def ByteSize(self):
1099    if not self._cached_byte_size_dirty:
1100      return self._cached_byte_size
1101
1102    size = 0
1103    descriptor = self.DESCRIPTOR
1104    if descriptor._is_map_entry:
1105      # Fields of map entry should always be serialized.
1106      key_field = descriptor.fields_by_name['key']
1107      _MaybeAddEncoder(cls, key_field)
1108      size = key_field._sizer(self.key)
1109      value_field = descriptor.fields_by_name['value']
1110      _MaybeAddEncoder(cls, value_field)
1111      size += value_field._sizer(self.value)
1112    else:
1113      for field_descriptor, field_value in self.ListFields():
1114        _MaybeAddEncoder(cls, field_descriptor)
1115        size += field_descriptor._sizer(field_value)
1116      for tag_bytes, value_bytes in self._unknown_fields:
1117        size += len(tag_bytes) + len(value_bytes)
1118
1119    self._cached_byte_size = size
1120    self._cached_byte_size_dirty = False
1121    self._listener_for_children.dirty = False
1122    return size

Returns the serialized size of this message.

Recursively calls ByteSize() on all contained messages.

Returns:

int: The number of bytes required to serialize this message.

def FromString(s):
826  def FromString(s):
827    message = cls()
828    message.MergeFromString(s)
829    return message
STATUS_FIELD_NUMBER = 1
status
711  def getter(self):
712    # TODO: This may be broken since there may not be
713    # default_value.  Combine with has_default_value somehow.
714    return self._fields.get(field, default_value)

Getter for status.

STATUS_STR_FIELD_NUMBER = 7
status_str
711  def getter(self):
712    # TODO: This may be broken since there may not be
713    # default_value.  Combine with has_default_value somehow.
714    return self._fields.get(field, default_value)

Getter for status_str.

OBJECTIVE_VALUE_FIELD_NUMBER = 2
objective_value
711  def getter(self):
712    # TODO: This may be broken since there may not be
713    # default_value.  Combine with has_default_value somehow.
714    return self._fields.get(field, default_value)

Getter for objective_value.

BEST_OBJECTIVE_BOUND_FIELD_NUMBER = 5
best_objective_bound
711  def getter(self):
712    # TODO: This may be broken since there may not be
713    # default_value.  Combine with has_default_value somehow.
714    return self._fields.get(field, default_value)

Getter for best_objective_bound.

VARIABLE_VALUE_FIELD_NUMBER = 3
variable_value
668  def getter(self):
669    field_value = self._fields.get(field)
670    if field_value is None:
671      # Construct a new object to represent this field.
672      field_value = field._default_constructor(self)
673
674      # Atomically check if another thread has preempted us and, if not, swap
675      # in the new object we just created.  If someone has preempted us, we
676      # take that object and discard ours.
677      # WARNING:  We are relying on setdefault() being atomic.  This is true
678      #   in CPython but we haven't investigated others.  This warning appears
679      #   in several other locations in this file.
680      field_value = self._fields.setdefault(field, field_value)
681    return field_value

Getter for variable_value.

SOLVE_INFO_FIELD_NUMBER = 10
solve_info
767  def getter(self):
768    field_value = self._fields.get(field)
769    if field_value is None:
770      # Construct a new object to represent this field.
771      field_value = field._default_constructor(self)
772
773      # Atomically check if another thread has preempted us and, if not, swap
774      # in the new object we just created.  If someone has preempted us, we
775      # take that object and discard ours.
776      # WARNING:  We are relying on setdefault() being atomic.  This is true
777      #   in CPython but we haven't investigated others.  This warning appears
778      #   in several other locations in this file.
779      field_value = self._fields.setdefault(field, field_value)
780    return field_value

Getter for solve_info.

SOLVER_SPECIFIC_INFO_FIELD_NUMBER = 11
solver_specific_info
711  def getter(self):
712    # TODO: This may be broken since there may not be
713    # default_value.  Combine with has_default_value somehow.
714    return self._fields.get(field, default_value)

Getter for solver_specific_info.

DUAL_VALUE_FIELD_NUMBER = 4
dual_value
668  def getter(self):
669    field_value = self._fields.get(field)
670    if field_value is None:
671      # Construct a new object to represent this field.
672      field_value = field._default_constructor(self)
673
674      # Atomically check if another thread has preempted us and, if not, swap
675      # in the new object we just created.  If someone has preempted us, we
676      # take that object and discard ours.
677      # WARNING:  We are relying on setdefault() being atomic.  This is true
678      #   in CPython but we haven't investigated others.  This warning appears
679      #   in several other locations in this file.
680      field_value = self._fields.setdefault(field, field_value)
681    return field_value

Getter for dual_value.

REDUCED_COST_FIELD_NUMBER = 6
reduced_cost
668  def getter(self):
669    field_value = self._fields.get(field)
670    if field_value is None:
671      # Construct a new object to represent this field.
672      field_value = field._default_constructor(self)
673
674      # Atomically check if another thread has preempted us and, if not, swap
675      # in the new object we just created.  If someone has preempted us, we
676      # take that object and discard ours.
677      # WARNING:  We are relying on setdefault() being atomic.  This is true
678      #   in CPython but we haven't investigated others.  This warning appears
679      #   in several other locations in this file.
680      field_value = self._fields.setdefault(field, field_value)
681    return field_value

Getter for reduced_cost.

ADDITIONAL_SOLUTIONS_FIELD_NUMBER = 8
additional_solutions
668  def getter(self):
669    field_value = self._fields.get(field)
670    if field_value is None:
671      # Construct a new object to represent this field.
672      field_value = field._default_constructor(self)
673
674      # Atomically check if another thread has preempted us and, if not, swap
675      # in the new object we just created.  If someone has preempted us, we
676      # take that object and discard ours.
677      # WARNING:  We are relying on setdefault() being atomic.  This is true
678      #   in CPython but we haven't investigated others.  This warning appears
679      #   in several other locations in this file.
680      field_value = self._fields.setdefault(field, field_value)
681    return field_value

Getter for additional_solutions.

def FindInitializationErrors(self):
1302  def FindInitializationErrors(self):
1303    """Finds required fields which are not initialized.
1304
1305    Returns:
1306      A list of strings.  Each string is a path to an uninitialized field from
1307      the top-level message, e.g. "foo.bar[5].baz".
1308    """
1309
1310    errors = []  # simplify things
1311
1312    for field in required_fields:
1313      if not self.HasField(field.name):
1314        errors.append(field.name)
1315
1316    for field, value in self.ListFields():
1317      if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
1318        if field.is_extension:
1319          name = '(%s)' % field.full_name
1320        else:
1321          name = field.name
1322
1323        if _IsMapField(field):
1324          if _IsMessageMapField(field):
1325            for key in value:
1326              element = value[key]
1327              prefix = '%s[%s].' % (name, key)
1328              sub_errors = element.FindInitializationErrors()
1329              errors += [prefix + error for error in sub_errors]
1330          else:
1331            # ScalarMaps can't have any initialization errors.
1332            pass
1333        elif field.label == _FieldDescriptor.LABEL_REPEATED:
1334          for i in range(len(value)):
1335            element = value[i]
1336            prefix = '%s[%d].' % (name, i)
1337            sub_errors = element.FindInitializationErrors()
1338            errors += [prefix + error for error in sub_errors]
1339        else:
1340          prefix = name + '.'
1341          sub_errors = value.FindInitializationErrors()
1342          errors += [prefix + error for error in sub_errors]
1343
1344    return errors

Finds required fields which are not initialized.

Returns:

A list of strings. Each string is a path to an uninitialized field from the top-level message, e.g. "foo.bar[5].baz".

Inherited Members
google.protobuf.message.Message
CopyFrom
ParseFromString
HasExtension
ClearExtension
UnknownFields