UsefulProg 0.1

March 14, 2005 | categories: Uncategorized | View Comments

As a documentation enthusiast, I find myself needing to change documents from one silly format to another on regular basis. Usually, I end up writing stupid Perl scripts to convert from one format to another, but I can never remember Perl syntax between efforts. I swear I've written the same text parsing code 4 times, and it hasn't gotten easier.

In an effort to end this depressing cycle, I spent some time this afternoon learning a little wxPython to make what I call UsefulProg. UsefulProg reads in a text file. You enter some regular expressions, and then it filters the file with those expressions. When you screw up (like I always do), you hit the reset button, and it erases your mistakes. When you get the filters working the way you want, you cut and paste the result into wherever you need it.

Here's the code under a GPL license. It's based on an example from the wxPython documentation.

from wxPython.wx import *
import os
import re

class Form1(wxPanel): def init(self, parent, id): wxPanel.init(self, parent, id)

self.logger = wxTextCtrl(self,5, <span class="s">"&quot;,wxPoint(250,20), wxSize(250,100),wxTE_MULTILINE | wxTE_READONLY)

#The input file display self.inputlbl = wxStaticText(self,-1, <span class="s">"Input file&quot;,wxPoint(20,5)) f = open(<span class="s">"/home/brandon/Python Experiments/dd_tpb.txt&quot;, &quot;rb&quot;) self.inputfield = wxTextCtrl(self,6,f.read(),wxPoint(20,20), wxSize(200,700),wxTE_MULTILINE | wxTE_READONLY)

#The output file display self.outputlbl = wxStaticText(self,-1, <span class="s">"Output file&quot;,wxPoint(550,5)) self.outputfield = wxTextCtrl(self,6,<span class="s">"&quot;,wxPoint(550,20), wxSize(500,700),wxTE_MULTILINE | wxTE_READONLY) self.outputfield.write(self.inputfield.GetValue())

# The filter button self.button =wxButton(self, 11, <span class="s">"Filter&quot;, wxPoint(300, 200)) EVT_BUTTON(self, 11, self.EvtFilterButton)

# The reset button self.button =wxButton(self, 12, <span class="s">"Reset&quot;, wxPoint(300, 240)) EVT_BUTTON(self, 12, self.EvtResetButton)

# the edit control - one line version. self.regexsample = wxStaticText(self,-1, <span class="s">"Useful: [D|d]*? is non greedy match anything&quot;,wxPoint(250,320)) self.lblname1 = wxStaticText(self, -1, <span class="s">"Removal string 1 :&quot;,wxPoint(250,380)) self.filter1 = wxTextCtrl(self, 20, <span class="s">"(Rate[D|d]*?mments:n&quot;, wxPoint(400, 380), wxSize(140,-1))

self.lblname2 = wxStaticText(self, -1, <span class="s">"Removal string 2 :&quot;,wxPoint(250,480)) self.filter2 = wxTextCtrl(self, 20, <span class="s">" *pixel&quot;, wxPoint(400, 480), wxSize(140,-1))

self.lblname3 = wxStaticText(self, -1, <span class="s">"Removal string 3 :&quot;,wxPoint(250,580)) self.filter3 = wxTextCtrl(self, 20, <span class="s">"d+?. nt&quot;, wxPoint(400, 580), wxSize(140,-1))

self.filtlblA = wxStaticText(self, -1, <span class="s">"Removal string 4 :&quot;,wxPoint(250,680)) self.filterA = wxTextCtrl(self, 20, <span class="s">"nDare&quot;, wxPoint(400, 680), wxSize(140,-1)) self.replblA = wxStaticText(self, -1, <span class="s">"Replacement string 4 :&quot;,wxPoint(250,730)) self.newtextA = wxTextCtrl(self, 20, <span class="s">"nnDare&quot;, wxPoint(400, 730), wxSize(140,-1))

<span class="k">def</span> <span class="nf">OnClick</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">event</span><span class="p">):</span>

self.logger.AppendText(<span class="s">" Click on object with Id %dn&quot; %event.GetId()) def EvtText(self, event): self.logger.AppendText(<span class="s">"&quot;) #('EvtText: %sn' % event.GetString()) def EvtChar(self, event): self.logger.AppendText(<span class="s">"&quot;) event.Skip() def EvtFilterButton(self, event): text = self.outputfield.GetValue()

filter = self.filter1.GetValue() text, reps = re.subn(filter,<span class="s">"&quot;,text) self.outputfield.Clear() self.outputfield.write(text) self.logger.AppendText(<span class="s">"%d removals made with filter 1n&quot; % reps)

filter = self.filter2.GetValue() text, reps = re.subn(filter,<span class="s">"&quot;,text) self.outputfield.Clear() self.outputfield.write(text) self.logger.AppendText(<span class="s">"%d removals made with filter 2n&quot; % reps)

filter = self.filter3.GetValue() text, reps = re.subn(filter,<span class="s">"&quot;,text) self.outputfield.Clear() self.outputfield.write(text) self.logger.AppendText(<span class="s">"%d removals made with filter 3n&quot; % reps)

filter = self.filterA.GetValue() newtext = self.newtextA.GetValue() text, reps = re.subn(filter,newtext,text) self.outputfield.Clear() self.outputfield.write(text) self.logger.AppendText(<span class="s">"%d replacements made with filter An&quot; % reps)

def EvtResetButton(self, event): self.outputfield.Clear() self.outputfield.write(self.inputfield.GetValue())

app = wxPySimpleApp() frame = wxFrame(None, -1,<span class="s">" UsefulProgPRO&quot;,(100,100),(1100,800)) Form1(frame,-1) frame.Show(1) app.MainLoop()

blog comments powered by Disqus