############################################################################### ## Character Count v0.1 ## Counts characters in the selected text ## If there is no selection, the whole document is selected ## Based on HexDump by NickDMax ## Counting code from http://www.daniweb.com/code/snippet216639.html ## By: Scott (wischeese/ZedWonk) import pn import scintilla from pypn.decorators import script def CreateCount(text): output = "" if text is not None: charCount = {} for char in text: charCount[char] = charCount.get(char, 0) + 1 sortedCharTuples = sorted(charCount.items()) for charTuple in sortedCharTuples: charshow = charTuple[0] charint = ord(charshow) if charint <= 32: charshow = " " output += "%02X (%s) - %04d \n" % (charint, charshow, charTuple[1]) return output @script("Character Count", "Analysis") def CharacterCount(): """ Character Count wil create a distribution graph of the selected text""" doc = pn.CurrentDoc() if doc is not None: editor = scintilla.Scintilla(pn.CurrentDoc()) start = editor.SelectionStart end = editor.SelectionEnd if (start == end): start = 0 end = editor.Length text = editor.GetTextRange(start, end) newDoc = pn.NewDocument(None) newEditor = scintilla.Scintilla(newDoc) newEditor.BeginUndoAction() count = CreateCount(text) newEditor.AppendText(len(count), count) newEditor.EndUndoAction()