Re: [Tutor] Tutor Digest, Vol 38, Issue 1

2007-04-01 Thread Jay Mutter III
Alan thanks for the response;


> Message: 8
> Date: Sun, 1 Apr 2007 08:54:02 +0100
> From: "Alan Gauld" <[EMAIL PROTECTED]>
> Subject: Re: [Tutor] Another parsing question
> To: tutor@python.org
> Message-ID: <[EMAIL PROTECTED]>
> Content-Type: text/plain; format=flowed; charset="iso-8859-1";
>   reply-type=original
>
>
> "Jay Mutter III" <[EMAIL PROTECTED]> 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()
>

For some reason this never works for me;
i am using an intel imac with OS X 10.4.9 which has python 2.3.5

inp = open('test.txt','r')
s = inp.readlines()
for line in s:
 if line.endswith('No.'):
 line = line.rstrip()
 print line
and it never ever removes the line feed.  (These are unix \r  
according to Text wrangler)
I am beginning to think that it is a problem with readlines.

But then i thought well why not

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

however that doesn't work either which leads me to believe that it is  
me and my interpretation of the above.

Thanks

Jay



>> 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,
>
> -- 
> 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
>
>
> End of Tutor Digest, Vol 38, Issue 1
> 

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


Re: [Tutor] Tutor Digest, Vol 38, Issue 1

2007-04-01 Thread Rikard Bosnjakovic
On 4/1/07, Jay Mutter III <[EMAIL PROTECTED]> 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@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tutor Digest, Vol 38, Issue 1

2007-04-01 Thread Alan Gauld

"Rikard Bosnjakovic" <[EMAIL PROTECTED]> 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@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tutor Digest, Vol 38, Issue 1

2007-04-01 Thread Alan Gauld
"Jay Mutter III" <[EMAIL PROTECTED]> 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@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Communication between classes

2007-04-01 Thread Greg Perry
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@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Communication between classes

2007-04-01 Thread Andrei
Hi Greg,

Greg Perry wrote:
> 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,

Good question. Unfortunately there's no general rule that you can apply 
and end up with an undisputably perfect solution.

Classes should communicate on a need-to-know basis. Take for example a 
RSS feed reader application. You may have a class representing a feed 
and a class representing a post. The feed will know what posts it 
contains, but the post probably won't know what feed it comes from. The 
interface would display a list of feeds (without knowing their 
contents), a list of posts within a feed (this needs to know both feed 
and feed contents) and the contents of a single post (knows only about 
an individual post).

 > I have created an Args class that sets a variety of internal
 > variables (__filename, __outputdir etc) by parsing the argv

Be careful with classes that simply act as a container for what are in 
fact global variables. A class should do one thing only (of course what 
you accept as 'one thing' is open for debate) and encapsulate all that's 
necessary for that particular thing. Make sure you're not 
overcomplicating your solution by making classes where they're not 
really necessary.

 > array from th command line.  What would be the preferred
 > mechanism for returning or passing along those variables

In some cases only some parts of the information contained in class A 
are relevant to class B - you should pass only that particular 
information, e.g. in the constructor or by setting a property of B. In 
your example, if you have a Reader class that is interested in the 
filename, you would not pass the whole Args object to it - only the 
filename, like this:

 myreader = Reader(Args.FileName)

 > to another class?  Maybe by a function method that returns
 > all of those variables?

Use properties if you need getter/setter methods or simple attributes 
otherwise. In your case, I would not make __filename etc. 'private' 
(that's what the double underscore suggests), then write a getter method 
for it - just call it FileName and be done with it. Python idiom here is 
more flexible than other languages.

-- 
Yours,

Andrei

=
Mail address in header catches spam. Real contact info:
''.join([''.join(s) for s in zip(
"[EMAIL PROTECTED] pmfe!Pes ontuei ulcpss  edtels,s hr' one oC.",
"rjc5wndon.Sa-re laed o s npbi ot.Ira h it oteesn edt C")])

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


[Tutor] A bug or a feature - complex arguments in special functions

2007-04-01 Thread Eli Brosh
Hello
I am trying to convert from MATLAB to Python.
I am using Python 2.4.3 for Windows (Enthought Edition)
In one of the first programs, I tried to use the special functions from the 
SciPy  "special" module.
However, when I tryed:
 
>> from scipy import *
>> special.jv(0,1+1j)
 
I got an error message and python restarted.
 
The problem did not go away after I installed the latest version of SciPy.
 
Is there a significant bug in the bessel functions when handling complex 
arguments ?
Or, is it some feature that I do not understand ?
 
 
Thanks
Eli Brosh
 
 
 
 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Communication between classes

2007-04-01 Thread Greg Perry
That makes sense, thank you for the detailed explanation Andrei.  For this 
simple project I am working on, it looks like the most direct route would be to 
use functions and only develop classes for the portions of the program that can 
be reused.

Is it safe to say that classes are only useful for instances where reuse is a 
key consideration?  From my very limited perspective, it seems that classes are 
in most cases overkill for simple tasks (such as reading the command line then 
calculating a hash/checksum to verify integrity).

Thanks again for your very descriptive answer.

-Original Message-
From: Andrei

Hi Greg,
>
>Greg Perry wrote:
> 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,
>
>Good question. Unfortunately there's no general rule that you can apply 
>and end up with an undisputably perfect solution.
>
>Classes should communicate on a need-to-know basis. Take for example a 
>RSS feed reader application. You may have a class representing a feed 
>and a class representing a post. The feed will know what posts it 
>contains, but the post probably won't know what feed it comes from. The 
>interface would display a list of feeds (without knowing their 
>contents), a list of posts within a feed (this needs to know both feed 
>and feed contents) and the contents of a single post (knows only about 
>an individual post).
>
> > I have created an Args class that sets a variety of internal
> > variables (__filename, __outputdir etc) by parsing the argv
>
>Be careful with classes that simply act as a container for what are in 
>fact global variables. A class should do one thing only (of course what 
>you accept as 'one thing' is open for debate) and encapsulate all that's 
>necessary for that particular thing. Make sure you're not 
>overcomplicating your solution by making classes where they're not 
>really necessary.
>
> > array from th command line.  What would be the preferred
> > mechanism for returning or passing along those variables
>
>In some cases only some parts of the information contained in class A 
>are relevant to class B - you should pass only that particular 
>information, e.g. in the constructor or by setting a property of B. In 
>your example, if you have a Reader class that is interested in the 
>filename, you would not pass the whole Args object to it - only the 
>filename, like this:
>
> myreader = Reader(Args.FileName)
>
> > to another class?  Maybe by a function method that returns
> > all of those variables?
>
>Use properties if you need getter/setter methods or simple attributes 
>otherwise. In your case, I would not make __filename etc. 'private' 
>(that's what the double underscore suggests), then write a getter method 
>for it - just call it FileName and be done with it. Python idiom here is 
>more flexible than other languages.
>
>-- 
>Yours,
>
>Andrei
>
>=
>Mail address in header catches spam. Real contact info:
>''.join([''.join(s) for s in zip(
>"[EMAIL PROTECTED] pmfe!Pes ontuei ulcpss  edtels,s hr' one oC.",
>"rjc5wndon.Sa-re laed o s npbi ot.Ira h it oteesn edt C")])
>
>___
>Tutor maillist  -  Tutor@python.org
>http://mail.python.org/mailman/listinfo/tutor
>
>


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


[Tutor] datetime.timedelta Output Format

2007-04-01 Thread William Allison
Is there a way to have the output of "print tis" in the same format as 
"print now" and "print tafmsd" in the code below?
Thanks,
Will


savage:~ wallison$ python
Python 2.5 (r25:51918, Sep 19 2006, 08:49:13)
[GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
 >>> import datetime
 >>> now = datetime.date.today()
 >>> print now
2007-04-01
 >>> tafmsd = datetime.date(1994, 2, 23)
 >>> print tafmsd
1994-02-23
 >>> tis = now - tafmsd
 >>> print tis
4785 days, 0:00:00
 >>>

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


Re: [Tutor] Communication between classes

2007-04-01 Thread Alan Gauld
"Greg Perry" <[EMAIL PROTECTED]> wrote 

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

That's OK, many folks find the transition hard at first.
It is a new way of looking at problems.

> One thing that is not apparent to me is the best way 
> for classes to communicate with each other.  

Classes don't.
Its *object* oriented programming and its objects 
which communicate. Objects pass messages 
between each other and the parameters are other 
objects. Classes are just the mechanisms for defining 
and creating objects. And thats a very important distinction.

> 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.  

OK, What is the responsibility of an Args object?
What kind of things would you do with/to an Args 
instance? You might want to get the vatrious values 
from it - treating it somewhat like a dictionary maybe?
You might want to store it so the same set of args 
can be used over and over. What else do you want 
the args to do? Focus on the responsibility of the object 
as a whole.

> What would be the preferred mechanism for returning 
> or passing along those variables to another class?  

You pass an instance of the args class as an object to 
another object via a message.

> Maybe by a function method that returns all of those variables?

You might have a method of the Args class that can get 
the values for a given instance but you wouldn't want to 
extract the values from the object and manage them elsewhere. 
That would be to defeat the point of the object, which is to 
encapsulate both the data and the operations on the data 
in one entity, the object, which can be passed around in 
its entirety.

For example when you drive your car(an object) you don't 
dismantle it and then reassemble just the bits you need.
Let's see its only me so only one seat and its a warm day 
so I don't need the heater... You just jump into the complete 
car and drive it. Same with your args class(*). You create 
an instance and use it whichever way you need. If somebody 
else needs your args to do something you give them the args
(just like if somebody borrows your car you don't give them 
bits of it you lend them the whole car)


(*)And same with the car, you don't lend someone the factory 
blueprint (the class) and tell them to build their own car, you 
lend them your specific instance of the car.

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] Communication between classes

2007-04-01 Thread Alan Gauld
"Greg Perry" <[EMAIL PROTECTED]> wrote 

> That makes sense, thank you for the detailed explanation 
> Andrei.  For this simple project I am working on, it looks 
> like the most direct route would be to use functions 

Thats often the case. Often when people start with OOP 
they try to do everything with objects. Its possible but often 
not very efficient.

> and only develop classes for the portions of the program 
> that can be reused.
> Is it safe to say that classes are only useful for instances 
> where reuse is a key consideration?  

Its not only for reuse. Classes and objects are often a 
more natural model of the real world. One of the best 
examples is a GUI program where each window or control 
can be treated as an object. Thats intuitively obvious because 
to our eyews it looks and acts somewhat like a real world 
object. But you rarely reuse the specific controls or windows 
developeed for a particular project. (Widgets are another 
matter, they are designed for reuse)

> seems that classes are in most cases overkill for simple 
> tasks 

Yes thats true. OOP was invented as a way to control 
complexity in large programs. It can be used in smaller 
programs but if you only have a dozen or so linres of 
executable code then classes may well be overkill.

> such as reading the command line then calculating a 
> hash/checksum to verify integrity

If you had a lot of different data types each with their own 
checksum algorithm then classes and objects might be 
appropriate but a single case would usually be easier 
using functions.

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] A bug or a feature - complex arguments in special functions

2007-04-01 Thread Alan Gauld

"Eli Brosh" <[EMAIL PROTECTED]> wrote 

>> from scipy import *
>> special.jv(0,1+1j)
> 
> I got an error message and python restarted.

It would be good if you could include the error text.

However, did you try putting the complex number in parens?
or assigning to a variable and then pass the variable into the call?
It may simply be that Python is interpreting it as

special.jv((0,1)+1j)

 or somesuch strangeness.

But I don't use SciPy so I can't check. An error text may help.

Alan G

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


Re: [Tutor] datetime.timedelta Output Format

2007-04-01 Thread R. Alan Monroe
> Is there a way to have the output of "print tis" in the same format as 
> "print now" and "print tafmsd" in the code below?
> Thanks,
> Will


> savage:~ wallison$ python
> Python 2.5 (r25:51918, Sep 19 2006, 08:49:13)
> [GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin
> Type "help", "copyright", "credits" or "license" for more information.
>  >>> import datetime
>  >>> now = datetime.date.today()
>  >>> print now
> 2007-04-01
>  >>> tafmsd = datetime.date(1994, 2, 23)
>  >>> print tafmsd
> 1994-02-23
>  >>> tis = now - tafmsd
>  >>> print tis
> 4785 days, 0:00:00
>  >>>

That seems like a weird idea. Are you really sure you want the number
of days between start and end dates displayed itself as a date? What
date would that be?

That's kind of like asking how to say "128 shopping days left until
Christmas" in the format of "2007-04-01 shopping days left until
Christmas". It doesn't work, somehow.

Alan

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