OSDN Git Service

Use generator.
authorAiwota Programmer <aiwotaprog@tetteke.tk>
Tue, 19 Sep 2006 18:08:20 +0000 (03:08 +0900)
committerAiwota Programmer <aiwotaprog@tetteke.tk>
Tue, 19 Sep 2006 18:08:20 +0000 (03:08 +0900)
src/FukuiNoNamari/board_data.py
src/FukuiNoNamari/cachefile.py

index 24b32a0..298df3b 100644 (file)
@@ -90,13 +90,11 @@ class BoardData:
         return dic
 
     def load_idxfiles(self):
-        datalist = {}
-
-        def on_load_record(id, metadata_dic):
-            datalist[id] = self._init_extra_data(metadata_dic)
-
         print "load_cache"
-        cachefile.load_cache(self.bbs_type, on_load_record)
+        iterable = cachefile.load_cache(self.bbs_type)
+        iterable = itertools.imap(self._init_extra_data, iterable)
+        datalist = dict([(dic["id"], dic) for dic in iterable])
+
         print "load_idx"
         self._load_modified_idxfiles(datalist)
         print "save_cache"
index f913ab6..8a7af70 100644 (file)
@@ -67,16 +67,12 @@ def _dict_to_tabbed(dic):
 
     return line
 
-def load_cache(bbs_type, func):
-    """ Loads metadata from .cache file
+def load_cache(bbs_type):
+    """ Loads metadata from .cache file. This is generator.
 
-    loads metadata and invokes handler per one thread
+    loads metadata and yield per one thread
 
-    func: invoked at loading one thread's metadata
-    should be of the form:
-    def handler_example(id, metadata_dic):
-    where the first argument is thread id and
-    the second metadata dic which key is in metadata_namelist
+    metadata dic key is in metadata_namelist
     """
 
     cachefile_path = misc.get_board_cache_path(bbs_type)
@@ -89,7 +85,7 @@ def load_cache(bbs_type, func):
 
             # only if id exists
             if "id" in dic and dic["id"]:
-                func(dic["id"], dic)
+                yield dic
 
     except IOError:
         traceback.print_exc()