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

Improve output from list/describe actions on indexes and collections #387

Merged
merged 3 commits into from
Aug 28, 2024
Merged
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
4 changes: 4 additions & 0 deletions pinecone/control/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
from .pinecone import Pinecone

from .repr_overrides import install_repr_overrides

install_repr_overrides()
17 changes: 17 additions & 0 deletions pinecone/control/repr_overrides.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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)
3 changes: 2 additions & 1 deletion pinecone/models/collection_list.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
from pinecone.core.openapi.control.models import (
CollectionList as OpenAPICollectionList,
)
Expand Down Expand Up @@ -28,7 +29,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)
5 changes: 3 additions & 2 deletions pinecone/models/index_list.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
from pinecone.core.openapi.control.models import IndexList as OpenAPIIndexList
from .index_model import IndexModel

Expand All @@ -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)
6 changes: 3 additions & 3 deletions pinecone/models/index_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ 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)

def __getitem__(self, key):
return self.__getattr__(key)

def to_dict(self):
return self.index.to_dict()
Loading