def _get_hash(self): config = settings.get_section('General') encoding = config['encoding'] result = hashlib.md5() for chunk in self.iter_chunks(): chunk = chunk.encode(encoding, errors='replace') result.update(chunk) # hash objects don't define an __eq__ so we need to use a string # representation of the hash return result.hexdigest()
def save(self): # Save the file to the current path if self.path is None: return self.save_as() self.event_generate('<<Save>>') encoding = settings.get_section('General')['encoding'] try: with utils.backup_open(self.path, 'w', encoding=encoding) as f: for chunk in self.iter_chunks(): f.write(chunk) except (OSError, UnicodeError) as e: utils.errordialog( type(e).__name__, "Saving failed!", traceback.format_exc()) return None self.mark_saved() return True
def get_statistics(): #read in stopwords m_stop_words = [] path = os.path.dirname(os.path.abspath(__file__)) path = os.path.join(path, 'stop_words.txt') config = settings.get_section('General') with open(path, 'r', encoding=config['encoding']) as file: content = file.read() m_stop_words = content.split('\n') tab = m_tab_manager.select() #tab.tokens (property getter) words = tab.tokens if len(words) == 0: tokenize_file(show=False) #tab.tokens (property getter) words = tab.tokens # compute word frequency via dictionary dict = {} for word in words: if word not in dict: dict[word] = 1 else: dict[word] += 1 sorted_dict = sorted(dict.items(), key=operator.itemgetter(1), reverse=True) new_dict = {} ranking_text = 'Total words: ' + str( len(words)) + "\nWord frequencies: \n" for key, val in sorted_dict: ranking_text += (key + "\t\t" + str(val) + "\n") if key not in m_stop_words: new_dict[key] = val # setup statistics dialog m_dialog = tkinter.Toplevel() m_dialog.withdraw() m_dialog.title("Statistics") m_dialog.protocol('WM_DELETE_WINDOW', m_dialog.withdraw) m_dialog.geometry('600x400') m_notebook = ttk.Notebook(m_dialog) m_notebook.pack(fill='both', expand=True) # add tabs to the frame # ranking tab in tk.Text ranking_frame = tkinter.Text(m_notebook) m_notebook.add(ranking_frame, text="Ranking") ranking_frame.insert('1.0', ranking_text) # tab show bar graph draw_frame = ttk.Frame(m_notebook) m_notebook.add(draw_frame, text="Graph") if len(m_stop_words) == 0: raise RuntimeError("stop word list is empty") fig = Figure(figsize=(5, 4), dpi=100) ax = fig.add_subplot(111) label = [] data = [] i = 0 for key, val in new_dict.items(): if (i >= 6) or (i >= len(new_dict)): break if key in m_stop_words: continue data.append(val) label.append(key.lower()) i += 1 ind = numpy.arange(len(data)) graph = ax.bar(ind, data, width=0.5) ax.set_ylabel('Frequency') ax.set_xlabel('Words') ax.set_xticks(ind) ax.set_xticklabels(label) # ax.set_xticklabels(label, rotation=45, ha="right") ax.set_title('Top used words') canvas = FigureCanvasTkAgg(fig, master=draw_frame) canvas.draw() canvas.get_tk_widget().pack(side='top', fill='both', expand=True) m_dialog.transient(m_root) m_dialog.deiconify()
def open_file(cls, manager, path): # Read a file and return a new FileTab object. config = settings.get_section('General') with open(path, 'r', encoding=config['encoding']) as file: content = file.read() return cls(manager, content, path)
from _run import get_main_window import settings config = settings.get_section('General') config.add_option('default_geometry', '650x600', reset=False) def save_geometry(event): config['default_geometry'] = event.widget.geometry() def setup(): get_main_window().geometry(config['default_geometry']) get_main_window().bind('<<SimpleEditorQuit>>', save_geometry, add=True)