Re: [Tutor] Compiling Python 2.6.1 on Leopard

2008-12-21 Thread Kent Johnson
On Sat, Dec 20, 2008 at 3:40 PM, R. Ellsworth Pollard
rellsworthpoll...@gmail.com wrote:
 Where might I find instructions for compiling Python on Leopard?

If your goal is just to install Python, there is now a Mac installer available.
http://www.python.org/download/releases/2.6.1/

If you really want to build, download the source and see the
Mac/README file. A basic framework build is
./configure --enable-framework
make
sudo make install

If you want it to work with Tkinter, there is a problem in the
released setup.py, see this bug for a fix:
http://bugs.python.org/issue4017

in particular
http://bugs.python.org/msg74544

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


[Tutor] Equivalent of grep in python

2008-12-21 Thread ppaarrkk

The following works :

file1 = open (file0, r)

re.findall ( 'some_text', file1.readline() )


But this doesn't :

re.findall ( 'some_text', file1.readlines() )



How do I use grep for a whole text file, not just a single string ?
-- 
View this message in context: 
http://www.nabble.com/Equivalent-of-grep-in-python-tp2356p2356.html
Sent from the Python - tutor mailing list archive at Nabble.com.

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


Re: [Tutor] Equivalent of grep in python

2008-12-21 Thread Tiago Katcipis
i believe that the following should work

file1 = open(fileO, 'r')
re.findall ('some_text', file1.read())

readlines returns a list with lists inside, where every list is a line of
the text. The read function returns the entire file as one string, so it
should work to what you are wanting to do.

best regards

Katcipis

On Sun, Dec 21, 2008 at 12:02 AM, ppaarrkk simon_...@yahoo.co.uk wrote:


 The following works :

 file1 = open (file0, r)

 re.findall ( 'some_text', file1.readline() )


 But this doesn't :

 re.findall ( 'some_text', file1.readlines() )



 How do I use grep for a whole text file, not just a single string ?
 --
 View this message in context:
 http://www.nabble.com/Equivalent-of-grep-in-python-tp2356p2356.html
 Sent from the Python - tutor mailing list archive at Nabble.com.

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




-- 
it might be a profitable thing to learn Java, but it has no intellectual
value whatsoever Alexander Stepanov
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Equivalent of grep in python

2008-12-21 Thread Tiago Katcipis
i forgot, this might help you

http://docs.python.org/tutorial/inputoutput.html#reading-and-writing-files

On Sun, Dec 21, 2008 at 11:57 AM, Tiago Katcipis katci...@inf.ufsc.brwrote:

 i believe that the following should work

 file1 = open(fileO, 'r')
 re.findall ('some_text', file1.read())

 readlines returns a list with lists inside, where every list is a line of
 the text. The read function returns the entire file as one string, so it
 should work to what you are wanting to do.

 best regards

 Katcipis


 On Sun, Dec 21, 2008 at 12:02 AM, ppaarrkk simon_...@yahoo.co.uk wrote:


 The following works :

 file1 = open (file0, r)

 re.findall ( 'some_text', file1.readline() )


 But this doesn't :

 re.findall ( 'some_text', file1.readlines() )



 How do I use grep for a whole text file, not just a single string ?
 --
 View this message in context:
 http://www.nabble.com/Equivalent-of-grep-in-python-tp2356p2356.html
 Sent from the Python - tutor mailing list archive at Nabble.com.

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




 --
 it might be a profitable thing to learn Java, but it has no intellectual
 value whatsoever Alexander Stepanov




-- 
it might be a profitable thing to learn Java, but it has no intellectual
value whatsoever Alexander Stepanov
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Equivalent of grep in python

2008-12-21 Thread Tiago Katcipis
Sorry its true, i made a mistake. Readlines is a list with all the lines
inside. I never used readlines (i usually use read), i just read about it on
the tutorial long time ago.

 f.readlines()
['This is the first line of the file.\n', 'Second line of the file\n'


Thanks for the help.


On Sun, Dec 21, 2008 at 4:44 PM, Luke Paireepinart
rabidpoob...@gmail.comwrote:

 I believe readlines returns a list of strings, not a list of lists.
 You can iterate over the characters in a string if you want, though.

 On 12/21/08, Tiago Katcipis katci...@inf.ufsc.br wrote:
  i forgot, this might help you
 
 
 http://docs.python.org/tutorial/inputoutput.html#reading-and-writing-files
 
 
   On Sun, Dec 21, 2008 at 11:57 AM, Tiago Katcipis katci...@inf.ufsc.br
  wrote:
   i believe that the following should work
  
   file1 = open(fileO, 'r')
   re.findall ('some_text', file1.read())
  
   readlines returns a list with lists inside, where every list is a line
 of
  the text. The read function returns the entire file as one string, so it
  should work to what you are wanting to do.
  
   best regards
  
   Katcipis
  
  
  
  
  
   On Sun, Dec 21, 2008 at 12:02 AM, ppaarrkk simon_...@yahoo.co.uk
 wrote:
  
   
The following works :
   
file1 = open (file0, r)
   
re.findall ( 'some_text', file1.readline() )
   
   
But this doesn't :
   
re.findall ( 'some_text', file1.readlines() )
   
   
   
How do I use grep for a whole text file, not just a single string ?
--
View this message in context:
 
 http://www.nabble.com/Equivalent-of-grep-in-python-tp2356p2356.html
Sent from the Python - tutor mailing list archive at Nabble.com.
   
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor
   
  
  
  
   --
   it might be a profitable thing to learn Java, but it has no
 intellectual
  value whatsoever Alexander Stepanov
  
 
 
 
  --
  it might be a profitable thing to learn Java, but it has no intellectual
  value whatsoever Alexander Stepanov
 
  ___
   Tutor maillist  -  Tutor@python.org
   http://mail.python.org/mailman/listinfo/tutor
 
 




-- 
it might be a profitable thing to learn Java, but it has no intellectual
value whatsoever Alexander Stepanov
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Equivalent of grep in python

2008-12-21 Thread Matt Herzog
- Forwarded message from Tiago Katcipis katci...@inf.ufsc.br -

i forgot, this might help you

http://docs.python.org/tutorial/inputoutput.html#reading-and-writing-files


I can't help wondering how to do this in python:

perl -wnl -e '/string/ and print;' filename(s)

Not that I want to forget the precious few bits of Perl I do know, but I'd 
rather be totally reliant on python for such tasks.


On Sun, Dec 21, 2008 at 11:57 AM, Tiago Katcipis katci...@inf.ufsc.brwrote:

 i believe that the following should work

 file1 = open(fileO, 'r')
 re.findall ('some_text', file1.read())

 readlines returns a list with lists inside, where every list is a line of
 the text. The read function returns the entire file as one string, so it
 should work to what you are wanting to do.

 best regards

 Katcipis


 On Sun, Dec 21, 2008 at 12:02 AM, ppaarrkk simon_...@yahoo.co.uk wrote:


 The following works :

 file1 = open (file0, r)

 re.findall ( 'some_text', file1.readline() )


 But this doesn't :

 re.findall ( 'some_text', file1.readlines() )



 How do I use grep for a whole text file, not just a single string ?
 --
 View this message in context:
 http://www.nabble.com/Equivalent-of-grep-in-python-tp2356p2356.html
 Sent from the Python - tutor mailing list archive at Nabble.com.

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




 --
 it might be a profitable thing to learn Java, but it has no intellectual
 value whatsoever Alexander Stepanov




-- 
it might be a profitable thing to learn Java, but it has no intellectual
value whatsoever Alexander Stepanov

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


- End forwarded message -

-- 
I fear you speak upon the rack,
Where men enforced do speak anything.

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


Re: [Tutor] Equivalent of grep in python

2008-12-21 Thread ppaarrkk

file1.read() works.


What do I do if I want line numbers ?

I found this code :

http://snippets.dzone.com/posts/show/1638

src = open('2.htm').read()
pattern = 'P([^]+)SUP'  # or anything else
for m in re.finditer(pattern, src):
   start = m.start()
lineno = src.count('\n', 0, start) + 1
offset = start - src.rfind('\n', 0, start)
word = m.group(1)
print 2.htm(%s,%s): %s % (lineno, offset, word)




Is there not a simpler way than this ?




ppaarrkk wrote:
 
 The following works :
 
 file1 = open (file0, r)
 
 re.findall ( 'some_text', file1.readline() )
 
 
 But this doesn't :
 
 re.findall ( 'some_text', file1.readlines() )
 
 
 
 How do I use grep for a whole text file, not just a single string ?
 

-- 
View this message in context: 
http://www.nabble.com/Equivalent-of-grep-in-python-tp2356p21118578.html
Sent from the Python - tutor mailing list archive at Nabble.com.

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


[Tutor] subprocess adds %0A to end of string

2008-12-21 Thread David

Hi everyone.
Just learning :) I have one program to parse a podcast feed and put it 
into a file.


#!/usr/bin/python
Get feed date and link details
import feedparser
import sys

def getFeed():
url = raw_input(Please enter the feed: )
data = feedparser.parse(url)
for entry in data.entries:
sys.stdout = open(podcast.txt, a)
print '%s: %s' % (entry.updated, entry.link)
sys.stdout.close()
for entry in data.entries:
sys.stdout = open(podcast_links.txt, a)
print '%s' % (entry.link)
sys.stdout.close()
getFeed()

next to get the latest item;

#!/usr/bin/python
read the last entry and download podcast
import subprocess
fname = podcast.txt
f = open(fname, 'r')
print The Latest Podcast, f.readline()
print f.readline()
f.close()
lname = podcast_links.txt
L = open(lname, 'r')
print The Latest Link\n
download = L.readline()
print download

answer = raw_input(Do you want to download the podcast? )
if answer == y:
wget = wget
subprocess.call([wget, download])
else:
print oops

and here is the output;

david [06:37 PM] opteron ~ $ ./py_read_podcast.py
The Latest Podcast

Sat, 20 Dec 2008 01:52:00 GMT: http://linuxcrazy.com/podcasts/LC-44-arne.mp3

The Latest Link

http://linuxcrazy.com/podcasts/LC-44-arne.mp3

Do you want to download the podcast? y
--2008-12-21 18:38:46--  http://linuxcrazy.com/podcasts/LC-44-arne.mp3%0A
Resolving linuxcrazy.com... 74.220.207.171
Connecting to linuxcrazy.com|74.220.207.171|:80... connected.
HTTP request sent, awaiting response... 404 Not Found
2008-12-21 18:38:46 ERROR 404: Not Found.

See how it adds the %0A to the end ?

I am just trying to get it to work, I know it needs a lot of work, error 
checking etc. And I am sure there are better ways, I am new.


thanks
-david


--
Powered by Gentoo GNU/LINUX
http://www.linuxcrazy.com
pgp.mit.edu

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


Re: [Tutor] Equivalent of grep in python

2008-12-21 Thread Alan Gauld

Matt Herzog m...@blisses.org wrote


I can't help wondering how to do this in python:

perl -wnl -e '/string/ and print;' filename(s)


The first thing to say is that python is not Perl so there will 
be things Perl does more easily than Python and vice versa. 
(For example Pythons intersactive mode is miles better 
than perls read from stdin mode) Perl is better than python 
for quick n dirty one liners, no question. And for every Perl 
one liner there will be a relatively short script that can be 
written. But you will probably be as well using Perl for 
those - its not a bad thing to know and use more than 
one language, quite the opposite.


Not that I want to forget the precious few bits of Perl I do know, 



but I'd rather be totally reliant on python for such tasks.


Why? I'd hate to be totally reliant on any one language for anything
Whether it be Python, Lisp, C or assembler!

To answer your question I think the answer to your one liner 
would look something like:


import fileinput   # for iterating over several files

for line in fileinput.input(inplace=1):  # files = argv[1:]
   try: 
   line.replace('string', 'edit')

   print line
   except IOError:
   continue# ignore bad filenames

This also should help the OP answer his question about how to 
generate filenames, line numbers etc - use fileinput and the 
filename(), filelineno() etc functions...


Not as short as Perl but more maintainable.

If you really must do a one liner its probably possible although 
it will be longer in Python, but I'm not even going to attempt 
it - thats what awk/perl are there for!


HTH,

--
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] subprocess adds %0A to end of string

2008-12-21 Thread David

This seems to work;

download = L.readline()
print download
download = download[0:-1]

What is that last character that is added;
.mp3%0A


--
Powered by Gentoo GNU/LINUX
http://www.linuxcrazy.com
pgp.mit.edu

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


Re: [Tutor] subprocess adds %0A to end of string

2008-12-21 Thread Kent Johnson
On Sun, Dec 21, 2008 at 7:29 PM, David da...@abbottdavid.com wrote:
 This seems to work;

 download = L.readline()
 print download
 download = download[0:-1]

 What is that last character that is added;
 .mp3%0A

It is a newline character (line feed). readline() includes the line
endings in the returned lines. Try
download = L.readline().rstrip()

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


Re: [Tutor] subprocess adds %0A to end of string

2008-12-21 Thread Martin Walsh
Hi David,

David wrote:
 Hi everyone.
 Just learning :) I have one program to parse a podcast feed and put it
 into a file.

Welcome!

snip
 
 def getFeed():
 url = raw_input(Please enter the feed: )
 data = feedparser.parse(url)
 for entry in data.entries:
 sys.stdout = open(podcast.txt, a)

You should probably try to avoid reassigning sys.stdout. This is usually
a bad idea, and can cause odd behavior that is difficult to
troubleshoot, especially for a beginner. A reasonable approach is to
assign the open file object to a name of your own choosing...

. podcast_file = open('podcast.txt', 'a')

... and then, use the write method of the file object ...

. podcast_file.write('%s: %s' % (entry.updated, entry.link))

More info here:
http://www.python.org/doc/2.5.3/tut/node9.html#SECTION00920


 print '%s: %s' % (entry.updated, entry.link)
 sys.stdout.close()
 for entry in data.entries:
 sys.stdout = open(podcast_links.txt, a)
 print '%s' % (entry.link)
 sys.stdout.close()
 getFeed()
 
 next to get the latest item;
 
snip
 lname = podcast_links.txt
 L = open(lname, 'r')
 print The Latest Link\n
 download = L.readline()

The readline method returns a line from the file *including* the newline
 character(s) ('\n').

 print download
 
 answer = raw_input(Do you want to download the podcast? )
 if answer == y:
 wget = wget
 subprocess.call([wget, download])
 else:
 print oops

OK. There's the problem. Let's assume that after 'download =
L.readline()' that download equals this (you can confirm by adding a
'print repr(download)'):

'http://linuxcrazy.com/podcasts/LC-44-arne.mp3\n'

... then the call becomes (behind the scenes)

subprocess.call(['wget',
'http://linuxcrazy.com/podcasts/LC-44-arne.mp3\n'])

... so the newline is passed as part of the first argument to the wget
command.

Not so coincidentally, the '%0A' represents a newline ('\n') in a
properly quoted/escaped URL.

. import urllib2
. urllib2.unquote('%0A')
'\n'

I suspect it is the wget command which is quoting the newline, not the
subprocess call, as subprocess doesn't know anything about valid
characters for urls.

You can work around this problem as you already have by dropping the
last character as in 'download[:-1]', or use the strip (or rstrip) str
method:

. download.rstrip()
'http://linuxcrazy.com/podcasts/LC-44-arne.mp3'

More info here: http://www.python.org/doc/2.5.3/lib/string-methods.html

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


Re: [Tutor] subprocess adds %0A to end of string

2008-12-21 Thread David

Martin Walsh wrote:



Welcome!



thanks


You should probably try to avoid reassigning sys.stdout. This is usually
a bad idea, and can cause odd behavior that is difficult to
troubleshoot, especially for a beginner. A reasonable approach is to
assign the open file object to a name of your own choosing...

. podcast_file = open('podcast.txt', 'a')

... and then, use the write method of the file object ...

. podcast_file.write('%s: %s' % (entry.updated, entry.link))

More info here:
http://www.python.org/doc/2.5.3/tut/node9.html#SECTION00920



print '%s: %s' % (entry.updated, entry.link)
sys.stdout.close()
for entry in data.entries:
sys.stdout = open(podcast_links.txt, a)
print '%s' % (entry.link)
sys.stdout.close()
getFeed()

This podcast_file.write('%s: %s' % (entry.updated, entry.link))
writes it in one very long string

The Latest Link
http://linuxcrazy.com/podcasts/LC-44-arne.ogghttp://linuxcrazy.com/podcas=

and sys.stdout prints to the file one line at a time

How do I split the long string, not even sure if that is the correct term?

for i in somefile: ?

re.somefile(do_something) ?



--
Powered by Gentoo GNU/LINUX
http://www.linuxcrazy.com
pgp.mit.edu

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


Re: [Tutor] subprocess adds %0A to end of string

2008-12-21 Thread Martin Walsh
David wrote:
 Martin Walsh wrote:
 
 Welcome!
 
 thanks

welcome (uh oh, infinite loop warning)

 This podcast_file.write('%s: %s' % (entry.updated, entry.link))
 writes it in one very long string

Copy and paste gets me every time. Try this, and note the presence of
the newline ('\n'):

podcast_file.write('%s: %s\n' % (entry.updated, entry.link))

 
 The Latest Link
 http://linuxcrazy.com/podcasts/LC-44-arne.ogghttp://linuxcrazy.com/podcas=
 
 
 and sys.stdout prints to the file one line at a time

The primary issue is not that you're writing to sys.stdout, it's that
you're using the print statement which implicitly adds a newline.
Technically you can also do the following (python = 2.5, and maybe 2.6)
which should demonstrate the concept...

podcastfile = file(somepath, 'a')
print  podcastfile, '%s: %s' (entry.updated, entry.link)

... however, I would recommend against using the above print statement
syntax for anything other than experimentation because 1) it's not
common practice IMHO, and 2) it's gone starting with python 3.0. 'print'
will be a builtin function instead of a statement going forward.

I second Alan's recommendation to read a tutorial or two to solidify
your understanding of the basic concepts. Alan's tutorial is very good,
and covers everything we have discussed so far, particularly the
Handling Files section. Check the link in his email sig.

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