MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / NormalizeNumberFields

Function NormalizeNumberFields

tensorflow/python/util/protobuf/compare.py:121–186  ·  view source on GitHub ↗

Normalizes types and precisions of number fields in a protocol buffer. Due to subtleties in the python protocol buffer implementation, it is possible for values to have different types and precision depending on whether they were set and retrieved directly or deserialized from a protobuf. Thi

(pb)

Source from the content-addressed store, hash-verified

119
120
121def NormalizeNumberFields(pb):
122 """Normalizes types and precisions of number fields in a protocol buffer.
123
124 Due to subtleties in the python protocol buffer implementation, it is possible
125 for values to have different types and precision depending on whether they
126 were set and retrieved directly or deserialized from a protobuf. This function
127 normalizes integer values to ints and longs based on width, 32-bit floats to
128 five digits of precision to account for python always storing them as 64-bit,
129 and ensures doubles are floating point for when they're set to integers.
130
131 Modifies pb in place. Recurses into nested objects.
132
133 Args:
134 pb: proto2 message.
135
136 Returns:
137 the given pb, modified in place.
138 """
139 for desc, values in pb.ListFields():
140 is_repeated = True
141 if desc.label is not descriptor.FieldDescriptor.LABEL_REPEATED:
142 is_repeated = False
143 values = [values]
144
145 normalized_values = None
146
147 # We force 32-bit values to int and 64-bit values to long to make
148 # alternate implementations where the distinction is more significant
149 # (e.g. the C++ implementation) simpler.
150 if desc.type in (descriptor.FieldDescriptor.TYPE_INT64,
151 descriptor.FieldDescriptor.TYPE_UINT64,
152 descriptor.FieldDescriptor.TYPE_SINT64):
153 normalized_values = [int(x) for x in values]
154 elif desc.type in (descriptor.FieldDescriptor.TYPE_INT32,
155 descriptor.FieldDescriptor.TYPE_UINT32,
156 descriptor.FieldDescriptor.TYPE_SINT32,
157 descriptor.FieldDescriptor.TYPE_ENUM):
158 normalized_values = [int(x) for x in values]
159 elif desc.type == descriptor.FieldDescriptor.TYPE_FLOAT:
160 normalized_values = [round(x, 6) for x in values]
161 elif desc.type == descriptor.FieldDescriptor.TYPE_DOUBLE:
162 normalized_values = [round(float(x), 7) for x in values]
163
164 if normalized_values is not None:
165 if is_repeated:
166 pb.ClearField(desc.name)
167 getattr(pb, desc.name).extend(normalized_values)
168 else:
169 setattr(pb, desc.name, normalized_values[0])
170
171 if (desc.type == descriptor.FieldDescriptor.TYPE_MESSAGE or
172 desc.type == descriptor.FieldDescriptor.TYPE_GROUP):
173 if (desc.type == descriptor.FieldDescriptor.TYPE_MESSAGE and
174 desc.message_type.has_options and
175 desc.message_type.GetOptions().map_entry):
176 # This is a map, only recurse if the values have a message type.
177 if (desc.message_type.fields_by_number[2].type ==
178 descriptor.FieldDescriptor.TYPE_MESSAGE):

Callers 1

assertProtoEqualFunction · 0.85

Calls 3

GetOptionsMethod · 0.80
roundFunction · 0.50
extendMethod · 0.45

Tested by

no test coverage detected