我目前正在使用Python 3.5下的geopy软件包。我使用geopy.Point包。我将其包含在许多文件中,并且从未出现过问题。不幸的是,由于Point __str__
函数默认使用大地测量基准系统作为输出,因此现在调试起来很麻烦。
如果可能的话,我宁愿只看到纬度和经度。我不知道为什么geopy使用这种大地测量系统,所以可能只是我没有利用它。但据我所见,该系统的调试不是很有帮助。
因此,现在我寻求一种非常优雅的方式来更改__str__
函数。什么是最pythonic的方式做到这一点?据我所知,仅编写包装程序就使代码完成陷入混乱。
这里有一个例子来解释我的意思:
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
答案 0 :(得分:1)
这很骇人,这可能不是最佳解决方案,但是出于调试目的,您可以随时进行猴子补丁:
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