0

I am currently working with the geopy package under Python 3.5. I use the geopy.Point package. I included it in very many files and never had issues. Unfortunately I now have trouble debugging since the Point __str__ function uses a geodetic datum system as output by default.

I would prefer to see only latitude and longitude if possible. I do not know why geopy is using this geodetic system, so it might be that I just don't get the advantage of it. But as far as I can see this system is not very helpful debuging. So now I seek a way to change the __str__ function in a very elegant way. What is the most pythonic way to do this? Just writing an wrapper messes with code completion as far as I can tell.

Here an example to explain what I mean:

from geopy.point import * #import the package

def str_p(point): #a function to get the format I actually can read
    return "Lat= "+str(point.latitude)+" | "+"Long= "+str(point.longitude) #always writing .latitude or .longitude when debugging is troublesome

p = Point(55, 17) #create a Point, note that we are using Lat, Lng
print(p) # 55 0m 0s N, 17 0m 0s E , a good format if you are manning a pirate ship. But confusing to me
print(str_p(p))# what I want
  • 1
    geopy maintainer here.You don't have to be a pirate to actually need the geographic location format. I believe that what you're searching for is `repr(Point(55, 17))`, which would give you `'Point(55.0, 17.0, 0.0)'`. – KostyaEsmukov Feb 04 '19 at 05:56

1 Answers1

2

This is hacky, this may not be the best solution, but for debugging purposes you can always monkey-patch:

In [1]: from geopy.point import Point

In [2]: old_str = Point.__str__

In [3]: Point.__str__ = lambda self:  "Lat= {} | Long= {}".format(self.latitude, self.longitude)

In [4]: p = Point(55, 17)

In [5]: print(p)
Lat= 55.0 | Long= 17.0

In [6]: Point.__str__ = old_str

In [7]: print(p)
55 0m 0s N, 17 0m 0s E
juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
  • Doable but I would really like a more elegant solution. Preferably one I do not have to redo every time I include the package. – not_a_bot_no_really_82353 Feb 03 '19 at 23:09
  • @not_a_bot_no_really_82353 what do you mean "every time I include a package"? – juanpa.arrivillaga Feb 04 '19 at 19:11
  • I said every time I include the package as in every time I use geopy.point and therefore include geopy.point in a file. Which is a lot. I do want to use the package but without jumping trough hoops. So I currently write a wrapper that will wrap everything. Pretty bad but it bugs me – not_a_bot_no_really_82353 Feb 08 '19 at 23:00
  • @not_a_bot_no_really_82353 you don't have to do it every time, only once per process. How would be different than using a wrapper? – juanpa.arrivillaga Feb 08 '19 at 23:02
  • I include the geopy.point Point in at least 16 different files and 3 projects. So I hoped for a solution that would actually alter the geopy.point package itself. Now I build my own package and just change the imports. It is the least invasive I think. – not_a_bot_no_really_82353 Feb 09 '19 at 00:45
  • @not_a_bot_no_really_82353 if you have three projects then you only need it three times. That doesn't seem very onerous to me, but whatever – juanpa.arrivillaga Feb 09 '19 at 01:16