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

Add value checking for location #625 #626

Merged
merged 7 commits into from Jun 27, 2017

Conversation

radumas
Copy link
Contributor

@radumas radumas commented May 29, 2017

Using property decorators.
Need to implement testing.

folium/map.py Outdated
@@ -297,6 +297,26 @@ def render(self, **kwargs):
'</style>'), name='map_style')

super(LegacyMap, self).render(**kwargs)

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

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]")

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]")

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))

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))

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))


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

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))

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

@radumas
Copy link
Contributor Author

radumas commented May 29, 2017

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
Copy link
Member

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.

Copy link
Contributor Author

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?

Copy link
Member

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]")
Copy link
Member

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))
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

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.

@ocefpaf
Copy link
Member

ocefpaf commented Jun 24, 2017

@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.

@radumas
Copy link
Contributor Author

radumas commented Jun 26, 2017

@ocefpaf I was waiting for your response to my replies to your comments.

@ocefpaf
Copy link
Member

ocefpaf commented Jun 27, 2017

@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):

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

Choose a reason for hiding this comment

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

F821 undefined name 'value'

@radumas
Copy link
Contributor Author

radumas commented Jun 27, 2017

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))
Copy link
Member

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):
Copy link
Member

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):

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

Copy link
Member

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)

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'

Copy link
Contributor Author

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

@ocefpaf ocefpaf merged commit cdca018 into python-visualization:master Jun 27, 2017
sanga pushed a commit to sanga/folium that referenced this pull request Oct 24, 2017
Bugfix on type checking numbers
sanga pushed a commit to sanga/folium that referenced this pull request Oct 24, 2017
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.

None yet

3 participants