From a1ebff83d39e65cf4ed34281a1e81ea6cd108fe0 Mon Sep 17 00:00:00 2001 From: Paul Tremberth Date: Fri, 29 Jan 2016 18:39:34 +0100 Subject: [PATCH] Remove __str__ and __repr__ from settings, introduce copy_to_dict() instead Settings instances as dict's are easier to print or pretty print in the shell Fixes #1732 --- scrapy/settings/__init__.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/scrapy/settings/__init__.py b/scrapy/settings/__init__.py index 342d2585e..918bfc1e5 100644 --- a/scrapy/settings/__init__.py +++ b/scrapy/settings/__init__.py @@ -368,11 +368,25 @@ class BaseSettings(MutableMapping): def __len__(self): return len(self.attributes) - def __str__(self): - return str(self.attributes) + def _to_dict(self): + return {k: (v._to_dict() if isinstance(v, BaseSettings) else v) + for k, v in six.iteritems(self)} - def __repr__(self): - return "<%s %s>" % (self.__class__.__name__, self.attributes) + def copy_to_dict(self): + """ + Make a copy of current settings and convert to a dict. + + This method returns a new dict populated with the same values + and their priorities as the current settings. + + Modifications to the returned dict won't be reflected on the original + settings. + + This method can be useful for example for printing settings + in Scrapy shell. + """ + settings = self.copy() + return settings._to_dict() @property def overrides(self):