-
Notifications
You must be signed in to change notification settings - Fork 49
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 possible ValueError in convert_to_int caused by values like 1px #10
Conversation
…om dimensions attributes values in convert_int. Also be more liberal allowing values with px or percentage suffixes. Added test for image dimensions.
Very good. I'm glad that adding tests and modifying this source code has been understandable. Now for the pull request, the only gripe I have is that it is invalid HTML to have We could either:
If we chose the second option, the def convert_to_int(value):
"""Attempts to convert a specified value to an integer
:param value: Content to be converted into an integer
:type value: string or int
"""
try:
return int(value)
except TypeError:
return None Thoughts @ashibble? |
Although it may be invalid to write width="100px", I have seen it done before. I feel that the intended meaning is the same for px sizes, and so it wouldn't necessarily be a BAD thing. I do think that we should ignore sizes with em or % in them, as these may not display correctly converted to px sizes. Changing % to pixels would reflect an incorrect width. |
As I mentioned, I'm not sure, whether the value should be changed at all, except maybe for calling strip() on it. I agree that just removing/ignoring em and % is not a good idea. That being said valid HTML and real-world usage are 2 different things, so I suggest the following.
If a value contains a px suffix, although not valid, accept it and use the digits only. Everything else that can't be converted to int will be omitted. |
We could probably do this: pattern = re.compile(r'(?P<value>\d+)?px+')
def convert_to_int(value):
try:
match = re.search(pattern, value))
if match:
value = int(match.group('value')
except (TypeError, ValueError):
return None I don't know if the double strip or the regex pattern search is faster. |
Alright, it appears the basic double strip is faster! Think you can update your pull request? Except... instead of excepting |
Okay I'll update it soon. |
Thanks! Sent from my iPhone
|
One more thing if it isn't too much trouble! Change value = value.strip().strip('px') to value = value.strip(' px') I actually just did some reading and found out that the all instances of characters passed to Crazy, I thought it was just striping the phrase. My change has a space along side p and x |
Interesting, I wasn't aware of that either, I'll update that. |
Fix possible ValueError in convert_to_int caused by values like 1px
Merged, thanks! |
You're welcome! Glad I could contribute to lassie. |
When trying to parse http://www.wired.com/wiredscience/2013/09/rim-fire-map-color-scale/ a ValueError was raised in convert_to_img, because the page has image width and height values ending in px.
I changed the function to be more liberal regarding dimension values, by extracting the digits before casting to int. I added a test for this.
Not sure though if the value should be converted to int at all or kept as a string.