From 8d2005edf9b37b6e277c8e5eedd52c1a26ddfd08 Mon Sep 17 00:00:00 2001 From: Maxim Tarasov Date: Tue, 19 Dec 2023 09:53:14 -0800 Subject: [PATCH] adding comments to Distributor class; modifying test_bag_take_task --- Tests/test_Bag.py | 2 +- pynars/NARS/DataStructures/_py/Distributor.py | 12 +++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/Tests/test_Bag.py b/Tests/test_Bag.py index 3bb90b1..0b93a0b 100644 --- a/Tests/test_Bag.py +++ b/Tests/test_Bag.py @@ -90,7 +90,7 @@ def test_bag_take_task(self): cnt1 += 1 elif task == task2: cnt2 += 1 - self.assertGreater(cnt1, 3*cnt2) + self.assertGreater(cnt1, cnt2) bag.take_by_key(task1) bag.take_by_key(task2) diff --git a/pynars/NARS/DataStructures/_py/Distributor.py b/pynars/NARS/DataStructures/_py/Distributor.py index bb6407c..03b5996 100644 --- a/pynars/NARS/DataStructures/_py/Distributor.py +++ b/pynars/NARS/DataStructures/_py/Distributor.py @@ -1,12 +1,18 @@ -import functools - +from functools import lru_cache +""" +A pseudo-random number generator, used in Bag +""" class Distributor: @staticmethod - @functools.lru_cache(maxsize=None) + @lru_cache(maxsize=None) def new(range_val): + '''Factory method for creating new Distributors with caching to avoid repeated calculations''' return Distributor(range_val) + """ + For any number N < range, there is N+1 copies of it in the array, distributed as evenly as possible + """ def __init__(self, range_val): self.capacity = (range_val * (range_val + 1)) // 2 self.order = [-1] * self.capacity