Ticket #2441: multivalueupdate.diff

File multivalueupdate.diff, 1.8 KB (added by Pete Shinners <pete@…>, 9 years ago)

Patch for MultiValueDict.update

  • utils/datastructures.py

     
    187187        "Returns a copy of this object."
    188188        return self.__deepcopy__()
    189189
    190     def update(self, other_dict):
    191         "update() extends rather than replaces existing key lists."
    192         if isinstance(other_dict, MultiValueDict):
    193             for key, value_list in other_dict.lists():
    194                 self.setlistdefault(key, []).extend(value_list)
    195         else:
    196             try:
    197                 for key, value in other_dict.items():
    198                     self.setlistdefault(key, []).append(value)
    199             except TypeError:
    200                 raise ValueError, "MultiValueDict.update() takes either a MultiValueDict or dictionary"
     190    def update(self, *args, **kwargs):
     191        "update() extends rather than replaces existing key lists. also accepts keyword args."
     192        if len(args) > 1:
     193            raise TypeError, "update expected at most 1 arguments, got %d", len(args)
     194        if args:
     195            other_dict = args[0]
     196            if isinstance(other_dict, MultiValueDict):
     197                for key, value_list in other_dict.lists():
     198                    self.setlistdefault(key, []).extend(value_list)
     199            else:
     200                try:
     201                    for key, value in other_dict.items():
     202                        self.setlistdefault(key, []).append(value)
     203                except TypeError:
     204                    raise ValueError, "MultiValueDict.update() takes either a MultiValueDict or dictionary"
     205        for key, value in kwargs.iteritems():
     206            self.setlistdefault(key, []).append(value)
    201207
    202208class DotExpandedDict(dict):
    203209    """
Back to Top