[Tutor] Tutor Digest, Vol 37, Issue 63

Jay Mutter III jmutter at uakron.edu
Sun Mar 25 13:47:11 CEST 2007


>
> Message: 1
> Date: Sat, 24 Mar 2007 16:41:22 -0700 (PDT)
> From: Jaggo <jaggojaggo at yahoo.com>
> Subject: Re: [Tutor] Tutor Digest, Vol 37, Issue 62
> To: tutor at python.org
> Message-ID: <20070324234122.86838.qmail at web52504.mail.re2.yahoo.com>
> Content-Type: text/plain; charset="iso-8859-1"
>
> Message: 2
> Date: Sat, 24 Mar 2007 19:25:10 -0400
> From: Jay Mutter III
> Subject: [Tutor] parsing text
> [...]
> 1.) when i do readlines and create a list and then print the list it
> adds a blank line between every line of text
> [...]
> ideas?
>
> Thanks again
>
> jay
> Well,
> regarding your first question:
> "print string" automatically breaks a line at the end of string.  
> Use "print string," instead [note that trailin' , .]
>

yes, thank you for that


> [I'm not sure about your n. 2, that's why no answer is included.
>
>
> ---------------------------------
> TV dinner still cooling?
> Check out "Tonight's Picks" on Yahoo! TV.
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: http://mail.python.org/pipermail/tutor/attachments/ 
> 20070324/2d731ac8/attachment-0001.htm
>
> ------------------------------
>
> Message: 2
> Date: Sun, 25 Mar 2007 00:00:29 -0000
> From: "Alan Gauld" <alan.gauld at btinternet.com>
> Subject: Re: [Tutor] parsing text
> To: tutor at python.org
> Message-ID: <eu4e2v$o5h$1 at sea.gmane.org>
> Content-Type: text/plain; format=flowed; charset="iso-8859-1";
> 	reply-type=original
>
> "Jay Mutter III" <jmutter at uakron.edu> wrote
>
>> i have the following text:
>>
>> Barnett, John B., assignor of one-half to R. N. Tutt, Kansas City,
>> Mo.    Automatic display-sign.    No. 1,330 411-Apr. 13 ; v. 273 ;
>> p.
>> 193.
>> Barnett,  John  II..  Tettenhall,  England.     Seat  of
>> motorcars.    No. 1.353,708; Sept. 21 ; v. 278; p. 487. Barnett,
>> Otto
>> R.    (See Scott, John M., assignor.)
>>
>> 1.) when i do readlines and create a list and then print the list it
>> adds a blank line between every line of text
>
> I suspect that's because you are reading a newline character
> from the file and print adds a newline of its own. You need to
> use rstrip() to take out the newline from the file.
>
>> 2.)in the second line after p.487 there is the beginning of a new
>> line of data only it isn't on a newline.
>
> I'm not quite sure what you mean here.
> It would be helpful if you can show us the problematic output
> as well as the input. Also to send us the actual code fragments
> that are causing the damage.

Yes after i received the reply i realized that i was not very clear.
i have a text file of inventors which should have one inventor on  
each line in alphabetical order but of course the lines
do not break at the end of 'p. xxx.'  (where p.xxx is the relevant  
page number)

I read the data in as a string figuring that I could then replace p.  
xxx with a carriage return, somehow write the data out to a text file
and the problem would be solved. Not quite so simple given my limited  
skill set.
The following is what I put in (interactively) and what I got out.

 >>> ss = open('inp.txt')
 >>> s = ss.read()
 >>> s.replace('p. ','\n')
'Barnett, John B., assignor of one-half to R. N. Tutt, Kansas City,  
Mo. Automatic display-sign.\xc2\xa0 \xc2\xa0 No. 1,330 411-Apr. 13 ;  
v. 273 ;\xc2\xa0\n\n193. Barnett,\xc2\xa0 John\xc2\xa0 II..\xc2\xa0  
Tettenhall,\xc2\xa0 England. \xc2\xa0 \xc2\xa0 Seat\xc2\xa0 of 
\nmotorcars.\xc2\xa0 \xc2\xa0 No. 1.353,708; Sept. 21 ; v. 278;  
\n487. Barnett,\xc2\xa0\nOtto R.\xc2\xa0 \xc2\xa0 (See Scott, John  
M., assignor.)'
 >>>

I though about treating it as a list of lines, stripping carriage  
returns on the basis of some criteria but i have never gotten rstrip  
to work



>
>> i tried string.replace(s,'p.','\n') in an attempt to put a CR in but
>> it just put the characters\n in the string.
>
> Dont use the string module functions. Use the string methods,
> so it becomes:
>
> s.replace('p.', '\n')
>
> However that doesn't explain why you are getting the literal
> characters! Can you send us the actual code you are using?
> And the output showing the error?
>
> HTH,
>
> Alan G.
>
>
>
>
> ------------------------------
>
> Message: 3
> Date: Sat, 24 Mar 2007 19:32:36 -0500
> From: "Cecilia Alm" <ebbaalm at uiuc.edu>
> Subject: [Tutor] No need to seed random?
> To: tutor at python.org
> Message-ID:
> 	<7a4620dc0703241732m355f23d3m26cfcbe2a06dfad5 at mail.gmail.com>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> After importing random, there is no need to call random.seed() in a
> python program, is there? (unless one wishes to specifically control
> what the seed is.)
>
> Thanks!
>
>
> ------------------------------
>
> Message: 4
> Date: Fri, 23 Mar 2007 22:54:56 -0600
> From: Lurch <erik.kloeppel at forethought.net>
> Subject: [Tutor] nube question...
> To: tutor at python.org
> Message-ID: <4604AF20.10709 at forethought.net>
> Content-Type: text/plain; charset="iso-8859-1"
>
> Gentlemen,
>
> I don't consider myself a rank beginner (I did take a shower this  
> week),
> but I've been beating my brains out over this for the past week.
>
> I'm writing this GUI app (Tkinter).  Just about everything is done  
> (all
> the nuts,bolts, and the occasional nail.  Now Im trying to code a  
> simple
> status window to let the dumb user (me) know what's going on behind  
> the
> curtain.  The idea is, the application calls the subroutine, the
> subroutine pops open a frame and print some text in it, then returns
> control back to the  main app - leaving the frame/text visible.  Then,
> the app calls the subroutine and *updates* the frame, so you end up  
> with
> a growing list of notices...(basically, I'm working with a variable  
> set
> of files, and it's convenient to know which is being operated on)
>
> I've ripped code from Mark Lutz's book (3rd edition), from Welch's  
> book
> (also 3rd edition), and from around the net. It seems to me that I'm
> missing something obvious when it comes to self.update.  The  
> subroutine
> works as advertised the first time through, but one the window is open
> and the text displays, the app stops - unless I close the window.  Of
> course, then everything works until the NEXT message is displayed.
>
> Sure, I could add a button and work from that, but I don't *WANT* to -
> the idea is a (more or less) continuous flow of information that I can
> scroll through if need be.
>
> As you can see from the sample code below, I'm using the Text widget
> instead of Message.  I had a reason for that when I started this
> nightmare, but I'm not wedded to it.
>
> Any assistance from anybody would be much appreciated.
>
> Thanks in advance!
>
> Erik
>
> -----------------------
>
> from Tkinter import *
>
> data={}
> data[4,9]="you are here"
> data[4,10]=0
> msg=1
> x={}
>
> def loop():
>     global x
>     x[1]=0
>     while x[1]<10:
>         messages(x)
>         print "howdee"
>         x[1]=x[1]+1
>
> def messages(msg):
>     class MESSAGES(Frame):
>         def __init__(self,parent=None,text='', file=None):
>             Frame.__init__(self,parent)
>             self.pack(expand=YES,fill=BOTH)
>             self.makewidgets()
>             self.message(text)
>             self.update()
>
>         def makewidgets(self):
>             sbar=Scrollbar(self)
>             a=Text(self,relief=RAISED)
>             sbar.config(command=a.yview,bg='#fcfcfc')
>             a.config(yscrollcommand=sbar.set,bg='#f0f0f0')
>             sbar.pack(side=RIGHT,fill=Y)
>             a.pack(side=LEFT,expand=YES,fill=BOTH)
>             self.a=a
>             data[4,10]=1
>
>         def message(self,text):
>             self.a.insert(END,text)
>             self.a.focus()
>             return self.a.get('35.2',END+'-1c')
>
>     if __name__=='__main__':
>         root=Tk()
>         MESSAGES(text=data[4,9]).mainloop()
>         return
>
> loop()
>
> I've tried this small mod, just now, but it *really* breaks things the
> second time through:
>
>
>     class MESSAGES(Frame):
>         def __init__(self,parent=None,text='', file=None):
>             Frame.__init__(self,parent)
>             self.pack(expand=YES,fill=BOTH)
>             if data[4,10]==0:
>                 self.makewidgets()
>             self.message(text)
>             self.update
>
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: http://mail.python.org/pipermail/tutor/attachments/ 
> 20070323/0c786de6/attachment.html
>
> ------------------------------
>
> Message: 5
> Date: Sat, 24 Mar 2007 22:26:33 -0400
> From: Kent Johnson <kent37 at tds.net>
> Subject: Re: [Tutor] parsing text
> Cc: tutor at python.org
> Message-ID: <4605DDD9.8010808 at tds.net>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> Alan Gauld wrote:
>
>>> 1.) when i do readlines and create a list and then print the list it
>>> adds a blank line between every line of text
>>
>> I suspect that's because you are reading a newline character
>> from the file and print adds a newline of its own. You need to
>> use rstrip() to take out the newline from the file.
>
> or use sys.stdout.write() instead of print, it doesn't add a newline.
>
> Kent
>
>
> ------------------------------
>
> Message: 6
> Date: Sat, 24 Mar 2007 22:32:32 -0400
> From: Kent Johnson <kent37 at tds.net>
> Subject: Re: [Tutor] No need to seed random?
> To: Cecilia Alm <ebbaalm at uiuc.edu>
> Cc: tutor at python.org
> Message-ID: <4605DF40.3090007 at tds.net>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> Cecilia Alm wrote:
>> After importing random, there is no need to call random.seed() in a
>> python program, is there? (unless one wishes to specifically control
>> what the seed is.)
>
> Right.
>
> Kent
>
>
> ------------------------------
>
> Message: 7
> Date: Sat, 24 Mar 2007 22:23:06 -0500
> From: Chris Smith <mc_anjo at tamu.edu>
> Subject: [Tutor] Pmw BLT help
> To: tutor at python.org
> Message-ID: <eu4puq$ubm$1 at sea.gmane.org>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> I'm trying to learn how to use Pmw.BLT to have plotting in some GUIs I
> would like to create. I have a book with some examples but when I run
> the code I get this error message which seems to be related to the
> packages I installed and not my code:
>
> Traceback (most recent call last):
>    File "C:\Documents and Settings\Chris & Esther\My
> Documents\Python\Books&relatedcode\Python4ComputationalScience\src 
> \py\gui\plotdemo_blt.py",
> line 105, in ?
>      blt = BltDemo1(root)
>    File "C:\Documents and Settings\Chris & Esther\My
> Documents\Python\Books&relatedcode\Python4ComputationalScience\src 
> \py\gui\plotdemo_blt.py",
> line 16, in __init__
>      self.vector_x = Pmw.Blt.Vector()
>    File "C:\Python24\lib\site-packages\Pmw\Pmw_1_2\lib\PmwBlt.py",  
> line
> 99, in __init__
>      self.tk.call(_vectorCommand, 'create', self._name)
> TclError: invalid command name "::blt::vector"
>
> I've read over the installation instructions and even reinstalled the
> software and that didn't help any. Could anyone help me out?
>
> C. Smith
>
>
>
> ------------------------------
>
> Message: 8
> Date: Sun, 25 Mar 2007 09:50:47 +0100
> From: "Alan Gauld" <alan.gauld at btinternet.com>
> Subject: Re: [Tutor] nube question...
> To: tutor at python.org
> Message-ID: <eu5d59$fvt$1 at sea.gmane.org>
> Content-Type: text/plain; format=flowed; charset="iso-8859-1";
> 	reply-type=original
>
> "Lurch" <erik.kloeppel at forethought.net> wrote
>
>> the nuts,bolts, and the occasional nail.  Now Im trying to code a
>> simple
>> status window to let the dumb user (me) know what's going on behind
>> the
>> curtain.  The idea is, the application calls the subroutine, the
>> subroutine pops open a frame and print some text in it, then returns
>> control back to the  main app - leaving the frame/text visible.
>> Then,
>> the app calls the subroutine and *updates* the frame, so you end up
>> with
>> a growing list of notices...(basically, I'm working with a variable
>> set
>> of files, and it's convenient to know which is being operated on)
>
>> I've ripped code from Mark Lutz's book (3rd edition), from Welch's
>> book
>> (also 3rd edition), and from around the net.
>
> One of the problems with ripping code from books is that you
> often wind up with a strange mix of styles without really
> understanding any of it. And I think that's happening here.
>
> Do you actually understand how this program is supposed to work?
> Can you explain why the MESSAGES class definition is inside
> the messages() function?
> Do you know what sequence of events leads to your window
> being created and where the reference to the window is held?
>
> It would be better to structure your Tkinter app in a more
> conventional manner - have you read the online Tkinter
> tutorial/reference pages?
>
> The reason I ask is that your code calls messages() every time
> is goes round the loopp, but messages() only defines the
> MESSAGES class, it doesn't do anything with it. Why are
> you doing that?
>
> I also don't understand why you are using a dictionary
> as a counter? Why not just use a simple variable to hold
> the number and avoid all the x[1] stuff?
>
> If you trace what happens when you run this program:
> It creates a MESSAGES instance and then calls mainloop.
> But you don't hold a refernce to the object nor do you have
> any useful event handlers defined in the GUI.
> It then processes (standard) events. When the mainloop exits
> it then goes on to call your loop() function which creates the
> class definition and prints 'howdee' multiple times. It doesn't
> make sense! (to me!)
>
> I suggest you sit down, work out what you actually want
> to happen and design the straucture of your code to do that.
> That means thinking in terms of events and messages.
> You see to be trying to mix a non event-driven model
> (your loop() function) and an event driven model - your GUI.
> That's not a good idea. You need to rethink what you are doing
> so that you crewate the GUI then launch your processing
> from a GUI event. You can then update the GUI from your
> code. If your processing takes a long time as implied in the
> message you will either want to put your code in a thread
> (see an earlier thread last week) or break it into chunks
> controlled by the GUI - maybe via timers.
>
>> missing something obvious when it comes to self.update.  The
>> subroutine
>> works as advertised the first time through, but one the window is
>> open
>> and the text displays, the app stops - unless I close the window.
>> Of
>> course, then everything works until the NEXT message is displayed.
>
> I don't understand this either. What NEXT message?
> There is no reference to NEXT in your code? Also your code
> prints to the console but also displays a GUI window, which
> window are you referring to? The GUI or the console?
>
> As to self.update - you only call that once when you create
> the instance. Do you understand OOP and how objects are
> created? The difference between classes and instances?
>
>> Sure, I could add a button and work from that, but I don't *WANT*
>> to -
>> the idea is a (more or less) continuous flow of information that I
>> can
>> scroll through if need be.
>
> Thats not a problem, you don;t need a button but you do need
> to reestructure your code so that your function runs (probably
> as a thread) within the GUI mainloop().
>
>> As you can see from the sample code below, I'm using the Text widget
>> instead of Message.  I had a reason for that when I started this
>> nightmare, but I'm not wedded to it.
>
> Text is probably the right thing to use for what you want.
>
>> Any assistance from anybody would be much appreciated.
>
> I'd start again.
> First write a GUI class that just contains the Text widget - forget
> about scroll bars for now. Then write a method within that class
> that does the looping - your loop() function effectively, and call
> that at the end of the init(). Instead of printing make the loop
> insert text into the TEXT widget.
>
> That should result in a program that does what you want but
> doesn't have scroll bars and is totally unresponsive until the
> loop finishes. The next tricxk is to make your methoid run in
> a thread, but get the basics working first. It should be a really
> short program, maybe 10-15 lines.
>
> HTH,
>
>
> -- 
> Alan Gauld
> Author of the Learn to Program web site
> http://www.freenetpages.co.uk/hp/alan.gauld
>
>> -----------------------
>> from Tkinter import *
>>
>> data={}
>> data[4,9]="you are here"
>> data[4,10]=0
>> msg=1
>> x={}
>>
>> def loop():
>>    global x
>>    x[1]=0
>>    while x[1]<10:
>>        messages(x)
>>        print "howdee"
>>        x[1]=x[1]+1
>>
>> def messages(msg):
>>    class MESSAGES(Frame):
>>        def __init__(self,parent=None,text='', file=None):
>>            Frame.__init__(self,parent)
>>            self.pack(expand=YES,fill=BOTH)
>>            self.makewidgets()
>>            self.message(text)
>>            self.update()
>>
>>        def makewidgets(self):
>>            sbar=Scrollbar(self)
>>            a=Text(self,relief=RAISED)
>>            sbar.config(command=a.yview,bg='#fcfcfc')
>>            a.config(yscrollcommand=sbar.set,bg='#f0f0f0')
>>            sbar.pack(side=RIGHT,fill=Y)
>>            a.pack(side=LEFT,expand=YES,fill=BOTH)
>>            self.a=a
>>            data[4,10]=1
>>
>>        def message(self,text):
>>            self.a.insert(END,text)
>>            self.a.focus()
>>            return self.a.get('35.2',END+'-1c')
>>
>>    if __name__=='__main__':
>>        root=Tk()
>>        MESSAGES(text=data[4,9]).mainloop()
>>        return
>>
>> loop()
>>
>
>
>
>
> ------------------------------
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
> End of Tutor Digest, Vol 37, Issue 63
> *************************************



More information about the Tutor mailing list