Changeset 574adc7 in sasview for src/sas/sascalc/data_util/odict.py
- Timestamp:
- Sep 22, 2017 4:01:32 PM (7 years ago)
- Branches:
- master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, magnetic_scatt, release-4.2.2, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
- Children:
- 34d7b35
- Parents:
- 9706d88
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sas/sascalc/data_util/odict.py
rb699768 r574adc7 39 39 """ 40 40 A class of dictionary that keeps the insertion order of keys. 41 41 42 42 All appropriate methods return keys, items, or values in an ordered way. 43 43 44 44 All normal dictionary methods are available. Update and comparison is 45 45 restricted to other OrderedDict objects. 46 46 47 47 Various sequence methods are available, including the ability to explicitly 48 48 mutate the key ordering. 49 49 50 50 __contains__ tests: 51 51 52 52 >>> d = OrderedDict(((1, 3),)) 53 53 >>> 1 in d … … 55 55 >>> 4 in d 56 56 0 57 57 58 58 __getitem__ tests: 59 59 60 60 >>> OrderedDict(((1, 3), (3, 2), (2, 1)))[2] 61 61 1 … … 63 63 Traceback (most recent call last): 64 64 KeyError: 4 65 65 66 66 __len__ tests: 67 67 68 68 >>> len(OrderedDict()) 69 69 0 70 70 >>> len(OrderedDict(((1, 3), (3, 2), (2, 1)))) 71 71 3 72 72 73 73 get tests: 74 74 75 75 >>> d = OrderedDict(((1, 3), (3, 2), (2, 1))) 76 76 >>> d.get(1) … … 82 82 >>> d 83 83 OrderedDict([(1, 3), (3, 2), (2, 1)]) 84 84 85 85 has_key tests: 86 86 87 87 >>> d = OrderedDict(((1, 3), (3, 2), (2, 1))) 88 88 >>> d.has_key(1) … … 96 96 Create a new ordered dictionary. Cannot init from a normal dict, 97 97 nor from kwargs, since items order is undefined in those cases. 98 98 99 99 If the ``strict`` keyword argument is ``True`` (``False`` is the 100 100 default) then when doing slice assignment - the ``OrderedDict`` you are 101 101 assigning from *must not* contain any keys in the remaining dict. 102 102 103 103 >>> OrderedDict() 104 104 OrderedDict([]) … … 283 283 """ 284 284 Used for __repr__ and __str__ 285 285 286 286 >>> r1 = repr(OrderedDict((('a', 'b'), ('c', 'd'), ('e', 'f')))) 287 287 >>> r1 … … 321 321 >>> d 322 322 OrderedDict([(0, 1), (1, 2), (5, 6), (7, 8), (3, 4)]) 323 323 324 324 >>> a = OrderedDict(((0, 1), (1, 2), (2, 3)), strict=True) 325 325 >>> a[3] = 4 … … 345 345 >>> a 346 346 OrderedDict([(3, 4), (2, 3), (1, 2), (0, 1)]) 347 347 348 348 >>> d = OrderedDict([(0, 1), (1, 2), (2, 3), (3, 4)]) 349 349 >>> d[:1] = 3 350 350 Traceback (most recent call last): 351 351 TypeError: slice assignment requires an OrderedDict 352 352 353 353 >>> d = OrderedDict([(0, 1), (1, 2), (2, 3), (3, 4)]) 354 354 >>> d[:1] = OrderedDict([(9, 8)]) … … 444 444 """ 445 445 Implemented so that access to ``sequence`` raises a warning. 446 446 447 447 >>> d = OrderedDict() 448 448 >>> d.sequence … … 463 463 """ 464 464 To allow deepcopy to work with OrderedDict. 465 465 466 466 >>> from copy import deepcopy 467 467 >>> a = OrderedDict([(1, 1), (2, 2), (3, 3)]) … … 490 490 def items(self): 491 491 """ 492 ``items`` returns a list of tuples representing all the 492 ``items`` returns a list of tuples representing all the 493 493 ``(key, value)`` pairs in the dictionary. 494 494 495 495 >>> d = OrderedDict(((1, 3), (3, 2), (2, 1))) 496 496 >>> d.items() … … 505 505 """ 506 506 Return a list of keys in the ``OrderedDict``. 507 507 508 508 >>> d = OrderedDict(((1, 3), (3, 2), (2, 1))) 509 509 >>> d.keys() … … 515 515 """ 516 516 Return a list of all the values in the OrderedDict. 517 517 518 518 Optionally you can pass in a list of values, which will replace the 519 519 current list. The value list must be the same len as the OrderedDict. 520 520 521 521 >>> d = OrderedDict(((1, 3), (3, 2), (2, 1))) 522 522 >>> d.values() … … 596 596 """ 597 597 No dict.pop in Python 2.2, gotta reimplement it 598 598 599 599 >>> d = OrderedDict(((1, 3), (3, 2), (2, 1))) 600 600 >>> d.pop(3) … … 612 612 """ 613 613 if len(args) > 1: 614 raise TypeError ,('pop expected at most 2 arguments, got %s' %614 raise TypeError('pop expected at most 2 arguments, got %s' % 615 615 (len(args) + 1)) 616 616 if key in self: … … 628 628 Delete and return an item specified by index, not a random one as in 629 629 dict. The index is -1 by default (the last item). 630 630 631 631 >>> d = OrderedDict(((1, 3), (3, 2), (2, 1))) 632 632 >>> d.popitem() … … 674 674 """ 675 675 Update from another OrderedDict or sequence of (key, value) pairs 676 676 677 677 >>> d = OrderedDict(((1, 0), (0, 1))) 678 678 >>> d.update(OrderedDict(((1, 3), (3, 2), (2, 1)))) … … 706 706 """ 707 707 Rename the key for a given value, without modifying sequence order. 708 708 709 709 For the case where new_key already exists this raise an exception, 710 710 since if new_key exists, it is ambiguous as to what happens to the 711 711 associated values, and the position of new_key in the sequence. 712 712 713 713 >>> od = OrderedDict() 714 714 >>> od['a'] = 1 … … 732 732 raise ValueError("New key already exists: %r" % new_key) 733 733 # rename sequence entry 734 value = self[old_key] 734 value = self[old_key] 735 735 old_idx = self._sequence.index(old_key) 736 736 self._sequence[old_idx] = new_key … … 742 742 """ 743 743 This method allows you to set the items in the dict. 744 744 745 745 It takes a list of tuples - of the same sort returned by the ``items`` 746 746 method. 747 747 748 748 >>> d = OrderedDict() 749 749 >>> d.setitems(((3, 1), (2, 3), (1, 2))) … … 760 760 replace the current set. This must contain the same set of keys, but 761 761 need not be in the same order. 762 762 763 763 If you pass in new keys that don't match, a ``KeyError`` will be 764 764 raised. 765 765 766 766 >>> d = OrderedDict(((1, 3), (3, 2), (2, 1))) 767 767 >>> d.keys() … … 791 791 You can pass in a list of values, which will replace the 792 792 current list. The value list must be the same len as the OrderedDict. 793 793 794 794 (Or a ``ValueError`` is raised.) 795 795 796 796 >>> d = OrderedDict(((1, 3), (3, 2), (2, 1))) 797 797 >>> d.setvalues((1, 2, 3)) … … 813 813 """ 814 814 Return the position of the specified key in the OrderedDict. 815 815 816 816 >>> d = OrderedDict(((1, 3), (3, 2), (2, 1))) 817 817 >>> d.index(3) … … 826 826 """ 827 827 Takes ``index``, ``key``, and ``value`` as arguments. 828 828 829 829 Sets ``key`` to ``value``, so that ``key`` is at position ``index`` in 830 830 the OrderedDict. 831 831 832 832 >>> d = OrderedDict(((1, 3), (3, 2), (2, 1))) 833 833 >>> d.insert(0, 4, 0) … … 850 850 """ 851 851 Reverse the order of the OrderedDict. 852 852 853 853 >>> d = OrderedDict(((1, 3), (3, 2), (2, 1))) 854 854 >>> d.reverse() … … 861 861 """ 862 862 Sort the key order in the OrderedDict. 863 863 864 864 This method takes the same arguments as the ``list.sort`` method on 865 865 your version of Python. 866 866 867 867 >>> d = OrderedDict(((4, 1), (2, 2), (3, 3), (1, 4))) 868 868 >>> d.sort() … … 876 876 """ 877 877 Custom object for accessing the keys of an OrderedDict. 878 878 879 879 Can be called like the normal ``OrderedDict.keys`` method, but also 880 880 supports indexing and sequence methods. … … 897 897 You cannot assign to keys, but you can do slice assignment to re-order 898 898 them. 899 899 900 900 You can only do slice assignment if the new set of keys is a reordering 901 901 of the original set. … … 967 967 """ 968 968 Custom object for accessing the items of an OrderedDict. 969 969 970 970 Can be called like the normal ``OrderedDict.items`` method, but also 971 971 supports indexing and sequence methods. … … 1077 1077 """ 1078 1078 Custom object for accessing the values of an OrderedDict. 1079 1079 1080 1080 Can be called like the normal ``OrderedDict.values`` method, but also 1081 1081 supports indexing and sequence methods. … … 1099 1099 """ 1100 1100 Set the value at position i to value. 1101 1101 1102 1102 You can only do slice assignment to values if you supply a sequence of 1103 1103 equal length to the slice you are replacing. … … 1168 1168 Experimental version of OrderedDict that has a custom object for ``keys``, 1169 1169 ``values``, and ``items``. 1170 1170 1171 1171 These are callable sequence objects that work as methods, or can be 1172 1172 manipulated directly as sequences. 1173 1173 1174 1174 Test for ``keys``, ``items`` and ``values``. 1175 1175 1176 1176 >>> d = SequenceOrderedDict(((1, 2), (2, 3), (3, 4))) 1177 1177 >>> d … … 1293 1293 >>> d 1294 1294 SequenceOrderedDict([(1, 1), (2, 2), (3, 3)]) 1295 1295 1296 1296 >>> d = SequenceOrderedDict(((1, 2), (2, 3), (3, 4))) 1297 1297 >>> d
Note: See TracChangeset
for help on using the changeset viewer.