New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[MRG+1] Revise/modernize item exporter example in docs #2989
Conversation
Codecov Report
@@ Coverage Diff @@
## master #2989 +/- ##
==========================================
+ Coverage 84.58% 84.59% +0.01%
==========================================
Files 164 164
Lines 9249 9249
Branches 1376 1376
==========================================
+ Hits 7823 7824 +1
Misses 1167 1167
+ Partials 259 258 -1
|
docs/topics/exporters.rst
Outdated
self.year_to_exporter = {} | ||
|
||
def close_spider(self, spider): | ||
for exporter in self.year_to_exporter.itervalues(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be .values() in order to be compatible with Python 3. Creating a temporary list in Python 2 is not a problem as a list would be small.
docs/topics/exporters.rst
Outdated
def _exporter_for_item(self, item): | ||
year = item['year'] | ||
if year not in self.year_to_exporter: | ||
f = open('{}.xml'.format(year), 'w+b') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is 'w+b' needed, not just 'wb'?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call. I just copied it from the previous example, but as you say, it doesn't seem to be necessary.
I like the revised example, exporting to several locations is a common problem users ask for 👍 |
Looks good to me, thanks @colinmorris! |
Addresses #2932
@kmike pointed out that the concept of a file-per-spider no longer made sense, so I changed the use case to one inspired by my own experience. It's maybe a little more complicated than the previous example, but it still comes out to fewer lines of code after removing the overhead of manually connecting the signals.