OLD | NEW |
1 # Copyright 2015 The LUCI Authors. All rights reserved. | 1 # Copyright 2015 The LUCI Authors. All rights reserved. |
2 # Use of this source code is governed by the Apache v2.0 license that can be | 2 # Use of this source code is governed under the Apache License, Version 2.0 |
3 # found in the LICENSE file. | 3 # that can be found in the LICENSE file. |
4 | 4 |
5 """Miscellaneous helper functions.""" | 5 """Miscellaneous helper functions.""" |
6 | 6 |
7 import collections | 7 import collections |
8 | 8 |
9 | 9 |
10 def fingerprint(proto): | 10 def fingerprint(proto): |
11 """Returns a fingerprint of a simple messages.Message instance. | 11 """Returns a fingerprint of a simple messages.Message instance. |
12 | 12 |
13 Args: | 13 Args: |
14 proto: A messages.Message instance. | 14 proto: A messages.Message instance. |
15 | 15 |
16 Returns: | 16 Returns: |
17 A string which uniquely identifies proto. | 17 A string which uniquely identifies proto. |
18 """ | 18 """ |
19 components = collections.OrderedDict() | 19 components = collections.OrderedDict() |
20 | 20 |
21 for field in sorted(proto.all_fields(), key=lambda field: field.number): | 21 for field in sorted(proto.all_fields(), key=lambda field: field.number): |
22 if proto.get_assigned_value(field.name) is not None: | 22 if proto.get_assigned_value(field.name) is not None: |
23 components[field.number] = proto.get_assigned_value(field.name) | 23 components[field.number] = proto.get_assigned_value(field.name) |
24 | 24 |
25 return '\0'.join( | 25 return '\0'.join( |
26 '%s\0%s' % (key, value) for key, value in components.iteritems() | 26 '%s\0%s' % (key, value) for key, value in components.iteritems() |
27 ) | 27 ) |
OLD | NEW |