def format_data(self, columns): data = columns if self.args.metaj: data = self.metaj_content % tuple(columns) elif self.args.format == JSON_FORMAT: data = json_item(self.column_names, columns) return data
def format_data(self, columns): if self.args.metaj: data = self.metaj_content % tuple(columns) else: data = json_item(self.column_names, columns) return data
def format_data(self, columns): data = columns if self.args.metaj: data = self.metaj_content % tuple(columns) elif self.args.format == JSON_FORMAT: data = json_item(self.column_names, columns) elif self.args.format == TEXT_FORMAT: data = self.args.outspliter.join( [str(safe_encode(word)) for word in columns]) return data
def fake_data(self): """ sleep是为了防止产生数据后消费数据过慢 :return: """ while self.get_cur_num() < self.args.num: columns = self.fake_column(self.cur_num.value) if self.args.format == JSON_FORMAT: columns = json_item(self.column_names, columns) self.queue.put(columns) sleep(0.1) self.isover.value = True
def do_fake(self): i = 0 try: while True: lines = self.fake_column() content = json_item(self.column_names, lines) if self.args.outprint: print(content) self.save_data(content) i += 1 print('insert %d records' % i) if self.args.interval: time.sleep(self.args.interval) except KeyboardInterrupt: print("generated records : %d" % i) print("insert records : %d" % i)
def fake_data(self): """ 多线程产生数据,需要先加一再产生数据,不然其他线程会执行产生多的数据 sleep是为了防止产生数据后消费数据过慢 :return: """ while self.cur_num.value < self.args.num: with self.lock: self.cur_num.value += 1 columns = self.fake_column() if self.args.format == JSON_FORMAT: columns = json_item(self.column_names, columns) self.queue.put(columns) sleep(0.1) self.isover.value = True
def test_json_item(): list1 = ['name', 'age'] list2 = ['mary', 12] assert json.loads('''{"name":"mary", "age":12}''') == json.loads( json_item(list1, list2))