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

Fix postmerge errors #35

Merged
merged 5 commits into from
May 11, 2017
Merged

Fix postmerge errors #35

merged 5 commits into from
May 11, 2017

Conversation

mattiasostmar
Copy link
Collaborator

""""
Check if there are any content cats added and add generic content category to image.

Populate self.content_cat with commons category, if present.
"""
if not self.content_cats:
if len(self.content_cats) == 0:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change? The previous one should catch both empty list and None

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed because I tried this:
In [25]: l = []

In [26]: l == True
Out[26]: False

In [27]: l is True
Out[27]: False

In [28]: l == False
Out[28]: False

In [29]: l is False
Out[29]: False

However, quirky enough the following works:

In [30]: if l:
             print("l is True?")
         else:
             print("nah, l is False.")
nah, l is False.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is because if doesn't just test for True/False but (in this case also interprets None,[],{},0 and likely other empty elements as False.

The only one of these that might be very dangerous is 0. I.e. if you are evaluating numbers and num=0 is a valid number then you cannot use if num to guard agains num is None.

But that is not the case here so if not self.content_cats: should be used.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -371,7 +371,11 @@ def process_depicted_place(self, place_string, places_mapping, desc_string):
place_as_wikitext = ""

if place_string:
if place_string in places_mapping:
# Don't forget to add the commons categories, even though only wikidata is used in depicted people field
if places_mapping[place_string].get('commonscat'):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This re-jig doesn't work.

In the previous version we first checked that place_string was in places_mapping. If this was the case we then also checked for a commonscat. Here places_mapping[place_string] is likely to throw a keyError.

To fix re-add if place_string in places_mapping: before this (see also next comment).

if places_mapping[place_string].get('commonscat'):
self.content_cats.append(places_mapping[place_string]["commonscat"])

if place_string.lower() in [place.lower() for place in places_mapping.keys()]:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there really a mixture of upper/lower in both the mapping and the in-data?

If so I would standardise the mapping data (just make them all lower). Then in the if place_string in places_mapping above (see previous comment) simply change place_string to place_string.lower() (and drop this if).

else:
place_matches = []
for place in places_mapping:
if place.lower() in desc_string.lower():
place_matches.append(place)
# Don't forget to add the commons categories if present.
if places_mapping[place].get('commonscat'):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't move this up from L415. This was on purpose inside the if len(place_matches) == 1: to ensure we only add the category on an exact match.

That said I noticed that on the line before L409 we should have introduced: place=place_matches[0].

The commonscat on L423 was correctly removed.

@mattiasostmar mattiasostmar merged commit d3f8a5a into master May 11, 2017
@lokal-profil lokal-profil deleted the fix_postmerge_errors branch January 8, 2018 15:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants