summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortmartins <thigm85@gmail.com>2020-07-07 10:52:53 +0200
committertmartins <thigm85@gmail.com>2020-07-07 10:52:53 +0200
commitb4825ad2f4f874f4675b35c86c19ca4bf96ff241 (patch)
tree30218b4778b3449301fcd7dd3ebbea579fe5eb2a
parentb7c5ab66c1ef1fb38d321848627e66954ac5c64a (diff)
refactor __repr__ as a python expression
-rw-r--r--python/vespa/vespa/package.py41
-rw-r--r--python/vespa/vespa/test_package.py122
2 files changed, 102 insertions, 61 deletions
diff --git a/python/vespa/vespa/package.py b/python/vespa/vespa/package.py
index a59177a7028..5fb18c38302 100644
--- a/python/vespa/vespa/package.py
+++ b/python/vespa/vespa/package.py
@@ -65,7 +65,13 @@ class Field(ToJson, FromJson["Field"]):
)
def __repr__(self):
- return "{0}\n{1}".format(self.__class__.__name__, str(self.to_dict))
+ return "{0}({1}, {2}, {3}, {4})".format(
+ self.__class__.__name__,
+ str(self.name),
+ str(self.type),
+ str(self.indexing),
+ str(self.index),
+ )
class Document(ToJson, FromJson["Document"]):
@@ -100,7 +106,9 @@ class Document(ToJson, FromJson["Document"]):
return self.fields == other.fields
def __repr__(self):
- return "{0}\n{1}".format(self.__class__.__name__, str(self.to_dict))
+ return "{0}({1})".format(
+ self.__class__.__name__, str(self.fields) if self.fields else None
+ )
class FieldSet(ToJson, FromJson["FieldSet"]):
@@ -134,7 +142,9 @@ class FieldSet(ToJson, FromJson["FieldSet"]):
return self.name == other.name and self.fields == other.fields
def __repr__(self):
- return "{0}\n{1}".format(self.__class__.__name__, str(self.to_dict))
+ return "{0}({1}, {2})".format(
+ self.__class__.__name__, str(self.name), str(self.fields)
+ )
class RankProfile(ToJson, FromJson["RankProfile"]):
@@ -176,7 +186,12 @@ class RankProfile(ToJson, FromJson["RankProfile"]):
)
def __repr__(self):
- return "{0}\n{1}".format(self.__class__.__name__, str(self.to_dict))
+ return "{0}({1}, {2}, {3})".format(
+ self.__class__.__name__,
+ str(self.name),
+ str(self.first_phase),
+ str(self.inherits),
+ )
class Schema(ToJson, FromJson["Schema"]):
@@ -253,7 +268,19 @@ class Schema(ToJson, FromJson["Schema"]):
)
def __repr__(self):
- return "{0}\n{1}".format(self.__class__.__name__, str(self.to_dict))
+ return "{0}({1}, {2}, {3}, {4})".format(
+ self.__class__.__name__,
+ str(self.name),
+ str(self.document),
+ str(
+ [field for field in self.fieldsets.values()] if self.fieldsets else None
+ ),
+ str(
+ [rank_profile for rank_profile in self.rank_profiles.values()]
+ if self.rank_profiles
+ else None
+ ),
+ )
class ApplicationPackage(ToJson, FromJson["ApplicationPackage"]):
@@ -408,4 +435,6 @@ class ApplicationPackage(ToJson, FromJson["ApplicationPackage"]):
return self.name == other.name and self.schema == other.schema
def __repr__(self):
- return "{0}\n{1}".format(self.__class__.__name__, str(self.to_dict))
+ return "{0}({1}, {2})".format(
+ self.__class__.__name__, str(self.name), str(self.schema)
+ )
diff --git a/python/vespa/vespa/test_package.py b/python/vespa/vespa/test_package.py
index e76e1cf3994..c37766131ca 100644
--- a/python/vespa/vespa/test_package.py
+++ b/python/vespa/vespa/test_package.py
@@ -100,7 +100,9 @@ class TestRankProfile(unittest.TestCase):
self.assertEqual(rank_profile, RankProfile.from_dict(rank_profile.to_dict))
def test_rank_profile_inherits(self):
- rank_profile = RankProfile(name="bm25", first_phase="bm25(title) + bm25(body)", inherits="default")
+ rank_profile = RankProfile(
+ name="bm25", first_phase="bm25(title) + bm25(body)", inherits="default"
+ )
self.assertEqual(rank_profile.name, "bm25")
self.assertEqual(rank_profile.first_phase, "bm25(title) + bm25(body)")
self.assertEqual(rank_profile, RankProfile.from_dict(rank_profile.to_dict))
@@ -159,7 +161,11 @@ class TestApplicationPackage(unittest.TestCase):
fieldsets=[FieldSet(name="default", fields=["title", "body"])],
rank_profiles=[
RankProfile(name="default", first_phase="nativeRank(title, body)"),
- RankProfile(name="bm25", first_phase="bm25(title) + bm25(body)", inherits="default")
+ RankProfile(
+ name="bm25",
+ first_phase="bm25(title) + bm25(body)",
+ inherits="default",
+ ),
],
)
self.app_package = ApplicationPackage(name="test_app", schema=test_schema)
@@ -170,64 +176,70 @@ class TestApplicationPackage(unittest.TestCase):
)
def test_schema_to_text(self):
- expected_result = "schema msmarco {\n" \
- " document msmarco {\n" \
- " field id type string {\n" \
- " indexing: attribute | summary\n" \
- " }\n" \
- " field title type string {\n" \
- " indexing: index | summary\n" \
- " index: enable-bm25\n" \
- " }\n" \
- " field body type string {\n" \
- " indexing: index | summary\n" \
- " index: enable-bm25\n" \
- " }\n" \
- " }\n" \
- " fieldset default {\n" \
- " fields: title, body\n" \
- " }\n" \
- " rank-profile default {\n" \
- " first-phase {\n" \
- " expression: nativeRank(title, body)\n" \
- " }\n" \
- " }\n" \
- " rank-profile bm25 inherits default {\n" \
- " first-phase {\n" \
- " expression: bm25(title) + bm25(body)\n" \
- " }\n" \
- " }\n" \
- "}"
+ expected_result = (
+ "schema msmarco {\n"
+ " document msmarco {\n"
+ " field id type string {\n"
+ " indexing: attribute | summary\n"
+ " }\n"
+ " field title type string {\n"
+ " indexing: index | summary\n"
+ " index: enable-bm25\n"
+ " }\n"
+ " field body type string {\n"
+ " indexing: index | summary\n"
+ " index: enable-bm25\n"
+ " }\n"
+ " }\n"
+ " fieldset default {\n"
+ " fields: title, body\n"
+ " }\n"
+ " rank-profile default {\n"
+ " first-phase {\n"
+ " expression: nativeRank(title, body)\n"
+ " }\n"
+ " }\n"
+ " rank-profile bm25 inherits default {\n"
+ " first-phase {\n"
+ " expression: bm25(title) + bm25(body)\n"
+ " }\n"
+ " }\n"
+ "}"
+ )
self.assertEqual(self.app_package.schema_to_text, expected_result)
def test_hosts_to_text(self):
- expected_result = '<?xml version="1.0" encoding="utf-8" ?>\n' \
- '<!-- Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -->\n' \
- '<hosts>\n' \
- ' <host name="localhost">\n' \
- ' <alias>node1</alias>\n' \
- ' </host>\n' \
- '</hosts>'
+ expected_result = (
+ '<?xml version="1.0" encoding="utf-8" ?>\n'
+ "<!-- Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -->\n"
+ "<hosts>\n"
+ ' <host name="localhost">\n'
+ " <alias>node1</alias>\n"
+ " </host>\n"
+ "</hosts>"
+ )
self.assertEqual(self.app_package.hosts_to_text, expected_result)
def test_services_to_text(self):
- expected_result = '<?xml version="1.0" encoding="UTF-8"?>\n' \
- '<services version="1.0">\n' \
- ' <container id="test_app_container" version="1.0">\n' \
- ' <search></search>\n' \
- ' <document-processing></document-processing>\n' \
- ' <document-api></document-api>\n' \
- ' </container>\n' \
- ' <content id="test_app_content" version="1.0">\n' \
- ' <redundancy reply-after="1">1</redundancy>\n' \
- ' <documents>\n' \
- ' <document type="msmarco" mode="index"></document>\n' \
- ' <document-processing cluster="test_app_container"></document-processing>\n' \
- ' </documents>\n' \
- ' <nodes>\n' \
- ' <node distribution-key="0" hostalias="node1"></node>\n' \
- ' </nodes>\n' \
- ' </content>\n' \
- '</services>'
+ expected_result = (
+ '<?xml version="1.0" encoding="UTF-8"?>\n'
+ '<services version="1.0">\n'
+ ' <container id="test_app_container" version="1.0">\n'
+ " <search></search>\n"
+ " <document-processing></document-processing>\n"
+ " <document-api></document-api>\n"
+ " </container>\n"
+ ' <content id="test_app_content" version="1.0">\n'
+ ' <redundancy reply-after="1">1</redundancy>\n'
+ " <documents>\n"
+ ' <document type="msmarco" mode="index"></document>\n'
+ ' <document-processing cluster="test_app_container"></document-processing>\n'
+ " </documents>\n"
+ " <nodes>\n"
+ ' <node distribution-key="0" hostalias="node1"></node>\n'
+ " </nodes>\n"
+ " </content>\n"
+ "</services>"
+ )
self.assertEqual(self.app_package.services_to_text, expected_result)