def __init__(self, json_file: TextIO, close_fd: bool, use_rate: bool = True): data = json.load(json_file) y_list = data["y_list"] total = data["total"] if use_rate: y_list = [cracked / total * 100 for cracked in y_list] if close_fd: json_file.close() elif json_file.seekable(): json_file.seek(0) self.x_list = data["x_list"] self.y_list = y_list self.color = data['color'] self.marker = data['marker'] self.marker_size = data['marker_size'] self.mark_every = data["mark_every"] self.line_width = data['line_width'] if type(data['line_style']) is str: self.line_style = data['line_style'] else: self.line_style = (data['line_style'][0], tuple(list(data['line_style'][1]))) self.label = data['label'] self.text = data['label'] self.show_text = data['show_text'] self.text_x = data['text_x'] self.text_y = data['text_y'] self.text_fontsize = data['text_fontsize'] self.text_color = data['text_color']
def dump_mordict(self, buffer: typing.TextIO, with_comments: bool = True) -> typing.NoReturn: if self.enabled: self.phon.dump_mordict(buffer, with_comments) buffer.write("\t") self.cat.dump_mordict(buffer, with_comments) if self.sem.value: buffer.write(" ") # === END IF === self.sem.dump_mordict(buffer, with_comments) if self.gloss.value: buffer.write(" ") # === END IF === self.gloss.dump_mordict(buffer, with_comments) else: # discard contents and just dump comments disabled_entry = io.StringIO() # type: io.StringIO self.phon.dump_mordict(disabled_entry, with_comments) buffer.write("\t") self.cat.dump_mordict(disabled_entry, with_comments) if self.sem.value: buffer.write(" ") # === END IF === self.sem.dump_mordict(disabled_entry, with_comments) if self.gloss.value: buffer.write(" ") # === END IF === self.gloss.dump_mordict(disabled_entry, with_comments) disabled_entry_res = disabled_entry.getvalue().replace( "\r\n", " ").replace("\n", " ").replace("\r", " ") # type: str buffer.writelines(( "% DISABLED: ", disabled_entry_res, )) # === END IF === if with_comments: for c in self.comments: c.dump_mordict(buffer, with_comments=True) # === END FOR c === # === END IF === if buffer.seekable(): buffer.seek(buffer.tell() - 1) last_char = buffer.read() # type: str if last_char not in "\n\r": buffer.write("\n") # === END IF === else: buffer.write("\n")
def wc_l(file: TextIO, new_line: str = "\n", silence: bool = False): """ a pure function, file will not be closed and move the pointer to the begin :param new_line: how to detect a new line :param silence: show warnings :param file: file to count lines :return: number of lines """ if file.seekable(): file.seek(0) elif not silence: print("WARNING: file cannot seekable", file=sys.stderr) buf_size = 8 * 1024 * 1024 count = 0 while True: buffer = file.read(buf_size) if not buffer: count += 1 break count += buffer.count(new_line) if file.seekable(): file.seek(0) return count
def wrapper(dataset: TextIO, save: TextIO): if not dataset.seekable(): raise Exception("Not seekable, do not use stdin please") if not save.writable(): raise Exception(f"{save.name} Not writable") total_size, len_dict = len_dist(dataset, False) dataset.seek(0) total_chr, chr_dict, cls_dict = chr_dist(dataset, True) json.dump( { "#len": total_size, "len": len_dict, "#chr": total_chr, "chr": chr_dict, "cls": cls_dict }, save, indent=2) save.close()