Skip to content

Commit ea4077c

Browse files
authored
enhance: hide zero values ​​when printing (#2201)
- issue: #2199 - pr: #2200 Signed-off-by: SimFG <[email protected]>
1 parent 029403a commit ea4077c

File tree

4 files changed

+44
-18
lines changed

4 files changed

+44
-18
lines changed

examples/milvus_client/simple_cost.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
fmt = "\n=== {:30} ===\n"
88
dim = 8
99
collection_name = "hello_client_cost"
10-
# milvus_client = MilvusClient("http://localhost:19530")
11-
milvus_client = MilvusClient(uri="https://in01-20fa6a32462c074.aws-us-west-2.vectordb-uat3.zillizcloud.com:19541",
12-
token="root:j6|y3/g$5Lq,a[TJ^ckphSMs{-F[&Jl)")
10+
milvus_client = MilvusClient("http://localhost:19530")
1311

1412
has_collection = milvus_client.has_collection(collection_name, timeout=5)
1513
if has_collection:

pymilvus/client/abstract.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,15 @@ def cost(self):
241241
return self._cost
242242

243243
def __str__(self):
244+
if self.cost:
245+
return (
246+
f"(insert count: {self._insert_cnt}, delete count: {self._delete_cnt}, upsert count: {self._upsert_cnt}, "
247+
f"timestamp: {self._timestamp}, success count: {self.succ_count}, err count: {self.err_count}, "
248+
f"cost: {self._cost})"
249+
)
244250
return (
245251
f"(insert count: {self._insert_cnt}, delete count: {self._delete_cnt}, upsert count: {self._upsert_cnt}, "
246-
f"timestamp: {self._timestamp}, success count: {self.succ_count}, err count: {self.err_count}, "
247-
f"cost: {self._cost})"
252+
f"timestamp: {self._timestamp}, success count: {self.succ_count}, err count: {self.err_count}"
248253
)
249254

250255
__repr__ = __str__
@@ -515,7 +520,9 @@ def __iter__(self) -> SequenceIterator:
515520
def __str__(self) -> str:
516521
"""Only print at most 10 query results"""
517522
reminder = f" ... and {len(self) - 10} results remaining" if len(self) > 10 else ""
518-
return f"data: {list(map(str, self[:10]))}{reminder}, cost: {self.cost}"
523+
if self.cost:
524+
return f"data: {list(map(str, self[:10]))}{reminder}, cost: {self.cost}"
525+
return f"data: {list(map(str, self[:10]))}{reminder}"
519526

520527
__repr__ = __str__
521528

pymilvus/client/types.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@
1414
ConsistencyLevel = common_pb2.ConsistencyLevel
1515

1616

17+
# OmitZeroDict: ignore the key-value pairs with value as 0 when printing
18+
class OmitZeroDict(dict):
19+
def omit_zero_len(self):
20+
return len(dict(filter(lambda x: x[1], self.items())))
21+
22+
# filter the key-value pairs with value as 0
23+
def __str__(self):
24+
return str(dict(filter(lambda x: x[1], self.items())))
25+
26+
# no filter
27+
def __repr__(self):
28+
return str(dict(self))
29+
30+
1731
class Status:
1832
"""
1933
:attribute code: int (optional) default as ok
@@ -928,11 +942,13 @@ class ExtraList(list):
928942

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

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

937953
__repr__ = __str__
938954

pymilvus/milvus_client/milvus_client.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
ExceptionsMessage,
1010
ExtraList,
1111
LoadState,
12+
OmitZeroDict,
1213
construct_cost_extra,
1314
)
1415
from pymilvus.exceptions import (
@@ -221,11 +222,13 @@ def insert(
221222
)
222223
except Exception as ex:
223224
raise ex from ex
224-
return {
225-
"insert_count": res.insert_count,
226-
"ids": res.primary_keys,
227-
"cost": res.cost,
228-
}
225+
return OmitZeroDict(
226+
{
227+
"insert_count": res.insert_count,
228+
"ids": res.primary_keys,
229+
"cost": res.cost,
230+
}
231+
)
229232

230233
def upsert(
231234
self,
@@ -272,10 +275,12 @@ def upsert(
272275
except Exception as ex:
273276
raise ex from ex
274277

275-
return {
276-
"upsert_count": res.upsert_count,
277-
"cost": res.cost,
278-
}
278+
return OmitZeroDict(
279+
{
280+
"upsert_count": res.upsert_count,
281+
"cost": res.cost,
282+
}
283+
)
279284

280285
def search(
281286
self,
@@ -555,7 +560,7 @@ def delete(
555560
if ret_pks:
556561
return ret_pks
557562

558-
return {"delete_count": res.delete_count, "cost": res.cost}
563+
return OmitZeroDict({"delete_count": res.delete_count, "cost": res.cost})
559564

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

0 commit comments

Comments
 (0)