簡體   English   中英

以pythonic方式使geopy.Point更具可讀性

[英]Make geopy.Point more readable in an pythonic way

我目前正在使用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,這可能不是最好的解決方案,但是出於調試目的,您可以隨時進行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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM