當前位置: 首頁>>代碼示例>>Python>>正文


Python settings.BaseSettings類代碼示例

本文整理匯總了Python中scrapy.settings.BaseSettings的典型用法代碼示例。如果您正苦於以下問題:Python BaseSettings類的具體用法?Python BaseSettings怎麽用?Python BaseSettings使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了BaseSettings類的13個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_update_jsonstring

 def test_update_jsonstring(self):
     settings = BaseSettings({'number': 0, 'dict': BaseSettings({'key': 'val'})})
     settings.update('{"number": 1, "newnumber": 2}')
     self.assertEqual(settings['number'], 1)
     self.assertEqual(settings['newnumber'], 2)
     settings.set("dict", '{"key": "newval", "newkey": "newval2"}')
     self.assertEqual(settings['dict']['key'], "newval")
     self.assertEqual(settings['dict']['newkey'], "newval2")
開發者ID:q1ang,項目名稱:scrapy,代碼行數:8,代碼來源:__init__.py

示例2: test_getwithbase

 def test_getwithbase(self):
     s = BaseSettings({'TEST_BASE': BaseSettings({1: 1, 2: 2}, 'project'),
                       'TEST': BaseSettings({1: 10, 3: 30}, 'default'),
                       'HASNOBASE': BaseSettings({3: 3000}, 'default')})
     s['TEST'].set(2, 200, 'cmdline')
     six.assertCountEqual(self, s.getwithbase('TEST'),
                          {1: 1, 2: 200, 3: 30})
     six.assertCountEqual(self, s.getwithbase('HASNOBASE'), s['HASNOBASE'])
     self.assertEqual(s.getwithbase('NONEXISTENT'), {})
開發者ID:LMKight,項目名稱:scrapy,代碼行數:9,代碼來源:__init__.py

示例3: test_setitem

 def test_setitem(self):
     settings = BaseSettings()
     settings.set('key', 'a', 'default')
     settings['key'] = 'b'
     self.assertEqual(settings['key'], 'b')
     self.assertEqual(settings.getpriority('key'), 20)
     settings['key'] = 'c'
     self.assertEqual(settings['key'], 'c')
     settings['key2'] = 'x'
     self.assertIn('key2', settings)
     self.assertEqual(settings['key2'], 'x')
     self.assertEqual(settings.getpriority('key2'), 20)
開發者ID:q1ang,項目名稱:scrapy,代碼行數:12,代碼來源:__init__.py

示例4: test_copy_to_dict

 def test_copy_to_dict(self):
     s = BaseSettings({'TEST_STRING': 'a string',
                       'TEST_LIST': [1, 2],
                       'TEST_BOOLEAN': False,
                       'TEST_BASE': BaseSettings({1: 1, 2: 2}, 'project'),
                       'TEST': BaseSettings({1: 10, 3: 30}, 'default'),
                       'HASNOBASE': BaseSettings({3: 3000}, 'default')})
     self.assertDictEqual(s.copy_to_dict(),
                         {'HASNOBASE': {3: 3000},
                          'TEST': {1: 10, 3: 30},
                          'TEST_BASE': {1: 1, 2: 2},
                          'TEST_BOOLEAN': False,
                          'TEST_LIST': [1, 2],
                          'TEST_STRING': 'a string'})
開發者ID:LMKight,項目名稱:scrapy,代碼行數:14,代碼來源:__init__.py

示例5: test_set_per_key_priorities

    def test_set_per_key_priorities(self):
        attribute = SettingsAttribute(
            BaseSettings({'one': 10, 'two': 20}, 0), 0)

        new_dict = {'one': 11, 'two': 21}
        attribute.set(new_dict, 10)
        self.assertEqual(attribute.value['one'], 11)
        self.assertEqual(attribute.value['two'], 21)

        new_settings = BaseSettings()
        new_settings.set('one', 12, 20)
        new_settings.set('two', 12, 0)
        attribute.set(new_settings, 0)
        self.assertEqual(attribute.value['one'], 12)
        self.assertEqual(attribute.value['two'], 21)
開發者ID:nick198205,項目名稱:scrapy,代碼行數:15,代碼來源:__init__.py

示例6: _map_keys

 def _map_keys(compdict):
     if isinstance(compdict, BaseSettings):
         compbs = BaseSettings()
         for k, v in six.iteritems(compdict):
             prio = compdict.getpriority(k)
             if compbs.getpriority(convert(k)) == prio:
                 raise ValueError('Some paths in {!r} convert to the same '
                                  'object, please update your settings'
                                  ''.format(list(compdict.keys())))
             else:
                 compbs.set(convert(k), v, priority=prio)
         return compbs
     else:
         _check_components(compdict)
         return {convert(k): v for k, v in six.iteritems(compdict)}
開發者ID:nick198205,項目名稱:scrapy,代碼行數:15,代碼來源:conf.py

示例7: test_getcomposite

 def test_getcomposite(self):
     s = BaseSettings({'TEST_BASE': {1: 1, 2: 2},
                       'TEST': BaseSettings({1: 10, 3: 30}, 'default'),
                       'HASNOBASE': BaseSettings({1: 1}, 'default')})
     s['TEST'].set(4, 4, priority='project')
     # When users specify a _BASE setting they explicitly don't want to use
     # Scrapy's defaults, so we don't want to see anything that has a
     # 'default' priority from TEST
     cs = s._getcomposite('TEST')
     self.assertEqual(len(cs), 3)
     self.assertEqual(cs[1], 1)
     self.assertEqual(cs[2], 2)
     self.assertEqual(cs[4], 4)
     cs = s._getcomposite('HASNOBASE')
     self.assertEqual(len(cs), 1)
     self.assertEqual(cs[1], 1)
     cs = s._getcomposite('NONEXISTENT')
     self.assertIsNone(cs)
開發者ID:q1ang,項目名稱:scrapy,代碼行數:18,代碼來源:__init__.py

示例8: test_delete

 def test_delete(self):
     settings = BaseSettings({'key': None})
     settings.set('key_highprio', None, priority=50)
     settings.delete('key')
     settings.delete('key_highprio')
     self.assertNotIn('key', settings)
     self.assertIn('key_highprio', settings)
     del settings['key_highprio']
     self.assertNotIn('key_highprio', settings)
開發者ID:q1ang,項目名稱:scrapy,代碼行數:9,代碼來源:__init__.py

示例9: test_duplicate_components_in_basesettings

 def test_duplicate_components_in_basesettings(self):
     # Higher priority takes precedence
     duplicate_bs = BaseSettings({'one': 1, 'two': 2}, priority=0)
     duplicate_bs.set('ONE', 4, priority=10)
     self.assertEqual(build_component_list(duplicate_bs, convert=lambda x: x.lower()),
                      ['two', 'one'])
     duplicate_bs.set('one', duplicate_bs['one'], priority=20)
     self.assertEqual(build_component_list(duplicate_bs, convert=lambda x: x.lower()),
                      ['one', 'two'])
     # Same priority raises ValueError
     duplicate_bs.set('ONE', duplicate_bs['ONE'], priority=20)
     self.assertRaises(ValueError,
                       build_component_list, duplicate_bs, convert=lambda x: x.lower())
開發者ID:nick198205,項目名稱:scrapy,代碼行數:13,代碼來源:test_utils_conf.py

示例10: setUp

 def setUp(self):
     self.settings = BaseSettings()
開發者ID:q1ang,項目名稱:scrapy,代碼行數:2,代碼來源:__init__.py

示例11: BaseSettingsTest

class BaseSettingsTest(unittest.TestCase):

    if six.PY3:
        assertItemsEqual = unittest.TestCase.assertCountEqual

    def setUp(self):
        self.settings = BaseSettings()

    def test_set_new_attribute(self):
        self.settings.set('TEST_OPTION', 'value', 0)
        self.assertIn('TEST_OPTION', self.settings.attributes)

        attr = self.settings.attributes['TEST_OPTION']
        self.assertIsInstance(attr, SettingsAttribute)
        self.assertEqual(attr.value, 'value')
        self.assertEqual(attr.priority, 0)

    def test_set_settingsattribute(self):
        myattr = SettingsAttribute(0, 30) # Note priority 30
        self.settings.set('TEST_ATTR', myattr, 10)
        self.assertEqual(self.settings.get('TEST_ATTR'), 0)
        self.assertEqual(self.settings.getpriority('TEST_ATTR'), 30)

    def test_set_instance_identity_on_update(self):
        attr = SettingsAttribute('value', 0)
        self.settings.attributes = {'TEST_OPTION': attr}
        self.settings.set('TEST_OPTION', 'othervalue', 10)

        self.assertIn('TEST_OPTION', self.settings.attributes)
        self.assertIs(attr, self.settings.attributes['TEST_OPTION'])

    def test_set_calls_settings_attributes_methods_on_update(self):
        attr = SettingsAttribute('value', 10)
        with mock.patch.object(attr, '__setattr__') as mock_setattr, \
                mock.patch.object(attr, 'set') as mock_set:

            self.settings.attributes = {'TEST_OPTION': attr}

            for priority in (0, 10, 20):
                self.settings.set('TEST_OPTION', 'othervalue', priority)
                mock_set.assert_called_once_with('othervalue', priority)
                self.assertFalse(mock_setattr.called)
                mock_set.reset_mock()
                mock_setattr.reset_mock()

    def test_setitem(self):
        settings = BaseSettings()
        settings.set('key', 'a', 'default')
        settings['key'] = 'b'
        self.assertEqual(settings['key'], 'b')
        self.assertEqual(settings.getpriority('key'), 20)
        settings['key'] = 'c'
        self.assertEqual(settings['key'], 'c')
        settings['key2'] = 'x'
        self.assertIn('key2', settings)
        self.assertEqual(settings['key2'], 'x')
        self.assertEqual(settings.getpriority('key2'), 20)

    def test_setdict_alias(self):
        with mock.patch.object(self.settings, 'set') as mock_set:
            self.settings.setdict({'TEST_1': 'value1', 'TEST_2': 'value2'}, 10)
            self.assertEqual(mock_set.call_count, 2)
            calls = [mock.call('TEST_1', 'value1', 10),
                     mock.call('TEST_2', 'value2', 10)]
            mock_set.assert_has_calls(calls, any_order=True)

    def test_setmodule_only_load_uppercase_vars(self):
        class ModuleMock():
            UPPERCASE_VAR = 'value'
            MIXEDcase_VAR = 'othervalue'
            lowercase_var = 'anothervalue'

        self.settings.attributes = {}
        self.settings.setmodule(ModuleMock(), 10)
        self.assertIn('UPPERCASE_VAR', self.settings.attributes)
        self.assertNotIn('MIXEDcase_VAR', self.settings.attributes)
        self.assertNotIn('lowercase_var', self.settings.attributes)
        self.assertEqual(len(self.settings.attributes), 1)

    def test_setmodule_alias(self):
        with mock.patch.object(self.settings, 'set') as mock_set:
            self.settings.setmodule(default_settings, 10)
            mock_set.assert_any_call('TEST_DEFAULT', 'defvalue', 10)
            mock_set.assert_any_call('TEST_DICT', {'key': 'val'}, 10)

    def test_setmodule_by_path(self):
        self.settings.attributes = {}
        self.settings.setmodule(default_settings, 10)
        ctrl_attributes = self.settings.attributes.copy()

        self.settings.attributes = {}
        self.settings.setmodule(
            'tests.test_settings.default_settings', 10)

        self.assertItemsEqual(six.iterkeys(self.settings.attributes),
                              six.iterkeys(ctrl_attributes))

        for key in six.iterkeys(ctrl_attributes):
            attr = self.settings.attributes[key]
            ctrl_attr = ctrl_attributes[key]
#.........這裏部分代碼省略.........
開發者ID:q1ang,項目名稱:scrapy,代碼行數:101,代碼來源:__init__.py

示例12: test_getpriority

 def test_getpriority(self):
     settings = BaseSettings({'key': 'value'}, priority=99)
     self.assertEqual(settings.getpriority('key'), 99)
     self.assertEqual(settings.getpriority('nonexistentkey'), None)
開發者ID:q1ang,項目名稱:scrapy,代碼行數:4,代碼來源:__init__.py

示例13: test_update

    def test_update(self):
        settings = BaseSettings({'key_lowprio': 0}, priority=0)
        settings.set('key_highprio', 10, priority=50)
        custom_settings = BaseSettings({'key_lowprio': 1, 'key_highprio': 11}, priority=30)
        custom_settings.set('newkey_one', None, priority=50)
        custom_dict = {'key_lowprio': 2, 'key_highprio': 12, 'newkey_two': None}

        settings.update(custom_dict, priority=20)
        self.assertEqual(settings['key_lowprio'], 2)
        self.assertEqual(settings.getpriority('key_lowprio'), 20)
        self.assertEqual(settings['key_highprio'], 10)
        self.assertIn('newkey_two', settings)
        self.assertEqual(settings.getpriority('newkey_two'), 20)

        settings.update(custom_settings)
        self.assertEqual(settings['key_lowprio'], 1)
        self.assertEqual(settings.getpriority('key_lowprio'), 30)
        self.assertEqual(settings['key_highprio'], 10)
        self.assertIn('newkey_one', settings)
        self.assertEqual(settings.getpriority('newkey_one'), 50)

        settings.update({'key_lowprio': 3}, priority=20)
        self.assertEqual(settings['key_lowprio'], 1)
開發者ID:q1ang,項目名稱:scrapy,代碼行數:23,代碼來源:__init__.py


注:本文中的scrapy.settings.BaseSettings類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。