OLD | NEW |
1 # Protocol Buffers - Google's data interchange format | 1 # Protocol Buffers - Google's data interchange format |
2 # Copyright 2008 Google Inc. All rights reserved. | 2 # Copyright 2008 Google Inc. All rights reserved. |
3 # https://developers.google.com/protocol-buffers/ | 3 # https://developers.google.com/protocol-buffers/ |
4 # | 4 # |
5 # Redistribution and use in source and binary forms, with or without | 5 # Redistribution and use in source and binary forms, with or without |
6 # modification, are permitted provided that the following conditions are | 6 # modification, are permitted provided that the following conditions are |
7 # met: | 7 # met: |
8 # | 8 # |
9 # * Redistributions of source code must retain the above copyright | 9 # * Redistributions of source code must retain the above copyright |
10 # notice, this list of conditions and the following disclaimer. | 10 # notice, this list of conditions and the following disclaimer. |
(...skipping 418 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
429 raise TypeError('Can only compare repeated composite fields against ' | 429 raise TypeError('Can only compare repeated composite fields against ' |
430 'other repeated composite fields.') | 430 'other repeated composite fields.') |
431 return self._values == other._values | 431 return self._values == other._values |
432 | 432 |
433 | 433 |
434 class ScalarMap(MutableMapping): | 434 class ScalarMap(MutableMapping): |
435 | 435 |
436 """Simple, type-checked, dict-like container for holding repeated scalars.""" | 436 """Simple, type-checked, dict-like container for holding repeated scalars.""" |
437 | 437 |
438 # Disallows assignment to other attributes. | 438 # Disallows assignment to other attributes. |
439 __slots__ = ['_key_checker', '_value_checker', '_values', '_message_listener', | 439 __slots__ = ['_key_checker', '_value_checker', '_values', '_message_listener'] |
440 '_entry_descriptor'] | |
441 | 440 |
442 def __init__(self, message_listener, key_checker, value_checker, | 441 def __init__(self, message_listener, key_checker, value_checker): |
443 entry_descriptor): | |
444 """ | 442 """ |
445 Args: | 443 Args: |
446 message_listener: A MessageListener implementation. | 444 message_listener: A MessageListener implementation. |
447 The ScalarMap will call this object's Modified() method when it | 445 The ScalarMap will call this object's Modified() method when it |
448 is modified. | 446 is modified. |
449 key_checker: A type_checkers.ValueChecker instance to run on keys | 447 key_checker: A type_checkers.ValueChecker instance to run on keys |
450 inserted into this container. | 448 inserted into this container. |
451 value_checker: A type_checkers.ValueChecker instance to run on values | 449 value_checker: A type_checkers.ValueChecker instance to run on values |
452 inserted into this container. | 450 inserted into this container. |
453 entry_descriptor: The MessageDescriptor of a map entry: key and value. | |
454 """ | 451 """ |
455 self._message_listener = message_listener | 452 self._message_listener = message_listener |
456 self._key_checker = key_checker | 453 self._key_checker = key_checker |
457 self._value_checker = value_checker | 454 self._value_checker = value_checker |
458 self._entry_descriptor = entry_descriptor | |
459 self._values = {} | 455 self._values = {} |
460 | 456 |
461 def __getitem__(self, key): | 457 def __getitem__(self, key): |
462 try: | 458 try: |
463 return self._values[key] | 459 return self._values[key] |
464 except KeyError: | 460 except KeyError: |
465 key = self._key_checker.CheckValue(key) | 461 key = self._key_checker.CheckValue(key) |
466 val = self._value_checker.DefaultValue() | 462 val = self._value_checker.DefaultValue() |
467 self._values[key] = val | 463 self._values[key] = val |
468 return val | 464 return val |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
510 # self._values is to ensure that its size changes. | 506 # self._values is to ensure that its size changes. |
511 original = self._values | 507 original = self._values |
512 self._values = original.copy() | 508 self._values = original.copy() |
513 original[None] = None | 509 original[None] = None |
514 | 510 |
515 # This is defined in the abstract base, but we can do it much more cheaply. | 511 # This is defined in the abstract base, but we can do it much more cheaply. |
516 def clear(self): | 512 def clear(self): |
517 self._values.clear() | 513 self._values.clear() |
518 self._message_listener.Modified() | 514 self._message_listener.Modified() |
519 | 515 |
520 def GetEntryClass(self): | |
521 return self._entry_descriptor._concrete_class | |
522 | |
523 | 516 |
524 class MessageMap(MutableMapping): | 517 class MessageMap(MutableMapping): |
525 | 518 |
526 """Simple, type-checked, dict-like container for with submessage values.""" | 519 """Simple, type-checked, dict-like container for with submessage values.""" |
527 | 520 |
528 # Disallows assignment to other attributes. | 521 # Disallows assignment to other attributes. |
529 __slots__ = ['_key_checker', '_values', '_message_listener', | 522 __slots__ = ['_key_checker', '_values', '_message_listener', |
530 '_message_descriptor', '_entry_descriptor'] | 523 '_message_descriptor'] |
531 | 524 |
532 def __init__(self, message_listener, message_descriptor, key_checker, | 525 def __init__(self, message_listener, message_descriptor, key_checker): |
533 entry_descriptor): | |
534 """ | 526 """ |
535 Args: | 527 Args: |
536 message_listener: A MessageListener implementation. | 528 message_listener: A MessageListener implementation. |
537 The ScalarMap will call this object's Modified() method when it | 529 The ScalarMap will call this object's Modified() method when it |
538 is modified. | 530 is modified. |
539 key_checker: A type_checkers.ValueChecker instance to run on keys | 531 key_checker: A type_checkers.ValueChecker instance to run on keys |
540 inserted into this container. | 532 inserted into this container. |
541 value_checker: A type_checkers.ValueChecker instance to run on values | 533 value_checker: A type_checkers.ValueChecker instance to run on values |
542 inserted into this container. | 534 inserted into this container. |
543 entry_descriptor: The MessageDescriptor of a map entry: key and value. | |
544 """ | 535 """ |
545 self._message_listener = message_listener | 536 self._message_listener = message_listener |
546 self._message_descriptor = message_descriptor | 537 self._message_descriptor = message_descriptor |
547 self._key_checker = key_checker | 538 self._key_checker = key_checker |
548 self._entry_descriptor = entry_descriptor | |
549 self._values = {} | 539 self._values = {} |
550 | 540 |
551 def __getitem__(self, key): | 541 def __getitem__(self, key): |
552 try: | 542 try: |
553 return self._values[key] | 543 return self._values[key] |
554 except KeyError: | 544 except KeyError: |
555 key = self._key_checker.CheckValue(key) | 545 key = self._key_checker.CheckValue(key) |
556 new_element = self._message_descriptor._concrete_class() | 546 new_element = self._message_descriptor._concrete_class() |
557 new_element._SetListener(self._message_listener) | 547 new_element._SetListener(self._message_listener) |
558 self._values[key] = new_element | 548 self._values[key] = new_element |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
597 return len(self._values) | 587 return len(self._values) |
598 | 588 |
599 def __iter__(self): | 589 def __iter__(self): |
600 return iter(self._values) | 590 return iter(self._values) |
601 | 591 |
602 def __repr__(self): | 592 def __repr__(self): |
603 return repr(self._values) | 593 return repr(self._values) |
604 | 594 |
605 def MergeFrom(self, other): | 595 def MergeFrom(self, other): |
606 for key in other: | 596 for key in other: |
607 # According to documentation: "When parsing from the wire or when merging, | 597 self[key].MergeFrom(other[key]) |
608 # if there are duplicate map keys the last key seen is used". | |
609 if key in self: | |
610 del self[key] | |
611 self[key].CopyFrom(other[key]) | |
612 # self._message_listener.Modified() not required here, because | 598 # self._message_listener.Modified() not required here, because |
613 # mutations to submessages already propagate. | 599 # mutations to submessages already propagate. |
614 | 600 |
615 def InvalidateIterators(self): | 601 def InvalidateIterators(self): |
616 # It appears that the only way to reliably invalidate iterators to | 602 # It appears that the only way to reliably invalidate iterators to |
617 # self._values is to ensure that its size changes. | 603 # self._values is to ensure that its size changes. |
618 original = self._values | 604 original = self._values |
619 self._values = original.copy() | 605 self._values = original.copy() |
620 original[None] = None | 606 original[None] = None |
621 | 607 |
622 # This is defined in the abstract base, but we can do it much more cheaply. | 608 # This is defined in the abstract base, but we can do it much more cheaply. |
623 def clear(self): | 609 def clear(self): |
624 self._values.clear() | 610 self._values.clear() |
625 self._message_listener.Modified() | 611 self._message_listener.Modified() |
626 | |
627 def GetEntryClass(self): | |
628 return self._entry_descriptor._concrete_class | |
OLD | NEW |