Skip to content

Commit

Permalink
enhance: hide zero values ​​when printing (#2201)
Browse files Browse the repository at this point in the history
- issue: #2199
- pr: #2200

Signed-off-by: SimFG <[email protected]>
  • Loading branch information
SimFG authored Jul 26, 2024
1 parent 029403a commit ea4077c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 18 deletions.
4 changes: 1 addition & 3 deletions examples/milvus_client/simple_cost.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
fmt = "\n=== {:30} ===\n"
dim = 8
collection_name = "hello_client_cost"
# milvus_client = MilvusClient("http://localhost:19530")
milvus_client = MilvusClient(uri="https://in01-20fa6a32462c074.aws-us-west-2.vectordb-uat3.zillizcloud.com:19541",
token="root:j6|y3/g$5Lq,a[TJ^ckphSMs{-F[&Jl)")
milvus_client = MilvusClient("http://localhost:19530")

has_collection = milvus_client.has_collection(collection_name, timeout=5)
if has_collection:
Expand Down
13 changes: 10 additions & 3 deletions pymilvus/client/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,15 @@ def cost(self):
return self._cost

def __str__(self):
if self.cost:
return (
f"(insert count: {self._insert_cnt}, delete count: {self._delete_cnt}, upsert count: {self._upsert_cnt}, "
f"timestamp: {self._timestamp}, success count: {self.succ_count}, err count: {self.err_count}, "
f"cost: {self._cost})"
)
return (
f"(insert count: {self._insert_cnt}, delete count: {self._delete_cnt}, upsert count: {self._upsert_cnt}, "
f"timestamp: {self._timestamp}, success count: {self.succ_count}, err count: {self.err_count}, "
f"cost: {self._cost})"
f"timestamp: {self._timestamp}, success count: {self.succ_count}, err count: {self.err_count}"
)

__repr__ = __str__
Expand Down Expand Up @@ -515,7 +520,9 @@ def __iter__(self) -> SequenceIterator:
def __str__(self) -> str:
"""Only print at most 10 query results"""
reminder = f" ... and {len(self) - 10} results remaining" if len(self) > 10 else ""
return f"data: {list(map(str, self[:10]))}{reminder}, cost: {self.cost}"
if self.cost:
return f"data: {list(map(str, self[:10]))}{reminder}, cost: {self.cost}"
return f"data: {list(map(str, self[:10]))}{reminder}"

__repr__ = __str__

Expand Down
20 changes: 18 additions & 2 deletions pymilvus/client/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@
ConsistencyLevel = common_pb2.ConsistencyLevel


# OmitZeroDict: ignore the key-value pairs with value as 0 when printing
class OmitZeroDict(dict):
def omit_zero_len(self):
return len(dict(filter(lambda x: x[1], self.items())))

# filter the key-value pairs with value as 0
def __str__(self):
return str(dict(filter(lambda x: x[1], self.items())))

# no filter
def __repr__(self):
return str(dict(self))


class Status:
"""
:attribute code: int (optional) default as ok
Expand Down Expand Up @@ -928,11 +942,13 @@ class ExtraList(list):

def __init__(self, *args, extra: Optional[Dict] = None, **kwargs) -> None:
super().__init__(*args, **kwargs)
self.extra = extra or {}
self.extra = OmitZeroDict(extra or {})

def __str__(self) -> str:
"""Only print at most 10 query results"""
return f"data: {list(map(str, self[:10]))} {'...' if len(self) > 10 else ''}, extra_info: {self.extra}"
if self.extra and self.extra.omit_zero_len() != 0:
return f"data: {list(map(str, self[:10]))} {'...' if len(self) > 10 else ''}, extra_info: {self.extra}"
return f"data: {list(map(str, self[:10]))} {'...' if len(self) > 10 else ''}"

__repr__ = __str__

Expand Down
25 changes: 15 additions & 10 deletions pymilvus/milvus_client/milvus_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
ExceptionsMessage,
ExtraList,
LoadState,
OmitZeroDict,
construct_cost_extra,
)
from pymilvus.exceptions import (
Expand Down Expand Up @@ -221,11 +222,13 @@ def insert(
)
except Exception as ex:
raise ex from ex
return {
"insert_count": res.insert_count,
"ids": res.primary_keys,
"cost": res.cost,
}
return OmitZeroDict(
{
"insert_count": res.insert_count,
"ids": res.primary_keys,
"cost": res.cost,
}
)

def upsert(
self,
Expand Down Expand Up @@ -272,10 +275,12 @@ def upsert(
except Exception as ex:
raise ex from ex

return {
"upsert_count": res.upsert_count,
"cost": res.cost,
}
return OmitZeroDict(
{
"upsert_count": res.upsert_count,
"cost": res.cost,
}
)

def search(
self,
Expand Down Expand Up @@ -555,7 +560,7 @@ def delete(
if ret_pks:
return ret_pks

return {"delete_count": res.delete_count, "cost": res.cost}
return OmitZeroDict({"delete_count": res.delete_count, "cost": res.cost})

def get_collection_stats(self, collection_name: str, timeout: Optional[float] = None) -> Dict:
conn = self._get_connection()
Expand Down

0 comments on commit ea4077c

Please sign in to comment.