Re: [Tutor] Returning multiple objects from a function

2012-07-03 Thread Steven D'Aprano
On Tue, Jul 03, 2012 at 07:45:53PM -0700, Alexander Q. wrote:

> -Alex
> 
> P.S. Let me know if this message is sent incorrectly (I scrolled down to
> the bottom of the text box to type this, as opposed to writing the message
> at the top of the text box. I am guessing this is what is meant by
> "top-posting.").

I can see your PS, but I can't (easily) see what your comment was that 
you are adding a PS to.

Alex, sorry to nag, but email protocol is that things you type yourself 
should be just ordinary text like this paragraph. Text you are quoting 
from a previous email should start with a less than sign > like the PS 
message I quoted. Multiple levels of quoting should start with multiple 
less than signs.

If you type text starting with a > sign, deliberately or accidentally, 
it looks like you are quoting somebody else. 

This is counter-productive, because many people reading your email will 
just skim quoted text and they won't notice your comment; worse, some 
email programs can be configured to hide large blocks of quoted text, so 
your comment will be hidden and they literally cannot see it.

Strictly speaking, you're also misattributing your own words to somebody 
else. (You are effectively claiming that *they* wrote what *you* 
actually wrote.) Not only does that cause confusion, but it is rude and 
possibly even actionable if serious enough. (Suppose you wrote something 
like "I have a serious drinking problem", but made it look like you were 
quoting Fred and therefore *Fred* had the drinking problem -- Fred might 
decide to sue you for defamation.)

So please take a modicum of care when writing emails that you start a 
new, BLANK line before typing, and not one starting with >

Last but not least, on technical forums like this, the usual protocol is 
neither "top posting" (type your message above the previous email) nor 
"bottom posting" (type your message below the previous email), but 
"inline posting". This takes a little more effort, but in a long thread 
it makes it much easier to follow the discussion.

In inline posting, you add your comments *between* quotes, as if in a 
dialog. Delete parts of the previous conversation that are no longer 
relevant, so as to keep the size of the email manageable. It is polite 
to indicate when you have deleted text, at least the first time, by 
putting in something like 

[...]

or 

[snip]

to indicate that you have edited the previous person's reply. Leave 
enough text to establish context. If you look back over some of the 
archives, you will see plenty of examples from regulars.

http://mail.python.org/pipermail/tutor/


See also:

http://en.wikipedia.org/wiki/Posting_style



-- 
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Returning multiple objects from a function

2012-07-03 Thread Alexander Q.
On Mon, Jul 2, 2012 at 8:56 PM, Dave Angel  wrote:

> On 07/02/2012 10:23 PM, Alexander Q. wrote:
> > I understand the basics of tuples, but that formulation returned the
> > following error:
> >
> > Traceback (most recent call last):
> >   File "C:\Users\Owner\Desktop\MIT\Sets\Set3.py", line 34, in 
> > list4 = tuplesresult[1]
> > TypeError: 'NoneType' object is not subscriptable
> >
> > When I tried to assign "tuplesresult[1]" to the variable "list4" (after
> > assigning tuplesresult = mainFunc(), which is the name of the function
> that
> > returns the tuple in my program), the error occurred. That aside, is it
> all
> > right if I just code "return list1, list2" without the parens? In that
> > case, how would I access list1 and list2 when needed?
>
> That's no different:   list1, list2  is a tuple of size 2.
> > Thanks for your help.
> >
> You top-posted, which loses all the context of the earlier messages.
> Please put your new message AFTER the part you're quoting
>
> Anyway, you never showed the function mainFunc(), but from the error
> traceback message, it didn't return anything, which means None.
>
> Chances are you have something like:
>
> def mainFunc():
>   if  something:
> return 4, 12
>
>
> And if the if fails, the function will return None.  To fix that, make
> sure all paths through the function return a similar object, generally a
> tuple of the same size.
>
>
>
> --
>
> DaveA
>
> Ok thanks- I will try that.

-Alex

P.S. Let me know if this message is sent incorrectly (I scrolled down to
the bottom of the text box to type this, as opposed to writing the message
at the top of the text box. I am guessing this is what is meant by
"top-posting.").
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how contact the administrator of PythonTutor?

2012-07-03 Thread Alan Gauld

On 03/07/12 20:43, Tamar Osher wrote:

How do I contact the administrator of PythonTutor?


You just have. But to be honest we don't do much administering.
Its mostly self-service!


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor




--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how contact the administrator of PythonTutor?

2012-07-03 Thread Emile van Sebille

On 7/3/2012 12:43 PM Tamar Osher said...

How do I contact the administrator of PythonTutor?


One should respond shortly.

Most admin related tasks can be self administered at

http://mail.python.org/mailman/listlinfo/tutor/

Emile

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] how contact the administrator of PythonTutor?

2012-07-03 Thread Tamar Osher

How do I contact the administrator of PythonTutor?



My email address is emeraldoff...@hotmail.com

 








  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Simple text file processing using fileinput module."Grabbing successive lines" failure

2012-07-03 Thread Flynn, Stephen (L & P - IT)
Hi Peter,

> As you already learned the csv module is the best tool to address your
> problem.

Yup - it's almost going to make my little coding exercise trivial. :)

> However, I'd like to show a generic way to get an extra item in a for-
> loop.

[snip]

> Here's a self-contained example:
> 
> >>> items = "alpha- beta gamma- delta- epsilon zeta".split()
> >>> it = iter(items)
> >>> for item in it:
> ... while item.endswith("-"):
> ... item += next(it)
> ... print item
> ...
> alpha-beta
> gamma-delta-epsilon
> zeta

Perfect - that's essentially what I was trying to do, but I doubt it
would have occurred to me to create my own iterator and make use of it.

Thanks for this example too - nice and easy to understand.

Steve.


This email and any attachment to it are confidential.  Unless you are the 
intended recipient, you may not use, copy or disclose either the message or any 
information contained in the message. If you are not the intended recipient, 
you should delete this email and notify the sender immediately.

Any views or opinions expressed in this email are those of the sender only, 
unless otherwise stated.  All copyright in any Capita material in this email is 
reserved.

All emails, incoming and outgoing, may be recorded by Capita and monitored for 
legitimate business purposes. 

Capita exclude all liability for any loss or damage arising or resulting from 
the receipt, use or transmission of this email to the fullest extent permitted 
by law.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Simple text file processing using fileinput module. "Grabbing successive lines" failure

2012-07-03 Thread Peter Otten
Flynn, Stephen (L & P - IT) wrote:

> Tutors,
> 
> Whilst having a play around with reading in textfiles and reformatting
> them I tried to write a python 3.2 script to read a CSV file, looking for
> any records which were short (indicating that the data may well contain an
> embedded CR/LF. I've attached a small sample file with a "split record" at
> line 3, and my code.
> 
> Call the code with
> 
> Python pipesmoker.py MyFile.txt ,
> 
> (first parameter is the file being read, second parameter is the field
> separator... a comma in this case)
> 
> I can read the file in, I can determine that I'm looking for records which
> have 13 fields and I can find a record which is too short (line 3).
> 
> What I can't do is read the successive line to a short line in order to
> append it onto the end of short line before writing the entire amended
> line out. I'm still thinking about how to persuade the fileinput module to
> leap over the successor line so it doesn't get processed again.
> 
> When I run the code as it stands, I get a traceback as I'm obviously not
> using fileinput.FileInput.readline() correctly.
> 
> value of file is C:\myfile.txt
> value of the delimiter is ,
> I'm looking for  13 , in each currentLine...
> "1","000688  ","ABCD","930020854","34","0","1"," ","930020854
> ","  ","0","0","0","0"
> 
> "2","000688  ","ABCD","930020854","99","0","1"," ","930020854 "," 
> ","0","0","0","0"
> 
> short line found at line 3
> Traceback (most recent call last):
>   File "C:\Documents and
>   Settings\flynns\workspace\PipeSmoker\src\pipesmoker\pipesmoker.py", line
>   35, in 
> nextLine = fileinput.FileInput.readline(args.file)
>   File "C:\Python32\lib\fileinput.py", line 301, in readline
> line = self._buffer[self._bufindex]
> AttributeError: 'str' object has no attribute '_buffer'
> 
> 
> Can someone explain to me how I am supposed to make use of readline() to
> grab the next line of a text file please? It may be that I should be using
> some other module, but chose fileinput as I was hoping to make the little
> routine as generic as possible; able to spot short lines in tab separated,
> comma separated, pipe separated, ^~~^ separated and anything else which my
> clients feel like sending me.

As you already learned the csv module is the best tool to address your 
problem. 

However, I'd like to show a generic way to get an extra item in a for-loop.

Instead of iterating over the "iterable" (a list or a FileInput object or 
whatever) you first convert it into an iterator explicitly with the iter() 
built-in function and keep the reference around:

iterable = ...
it = iter(iterable)

Then inside the for-loop you get an extra item with the next() function:

for item in it:
if some_condition():
extra = next(it)

next() also allows you to provide a default value; without it you may get a 
StopIteration exception when you apply it on an exhausted iterator.

Here's a self-contained example:

>>> items = "alpha- beta gamma- delta- epsilon zeta".split()
>>> it = iter(items)
>>> for item in it:
... while item.endswith("-"):
... item += next(it)
... print item
... 
alpha-beta
gamma-delta-epsilon
zeta


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Simple text file processing using fileinput module. "Grabbing successive lines" failure

2012-07-03 Thread Flynn, Stephen (L & P - IT)
> On 02/07/12 18:39, David Rock wrote:
> 
> > Essentially, your problem isn't with using fileinput, it's with how
you
> > handle each line that comes in.
> 
> The immediate problem is with mis-using fileinput.
> But once you solve that you then have to deal with the
> other issues David raises.
> 
> Once more I recommend the csv module...


Thanks gents - seems the CSV module does everything I require so I'll
get tinkering with it.

Steve.



This email and any attachment to it are confidential.  Unless you are the 
intended recipient, you may not use, copy or disclose either the message or any 
information contained in the message. If you are not the intended recipient, 
you should delete this email and notify the sender immediately.

Any views or opinions expressed in this email are those of the sender only, 
unless otherwise stated.  All copyright in any Capita material in this email is 
reserved.

All emails, incoming and outgoing, may be recorded by Capita and monitored for 
legitimate business purposes. 

Capita exclude all liability for any loss or damage arising or resulting from 
the receipt, use or transmission of this email to the fullest extent permitted 
by law.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Returning multiple objects from a function

2012-07-03 Thread Walter Prins
Hi

On 3 July 2012 03:23, Alexander Q.  wrote:
> > Traceback (most recent call last):
>   File "C:\Users\Owner\Desktop\MIT\Sets\Set3.py", line 34, in 
> list4 = tuplesresult[1]
> TypeError: 'NoneType' object is not subscriptable

Ok as a bit of clarification: When you have a function that doesn't
actually return anything, for example:

def mainFunc()
print "I don't return anything..."

And you then do:

x = mainFunc()

... then x will contain the value None with the type NoneType.

If you then try to index into x, e.g...

print x[0]

... you will get the error above, which is saying the the type of the
variable x, (e.g. NoneType) is not subscriptable, that is, you cannot
index into it.


So, what you have to ask yourself is where/how you omitted to have
mainfunc return some values.  (As per Dave's answer, if you have for
example conditional return statements it's probably possibe for
mainFun() to run without returning anything.)

> When I tried to assign "tuplesresult[1]" to the variable "list4" (after
> assigning tuplesresult = mainFunc(), which is the name of the function that
> returns the tuple in my program), the error occurred. That aside, is it all
> right if I just code "return list1, list2" without the parens? In that case,
> how would I access list1 and list2 when needed?

Yes, Python will automatically package the result into a tuple just the same.

Please note, I forgot to mention in my original reply the automatic
tuple unpacking mechanism in Python as mentioned by Alan.  In your
case (as in many cases), it's probably the more Pythonic/neater way to
express yourself.  If your function is returning 4 lists say, then
rather than getting all 4 in a tuple first and then unpacking them
manually yourself, you can just do...

list1, list2, list3, list4 = mainFunc()


HTH,

Walter
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor