Changeset 1535:50a0227812f7 in orange-bioinformatics


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

Added batch_get method to DBDataBase class.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • obiKEGG2/databases.py

    r1532 r1535  
    22DBGET database 
    33""" 
     4from __future__ import absolute_import 
     5 
     6import re 
    47 
    58from . import entry 
     
    7174     
    7275    def get_text(self, key): 
    73         if not key.startswith(self.DB + ":"): 
    74             key = self.DB + ":" + key  
     76        key = self._add_db(key) 
    7577        return self.api.bget([key]) 
    7678     
     
    102104            keys = self.keys() 
    103105             
     106        keys = list(keys) 
    104107        start = 0 
    105         key_count = len(keys) 
    106108        while start < len(keys): 
    107109            batch = keys[start: start + batch_size] 
     
    115117            start += batch_size 
    116118             
     119    def batch_get(self, keys): 
     120        """ Batch retrieve all entries for keys. This can be 
     121        significantly faster then getting each entry separately 
     122        especially if entries are not yet cached. 
     123         
     124        """ 
     125        entries = [] 
     126        batch_size = 100 
     127        keys = list(keys) 
     128        start = 0 
     129        while start < len(keys): 
     130            batch = keys[start: start + batch_size] 
     131            batch = map(self._add_db, batch) 
     132            batch_entries = self.api.bget(batch) 
     133            if batch_entries is not None: 
     134                batch_entries = batch_entries.split("///\n") 
     135                # Remove possible empty last line   
     136                batch_entries = [e for e in batch_entries if e.strip()]  
     137                entries.extend(map(self.ENTRY_TYPE, batch_entries)) 
     138            start += batch_size 
     139             
     140        return entries 
     141             
    117142    def _add_db(self, key): 
    118         """ Prefix the key with 'DB:' string if not already 
     143        """ Prefix the key with '%(DB)s:' string if not already 
    119144        prefixed.  
    120145        """ 
Note: See TracChangeset for help on using the changeset viewer.