Alan Gauld | 1 Apr 01:01 2007

Re: Detect errors when using os.popen.readlines()


"Peter" <lists1 <at> marscode.net> wrote

> Is there a way to detect errors when running shell commands using
> os.popen?

You have to parse the programs output.
Usually errors will appear on stderr so you need to read that as
well as stdout.

This may be slightly easier using the new subprocess module
and the Popen class.

> if an interface doesn't exist I get an error from the shell command.
> I tried using try and except, but that did seem to work.

Even if the program returns an error popen is still working just
fine so no exception gets raised. You must parse the output
(or check the status value, but thats not reliable in all programs)

HTH,

--

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor
(Continue reading)

Jay Mutter III | 1 Apr 02:45 2007

Re: Another parsing question

Kent;
Again thanks for the help.
i am not sure if this is what you menat but i put

for line in s:
     jay = patno.findall(line)
     jay2 = "".join(jay[0])
     print jay2

and it prints fine up until line 111 which is a line that had  
previously returned [ ] since a number didn't exist on that line and   
then exits with

Traceback (most recent call last):
   File "patentno2.py", line 12, in ?
     jay2 = "".join(jay[0])
IndexError: list index out of range

And as long as i am writing, how can I delete a return at the end of  
a line if the line ends in a certain pattern?

For instance, if line ends with the abbreviation  No.
I want to join the current line with next line.
Are lists immutable or can they be changed?

Thanks again

jay

On Mar 31, 2007, at 2:27 PM, Kent Johnson wrote:
(Continue reading)

Kent Johnson | 1 Apr 05:42 2007
Picon

Re: tokenizing a simple string with split()

Andrei Petre wrote:
> I want to split a string like "C:\My\Doc\;D:\backup\" with two 
> separators: \ and ;
> I found that \ is handled with /raw string/ notation r"". But the 
> problem i encountered is with split() function.
> In the 2.5 reference is said that "The sep argument of the split() 
> function may consist of multiple characters". 

The argument to split() is the literal string to split on, not a list of 
potential splitting characters. So to split on '; ' your string would 
have to be 'spam; egg; mail'.

To split on one of a list of characters you have to use a regular 
expression and re.split().

In [1]: import re
In [3]: re.split('[; ]', "spam;egg mail")
Out[3]: ['spam', 'egg', 'mail']

[; ] is a regular expression that means, "match either of ; or space".

Kent
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor

David Heiser | 1 Apr 08:43 2007

Re: tokenizing a simple string with split()


Or you can try something like:

x = r"C:\My\Doc\;D:\backup"
x = x.replace("\\", ";")
x = x.split(";")

-----Original Message-----
From: tutor-bounces <at> python.org [mailto:tutor-bounces <at> python.org] On
Behalf Of Kent Johnson
Sent: Saturday, March 31, 2007 9:42 PM
To: Andrei Petre
Cc: tutor <at> python.org
Subject: Re: [Tutor] tokenizing a simple string with split()

Andrei Petre wrote:
> I want to split a string like "C:\My\Doc\;D:\backup\" with two
> separators: \ and ;
> I found that \ is handled with /raw string/ notation r"". But the 
> problem i encountered is with split() function.
> In the 2.5 reference is said that "The sep argument of the split() 
> function may consist of multiple characters". 

The argument to split() is the literal string to split on, not a list of

potential splitting characters. So to split on '; ' your string would 
have to be 'spam; egg; mail'.

To split on one of a list of characters you have to use a regular 
expression and re.split().
(Continue reading)

Alan Gauld | 1 Apr 09:54 2007

Re: Another parsing question


"Jay Mutter III" <jmutter <at> uakron.edu> wrote

> for line in s:
>     jay = patno.findall(line)
>     jay2 = "".join(jay[0])
>     print jay2
>
> and it prints fine up until line 111 which is a line that had
> previously returned [ ] since a number didn't exist on that line and
> then exits with

> IndexError: list index out of range

Either try/catch the exception or add an
if not line: continue  # or return a default string

> And as long as i am writing, how can I delete a return at the end of
> a line if the line ends in a certain pattern?
>
> For instance, if line ends with the abbreviation  No.

if line.endswith(string): line = line.rstrip()

> I want to join the current line with next line.
> Are lists immutable or can they be changed?

lists can be changed, tuples cannot.

HTH,
(Continue reading)

Jay Mutter III | 1 Apr 13:40 2007

Re: Tutor Digest, Vol 38, Issue 1

Alan thanks for the response;

> Message: 8
> Date: Sun, 1 Apr 2007 08:54:02 +0100
> From: "Alan Gauld" <alan.gauld <at> btinternet.com>
> Subject: Re: [Tutor] Another parsing question
> To: tutor <at> python.org
> Message-ID: <eunoeu$prr$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
>
>> for line in s:
>>     jay = patno.findall(line)
>>     jay2 = "".join(jay[0])
>>     print jay2
>>
>> and it prints fine up until line 111 which is a line that had
>> previously returned [ ] since a number didn't exist on that line and
>> then exits with
>
>> IndexError: list index out of range
>
> Either try/catch the exception or add an
> if not line: continue  # or return a default string
>
>> And as long as i am writing, how can I delete a return at the end of
>> a line if the line ends in a certain pattern?
(Continue reading)

Rikard Bosnjakovic | 1 Apr 14:28 2007
Picon

Re: Tutor Digest, Vol 38, Issue 1

On 4/1/07, Jay Mutter III <jmutter <at> uakron.edu> wrote:

> For some reason this never works for me;

That's because you are ignoring the linefeed character:

[...]
>      if line.endswith('No.'):

>>> s1 = "some line\n"
>>> s2 = "some line"
>>> s1.endswith("line"), s2.endswith("line")
(False, True)

Just skip the if and simply rstrip the string.

--

-- 
- Rikard - http://bos.hack.org/cv/
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor

Alan Gauld | 1 Apr 17:42 2007

Re: Tutor Digest, Vol 38, Issue 1


"Rikard Bosnjakovic" <rikard.bosnjakovic <at> gmail.com> wrote 

>>>> s1 = "some line\n"
>>>> s2 = "some line"
>>>> s1.endswith("line"), s2.endswith("line")
> (False, True)
> 
> Just skip the if and simply rstrip the string.

Or add \n to the endswith() test string if you really only 
want to strip the newline in those cases....

Alan G.

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor

Alan Gauld | 1 Apr 17:46 2007

Re: Tutor Digest, Vol 38, Issue 1

"Jay Mutter III" <jmutter <at> uakron.edu> wrote 

> inp = open('test.txt','r')
> s = inp.readlines()
> for line in s:
>     if line.endswith('No.'):
>         line = line.rstrip()
>     print line

BTW,
You do know that you can shorten that considerably? 
With:

for line in open('test.txt'):
   if line.endswith('No.\n'):
      line = line.rstrip()
   print line

--

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor

Greg Perry | 1 Apr 18:17 2007

Communication between classes

Hi again,

I am still in the process of learning OOP concepts and reasons why classes should be used instead of
functions etc.

One thing that is not apparent to me is the best way for classes to communicate with each other.  For example, I
have created an Args class that sets a variety of internal variables (__filename, __outputdir etc) by
parsing the argv array from th command line.  What would be the preferred mechanism for returning or
passing along those variables to another class?  Maybe by a function method that returns all of those variables?

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor


Gmane