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
Add value checking for location #625 #626
Add value checking for location #625 #626
Conversation
Using property decorators.
folium/map.py
Outdated
@@ -297,6 +297,26 @@ def render(self, **kwargs): | |||
'</style>'), name='map_style') | |||
|
|||
super(LegacyMap, self).render(**kwargs) | |||
|
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.
W293 blank line contains whitespace
folium/map.py
Outdated
@property | ||
def location(self): | ||
return self._location | ||
|
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.
W293 blank line contains whitespace
folium/map.py
Outdated
"""Validates the location values before setting""" | ||
if type(value) not in [list, tuple]: | ||
raise TypeError("Location is not a list, expecting ex: location=[45.523, -122.675]") | ||
|
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.
W293 blank line contains whitespace
folium/map.py
Outdated
|
||
if len(value) != 2: | ||
raise ValueError("Location should have two values, [lat, lon]") | ||
|
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.
W293 blank line contains whitespace
folium/map.py
Outdated
|
||
for val in value: | ||
if not isinstance(val, numbers.Rational): | ||
raise TypeError("Location values should be numeric, {val} is not a number".format(val)) |
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.
E501 line too long (103 > 100 characters)
folium/map.py
Outdated
for val in value: | ||
if not isinstance(val, numbers.Rational): | ||
raise TypeError("Location values should be numeric, {val} is not a number".format(val)) | ||
|
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.
W293 blank line contains whitespace
folium/map.py
Outdated
if not isinstance(val, numbers.Rational): | ||
raise TypeError("Location values should be numeric, {val} is not a number".format(val)) | ||
|
||
|
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.
W293 blank line contains whitespace
folium/map.py
Outdated
raise TypeError("Location values should be numeric, {val} is not a number".format(val)) | ||
|
||
|
||
self._location = value |
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.
E303 too many blank lines (2)
Bugfix on type checking numbers
folium/map.py
Outdated
float(val) | ||
except: | ||
raise TypeError("Location values should be numeric, {} is not a number".format(val)) | ||
|
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.
W293 blank line contains whitespace
I'll wait to fix that whitespace error until there are other comments on this PR |
folium/map.py
Outdated
|
||
@property | ||
def location(self): | ||
return self._location |
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.
I am not sure we need a property and a setter for this. Can you explain what you have in mind?
I thought that we could have an internal function to check and raise a ValueError
that we would call in the places we need to check for the two value list.
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.
That's a fair point. I was more worried about changing anything in an __init()__
function, since it seems like the property decorators leave those alone, but it does result in unnecessary code. I could swap out for a.... class function?
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.
Not sure we need a class function. The location is used only once to create the map. We don't need to store it as a property b/c we won't do anything with it later. Just a private function to convert to float and issue a ValueError
when that cannot be done would suffice IMO. Then you can run that function in __init__()
, it would be just 1 extra line there. What do you think?
folium/map.py
Outdated
raise TypeError("Location is not a list, expecting ex: location=[45.523, -122.675]") | ||
|
||
if len(value) != 2: | ||
raise ValueError("Location should have two values, [lat, lon]") |
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.
I like that message. Simple and clear!
folium/map.py
Outdated
try: | ||
float(val) | ||
except: | ||
raise TypeError("Location values should be numeric, {} is not a number".format(val)) |
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.
In a way this is still a ValueError
b/c some strings will pass your float(val)
line. But that is not too important.
PS: I never tested if
location = ['12.5', 42.5]
would work on leaflet. But that will pass your test there.
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.
oh that's an interesting point. I guess I could actually convert the values to float or number and then we wouldn't need to know whether it works in leaflet or not.
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.
Yep. I would go for that approach.
@radumas what is the status on this? I am planning to isseu a new release and it would be nice to get this PR merged before that. |
@ocefpaf I was waiting for your response to my replies to your comments. |
Sorry. My time for folium is so limited that I always need to start from zero when I get back to PRs... |
Remove property setters based on PR comments
folium/map.py
Outdated
@@ -60,6 +60,19 @@ | |||
'https://rawgit.com/python-visualization/folium/master/folium/templates/leaflet.awesome.rotate.css'), # noqa | |||
] | |||
|
|||
def _format_lat_lon(values): |
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.
E302 expected 2 blank lines, found 1
folium/map.py
Outdated
values = [float(val) for val in values] | ||
except: | ||
raise ValueError("Location values should be numeric, {} is not a number".format(val)) | ||
return value |
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.
F821 undefined name 'value'
Totally understandable. I've updated based on comments. |
try: | ||
values = [float(val) for val in values] | ||
except: | ||
raise ValueError("Location values should be numeric, {} is not a number".format(val)) |
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.
👍
folium/map.py
Outdated
@@ -60,6 +60,19 @@ | |||
'https://rawgit.com/python-visualization/folium/master/folium/templates/leaflet.awesome.rotate.css'), # noqa | |||
] | |||
|
|||
def _format_lat_lon(values): |
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.
How about naming it _validate_location
?
@@ -60,6 +60,20 @@ | |||
'https://rawgit.com/python-visualization/folium/master/folium/templates/leaflet.awesome.rotate.css'), # noqa | |||
] | |||
|
|||
def _validate_location(values): |
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.
E302 expected 2 blank lines, found 1
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.
Just this one left to address. I'll merge as is and fix it later. Thanks @radumas!
folium/map.py
Outdated
@@ -170,7 +184,7 @@ def __init__(self, location=None, width='100%', height='100%', | |||
self.location = [0, 0] | |||
self.zoom_start = min_zoom | |||
else: | |||
self.location = location | |||
self.location = _format_lat_lon(location) |
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.
F821 undefined name '_format_lat_lon'
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.
Just as I pushed this I realized this mistake... sigh
Bugfix on type checking numbers
…t_check Add value checking for location python-visualization#625
Using property decorators.
Need to implement testing.