Exemple #1
0
    def store(self, key, value, timeout=1):
        """
        :param key: string sequence id
        :param value: iterable. if finite
        :param timeout: float , max time in seconds to store digits in db
        """
        n = self.lenghtOf(key)
        if n > 0:
            try:
                if n >= len(value):  # already stored, even more ...
                    return n
            except TypeError:
                pass  # value is a generator, we'll store as much as possible

        from Goulib.decorators import itimeout
        from multiprocessing import TimeoutError

        cursor = self.db.cursor()
        req = "INSERT OR REPLACE INTO seq (id, i, n, repeat) VALUES"
        i = 1
        try:
            for n, r in itimeout(itertools2.compress(value), timeout):
                s = req + str((key[1:], i, n, r))
                cursor.execute(s)
                i = i + r
        except TimeoutError:
            pass
        finally:
            cursor.close()
            self.db.commit()
            return i
Exemple #2
0
 def __iter__(self):
     """reset the generator
     
     :return: a tee-ed copy of iterf, optionally timeout decorated
     """
     if self.iterf:
         self.iterf, res = tee(self.iterf)
         if self.timeout:
             res = decorators.itimeout(res, self.timeout)
     else:
         it = count(self.offset)
         if self.timeout:
             it = decorators.itimeout(it, self.timeout)
         if self.itemf:
             res = map(lambda i: self[i], it)
         else:
             res = filter(lambda n: n in self, it)  # uses containf
     return res
Exemple #3
0
def assert_generator(f, l, name, time_limit=10):
    i = 0
    try:
        for item1, item2 in decorators.itimeout(zip(f, l), time_limit):
            m = "%s : First differing element %d: %s != %s\n" % (name, i, item1, item2)
            assert_equal(item1, item2, msg=m)
            i += 1
    except decorators.TimeoutError:
        if i < min(10, len(l) / 2):
            logging.warning("%s timeout after only %d loops" % (name, i))
        else:
            logging.debug("%s timeout after %d loops" % (name, i))
Exemple #4
0
def assert_generator(f, l, name, time_limit=10):
    i = 0
    try:
        for item1, item2 in decorators.itimeout(zip(f, l), time_limit):
            m = '%s : First differing element %d: %s != %s\n' % (name, i,
                                                                 item1, item2)
            assert_equal(item1, item2, msg=m)
            i += 1
    except decorators.TimeoutError:
        if i < min(10, len(l) / 2):
            logging.warning('%s timeout after only %d loops' % (name, i))
        else:
            logging.debug('%s timeout after %d loops' % (name, i))
Exemple #5
0
def pprint(iterable,indices=[0,1,2,3,4,5,6,7,8,9,-3,-2,-1],timeout=1):
    sep='...'
    s=[]
    try:
        items=pprint_gen(iterable, indices, sep)
        for item in decorators.itimeout(items, timeout): 
            if isinstance(item,str):
                s.append(item) #to keep unicode untouched
            else:
                s.append(str(item))
    except decorators.TimeoutError:
        if s[-1]!=sep:
            s.append(sep)
    return ','.join(s)
Exemple #6
0
def pprint(iterable,indices=[0,1,2,3,4,5,6,7,8,9,-3,-2,-1],timeout=1):
    sep='...'
    s=[]
    try:
        items=pprint_gen(iterable, indices, sep)
        for item in decorators.itimeout(items, timeout): 
            if isinstance(item,six.string_types):
                s.append(item) #to keep unicode untouched
            else:
                s.append(str(item))
    except decorators.TimeoutError:
        if s[-1]!=sep:
            s.append(sep)
    return ','.join(s)
Exemple #7
0
def pi_primes():
    v=0
    for i,d in decorators.itimeout(enumerates(pi_digits_gen()),10):
        v=10*v+d
        if is_prime(v):
            yield i+1, v
Exemple #8
0
 def _gen():
     p=b**start
     for _ in decorators.itimeout(count(),10):
         yield a*p+c
         p=p*b
Exemple #9
0
 def _gen():
     p=b**start
     for _ in decorators.itimeout(count(),10):
         yield a*p+c
         p=p*b