Skip to content

Commit

Permalink
Merge pull request #9 from manzik/8-fix-setting-an-array-element-with…
Browse files Browse the repository at this point in the history
…-a-sequence

Refactor data flattening in BenchmarkStats
  • Loading branch information
manzik authored Nov 20, 2024
2 parents 41e26ae + 86a2c4d commit 3afb175
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
18 changes: 15 additions & 3 deletions cmdbench/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ def get_dict_value_converted(value):
class BenchmarkStats:
def __init__(self, data):
mean_val = sd_val = min_val = max_val = None

# Convert to numpy array and flatten
data = np.array(data).flatten()
data = self.__flatten_to_array(data)

if type(data) is np.ndarray:
if len(data) > 0:
Expand All @@ -76,6 +76,18 @@ def __init__(self, data):

self.data = data
self.mean, self.stdev, self.min, self.max = mean_val, sd_val, min_val, max_val

def __flatten_to_array(self, items):
flat_list = []
for item in items:
if isinstance(item, np.ndarray):
flat_list.extend(item.flatten())
elif isinstance(item, (list, tuple)):
flat_list.extend(self.__flatten_to_array(item))
else:
flat_list.append(item)
return np.array(flat_list)

def __repr__(self):
return "(mean: %(mean)s, stdev: %(stdev)s, min: %(min)s, max: %(max)s)" % {
"mean": self.mean, "stdev": self.stdev,
Expand Down Expand Up @@ -128,4 +140,4 @@ def get_sec(time_str):
time_tokens.reverse()
for token_ind, time_token in enumerate(time_tokens):
secs += int(time_token) * 60 ** token_ind
return secs + time_decimal
return secs + time_decimal
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "cmdbench"
version = "0.1.21"
version = "0.1.22"
description = "Quick and easy benchmarking for any command's CPU, memory, disk usage and runtime."
authors = ["Mohsen Yousefian <[email protected]>"]
license = "MIT"
Expand Down

0 comments on commit 3afb175

Please sign in to comment.