Skip to content
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

scrapy-loader-upkeep migration #12

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
rename field_tracker into field_position_tracker for more clarity
BurnzZ committed May 26, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit dd96b7e4466354b5d270cedd5dd66da313ccd0a8
8 changes: 4 additions & 4 deletions itemloaders/__init__.py
Original file line number Diff line number Diff line change
@@ -105,7 +105,7 @@ def __init__(self, item=None, selector=None, parent=None, stats=None, **context)

# This keeps track of the position of the 'field' name that is being
# loaded for a more accurate logging in the stats.
self.field_tracker = defaultdict(int)
self.field_position_tracker = defaultdict(int)

@property
def _values(self):
@@ -335,7 +335,7 @@ def add_xpath(self, field_name, xpath, *processors, **kw):
loader.add_xpath('price', '//p[@id="price"]', re='the price is (.*)')
"""
self.field_tracker[f"{field_name}_xpath"] += 1
self.field_position_tracker[f"{field_name}_xpath"] += 1
values = self.get_selector_values(field_name, xpath, 'xpath', **kw)
self.add_value(field_name, values, *processors, **kw)

@@ -388,7 +388,7 @@ def add_css(self, field_name, css, *processors, **kw):
# HTML snippet: <p id="price">the price is $1200</p>
loader.add_css('price', 'p#price', re='the price is (.*)')
"""
self.field_tracker[f"{field_name}_css"] += 1
self.field_position_tracker[f"{field_name}_css"] += 1
values = self.get_selector_values(field_name, css, 'css', **kw)
self.add_value(field_name, values, *processors, **kw)

@@ -433,7 +433,7 @@ def get_selector_values(self, field_name, selector_rules, selector_type, **kw):

# For every call of `add_css()` and `add_xpath()` this is incremented.
# We'll use it as the base index of the position of the logged stats.
index = self.field_tracker[f"{field_name}_{selector_type}"]
index = self.field_position_tracker[f"{field_name}_{selector_type}"]

values = []
for position, rule in enumerate(arg_to_iter(selector_rules), index):
2 changes: 1 addition & 1 deletion tests/test_selector_loader.py
Original file line number Diff line number Diff line change
@@ -186,7 +186,7 @@ def test_get_selector_values():
loader.write_to_stats = mock.Mock()

# This wasn't actually initialized so it will return 0 by default otherwise.
loader.field_tracker["field_css"] = 1
loader.field_position_tracker["field_css"] = 1

result = loader.get_selector_values(field_name, selector_rules, "css")