From e2490810cfac763cc5a23c6edce015333e2308a6 Mon Sep 17 00:00:00 2001 From: Jen Hamon Date: Wed, 28 Aug 2024 13:31:45 -0400 Subject: [PATCH 1/3] Improve list index output --- pinecone/models/index_list.py | 5 +++-- pinecone/models/index_model.py | 3 +++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/pinecone/models/index_list.py b/pinecone/models/index_list.py index 6c524489..5240ebe4 100644 --- a/pinecone/models/index_list.py +++ b/pinecone/models/index_list.py @@ -1,3 +1,4 @@ +import json from pinecone.core.openapi.control.models import IndexList as OpenAPIIndexList from .index_model import IndexModel @@ -21,10 +22,10 @@ def __iter__(self): return iter(self.indexes) def __str__(self): - return str(self.index_list) + return str(self.indexes) def __repr__(self): - return repr(self.index_list) + return json.dumps([i.to_dict() for i in self.indexes], indent=4) def __getattr__(self, attr): return getattr(self.index_list, attr) diff --git a/pinecone/models/index_model.py b/pinecone/models/index_model.py index 9d46b0ed..b44767e3 100644 --- a/pinecone/models/index_model.py +++ b/pinecone/models/index_model.py @@ -17,3 +17,6 @@ def __getattr__(self, attr): def __getitem__(self, key): return self.__getattr__(key) + + def to_dict(self): + return self.index.to_dict() From 1cfb1dc90fe38c6ac803424103c974fea8ec711f Mon Sep 17 00:00:00 2001 From: Jen Hamon Date: Wed, 28 Aug 2024 15:09:08 -0400 Subject: [PATCH 2/3] Improve notebook output for list/describe indexes and collections --- pinecone/control/__init__.py | 3 +++ pinecone/control/pinecone.py | 1 - pinecone/control/repr_overrides.py | 16 ++++++++++++++++ pinecone/models/collection_list.py | 4 ++-- pinecone/models/index_model.py | 3 --- 5 files changed, 21 insertions(+), 6 deletions(-) create mode 100644 pinecone/control/repr_overrides.py diff --git a/pinecone/control/__init__.py b/pinecone/control/__init__.py index 4c739f1a..cb06d2b6 100644 --- a/pinecone/control/__init__.py +++ b/pinecone/control/__init__.py @@ -1 +1,4 @@ from .pinecone import Pinecone + +from .repr_overrides import install_repr_overrides +install_repr_overrides() \ No newline at end of file diff --git a/pinecone/control/pinecone.py b/pinecone/control/pinecone.py index 7d6ce02e..5232824d 100644 --- a/pinecone/control/pinecone.py +++ b/pinecone/control/pinecone.py @@ -39,7 +39,6 @@ logger = logging.getLogger(__name__) - class Pinecone: def __init__( self, diff --git a/pinecone/control/repr_overrides.py b/pinecone/control/repr_overrides.py new file mode 100644 index 00000000..ecc68da1 --- /dev/null +++ b/pinecone/control/repr_overrides.py @@ -0,0 +1,16 @@ +from pinecone.models.index_model import IndexModel +from pinecone.core.openapi.control.models import CollectionModel + +import json + +def install_repr_overrides(): + """ + The generator code uses pprint.pformat to format the repr output + which looks really poor when printing a list of large objects + in a notebook setting. We override it here for a few select models + instead of modifying the generator code because the more compact output + from pprint.pformat seems better for data plane objects such as lists of + query results. + """ + for model in [IndexModel, CollectionModel]: + model.__repr__ = lambda self: json.dumps(self.to_dict(), indent=4, sort_keys=False) diff --git a/pinecone/models/collection_list.py b/pinecone/models/collection_list.py index 9b788cee..8c584d80 100644 --- a/pinecone/models/collection_list.py +++ b/pinecone/models/collection_list.py @@ -1,8 +1,8 @@ +import json from pinecone.core.openapi.control.models import ( CollectionList as OpenAPICollectionList, ) - class CollectionList: """ A list of collections. @@ -28,7 +28,7 @@ def __str__(self): return str(self.collection_list) def __repr__(self): - return repr(self.collection_list) + return json.dumps([c.to_dict() for c in self.collection_list.collections], indent=4) def __getattr__(self, attr): return getattr(self.collection_list, attr) diff --git a/pinecone/models/index_model.py b/pinecone/models/index_model.py index b44767e3..d8784211 100644 --- a/pinecone/models/index_model.py +++ b/pinecone/models/index_model.py @@ -9,9 +9,6 @@ def __init__(self, index: OpenAPIIndexModel): def __str__(self): return str(self.index) - def __repr__(self): - return repr(self.index) - def __getattr__(self, attr): return getattr(self.index, attr) From 5bf3e8dadbb38fae43c37201a2ce005963a44f2d Mon Sep 17 00:00:00 2001 From: Jen Hamon Date: Wed, 28 Aug 2024 15:37:43 -0400 Subject: [PATCH 3/3] Black formatting --- pinecone/control/__init__.py | 3 ++- pinecone/control/pinecone.py | 1 + pinecone/control/repr_overrides.py | 3 ++- pinecone/models/collection_list.py | 1 + pinecone/models/index_model.py | 2 +- 5 files changed, 7 insertions(+), 3 deletions(-) diff --git a/pinecone/control/__init__.py b/pinecone/control/__init__.py index cb06d2b6..dc6bf1e2 100644 --- a/pinecone/control/__init__.py +++ b/pinecone/control/__init__.py @@ -1,4 +1,5 @@ from .pinecone import Pinecone from .repr_overrides import install_repr_overrides -install_repr_overrides() \ No newline at end of file + +install_repr_overrides() diff --git a/pinecone/control/pinecone.py b/pinecone/control/pinecone.py index 5232824d..7d6ce02e 100644 --- a/pinecone/control/pinecone.py +++ b/pinecone/control/pinecone.py @@ -39,6 +39,7 @@ logger = logging.getLogger(__name__) + class Pinecone: def __init__( self, diff --git a/pinecone/control/repr_overrides.py b/pinecone/control/repr_overrides.py index ecc68da1..4cf76c7c 100644 --- a/pinecone/control/repr_overrides.py +++ b/pinecone/control/repr_overrides.py @@ -3,13 +3,14 @@ import json + def install_repr_overrides(): """ The generator code uses pprint.pformat to format the repr output which looks really poor when printing a list of large objects in a notebook setting. We override it here for a few select models instead of modifying the generator code because the more compact output - from pprint.pformat seems better for data plane objects such as lists of + from pprint.pformat seems better for data plane objects such as lists of query results. """ for model in [IndexModel, CollectionModel]: diff --git a/pinecone/models/collection_list.py b/pinecone/models/collection_list.py index 8c584d80..865a55ac 100644 --- a/pinecone/models/collection_list.py +++ b/pinecone/models/collection_list.py @@ -3,6 +3,7 @@ CollectionList as OpenAPICollectionList, ) + class CollectionList: """ A list of collections. diff --git a/pinecone/models/index_model.py b/pinecone/models/index_model.py index d8784211..ebf5d238 100644 --- a/pinecone/models/index_model.py +++ b/pinecone/models/index_model.py @@ -14,6 +14,6 @@ def __getattr__(self, attr): def __getitem__(self, key): return self.__getattr__(key) - + def to_dict(self): return self.index.to_dict()