python - 以 Python 方式使 geopy.Point 更具可读性
问题描述
我目前正在使用 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
解决方案
这很hacky,这可能不是最好的解决方案,但出于调试目的,您总是可以猴子补丁:
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
推荐阅读
- amazon-web-services - Claudia.js create 在 typescript 项目中找不到模块 lambda
- java - Sqlite+Recyclerview ,应用已停止
- mysql - 拥有多个列表并将它们的值插入 MySql
- windows-10 - 在 Windows 上使用特殊字符时,Cygwin 别名运行不同的命令
- c# - 序列化和反序列化 XML 文件 c#
- html - 如何从 HTML 中识别静态资产
- azure-iot-hub - 显示旧数据的 Azure IoT Edge tempSensor 模块
- java - SOLR7错误解析word文档
- ios - 如何在目标 C 中使用 PSPDFKIT (iOS) 突出显示文本
- python - 创建用于 enaml 的 python 类时,传递 Atom 的目的是什么?