当前位置: 首页>>代码示例>>Python>>正文


Python BaseSettings.freeze方法代码示例

本文整理汇总了Python中scrapy.settings.BaseSettings.freeze方法的典型用法代码示例。如果您正苦于以下问题:Python BaseSettings.freeze方法的具体用法?Python BaseSettings.freeze怎么用?Python BaseSettings.freeze使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在scrapy.settings.BaseSettings的用法示例。


在下文中一共展示了BaseSettings.freeze方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: BaseSettingsTest

# 需要导入模块: from scrapy.settings import BaseSettings [as 别名]
# 或者: from scrapy.settings.BaseSettings import freeze [as 别名]

#.........这里部分代码省略.........
        self.assertEqual(settings.get('TEST_STRx', 'default'), 'default')
        self.assertEqual(settings.getdict('TEST_DICT1'), {'key1': 'val1', 'ke2': 3})
        self.assertEqual(settings.getdict('TEST_DICT2'), {'key1': 'val1', 'ke2': 3})
        self.assertEqual(settings.getdict('TEST_DICT3'), {})
        self.assertEqual(settings.getdict('TEST_DICT3', {'key1': 5}), {'key1': 5})
        self.assertRaises(ValueError, settings.getdict, 'TEST_LIST1')

    def test_getpriority(self):
        settings = BaseSettings({'key': 'value'}, priority=99)
        self.assertEqual(settings.getpriority('key'), 99)
        self.assertEqual(settings.getpriority('nonexistentkey'), None)

    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)

    def test_maxpriority(self):
        # Empty settings should return 'default'
        self.assertEqual(self.settings.maxpriority(), 0)
        self.settings.set('A', 0, 10)
        self.settings.set('B', 0, 30)
        self.assertEqual(self.settings.maxpriority(), 30)

    def test_copy(self):
        values = {
            'TEST_BOOL': True,
            'TEST_LIST': ['one', 'two'],
            'TEST_LIST_OF_LISTS': [['first_one', 'first_two'],
                                   ['second_one', 'second_two']]
        }
        self.settings.setdict(values)
        copy = self.settings.copy()
        self.settings.set('TEST_BOOL', False)
        self.assertTrue(copy.get('TEST_BOOL'))

        test_list = self.settings.get('TEST_LIST')
        test_list.append('three')
        self.assertListEqual(copy.get('TEST_LIST'), ['one', 'two'])

        test_list_of_lists = self.settings.get('TEST_LIST_OF_LISTS')
        test_list_of_lists[0].append('first_three')
        self.assertListEqual(copy.get('TEST_LIST_OF_LISTS')[0],
                             ['first_one', 'first_two'])

    def test_freeze(self):
        self.settings.freeze()
        with self.assertRaises(TypeError) as cm:
            self.settings.set('TEST_BOOL', False)
            self.assertEqual(str(cm.exception),
                             "Trying to modify an immutable Settings object")

    def test_frozencopy(self):
        frozencopy = self.settings.frozencopy()
        self.assertTrue(frozencopy.frozen)
        self.assertIsNot(frozencopy, self.settings)

    def test_deprecated_attribute_overrides(self):
        self.settings.set('BAR', 'fuz', priority='cmdline')
        with warnings.catch_warnings(record=True) as w:
            self.settings.overrides['BAR'] = 'foo'
            self.assertIn("Settings.overrides", str(w[0].message))
            self.assertEqual(self.settings.get('BAR'), 'foo')
            self.assertEqual(self.settings.overrides.get('BAR'), 'foo')
            self.assertIn('BAR', self.settings.overrides)

            self.settings.overrides.update(BAR='bus')
            self.assertEqual(self.settings.get('BAR'), 'bus')
            self.assertEqual(self.settings.overrides.get('BAR'), 'bus')

            self.settings.overrides.setdefault('BAR', 'fez')
            self.assertEqual(self.settings.get('BAR'), 'bus')

            self.settings.overrides.setdefault('FOO', 'fez')
            self.assertEqual(self.settings.get('FOO'), 'fez')
            self.assertEqual(self.settings.overrides.get('FOO'), 'fez')


    def test_deprecated_attribute_defaults(self):
        self.settings.set('BAR', 'fuz', priority='default')
        with warnings.catch_warnings(record=True) as w:
            self.settings.defaults['BAR'] = 'foo'
            self.assertIn("Settings.defaults", str(w[0].message))
            self.assertEqual(self.settings.get('BAR'), 'foo')
            self.assertEqual(self.settings.defaults.get('BAR'), 'foo')
            self.assertIn('BAR', self.settings.defaults)
开发者ID:q1ang,项目名称:scrapy,代码行数:104,代码来源:__init__.py


注:本文中的scrapy.settings.BaseSettings.freeze方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。