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

Python: Add repr() methods #164

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/python/pywraps2_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,33 @@ def testS2EarthMetricRadians(self):
radius_m = s2.S2Earth.RadiansToMeters(angle.radians())
self.assertEqual(radius_m, 12340.0)

def testRepr(self):
ll_null = s2.S2LatLng()
self.assertEqual(repr(ll_null), "<S2LatLng: (0.000000,0.000000)>")

lon1 = s2.S2LatLng.FromDegrees(51.3368602, 0.4931979)
self.assertEqual(repr(lon1), "<S2LatLng: (51.336860,0.493198)>")

london = s2.S2LatLngRect(lon1,
s2.S2LatLng.FromDegrees(51.7323965, 0.1495211))
self.assertEqual(repr(london),
"<S2LatLngRect: (51.3368602,0.4931979,51.7323965,0.1495211)>"
)

llr_null = s2.S2LatLngRect()
self.assertEqual(repr(llr_null),
"<S2LatLngRect: Empty>"
)

cell_id = s2.S2CellId.FromToken("487604c489f841c3")
self.assertEqual(repr(cell_id), "<S2CellId: 2/100323000212021010333002003201>")

cell = s2.S2Cell(cell_id)
self.assertEqual(repr(cell), "<S2Cell: 2/100323000212021010333002003201>")

cell_id = s2.S2CellId.FromToken("this is invalid")
self.assertEqual(repr(cell_id), "<S2CellId: Invalid: 0000000000000000>")


class RegionTermIndexerTest(unittest.TestCase):
def _randomCaps(self, query_type, **indexer_options):
Expand Down
38 changes: 38 additions & 0 deletions src/python/s2_common.i
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,44 @@ USE_EQUALS_FN_FOR_EQ_AND_NE(S2Loop)
USE_EQUALS_FN_FOR_EQ_AND_NE(S2Polygon)
USE_EQUALS_FN_FOR_EQ_AND_NE(S2Polyline)

// repr() methods for geodetic/cell classes

%extend S2CellId {
%pythoncode %{
def __repr__(self):
return '<S2CellId: %s>' % self.ToString()
%}
};

%extend S2Cell {
%pythoncode %{
def __repr__(self):
return '<S2Cell: %s>' % self.id().ToString()
%}
};

%extend S2LatLng {
%pythoncode %{
def __repr__(self):
return '<S2LatLng: (%s)>' % self.ToStringInDegrees()
%}
};

%extend S2LatLngRect {
%pythoncode %{
def __repr__(self):
if self.is_empty():
return '<S2LatLngRect: Empty>'
else:
return '<S2LatLngRect: (%s,%s,%s,%s)>' % (
self.lat_lo().degrees(),
self.lng_lo().degrees(),
self.lat_hi().degrees(),
self.lng_hi().degrees(),
)
%}
};

// Simple implementation of key S2Testing methods
%pythoncode %{
import random
Expand Down