atextcrawler/src/atextcrawler/utils/probe.py

23 lines
538 B
Python

"""
Utility functions for probing / sampling.
"""
def extract_samples(items, n=5):
"""
Extract up to n sample elements from the the given dict or list.
If *items* is a dict return the elements from the list of keys.
"""
l = len(items)
if l <= n:
return items
poss = []
step = (l + 1) / n
for i in range(n):
pos = int(step * i)
if pos < l and (not poss or pos > poss[-1]):
poss.append(pos)
items_list = list(items)
return [items_list[pos] for pos in poss]