コード例 #1
0
def test_map_items():
    def add10(x):
        return x + 10
    eq_(map_items(add10, {2: 3}), {12: 13})

    class Custom(object):
        """For testing with custom items possibly of varying length etc"""
        def __init__(self, items):
            self._items = list(items)

        def items(self):
            return self._items

    c = Custom([(1,), (2, 3), (4, 5, 6)])
    c_mapped = map_items(add10, c)
    assert type(c) is type(c_mapped)
    eq_(c_mapped.items(), [(11,), (12, 13), (14, 15, 16)])
コード例 #2
0
ファイル: network.py プロジェクト: christian-monch/datalad
 def _parse_qs(self, s, auto_delist=True):
     """Helper around parse_qs to strip unneeded 'list'ing etc and return a dict of key=values"""
     if not s:
         return {}
     out = map_items(ensure_unicode, OrderedDict(parse_qsl(s, 1)))
     if not auto_delist:
         return out
     for k in out:
         v = out[k]
         if isinstance(v, list) and len(v) == 1:
             v = v[0]
             out[k] = None if v == '' else v
     return out
コード例 #3
0
ファイル: network.py プロジェクト: datalad/datalad
 def _parse_qs(self, s, auto_delist=True):
     """Helper around parse_qs to strip unneeded 'list'ing etc and return a dict of key=values"""
     if not s:
         return {}
     out = map_items(assure_unicode, OrderedDict(parse_qsl(s, 1)))
     if not auto_delist:
         return out
     for k in out:
         v = out[k]
         if isinstance(v, list) and len(v) == 1:
             v = v[0]
             out[k] = None if v == '' else v
     return out
コード例 #4
0
ファイル: network.py プロジェクト: datalad/datalad
    def _set_from_fields(self, **fields):
        unknown_fields = set(fields).difference(self._FIELDS)
        if unknown_fields:
            raise ValueError("Do not know about %s. Known fields for %s are: %s"
                             % (unknown_fields, self.__class__, self._FIELDS))

        # encode dicts for query or fragment into
        for f in {'query', 'fragment'}:
            v = fields.get(f)
            if isinstance(v, dict):

                ev = urlencode(map_items(assure_bytes, v))
                # / is reserved char within query
                if f == 'fragment' and '%2F' not in str(v):
                    # but seems to be ok'ish within the fragment which is
                    # the last element of URI and anyways used only by the
                    # client (i.e. by us here if used to compose the URL)
                    # so let's return / back for clarity if there were no
                    # awkward %2F to startswith
                    ev = ev.replace('%2F', '/')
                fields[f] = ev

        self._fields.update(fields)
コード例 #5
0
    def _set_from_fields(self, **fields):
        unknown_fields = set(fields).difference(self._FIELDS)
        if unknown_fields:
            raise ValueError("Do not know about %s. Known fields for %s are: %s"
                             % (unknown_fields, self.__class__, self._FIELDS))

        # encode dicts for query or fragment into
        for f in {'query', 'fragment'}:
            v = fields.get(f)
            if isinstance(v, dict):

                ev = urlencode(map_items(ensure_bytes, v))
                # / is reserved char within query
                if f == 'fragment' and '%2F' not in str(v):
                    # but seems to be ok'ish within the fragment which is
                    # the last element of URI and anyways used only by the
                    # client (i.e. by us here if used to compose the URL)
                    # so let's return / back for clarity if there were no
                    # awkward %2F to startswith
                    ev = ev.replace('%2F', '/')
                fields[f] = ev

        self._fields.update(fields)