[Tutor] Tutor Digest, Vol 38, Issue 2

Jay Mutter III jmutter at uakron.edu
Thu Apr 5 15:35:57 CEST 2007


>
>
> Message: 3
> Date: Sun, 1 Apr 2007 16:42:56 +0100
> From: "Alan Gauld" <alan.gauld at btinternet.com>
> Subject: Re: [Tutor] Tutor Digest, Vol 38, Issue 1
> To: tutor at python.org
> Message-ID: <euoju5$a7c$1 at sea.gmane.org>
> Content-Type: text/plain; format=flowed; charset="iso-8859-1";
> 	reply-type=original
>
>
> "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.
>

see below

> Or add \n to the endswith() test string if you really only
> want to strip the newline in those cases....
>
> Alan G.
>
>
>
> ------------------------------
>
> Message: 4
> Date: Sun, 1 Apr 2007 16:46:05 +0100
> From: "Alan Gauld" <alan.gauld at btinternet.com>
> Subject: Re: [Tutor] Tutor Digest, Vol 38, Issue 1
> To: tutor at python.org
> Message-ID: <euok41$are$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
>
>> 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
>

Whether I attempt to just strip the string or attempt to

if line.endswith('No.\r'):
     line = line.rstrip()

It doesn't work.
Note - I tried \n, \r and \n\r although text wrangler claims that it  
does have unix line endings
When I used tr to do a few things \n or \r worked fine
I tried sed and it didn't work but from the command line in sed using  
ctrl-v and ctrl-j to insert the  line feed it worked
although i then could not figure out how to do the same in a script.
It is as if the python interpreter doesn't recognize the escaped n  
(or r) as a line feed.
This is an imac running python 2.3.5 under OS-X 10.4.9

Thanks again

> -- 
> Alan Gauld
> Author of the Learn to Program web site
> http://www.freenetpages.co.uk/hp/alan.gauld
>
>
>
> ------------------------------
>
> Message: 5
> Date: 01 Apr 2007 12:17:00 -0400
> From: "Greg Perry" <gregp at liveammo.com>
> Subject: [Tutor] Communication between classes
> To: <tutor at python.org>
> Message-ID: <20070401161218.5D9C117051 at smtp2.hushmail.com>
>
> 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?
>
>
>
>
>
> ------------------------------
>
> Message: 6
> Date: Sun, 01 Apr 2007 20:46:21 +0200
> From: Andrei <project5 at redrival.net>
> Subject: Re: [Tutor] Communication between classes
> To: tutor at python.org
> Message-ID: <euould$au7$1 at sea.gmane.org>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> 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(
> "poet at aao.l pmfe!Pes ontuei ulcpss  edtels,s hr' one oC.",
> "rjc5wndon.Sa-re laed o s npbi ot.Ira h it oteesn edt C")])
>
>
>
> ------------------------------
>
> Message: 7
> Date: Mon, 2 Apr 2007 00:25:39 +0300
> From: "Eli Brosh" <ebrosh at nana.co.il>
> Subject: [Tutor] A bug or a feature - complex arguments in special
> 	functions
> To: <tutor at python.org>
> Message-ID:
> 	<957526FB6E347743AAB42B212AB54FDA7A5AEB at NANAMAILBACK1.nanamail.co.il>
> Content-Type: text/plain; charset="iso-8859-1"
>
> 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
>
>
>
>
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: http://mail.python.org/pipermail/tutor/attachments/ 
> 20070402/41c2d81a/attachment.htm
>
> ------------------------------
>
> Message: 8
> Date: 01 Apr 2007 19:24:00 -0400
> From: "Greg Perry" <gregp at liveammo.com>
> Subject: Re: [Tutor] Communication between classes
> To: <project5 at redrival.net>
> Cc: tutor at python.org
> Message-ID: <20070401231953.2698917069 at smtp2.hushmail.com>
>
> 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(
>> "poet at aao.l 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 at python.org
>> http://mail.python.org/mailman/listinfo/tutor
>>
>>
>
>
>
>
> ------------------------------
>
> Message: 9
> Date: Sun, 01 Apr 2007 19:48:04 -0400
> From: William Allison <allison.william at comcast.net>
> Subject: [Tutor] datetime.timedelta Output Format
> To: tutor at python.org
> Message-ID: <461044B4.8080900 at comcast.net>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> 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 at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
> End of Tutor Digest, Vol 38, Issue 2
> ************************************



More information about the Tutor mailing list