Changeset 1536:8212c4a2204e in orange-bioinformatics for obiKEGG2/databases.py


Ignore:
Timestamp:
02/02/12 11:57:53 (2 years ago)
Author:
ales_erjavec
Branch:
default
Message:

Using batch_get method in iteritems, itervalues methods.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • obiKEGG2/databases.py

    r1535 r1536  
    99from .entry import fields 
    1010from . import api 
     11 
     12def iter_take(source_iter, n): 
     13    source_iter = iter(source_iter) 
     14    return [item for _, item in zip(range(n), source_iter)] 
     15 
     16def batch_iter(source_iter, n): 
     17    source_iter = iter(source_iter) 
     18    while True: 
     19        batch = iter_take(source_iter, n) 
     20        if batch: 
     21            yield batch 
     22        else: 
     23            break 
     24         
     25def chain_iter(chains_iter): 
     26    for iter in chains_iter: 
     27        for element in iter: 
     28            yield element 
    1129 
    1230class DBDataBase(object): 
     
    3755     
    3856    def items(self): 
    39         return [(key, self.__getitem__(key)) for key in self.keys()] 
     57        return list(zip(self.keys(), self.batch_get(self.keys()))) 
    4058     
    4159    def iteritems(self): 
    42         return ((key, self.__getitem__(key)) for key in self.iterkeys()) 
     60        batch_size = 100 
     61        iterkeys = self.iterkeys() 
     62        return chain_iter(zip(batch, self.batch_get(batch)) 
     63                          for batch in batch_iter(iterkeys, batch_size)) 
     64         
     65#        return ((key, self.__getitem__(key)) for key in self.iterkeys()) 
    4366     
    4467    def values(self): 
    45         return [self.__getitem__(key) for key in self.keys()] 
     68        return self.batch_get(self.keys()) 
    4669     
    4770    def itervalues(self): 
    48         return (self.__getitem__(key) for key in self.iterkeys()) 
     71        batch_size = 100 
     72        iterkeys = self.iterkeys() 
     73        return chain_iter(self.batch_get(batch) 
     74                          for batch in batch_iter(iterkeys, batch_size)) 
     75         
     76#        return (self.__getitem__(key) for key in self.iterkeys()) 
    4977     
    5078    def get(self, key, default=None): 
Note: See TracChangeset for help on using the changeset viewer.