コード例 #1
0
    def test_create(self):
        self.assertEqual(Quantity().raw_quant, '*')
        good = [
            '   *', ' +    ', '?    ', ' 1321     ', '007',
            ' 8800,    9231   ', '1,2', '9999', '5,5'
        ]
        for g in good:
            Quantity(g)

        bad = [
            '', None, ' * * ', '+*', '', '1 2', '1,2,3', '10000', '+2', '-2',
            '3,2'
        ]
        for b in bad:
            self.assertRaises(Exception, Quantity, b)
コード例 #2
0
ファイル: s.py プロジェクト: dahuangfeng123/crawlmi
    def __init__(self,
                 name,
                 xpath=None,
                 quant='*',
                 value=None,
                 callback=None,
                 group=None,
                 children=None,
                 css=None,
                 filter=None):
        '''
        name - the parsed item will be stored in a dictionary on this index (not to store the item, use '_' as a prefix of the name).
        xpath - xpath to the item.
        quant - used for validation. The expected number of items to be parsed.
        value - specify it, if you want the items to be extracted. Otherwise selector objects will be returned.
        callback - callback function to be called on each found item. It can take named argument "context", which is dictionary containing additionnal values.
        group - if not None, all the child nodes will be stored under one dictionary entry of group's name
        children - list of nested S objects. For each item found, each child will be called with the item as the selector.
        css - css selector, in a case xpath is not defined
        filter - one-argument function. Given the node from the xpath, return true, if to the node. Otherwise return False.
                 `quant` is checked AFTER the filter is applied.
        '''
        if (xpath is None) == (css is None):
            raise TypeError(
                'Exactly one of `xpath` or `css` arguments must be specified.')

        self.name = name
        if xpath is not None:
            self.raw_xpath = xpath
        elif css_supported:
            self.raw_xpath = GenericTranslator().css_to_xpath(css)
        else:
            raise TypeError(
                'Css selectors not supported, install cssselect library.')
        self.hashed_namespaces = None
        self.compiled_xpath = None
        self.quant = Quantity(quant)
        self.raw_value = value
        self.compiled_value = None
        self.callback = callback
        self.context_callback = callback and 'context' in get_func_args(
            callback)
        self.group = group
        self.children = children if children is not None else []
        self.filter = filter
コード例 #3
0
 def test_dig_2d(self):
     q = Quantity('5, 10')
     self._test_good(q, [5, 6, 7, 8, 9, 10])
     self._test_bad(q, [0, 1, 2, 3, 4, 11, 12, 13, -5, -10])
コード例 #4
0
 def test_dig_1d(self):
     q = Quantity('47')
     self._test_good(q, [47])
     self._test_bad(q, [0, 1, -1, -47, 100])
コード例 #5
0
 def test_ques(self):
     q = Quantity('?')
     self._test_good(q, [0, 1])
     self._test_bad(q, [-2, -1, 2, 3, 10, 100])
コード例 #6
0
 def test_plus(self):
     q = Quantity('+')
     self._test_good(q, [1, 2, 5, 10, 1000, 2**30])
     self._test_bad(q, [0, -1, -2])
コード例 #7
0
 def test_star(self):
     q = Quantity('*')
     self._test_good(q, [0, 1, 2, 5, 10, 1000, 2**30])
     self._test_bad(q, [-1, -2])
コード例 #8
0
 def test_err(self):
     q = Quantity('*')
     err = ['0', [0], None, 'help']
     for e in err:
         self.assertRaises(ValueError, q.check_quantity, e)