def pprint_gen(iterable,indices=[0,1,2,-3,-2,-1],sep='...'): """generates items at specified indices""" try: l=len(iterable) indices=(i if i>=0 else l+i for i in indices if i<l) except: #infinite iterable l=None indices=filter(lambda x:x>=0,indices) indices=list(itertools2.unique(indices)) #to remove overlaps indices.sort() j=0 hole=0 for i,item in enumerate(iterable): if i==indices[j]: yield item j+=1 hole=0 if j==len(indices): if l is None: yield sep break #finished else: hole+=1 if hole==1: if sep : yield sep
def pprint_gen(iterable, indices=[0, 1, 2, -3, -2, -1], sep='...'): """generates items at specified indices""" try: l = len(iterable) indices = (i if i >= 0 else l + i for i in indices if i < l) except: # infinite iterable l = None indices = filter(lambda x: x >= 0, indices) indices = list(itertools2.unique(indices)) # to remove overlaps indices.sort() j = 0 hole = 0 for i, item in enumerate(iterable): if i == indices[j]: yield item j += 1 hole = 0 if j == len(indices): if l is None: yield sep break # finished else: hole += 1 if hole == 1: if sep: yield sep
def is_powerful(n): """if a prime p divides n then p^2 must also divide n (also called squareful, square full, square-full or 2-full numbers). """ for f in itertools2.unique(math2.prime_factors(n)): if n%(f*f): return False return True
def unique(self, buffer=100): """ :param buffer: int number of last elements found. if two identical elements are separated by more than this number of elements in self, they might be generated twice in the resulting Sequence :return: Sequence made of unique elements of this one """ return Sequence(itertools2.unique(self, None, buffer))
def unique(self,buffer=100): """ :param buffer: int number of last elements found. if two identical elements are separated by more than this number of elements in self, they might be generated twice in the resulting Sequence :return: Sequence made of unique elements of this one """ return Sequence(itertools2.unique(self,None,buffer))
def search(self, value): # efficient search first requires to dynamically build a tricky request for j, (n, r) in enumerate(itertools2.compress(value)): if j == 0: s1 = "WITH q0 AS (SELECT * FROM seq WHERE n = %d AND repeat >= %d)," % ( n, r) s2 = "SELECT q0.id, q0.i FROM q0" else: s1 = s1 + "\nq%d AS (SELECT * FROM seq WHERE n = %d AND repeat = %d)," % ( j, n, r) s2 = s2 + '\nJOIN q%d ON q%d.id=q0.id AND q%d.i=q%d.i+q%d.repeat' % tuple( [j] * 3 + [j - 1] * 2) s = s1[:-1] + '\n\n' + s2 res = self.execute(s) # keep first result for each sequence only for (id, i) in itertools2.unique(res, key=lambda x: x[0], buffer=None): key = '000000' + str(id) yield 'A' + key[-6:], i
def __or__(self, other): """ :return: Sequence with items from both (sorted) operand Sequences """ return Sequence(itertools2.unique(itertools2.merge(self, other)), None, lambda x: x in self or x in other)