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

Add repr to the main class. #68

Merged
merged 1 commit into from
Oct 8, 2022
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
9 changes: 9 additions & 0 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ def num_compactions(request):
return request.param


def test_ctor():
hist = UDDSketch(initial_error=0.1, max_buckets=128)
hist.add(0.1)
hist.add(0.2)
hist.add(0.3)
expected = "<UDDSketch min=0.1000 max=0.3000 mean=0.2000 var=0.0067>"
assert repr(hist) == expected


def test_mean_var(arr_w, alpha):
arr = [v[0] for v in arr_w]
weights = [v[1] for v in arr_w]
Expand Down
11 changes: 11 additions & 0 deletions tests/test_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,29 @@ def test_basic():
s.add_to_bucket(2)
assert s._head == 2
assert s._tail == 2
assert s.buckets() == [(2, 1)]

s.add_to_bucket(3)
assert s._head == 2
assert s._tail == 3
assert s.buckets() == [(2, 1), (3, 1)]

s.add_to_bucket(4)
assert s._head == 2
assert s._tail == 4
assert s.buckets() == [(2, 1), (3, 1), (4, 1)]

s.add_to_bucket(1)
assert s._head == 1
assert s._tail == 4
assert s.buckets() == [(1, 1), (2, 1), (3, 1), (4, 1)]

s.add_to_bucket(2)
assert s._head == 1
assert s._tail == 4
assert s.buckets() == [(1, 1), (2, 2), (3, 1), (4, 1)]

assert repr(s) == "<_Store [(1, 1), (2, 2), (3, 1), (4, 1)]>"


def test_bucket_at_count():
Expand Down
18 changes: 17 additions & 1 deletion uddsketch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import math
from dataclasses import dataclass
from typing import Callable, Dict, Optional, Union
from typing import Callable, Dict, List, Optional, Tuple, Union

from ._version import version as _version

Expand All @@ -27,9 +27,17 @@ def __init__(self) -> None:
self._tail: Optional[int] = None
self._count: int = 0

def __repr__(self) -> str:
klass = self.__class__.__name__
return f"<{klass} {self.buckets()}>"

def size(self) -> int:
return len(self._store)

def buckets(self) -> List[Tuple[float, int]]:
bucket_counts = sorted(self._store.items(), key=lambda v: v[0])
return [(k, v.count) for k, v in bucket_counts]

@property
def num_values(self) -> int:
return self._count
Expand Down Expand Up @@ -134,6 +142,14 @@ def __init__(
self._zero_counts: int = 0
self._pos_storage: _Store = _Store()

def __repr__(self) -> str:
klass = self.__class__.__name__
t = (
f"<{klass} min={self.min():.4f} max={self.max():.4f} "
f"mean={self.mean():.4f} var={self.var():.4f}>"
)
return t

def min(self) -> float:
return self._min if self.num_values > 0 else float("-inf")

Expand Down