Пример #1
0
    def test_pickle_dict(self):
        items = {
            'name': u'jon',
            'animal': 'dog',
            'boolean': True,
            'number': 42,
            'list': ['a', 'b'],
            'dict': {
                'foo': 'bar'
            },
        }
        pickled = pickle_dict(items)
        expected = (
            ('name', u'jon'),
            ('animal', 'dog'),
            ('boolean', 'I01\n.'),
            ('number', 'I42\n.'),
            (
                'list',
                "(lp1\nS'a'\naS'b'\na.",
            ),
            ('dict', "(dp1\nS'foo'\np2\nS'bar'\np3\ns."),
        )

        self.assertEqual(len(pickled.items()), 7)
        for key, val in expected:
            self.assertEqual(pickled.get(key), val)
        fields = pickled.get('_pickled').split(',')
        self.assertEqual(sorted(fields), ['boolean', 'dict', 'list', 'number'])
        self.assertEqual(unpickle_dict(pickled), items)
        self.assertEqual(unpickle_dict(pickle_dict(items)), items)
Пример #2
0
    def test_pickle_dict(self):
        items = {
            'name': u'jon',
            'animal': 'dog',
            'boolean': True,
            'number': 42,
            'list': ['a', 'b'],
            'dict': {'foo': 'bar'},
        }
        pickled = pickle_dict(items)
        expected = (
            ('name', u'jon'),
            ('animal', 'dog'),
            ('boolean', 'I01\n.'),
            ('number', 'I42\n.'),
            ('list', "(lp0\nS'a'\np1\naS'b'\np2\na."),
            ('dict', "(dp0\nS'foo'\np1\nS'bar'\np2\ns."),
        )


        self.assertEqual(len(pickled.items()), 7)
        for key, val in expected:
            self.assertEqual(pickled.get(key), val)
        fields = pickled.get('_pickled').split(',')
        self.assertEqual(sorted(fields), ['boolean', 'dict', 'list', 'number'])
        self.assertEqual(unpickle_dict(pickled), items)
        self.assertEqual(unpickle_dict(pickle_dict(items)), items)
Пример #3
0
    def match(self, path):
        """Attempts to match a url to the given path. If successful, a tuple is
        returned. The first item is the matchd function and the second item is
        a dictionary containing items to be passed to the function parsed from
        the provided path.

        If the provided path does not match this url rule then a
        NotFoundException is raised.
        """
        m = self._regex.search(path)
        if not m:
            raise NotFoundException

        # urlunencode the values
        items = dict((key, unquote_plus(val))
                     for key, val in m.groupdict().items())

        # unpickle any items if present
        items = unpickle_dict(items)

        # Convert string to integers where possible
        for key, val in items.items():
            if isinstance(val, basestring):
                try:
                    items[key] = int(val)
                except ValueError:
                    pass

        # We need to update our dictionary with default values provided in
        # options if the keys don't already exist.
        [items.setdefault(key, val) for key, val in self._options.items()]
        return self._view_func, items
Пример #4
0
    def match(self, path):
        """Attempts to match a url to the given path. If successful, a tuple is
        returned. The first item is the matchd function and the second item is
        a dictionary containing items to be passed to the function parsed from
        the provided path.

        If the provided path does not match this url rule then a
        NotFoundException is raised.
        """
        m = self._regex.search(path)
        if not m:
            raise NotFoundException

        # urlunencode the values
        items = dict(
            (key, unquote_plus(val)) for key, val in m.groupdict().items())

        # unpickle any items if present
        items = unpickle_dict(items)

        # Convert string to integers where possible
        for key, val in items.items():
            if isinstance(val, basestring):
                try:
                    items[key] = int(val)
                except ValueError:
                    pass

        # We need to update our dictionary with default values provided in
        # options if the keys don't already exist.
        [items.setdefault(key, val) for key, val in self._options.items()]
        return self._view_func, items
Пример #5
0
    def match(self, path):
        '''Attempts to match a url to the given path. If successful, a tuple is
        returned. The first item is the matchd function and the second item is
        a dictionary containing items to be passed to the function parsed from
        the provided path.

        If the provided path does not match this url rule then a
        NotFoundException is raised.
        '''
        m = self._regex.search(path)
        if not m:
            raise NotFoundException

        # urlunencode the values
        items = dict(
            (key, unquote_plus(val)) for key, val in m.groupdict().items())

        # unpickle any items if present
        items = unpickle_dict(items)

        # We need to update our dictionary with default values provided in
        # options if the keys don't already exist.
        [items.setdefault(key, val) for key, val in self._options.items()]
        return self._view_func, items
Пример #6
0
    def match(self, path):
        '''Attempts to match a url to the given path. If successful, a tuple is
        returned. The first item is the matchd function and the second item is
        a dictionary containing items to be passed to the function parsed from
        the provided path.

        If the provided path does not match this url rule then a
        NotFoundException is raised.
        '''
        m = self._regex.search(path)
        if not m:
            raise NotFoundException

        # urlunencode the values
        items = dict((key, unquote_plus(val))
                     for key, val in m.groupdict().items())

        # unpickle any items if present
        items = unpickle_dict(items)

        # We need to update our dictionary with default values provided in
        # options if the keys don't already exist.
        [items.setdefault(key, val) for key, val in self._options.items()]
        return self._view_func, items