From ca487650a06fcdd66a132653c9afdbae38dfd85b Mon Sep 17 00:00:00 2001
From: dmt <>
Date: Thu, 3 Oct 2019 19:08:18 +0200
Subject: [PATCH] Implement the sequence protocol in DataSource.

---
 cml/domain/data_source.py | 28 +++++++++++++++++++++++++---
 1 file changed, 25 insertions(+), 3 deletions(-)

diff --git a/cml/domain/data_source.py b/cml/domain/data_source.py
index d9e6242..20cfdea 100644
--- a/cml/domain/data_source.py
+++ b/cml/domain/data_source.py
@@ -9,9 +9,31 @@ __all__ = (
 
 
 class DataSource:
-    def __init__(self, source, learnblock_identifier):
-        self.source = source
-        self.learnblock_identifier = learnblock_identifier
+    def __init__(self, source, learnblock_identifier, *, block_size):
+        self.block_size = block_size
+        self.__source = source
+        self.__learnblock_identifier = learnblock_identifier
+
+    @property
+    def learnblocks(self):
+        for block in self:
+            for learnblock in self.__learnblock_identifier.identify(block):
+                yield learnblock
+
+    def __getitem__(self, item):
+        start_index = item*self.block_size
+        stop_index = (item+1)*self.block_size
+
+        if stop_index > self.__source.length:
+            stop_index = None
+
+        if start_index > self.__source.length:
+            raise IndexError()
+
+        return self.__source.get_block(start_index, stop_index)
+
+    def __len__(self):
+        return self.__source.length
 
 
 class Preprocessor:
-- 
GitLab