def sample(self): if self.downloading: self.set_hdr_paths() while not len(self.hdr_paths): self.set_hdr_paths() if timegap(5, 'waiting for download ".hdr" file'): print('Waiting for download first ".hdr" file....') sleep(0.1) return random.choice(self.hdr_paths)
def sample(self): if self.downloading: self.set_hdr_paths() while not len(self.hdr_paths): assert ( self.downloading ), f'No hdri file in "{self.hdri_dir}", make sure HdriManager(download=True)' self.set_hdr_paths() if timegap(5, 'waiting for download ".hdr" file'): print('Waiting for download first ".hdr" file....') sleep(0.1) return random.choice(self.hdr_paths)
def __translteMultiInGoogle(self, ss, batch_size=200, gap=240): ''' give up ''' df = self.df toTrans = [s for i, s in enumerate(ss) if s not in df.index] batchs = listToBatch(toTrans, batch_size) for ind, batch in enumerate(batchs): for tryn in range(9): try: with timeit('%snd batch, %s requests' % (ind, len(batch))): tryn and sleep(gap * 1.5**tryn) res = self.translator.translate(list(batch), src=self.src, dest=self.dest) texts = [re.text for re in res] self.lock.acquire() for s, text in zip(toTrans, texts): df.loc[s] = text if self.cache_path: strr = '\n'.join([ ','.join( [s.replace(',', ' '), text.replace(',', ' ')]) for s, text in zip(toTrans, texts) ]) + '\n' with open(self.cache_path, 'a') as f: f.write(strr) self.lock.release() pred('【OK】 %snd/%s batch, %s requests' % (ind, len(toTrans) // batch_size, len(batch))) sleep(gap) break except Exception as exc: print('tryn:', tryn, exc.__repr__())