Re: [Tutor] Should I use python for parsing text

2007-03-20 Thread János Juhász
Hy Jay,

I just allways wonder how fine this book about text processing with 
python.

Text Processing in Python at http://gnosis.cx/TPiP/

It shows that Python can be as effective as Perl. The question is the how.
Take a look on it.


Yours sincerely,
__
János Juhász

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Should I use python for parsing text

2007-03-20 Thread Luke Paireepinart

> # The next 5 lines are so I have an idea of how many lines i started 
> with in the file.
>
> in_filename = raw_input('What is the COMPLETE name of the file you 
> want to open:')
> in_file = open(in_filename, 'r')
> text = in_file.read()
read() returns a one-dimensional list with all the data, not a 
2-dimensional one with each element a line.
Use readlines() for this functionality.
(Eg. A file with contents 'hello\nhoware\nyou?' would have this string 
returned by read(), but
readlines() would return ['hello\n','howare\n','you?'].)
> num_lines = text.count('\n')
or just len(text) if you're using readlines()
> print 'There are', num_lines, 'lines in the file', in_filename
>
> output = open("cleandata.txt","a")# file for writing data to after 
> stripping newline character
You might want to open this file in 'write' mode while you're testing, 
so previous test results don't confuse you.
>
> # read file, copying each line to new file
> for line in text:
since read() returns a 1-dimensional list, you're looping over every 
character in the file, not every line.
> if line[:-1] in '-':
In this case this is the same as "if line == '-':" because your 'line' 
variable only contains characters.
> line = line.rstrip()
> output.write(line)
> else: output.write(line)
>
> print "Data written to cleandata.txt."
>
> # close the files
> in_file.close()
> output.close()
>
> The above ran with no erros, gave me the number of lines in my orginal 
> file but then when i opened the cleandata.txt file
> I got:
>
> A.-C.䴀愀渀甀昀愀挀琀甀爀椀渀最 �Company.⠀匀攀攀�Sebastian,䄀⸀�A.,�and 
> 䌀愀瀀攀猀Ⰰ�assignors.)�A.䜀⸀�A.刀愀椀氀眀愀礀 �Light☀�Signal䌀漀⸀� 
> (See䴀攀搀攀渀Ⰰ�Elof�Hassignor.)�A-N䌀漀洀瀀愀渀礀Ⰰ�The.⠀匀攀攀 
> �Alexander愀渀搀�Nasb,愀猀ⴀ�猀椀最渀漀爀猀⸀㬀�䄀一�Company,吀栀攀⸀� 
> (See一愀猀栀Ⰰ�It.䨀⸀Ⰰ�and䄀氀攀砀愀渀搀攀爀Ⰰ�as-�
Not sure what caused all of those characters.
HTH,
-Luke
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to set value back to .conf file

2007-03-20 Thread John Fouhy
On 21/03/07, hok kakada <[EMAIL PROTECTED]> wrote:
> I just start to use ConfigParser to store the configuration for my
> application.
[...]
> Or i need to write it to the file using conf.write(fp)?
> If so, how can I get this file pointer?

Yes.  You need to create a file-like object open for writing.

To continue your example, you can do this:

f = open('test.conf', 'w')
conf.write(f)
f.close()

-- 
John.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] How to set value back to .conf file

2007-03-20 Thread hok kakada
Hi all,

I just start to use ConfigParser to store the configuration for my 
application.
I surfed the mails related to ConfigParser, however I couldn't found the way 
of how to set value back to the .conf file.

Let say, I have a test.conf file with:
[General]
userName="da"

later on, I wanna add another option:
email="[EMAIL PROTECTED]"

I did the following:
import ConfigParser
conf = ConfigParser.ConfigParser()
conf.read('test.conf')

conf.set('General', 'email', '[EMAIL PROTECTED]'

print conf.items('General')
[('username','da'),('email','[EMAIL PROTECTED]')]

But when I look into the file test.conf directly, I didn't see the option 
'email' . I don't know what I am missing here?

Or i need to write it to the file using conf.write(fp)?
If so, how can I get this file pointer?

Thanks very much for your input.

da
-- 
Ms.Kakada Hok
Open Source Engineer
KhmerOS project
Open Institute Organization
H/P:(+855-12) 653 155

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Should I use python for parsing text

2007-03-20 Thread Jay Mutter III

"Jay Mutter III"  wrote

> See example  next:
> A.-C. Manufacturing Company. (See Sebastian, A. A.,
> and Capes, assignors.)
>...
>Aaron, Solomon E., Boston, Mass. Pliers. No. 1,329,155 ;
>Jan. 27 ; v. 270 ; p. 554.
>
> For instance, I would like to go to end of line and if last
> character  is a comma or semicolon or hyphen then
> remove the CR.

It would look something like:

output = open('example.fixed','w')
for line in file('example.txt'):
if line[-1] in ',;-':# check last character
  line = line.strip() # lose the C/R
  output.write(line)# write to output
else: output.write(line)  # append the next line complete with C/R
output.close()




Working from the above suggestion ( and thank you very much - i did  
enjoy your online tutorial)

I came up with the following:

import os
import sys
import re
import string

# The next 5 lines are so I have an idea of how many lines i started  
with in the file.


in_filename = raw_input('What is the COMPLETE name of the file you  
want to open:')

in_file = open(in_filename, 'r')
text = in_file.read()
num_lines = text.count('\n')
print 'There are', num_lines, 'lines in the file', in_filename

output = open("cleandata.txt","a")# file for writing data to  
after stripping newline character


# read file, copying each line to new file
for line in text:
if line[:-1] in '-':
line = line.rstrip()
output.write(line)
else: output.write(line)

print "Data written to cleandata.txt."

# close the files
in_file.close()
output.close()

The above ran with no erros, gave me the number of lines in my  
orginal file but then when i opened the cleandata.txt file

I got:

A.-C.䴀愀渀甀昀愀挀琀甀爀椀渀最Company.⠀匀攀攀 
Sebastian,䄀⸀A.,and䌀愀瀀攀猀Ⰰassignors.)A.䜀⸀A.刀 
愀椀氀眀愀礀Light☀Signal䌀漀⸀(See䴀攀搀攀渀 
ⰀElofHassignor.)A-N䌀漀洀瀀愀渀礀ⰀThe.⠀匀攀攀 
Alexander愀渀搀Nasb,愀猀ⴀ猀椀最渀漀爀猀⸀㬀䄀一 
Company,吀栀攀⸀(See一愀猀栀ⰀIt.䨀⸀Ⰰand䄀氀攀砀 
愀渀搀攀爀Ⰰas-


So what did I do to cause all of the strange characters
Plus since this goes on it is as if it removed all \n and not just  
the ones after a hyphen which I was using as my test case.


Thanks again.

Jay



> Then move line by line through the file and delete everything
> after a  numerical sequence

Slightly more tricky because you need to use a regular expression.
But if you know regex then only slightly.

>  I am wondering if Python would be a good tool

Absolutely, its one of the areas where Python excels.

> find information on how to accomplish this

You could check  my tutorial on the three topics:

Handling text
Handling files
Regular Expressions.

Also the standard python documentation for the general tutorial
(assuming you've done basic programming in some other language
before) plus the re module

> using something like the unix tool awk or something else??

awk or sed could both be used, but Python is more generally
useful so unless you already know awk I'd take the time to
learn the basics of Python (a few hours maybe) and use that.

--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] The '45' bug in round()

2007-03-20 Thread Dick Moores
At 02:43 PM 3/20/2007, Terry Carroll wrote:
>On Tue, 20 Mar 2007, Dick Moores wrote:
>
> > So you're claiming there's a bug in round()?
>
>No.  I'm very reluctant to slap the "bug" label on behavior that strikes
>me as anomalous.  I've found a bug or two, but in most cases, it's far
>more likely to be working as designed, and the bug is in my perception!

Yeah, you're probably right. See this reply to me from the Tutor I 
respect the most, even if it might not seem so to him sometimes:


Dick


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Making table

2007-03-20 Thread János Juhász
Dear Barry,

>>Using a formatting string of "%10.4f", these would be rendered as:
>>
>>   '  253.'
>>   '   77.6000
>>   '9.0300'
>>   '0.0210'
>>
>>This formatting gives the impression that all values but the last are
>>more precise than they truly are.  A scientist or statistician would
>>prefer to see something like this:
>>
>>   '254.'
>>   ' 77.6   '
>>   '  9.03  '
>>   '  0.0210'
>>
>>Does numpy or some other math package have that capability?

You can use advanced regexp, called lookaround, lookbehind for this 
purpose.

###

import re

l = (253., 77.6, 9.03, .0210, 1000, 100.1230)
ending_zero = re.compile('0(?=0*$)') # zero followed with only zeros

for f in l:
print re.sub(ending_zero, ' ', ('%10.4f' % f))

###




Yours sincerely,
__
János Juhász
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] The '45' bug in round()

2007-03-20 Thread Terry Carroll
On Tue, 20 Mar 2007, Dick Moores wrote:

> So you're claiming there's a bug in round()? 

No.  I'm very reluctant to slap the "bug" label on behavior that strikes 
me as anomalous.  I've found a bug or two, but in most cases, it's far 
more likely to be working as designed, and the bug is in my perception!


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] The '45' bug in round()

2007-03-20 Thread Dick Moores
At 02:24 PM 3/20/2007, Terry Carroll wrote:
>On Tue, 20 Mar 2007, Dick Moores wrote:
>
> >  >>> print round(0.19965, 4)
> > 0.1997
> >
> > (which rounds up to an odd number, 7)
>
>Now that's weird.  It should (I say) round to .1996; not because 6 is even
>and 7 is not[1], but because 0.19965 is actually closer to 0.1996 than to
>0.1997:
>
> >>> 0.19965
>0.19964
>
>
>[1] The Python rounding rule is to round "away from zero," i.e., to round
>up for positive numbers and down (where "down" means "more negative") for
>negative numbers.

So you're claiming there's a bug in round()? So was I, the OP, in 
that original post, but a bug of a different sort. ;)

Dick Moores

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] The '45' bug in round()

2007-03-20 Thread Terry Carroll
On Tue, 20 Mar 2007, Dick Moores wrote:

>  >>> print round(0.19965, 4)
> 0.1997
> 
> (which rounds up to an odd number, 7)

Now that's weird.  It should (I say) round to .1996; not because 6 is even
and 7 is not[1], but because 0.19965 is actually closer to 0.1996 than to
0.1997:

>>> 0.19965
0.19964


[1] The Python rounding rule is to round "away from zero," i.e., to round 
up for positive numbers and down (where "down" means "more negative") for 
negative numbers.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] threading and gui programming

2007-03-20 Thread Ben

That makes sense. I am a newbie to python programming. What really confuses
me is that there are so many gui programming options for python (i.e.
pywin32, wxPython, etc). Is this means that all of these available gui
options be able to integrate with threading? Thanks.

On 3/20/07, Kent Johnson <[EMAIL PROTECTED]> wrote:


Ben wrote:
> Hi all,
>
> I am curious about one thing. I have been doing research on gui
> programming. One thing that I don't understand is why there are some
> examples uses threading in the gui examples. Is there any benefits or
> advantages for using the threading in the gui programming? Thanks.

This is commonly done when a user action starts some kind of processing
that will take some time. In order for the GUI to remain responsive, the
long process must be in a different thread. In general, the GUI will not
respond to new input until the event handler for the user action returns.

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] threading and gui programming

2007-03-20 Thread Kent Johnson
Ben wrote:
> Hi all,
> 
> I am curious about one thing. I have been doing research on gui 
> programming. One thing that I don't understand is why there are some 
> examples uses threading in the gui examples. Is there any benefits or 
> advantages for using the threading in the gui programming? Thanks.

This is commonly done when a user action starts some kind of processing 
that will take some time. In order for the GUI to remain responsive, the 
long process must be in a different thread. In general, the GUI will not 
respond to new input until the event handler for the user action returns.

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] threading and gui programming

2007-03-20 Thread Ben

Hi all,

I am curious about one thing. I have been doing research on gui programming.
One thing that I don't understand is why there are some examples uses
threading in the gui examples. Is there any benefits or advantages for using
the threading in the gui programming? Thanks.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] The '45' bug in round()

2007-03-20 Thread Michael Hannon
On Tue, Mar 20, 2007 at 09:03:43AM -0700, Dick Moores wrote:
.
.
.
> >Well, perhaps this is something for me to think about, but if you had asked
> >me to round 0.19945 to four decimal places, I would have told you the 
> >answer
> >is 0.1994, i.e., the same answer that Python gives.
> 
> Is this because that rounding conforms to "unbiased rounding"? If so, 
> then you won't like
> 
> >>> print round(0.19965, 4)
> 0.1997
> 
> (which rounds up to an odd number, 7)

Heh.  You're right.  I don't.  If I get some time, I'll try to dig
through the Python source code to see what they're doing.  Thanks.

- Mike
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] The '45' bug in round()

2007-03-20 Thread Dick Moores
At 08:31 AM 3/20/2007, you wrote:
>On Tue, Mar 20, 2007 at 04:09:49AM -0700, Dick Moores wrote:
> > At 11:00 AM 3/19/2007, Michael Hannon wrote:
> > >On Mon, Mar 19, 2007 at 03:04:03AM -0700, Dick Moores wrote:
> > >> Yesterday I was shocked, SHOCKED, to discover that round() is
> > >> occasionally rounding incorrectly. For example,
> > >>
> > >>  >>> print round(0.19945,4)
> > >> 0.1994
> > >.
> > >.
> > >.
> > >> Comments, Tutors? Am I way out in left field with this?
> > >
> > >
> > >I suggest you might want to look at the discussion of unbiased 
> rounding at:
> > >
> > >http://en.wikipedia.org/wiki/Rounding
> >
> > Thanks. I'm familiar with "unbiased rounding". I'm glad to know about
> > that Wikipedia article. Could you tell me what, in particular, you
> > wanted me to think about?
>
>Well, perhaps this is something for me to think about, but if you had asked
>me to round 0.19945 to four decimal places, I would have told you the answer
>is 0.1994, i.e., the same answer that Python gives.

Is this because that rounding conforms to "unbiased rounding"? If so, 
then you won't like

 >>> print round(0.19965, 4)
0.1997

(which rounds up to an odd number, 7)

But after thinking about it, I may revise my round2() function to do 
"unbiased rounding". It appeals to me, even though most of my life 
I've used what the Wikipedia article terms "the common method":

=
This method is commonly used, for example in accounting. It is the 
one generally taught in basic mathematics classes. This method is 
also known as Symmetric Arithmetic Rounding or Round-Half-Up 
(Symmetric Implementation):

Decide which is the last digit to keep.
Increase it by 1 if the next digit is 5 or more (this is called rounding up)
Leave it the same if the next digit is 4 or less (this is called 
rounding down)
===

Dick Moores




___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] breaking the 'while' loop

2007-03-20 Thread Luke Paireepinart
>
>
> I want to limit the number of tries to 5. To do that, I have tried the 
> /_if structure_/ along with the /_break statement_/ immediately below the
>
> ‘tries += 1’ line:
>
Or you could just add this condition to the while loop.
while (guess != the_number and tries < 6):
>
> if tries > 5:
>
> break
>
> print “Wrong guess!”
>
> Which still did not result in the /_while loop_/ breaking after 
> attempt No 5.
>
> I would appreciate being explained what I am doing wrong.
>
You should have included the source that didn't work. It does us no good 
to see working code that doesn't do what you want.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] The '45' bug in round()

2007-03-20 Thread Michael Hannon
On Tue, Mar 20, 2007 at 04:09:49AM -0700, Dick Moores wrote:
> At 11:00 AM 3/19/2007, Michael Hannon wrote:
> >On Mon, Mar 19, 2007 at 03:04:03AM -0700, Dick Moores wrote:
> >> Yesterday I was shocked, SHOCKED, to discover that round() is
> >> occasionally rounding incorrectly. For example,
> >>
> >>  >>> print round(0.19945,4)
> >> 0.1994
> >.
> >.
> >.
> >> Comments, Tutors? Am I way out in left field with this?
> >
> >
> >I suggest you might want to look at the discussion of unbiased rounding at:
> >
> >http://en.wikipedia.org/wiki/Rounding
> 
> Thanks. I'm familiar with "unbiased rounding". I'm glad to know about 
> that Wikipedia article. Could you tell me what, in particular, you 
> wanted me to think about?

Well, perhaps this is something for me to think about, but if you had asked
me to round 0.19945 to four decimal places, I would have told you the answer
is 0.1994, i.e., the same answer that Python gives.

- Mike
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] breaking the 'while' loop

2007-03-20 Thread Andre Engels

2007/3/20, Alexander Kapshuk <[EMAIL PROTECTED]>:


 Dear All,



I have been learning computer programming with Python only for a short
while. I have a question to do with breaking the *while loop*.



I have attached the source code of a program called 'Guess my number' with
the *while loop* running until the right number is guessed.



I want to limit the number of tries to 5. To do that, I have tried the *if
structure* along with the *break statement* immediately below the

'tries += 1' line:



if tries > 5:

  break

 print "Wrong guess!"



Which still did not result in the *while loop* breaking after attempt No
5.



I would appreciate being explained what I am doing wrong.


It would have helped if you had given the code *with* the extra lines in it.
My first guess is that you did a wrong indentation, but without the actual
code I cannot be sure.

--
Andre Engels, [EMAIL PROTECTED]
ICQ: 6260644  --  Skype: a_engels
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] breaking the 'while' loop

2007-03-20 Thread Alexander Kapshuk
Dear All,

 

I have been learning computer programming with Python only for a short
while. I have a question to do with breaking the while loop.

 

I have attached the source code of a program called 'Guess my number'
with the while loop running until the right number is guessed.

 

I want to limit the number of tries to 5. To do that, I have tried the
if structure along with the break statement immediately below the 

'tries += 1' line:

 

if tries > 5:

  break

 print "Wrong guess!"

 

Which still did not result in the while loop breaking after attempt No
5.

 

I would appreciate being explained what I am doing wrong.

 

Regards,

 

Alexander Kapshuk

 



guess_my_number.py
Description: guess_my_number.py
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] The '45' bug in round()

2007-03-20 Thread Dick Moores
At 11:00 AM 3/19/2007, Michael Hannon wrote:
>On Mon, Mar 19, 2007 at 03:04:03AM -0700, Dick Moores wrote:
> > Yesterday I was shocked, SHOCKED, to discover that round() is
> > occasionally rounding incorrectly. For example,
> >
> >  >>> print round(0.19945,4)
> > 0.1994
>.
>.
>.
> > Comments, Tutors? Am I way out in left field with this?
>
>
>I suggest you might want to look at the discussion of unbiased rounding at:
>
> http://en.wikipedia.org/wiki/Rounding

Thanks. I'm familiar with "unbiased rounding". I'm glad to know about 
that Wikipedia article. Could you tell me what, in particular, you 
wanted me to think about?

Dick


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Making table

2007-03-20 Thread Kent Johnson
Carroll, Barry wrote:
> This formatting gives the impression that all values but the last are
> more precise than they truly are.  A scientist or statistician would
> prefer to see something like this:
> 
>   '254.'
>   ' 77.6   '
>   '  9.03  '
>   '  0.0210'
> 
> Does numpy or some other math package have that capability?

I don't know, I don't use numpy.

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor