Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
dicts.py95 linesDownload Raw Back to cli
1from collections import OrderedDict2 3from multidict import MultiDict, CIMultiDict4 5 6class BaseMultiDict(MultiDict):7    """8    Base class for all MultiDicts.9    """10 11 12class HTTPHeadersDict(CIMultiDict, BaseMultiDict):13    """14    Headers are case-insensitive and multiple values are supported15    through the `add()` API.16    """17 18    def add(self, key, value):19        """20        Add or update a new header.21 22        If the given `value` is `None`, then all the previous23        values will be overwritten and the value will be set24        to `None`.25        """26        if value is None:27            self[key] = value28            return None29 30        # If the previous value for the given header is `None`31        # then discard it since we are explicitly giving a new32        # value for it.33        if key in self and self.getone(key) is None:34            self.popone(key)35 36        super().add(key, value)37 38    def remove_item(self, key, value):39        """40        Remove a (key, value) pair from the dict.41        """42        existing_values = self.popall(key)43        existing_values.remove(value)44 45        for value in existing_values:46            self.add(key, value)47 48 49class RequestJSONDataDict(OrderedDict):50    pass51 52 53class MultiValueOrderedDict(OrderedDict):54    """Multi-value dict for URL parameters and form data."""55 56    def __setitem__(self, key, value):57        """58        If `key` is assigned more than once, `self[key]` holds a59        `list` of all the values.60 61        This allows having multiple fields with the same name in form62        data and URL params.63 64        """65        assert not isinstance(value, list)66        if key not in self:67            super().__setitem__(key, value)68        else:69            if not isinstance(self[key], list):70                super().__setitem__(key, [self[key]])71            self[key].append(value)72 73    def items(self):74        for key, values in super().items():75            if not isinstance(values, list):76                values = [values]77            for value in values:78                yield key, value79 80 81class RequestQueryParamsDict(MultiValueOrderedDict):82    pass83 84 85class RequestDataDict(MultiValueOrderedDict):86    pass87 88 89class MultipartRequestDataDict(MultiValueOrderedDict):90    pass91 92 93class RequestFilesDict(RequestDataDict):94    pass95 
codekingpro/portable-devtools · Team Ai