Re: [Tutor] TypeError when io.open is used

2010-06-24 Thread Alan Gauld


 wrote


The error:
Traceback :
  File "insert_into_db_v9.py", line 55, in 
TypeError: an integer is required


Can you send the full error text please? I'm not sure which 
is line 55 and Python normally displays the faulty line as 
part of the error trace.


As it is I can't see any reason for it to fail but I'd like to 
be sure I'm looking at the right place!


Also is there any reason why you explicitly call io.open() 
instead of just using the built-in open() directly? I know 
they are the same function but its quite unusual to use 
the io version explicitly...


--
Alan Gauld
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] Use flag to exit?

2010-06-24 Thread Richard D. Moores
On Wed, Jun 23, 2010 at 23:47, Eike Welk  wrote:
> On Thursday June 24 2010 07:31:47 Richard D. Moores wrote:

> I hope I didn't break your algorithm; I'm typing this directly into the email
> program.

I did what you said (), and get
"invalid syntax" for the comma in line 40.

Taking a stab in the dark I change line 40 as shown in
http://tutoree7.pastebin.com/ybJsm3Rj

Now I get

>>>
The prime greater than or equal to 100 is 101
Traceback (most recent call last):
  File "C:/P31Working/prime_to_biggest_prime_tutor2_Eike2.py", line
40, in 
except (FoundPrimeException, e):
NameError: name 'FoundPrimeException' is not defined
>>>

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


Re: [Tutor] Use flag to exit?

2010-06-24 Thread Richard D. Moores
On Wed, Jun 23, 2010 at 22:31, Richard D. Moores  wrote:
> I've read (I can't remember where) that for every prime p there there
> are positive integers a and b such that p = a + b and such that
> 2**a*3**b is either 1 greater than or 1 less than another (much
> larger) prime. I don't know if this has been proven or not, but I've
> tested it on all primes 3 < p <= 5689. Here's my script that produces
> a big prime number from a small one (p > 3):
> .
>
> My question is how to best exit when the big prime has been found. I
> used a flag (see the highlighted lines 34,40,44), but I seem to
> remember that, though they can work, flags are frowned upon by
> Pythonistas, and should be used only when absolutely necessary. So, is
> one necessary in my script?
>
> Thanks,
>
> Dick Moores

Can't I use sys.exit(). In .
For n = 333 I get the output

The prime greater than or equal to 333 is 337
The smaller prime 337 determined this much larger prime, which has 121 digits:
12965282936790350290684656658525333839833593063568777527873447337564535715481689470015706033552806
62151567471574769467391
a and b were 231 106
Traceback (most recent call last):
  File "C:/P31Working/prime_to_biggest_prime_tutor3.py", line 45, in 
sys.exit()
SystemExit

How can I prevent

Traceback (most recent call last):
  File "C:/P31Working/prime_to_biggest_prime_tutor3.py", line 45, in 
sys.exit()
SystemExit

from printing? Or isn't using sys.exit() a good idea?

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


Re: [Tutor] Use flag to exit?

2010-06-24 Thread Eike Welk
On Thursday June 24 2010 09:16:05 Richard D. Moores wrote:

> I did what you said (), and get
> "invalid syntax" for the comma in line 40.

Are you using Python 3? (I'm using Python 2.6) For Python 3 the correct syntax 
of the except clause is: 

except FoundPrimeException as e:

See:
http://docs.python.org/py3k/tutorial/errors.html#handling-exceptions

> The prime greater than or equal to 100 is 101
> Traceback (most recent call last):
>   File "C:/P31Working/prime_to_biggest_prime_tutor2_Eike2.py", line
> 40, in 
> except (FoundPrimeException, e):
> NameError: name 'FoundPrimeException' is not defined

You have to define the class FoundPrimeException. The definition is in the 
first part of my email. I think a good place for it is before the function 
definitions.


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


Re: [Tutor] Use flag to exit?

2010-06-24 Thread Richard D. Moores
On Thu, Jun 24, 2010 at 02:33, Eike Welk  wrote:
> On Thursday June 24 2010 09:16:05 Richard D. Moores wrote:
>
>> I did what you said (), and get
>> "invalid syntax" for the comma in line 40.
>
> Are you using Python 3? (I'm using Python 2.6) For Python 3 the correct syntax
> of the except clause is:
>
> except FoundPrimeException as e:
>
> See:
> http://docs.python.org/py3k/tutorial/errors.html#handling-exceptions
>
>> The prime greater than or equal to 100 is 101
>> Traceback (most recent call last):
>>   File "C:/P31Working/prime_to_biggest_prime_tutor2_Eike2.py", line
>> 40, in 
>>     except (FoundPrimeException, e):
>> NameError: name 'FoundPrimeException' is not defined
>
> You have to define the class FoundPrimeException. The definition is in the
> first part of my email. I think a good place for it is before the function
> definitions.

OK. See . What now?

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


Re: [Tutor] Use flag to exit?

2010-06-24 Thread Eike Welk
On Thursday June 24 2010 12:15:25 Richard D. Moores wrote:
> OK. See . What now?

Ah... my bad. Line 8 must be changed to:
self.big_p = big_p


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


Re: [Tutor] Use flag to exit?

2010-06-24 Thread Richard D. Moores
On Thu, Jun 24, 2010 at 03:36, Eike Welk  wrote:
> On Thursday June 24 2010 12:15:25 Richard D. Moores wrote:
>> OK. See . What now?
>
> Ah... my bad. Line 8 must be changed to:
>        self.big_p = big_p

Yes! Perfect! Now, Eike, if I only understand what you wrote. You
kindly included an appropriate link to the docs, but...

How about using sys.exit() instead?

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


Re: [Tutor] Use flag to exit?

2010-06-24 Thread Steven D'Aprano
On Thu, 24 Jun 2010 08:51:26 pm Richard D. Moores wrote:

> How about using sys.exit() instead?

sys.exit() is almost always evil. Here's the problem:

One day, you find a nice library that has some function you need. 
Perhaps it's even a library you wrote yourself:


from library import print_next_prime
print_next_prime(5)

=> prints 7


And it works fine. So you decide to extend your program:


from library import print_next_prime
print_next_prime(5)
print_next_prime(13)

=> prints 7


And mysteriously it stops working. The second function call never 
happens, only the first. What's going on?

Eventually you work out that the print_next_prime function needlessly 
calls sys.exit. This being Python, you can fix it:


from library import print_next_prime

def print_next_prime2(n):
try:
print_next_prime(n)
except SystemExit:
pass

print_next_prime2(5)
print_next_prime2(13)


so it's not the end of the world, but you shouldn't have to work around 
the poor design of the function in the first place.

sys.exit is almost never needed. I'm yet to find a program that includes 
it where the program wouldn't be simpler to use, more flexible, more 
friendly, and generally more useful, without it.

One (rare) exception is, when it is part of the user interface, not the 
backend. For instance, in a GUI application, you might link the Quit 
menu command to sys.exit.

In Python, you rarely need to explicitly exit because your code will 
exit when it reaches the end of the file, or on an un-caught exception.



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


Re: [Tutor] Use flag to exit?

2010-06-24 Thread Eike Welk
On Thursday June 24 2010 12:51:26 Richard D. Moores wrote:
> On Thu, Jun 24, 2010 at 03:36, Eike Welk  wrote:
> > On Thursday June 24 2010 12:15:25 Richard D. Moores wrote:
> >> OK. See . What now?
> >
> > Ah... my bad. Line 8 must be changed to:
> >self.big_p = big_p
> 
> Yes! Perfect! Now, Eike, if I only understand what you wrote. You
> kindly included an appropriate link to the docs, but...
> 
> How about using sys.exit() instead?
>From looking at the documentation I think that sys.exit(0) should exit 
cleanly.


Looking at my solution now, I think it is too complicated for your relatively 
simple use-case. I think a better solution would be to put the nested for 
loops into a function and use return to exit the computation. 

I wrote a new untested version here:
http://tutoree7.pastebin.com/RL3b2bx6


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


Re: [Tutor] Use flag to exit?

2010-06-24 Thread Richard D. Moores
On Thu, Jun 24, 2010 at 04:55, Eike Welk  wrote:
> On Thursday June 24 2010 12:51:26 Richard D. Moores wrote:
>> On Thu, Jun 24, 2010 at 03:36, Eike Welk  wrote:
>> > On Thursday June 24 2010 12:15:25 Richard D. Moores wrote:

>> Yes! Perfect! Now, Eike, if I only understand what you wrote. You
>> kindly included an appropriate link to the docs, but...
>>
>> How about using sys.exit() instead?
> >From looking at the documentation I think that sys.exit(0) should exit
> cleanly.
>
>
> Looking at my solution now, I think it is too complicated for your relatively
> simple use-case. I think a better solution would be to put the nested for
> loops into a function and use return to exit the computation.
>
> I wrote a new untested version here:
> http://tutoree7.pastebin.com/RL3b2bx6

Of course! Now, without looking at your code, I'll try to do it
myself, and then look.

Thanks very much, Eike.

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


Re: [Tutor] Time

2010-06-24 Thread Hugo Arts
On Wed, Jun 23, 2010 at 3:11 AM, Dave Angel  wrote:
>
> If you're really looking to measure performance, you should use the timeit
> module.  But for simply deciding how much time has elapsed between two
> points in your code, you can use the time.time() function.
>

Another one I think is worth mentioning is the cProfile module. Though
timeit or time.time are more useful when you simply want to know how
long some task took, when you're optimizing your code and wondering
what's taking a lot of time, cProfile will tell you.

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


Re: [Tutor] finally

2010-06-24 Thread Hugo Arts
On Thu, Jun 24, 2010 at 4:36 AM, Christopher King  wrote:
>     In a try except clause, you can end with finally block. I know it runs
> after the try and except blocks regardless of the outcome, but why use it.
> Couldn't you just put the code after the try and except block without using
> a finally block. Does the finally command do something I don't know about.
> Does it make your program more understandable in some way?

The thing about the finally block is that it *always* runs. If you
just put some code after the try: except: clause, it won't run if

a) the exception is not handled in the try: except: block, but higher
up in the call stack
b) the exception is not handled at all
c) the exception handler terminates the program
d) the exception handler raises another exception

If you need to do some cleanup, putting it in a finally block is the
only way to *guarantee* it will run.

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


Re: [Tutor] finally

2010-06-24 Thread Christopher King
you mean it will always run even if the exception is handled?

On Thu, Jun 24, 2010 at 9:06 AM, Hugo Arts  wrote:

> On Thu, Jun 24, 2010 at 4:36 AM, Christopher King 
> wrote:
> > In a try except clause, you can end with finally block. I know it
> runs
> > after the try and except blocks regardless of the outcome, but why use
> it.
> > Couldn't you just put the code after the try and except block without
> using
> > a finally block. Does the finally command do something I don't know
> about.
> > Does it make your program more understandable in some way?
>
> The thing about the finally block is that it *always* runs. If you
> just put some code after the try: except: clause, it won't run if
>
> a) the exception is not handled in the try: except: block, but higher
> up in the call stack
> b) the exception is not handled at all
> c) the exception handler terminates the program
> d) the exception handler raises another exception
>
> If you need to do some cleanup, putting it in a finally block is the
> only way to *guarantee* it will run.
>
> Hugo
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] finally

2010-06-24 Thread Christopher King
i mean isn't handled

On Thu, Jun 24, 2010 at 9:12 AM, Christopher King wrote:

> you mean it will always run even if the exception is handled?
>
>
> On Thu, Jun 24, 2010 at 9:06 AM, Hugo Arts  wrote:
>
>> On Thu, Jun 24, 2010 at 4:36 AM, Christopher King 
>> wrote:
>> > In a try except clause, you can end with finally block. I know it
>> runs
>> > after the try and except blocks regardless of the outcome, but why use
>> it.
>> > Couldn't you just put the code after the try and except block without
>> using
>> > a finally block. Does the finally command do something I don't know
>> about.
>> > Does it make your program more understandable in some way?
>>
>> The thing about the finally block is that it *always* runs. If you
>> just put some code after the try: except: clause, it won't run if
>>
>> a) the exception is not handled in the try: except: block, but higher
>> up in the call stack
>> b) the exception is not handled at all
>> c) the exception handler terminates the program
>> d) the exception handler raises another exception
>>
>> If you need to do some cleanup, putting it in a finally block is the
>> only way to *guarantee* it will run.
>>
>> Hugo
>>
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] finally

2010-06-24 Thread Christopher King
what manual?

On Wed, Jun 23, 2010 at 10:59 PM, bob gailer  wrote:

>  On 6/23/2010 7:36 PM, Christopher King wrote:
>
> In a try except clause, you can end with finally block. I know it runs
> after the try and except blocks regardless of the outcome, but why use it.
> Couldn't you just put the code after the try and except block without using
> a finally block. Does the finally command do something I don't know about.
> Does it make your program more understandable in some way?
>
>
> Did you read the manual?
>
> "If finally <#12967e4e8be99cad_finally> is present, it specifies a
> ‘cleanup’ handler. The try <#12967e4e8be99cad_try> clause is executed,
> including any except <#12967e4e8be99cad_except> and 
> else<#12967e4e8be99cad_else>clauses. If an exception occurs in any of the 
> clauses and is not handled,
> the exception is temporarily saved. The 
> finally<#12967e4e8be99cad_finally>clause is executed. If there is a saved 
> exception, it is re-raised at the
> end of the finally <#12967e4e8be99cad_finally> clause. If the 
> finally<#12967e4e8be99cad_finally>clause raises another exception or executes 
> a
> return  or 
> breakstatement, the saved exception is lost. 
> The exception information is not
> available to the program during execution of the 
> finally<#12967e4e8be99cad_finally>clause.
>
> "When a return , 
> breakor
> continue  statement is executed in the
> try <#12967e4e8be99cad_try> suite of a try <#12967e4e8be99cad_try>...
> finally <#12967e4e8be99cad_finally> statement, the 
> finally<#12967e4e8be99cad_finally>clause is also executed ‘on the way out.’ A
> continue  statement is illegal in the
> finally <#12967e4e8be99cad_finally> clause. (The reason is a problem with
> the current implementation — this restriction may be lifted in the future)."
>
> --
> Bob Gailer
> 919-636-4239
> Chapel Hill NC
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Time

2010-06-24 Thread Christopher King
I have a module which measures elapsed time. It can also wait til a certain
amount of time has passed till ending. I can send it to you if you like.

On Thu, Jun 24, 2010 at 8:59 AM, Hugo Arts  wrote:

> On Wed, Jun 23, 2010 at 3:11 AM, Dave Angel  wrote:
> >
> > If you're really looking to measure performance, you should use the
> timeit
> > module.  But for simply deciding how much time has elapsed between two
> > points in your code, you can use the time.time() function.
> >
>
> Another one I think is worth mentioning is the cProfile module. Though
> timeit or time.time are more useful when you simply want to know how
> long some task took, when you're optimizing your code and wondering
> what's taking a lot of time, cProfile will tell you.
>
> Hugo
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] finally

2010-06-24 Thread Hugo Arts
On Thu, Jun 24, 2010 at 3:13 PM, Christopher King  wrote:
> i mean isn't handled
>

When I said "guaranteed to run," I meant it. finally will *always* run
after your try clause, exceptions or not, handled or not.
And by manual, he means the python reference manual. He was quoting a
part covering the try statement:

http://docs.python.org/reference/compound_stmts.html#the-try-statement

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


[Tutor] Line wrapping in IDLE possible? Horizontal scroll bar?

2010-06-24 Thread Richard D. Moores
I use IDLE v3.1.1 occasionally on Vista. There doesn't seem to be a
way to configure it to wrap long lines (they do wrap in the IDLE
shell). Or is there?

Also, when there's an unwrapped long line that extends past the right
edge of the window frame, the only way to see the line's tail is to
place the caret on the line and use the right arrow key. Is there a
way to get a horizontal scroll bar to appear?

Thanks,

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


Re: [Tutor] finally

2010-06-24 Thread Christopher King
so if you encounter an error that you won't handle, but you still to close
files and save data, you could use finally?

On Thu, Jun 24, 2010 at 10:05 AM, Christopher King wrote:

> let me try it
>
> On Thu, Jun 24, 2010 at 9:38 AM, Hugo Arts  wrote:
>
>> On Thu, Jun 24, 2010 at 3:13 PM, Christopher King 
>> wrote:
>> > i mean isn't handled
>> >
>>
>> When I said "guaranteed to run," I meant it. finally will *always* run
>> after your try clause, exceptions or not, handled or not.
>> And by manual, he means the python reference manual. He was quoting a
>> part covering the try statement:
>>
>> http://docs.python.org/reference/compound_stmts.html#the-try-statement
>>
>> Hugo
>>
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Use flag to exit?

2010-06-24 Thread Richard D. Moores
On Thu, Jun 24, 2010 at 05:27, Richard D. Moores  wrote:
> On Thu, Jun 24, 2010 at 04:55, Eike Welk  wrote:

>> Looking at my solution now, I think it is too complicated for your relatively
>> simple use-case. I think a better solution would be to put the nested for
>> loops into a function and use return to exit the computation.
>>
>> I wrote a new untested version here:
>> http://tutoree7.pastebin.com/RL3b2bx6
>
> Of course! Now, without looking at your code, I'll try to do it
> myself, and then look.
>
> Thanks very much, Eike.
>
> Dick

Mine is ; yours is
.
Almost identical!

Thanks again, Eike.

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


Re: [Tutor] Line wrapping in IDLE possible? Horizontal scroll bar?

2010-06-24 Thread Christopher King
well, if you use a backlash in the middle of a statement, you can continue
the statement on the next line like so.

>>> for \
i \
in \
('neat', 'ha') \
: \
print \
i

neat
ha
>>>
you can abuse it as much as you like [?]

On Thu, Jun 24, 2010 at 10:00 AM, Richard D. Moores wrote:

> I use IDLE v3.1.1 occasionally on Vista. There doesn't seem to be a
> way to configure it to wrap long lines (they do wrap in the IDLE
> shell). Or is there?
>
> Also, when there's an unwrapped long line that extends past the right
> edge of the window frame, the only way to see the line's tail is to
> place the caret on the line and use the right arrow key. Is there a
> way to get a horizontal scroll bar to appear?
>
> Thanks,
>
> Dick Moores
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
<<1B2.gif>>___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Line wrapping in IDLE possible? Horizontal scroll bar?

2010-06-24 Thread Richard D. Moores
Yes, that's true, but if the line is long because of a long int?

On Thu, Jun 24, 2010 at 07:18, Christopher King  wrote:
>
> well, if you use a backlash in the middle of a statement, you can continue 
> the statement on the next line like so.
> >>> for \
>     i \
>     in \
>     ('neat', 'ha') \
>     : \
>     print \
>     i
> neat
> ha
> >>>
> you can abuse it as much as you like
> On Thu, Jun 24, 2010 at 10:00 AM, Richard D. Moores  
> wrote:
>>
>> I use IDLE v3.1.1 occasionally on Vista. There doesn't seem to be a
>> way to configure it to wrap long lines (they do wrap in the IDLE
>> shell). Or is there?
>>
>> Also, when there's an unwrapped long line that extends past the right
>> edge of the window frame, the only way to see the line's tail is to
>> place the caret on the line and use the right arrow key. Is there a
>> way to get a horizontal scroll bar to appear?
>>
>> Thanks,
>>
>> Dick Moores
>> ___
>> Tutor maillist  -  tu...@python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] finally

2010-06-24 Thread Walter Prins
On 24 June 2010 15:08, Christopher King  wrote:

> so if you encounter an error that you won't handle, but you still to close
> files and save data, you could use finally?
>

More strongly, you *should* use finally, as there's no other way to ensure
your cleanup code will run regardless of unpredictable exceptions that may
be thrown...

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


Re: [Tutor] Line wrapping in IDLE possible? Horizontal scroll bar?

2010-06-24 Thread Chris

On 06/24/2010 10:24 AM, Richard D. Moores wrote:

Yes, that's true, but if the line is long because of a long int?

On Thu, Jun 24, 2010 at 07:18, Christopher King  wrote:
   

well, if you use a backlash in the middle of a statement, you can continue the 
statement on the next line like so.
 

for \
   

 i \
 in \
 ('neat', 'ha') \
 : \
 print \
 i
neat
ha
 
   

you can abuse it as much as you like
On Thu, Jun 24, 2010 at 10:00 AM, Richard D. Moores  wrote:
 

I use IDLE v3.1.1 occasionally on Vista. There doesn't seem to be a
way to configure it to wrap long lines (they do wrap in the IDLE
shell). Or is there?

Also, when there's an unwrapped long line that extends past the right
edge of the window frame, the only way to see the line's tail is to
place the caret on the line and use the right arrow key. Is there a
way to get a horizontal scroll bar to appear?

Thanks,

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

just give the number its on line like so
>>> function('neat',
7085910407358720820797840784720872097287409278407528907047908524087589046980786984380642784907290766890474876480786748728906789042089672687043287422308748902707842048 
\ + 4,

 'ha')
neat
7085910407358720820797840784720872097287409278407528907047908524087589046980786984380642784907290766890474876480786748728906789042089672687043287422308748902707842052
ha
>>>

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


Re: [Tutor] finally

2010-06-24 Thread Chris

On 06/24/2010 10:29 AM, Walter Prins wrote:
On 24 June 2010 15:08, Christopher King @gmail.com > wrote:


so if you encounter an error that you won't handle, but you still
to close files and save data, you could use finally?


More strongly, you *should* use finally, as there's no other way to 
ensure your cleanup code will run regardless of unpredictable 
exceptions that may be thrown...


Walter

cleanup code means code to close files and save data, right
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] finally

2010-06-24 Thread Walter Prins
On 24 June 2010 15:34, Chris  wrote:

> cleanup code means code to close files and save data, right
>

Possibly yes, although I'm referring generally to freeing any resources
(objects, memory, files, whatever) your code has acquired/opened that should
be freed whether or not the code succeeds (without throwing an exception) or
fails somehow (having thrown an exception.)

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


Re: [Tutor] finally

2010-06-24 Thread Chris

On 06/23/2010 10:59 PM, bob gailer wrote:

On 6/23/2010 7:36 PM, Christopher King wrote:
In a try except clause, you can end with finally block. I know it 
runs after the try and except blocks regardless of the outcome, but 
why use it. Couldn't you just put the code after the try and except 
block without using a finally block. Does the finally command do 
something I don't know about. Does it make your program more 
understandable in some way?


Did you read the manual?

"If finally <#finally> is present, it specifies a 'cleanup' handler. 
The try <#try> clause is executed, including any except <#except> and 
else <#else> clauses. If an exception occurs in any of the clauses and 
is not handled, the exception is temporarily saved. The finally 
<#finally> clause is executed. If there is a saved exception, it is 
re-raised at the end of the finally <#finally> clause. If the finally 
<#finally> clause raises another exception or executes a return 
 or break  
statement, the saved exception is lost. The exception information is 
not available to the program during execution of the finally 
<#finally> clause.


"When a return , break 
 or continue  
statement is executed in the try <#try> suite of a try 
<#try>...finally <#finally> statement, the finally <#finally> clause 
is also executed 'on the way out.' A continue 
 statement is illegal in the finally 
<#finally> clause. (The reason is a problem with the current 
implementation --- this restriction may be lifted in the future)."


--
Bob Gailer
919-636-4239
Chapel Hill NC
for the second quote, it means that the try...finally statement, finally 
will always execute even if you use return, break, or continue
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] finally

2010-06-24 Thread Chris

On 06/24/2010 10:44 AM, Walter Prins wrote:
On 24 June 2010 15:34, Chris http://g.nius.ck>@gmail.com 
> wrote:


cleanup code means code to close files and save data, right


Possibly yes, although I'm referring generally to freeing any 
resources (objects, memory, files, whatever) your code has 
acquired/opened that should be freed whether or not the code succeeds 
(without throwing an exception) or fails somehow (having thrown an 
exception.)


Walter


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


Re: [Tutor] unsubscribe

2010-06-24 Thread bob gailer

On 6/23/2010 8:35 AM, Benjamin Pritchard wrote:

unsubscribe
   


You have to do that yourself. The instructions are always included in 
posts sent from the list:



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

   



--
Bob Gailer
919-636-4239
Chapel Hill NC

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


Re: [Tutor] Use flag to exit?

2010-06-24 Thread Alan Gauld


"Richard D. Moores"  wrote


How can I prevent

Traceback (most recent call last):
 File "C:/P31Working/prime_to_biggest_prime_tutor3.py", line 45, in 


   sys.exit()
SystemExit

from printing? Or isn't using sys.exit() a good idea?


Steven has already given you an answer to that although
I'd add that I have no problems using sys.exit() in top level code.
Just don't use it inside a function. Either raise an exception
or return a value.

If you are using it at the top level you may be running your code
inside an IDE like IDLE or Pythonwin which always catches
sys.exit(). That's OK, it's intended to print that message rather
than exit so that you can debug the code if necessary, but it
won't do it if you run the code from the OS prompt it will just
exit silently as expected.

HTH,

--
Alan Gauld
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


[Tutor] os.startfile?

2010-06-24 Thread Jim Byrnes

I am trying to run an example program that contains the line
os.startfile('socket-nongui.py') which is Windows only.  What would be 
the command to use on Linux?  All files are in the same folder.


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


Re: [Tutor] os.startfile?

2010-06-24 Thread Nethirlon
On Thu, Jun 24, 2010 at 7:36 PM, Jim Byrnes  wrote:
> I am trying to run an example program that contains the line
> os.startfile('socket-nongui.py') which is Windows only.  What would be the
> command to use on Linux?  All files are in the same folder.
>
> Thanks,  Jim
> ___
> Tutor maillist  -  tu...@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>

Hi Jim,

Is this perhaps what you are looking for?

import os
os.system('ls -lt > output.txt')

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


Re: [Tutor] Confirm that Python 2.6 ftplib does not support Unicode file names? Alternatives?

2010-06-24 Thread Lie Ryan
On 06/24/10 02:10, pyt...@bdurham.com wrote:
> Can someone confirm that Python 2.6 ftplib does *NOT* support
> Unicode file names? Or must Unicode file names be specially
> encoded in order to be used with the ftplib module?
> 

I don't know the specifics about ftplib, however I believe in most file
systems, file names are plain byte-strings, i.e. most file systems do
not handle encoding, they only deal with plain bytes.

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


Re: [Tutor] os.startfile?

2010-06-24 Thread Jim Byrnes

Nethirlon wrote:

On Thu, Jun 24, 2010 at 7:36 PM, Jim Byrnes  wrote:

I am trying to run an example program that contains the line
os.startfile('socket-nongui.py') which is Windows only.  What would be the
command to use on Linux?  All files are in the same folder.

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



Hi Jim,

Is this perhaps what you are looking for?

import os
os.system('ls -lt>  output.txt')

Kind regards,
Nethirlon



I don't think so but it is my fault.  When I asked the question I 
thought I gave enough info but I see now that I didn't.


The os.startfile('socket-nongui.py) line was in a gui program that 
demonstrates how to start and communicate with a non-gui program.  It is 
Windows specific so I looked for but could not find a drop  in 
replacement that works on Linux.


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


Re: [Tutor] Use flag to exit?

2010-06-24 Thread Richard D. Moores
On Thu, Jun 24, 2010 at 10:13, Alan Gauld  wrote:
>
> "Richard D. Moores"  wrote
>
>> How can I prevent
>>
>> Traceback (most recent call last):
>>  File "C:/P31Working/prime_to_biggest_prime_tutor3.py", line 45, in
>> 
>>   sys.exit()
>> SystemExit
>>
>> from printing? Or isn't using sys.exit() a good idea?
>
> Steven has already given you an answer to that although
> I'd add that I have no problems using sys.exit() in top level code.
> Just don't use it inside a function. Either raise an exception
> or return a value.
>
> If you are using it at the top level you may be running your code
> inside an IDE like IDLE or Pythonwin which always catches
> sys.exit(). That's OK, it's intended to print that message rather
> than exit so that you can debug the code if necessary, but it
> won't do it if you run the code from the OS prompt it will just
> exit silently as expected.

Thanks, Alan. And thanks also to Steven.

I think I much prefer accomplishing an exit by a function return, as I
do in , but if I wanted to use
sys.exit() in a script (but not in a function) I run inside IDLE or
Wing, how do I suppress the message? I'd just like to get that nailed
down.

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


Re: [Tutor] Use flag to exit?

2010-06-24 Thread Alan Gauld


"Richard D. Moores"  wrote

I think I much prefer accomplishing an exit by a function return, as 
I
do in , but if I wanted to 
use

sys.exit() in a script (but not in a function) I run inside IDLE or
Wing, how do I suppress the message? I'd just like to get that 
nailed

down.


You can catch it like any other exception but that's not really the 
point.
IDLE is not intended to run programs it's for developing them. It is 
set

up to catch keyboard interrupts and sys exits deliberately because
that's what you want in a development tool. Just run the programs
outside IDLE and they won't get caught and you won't get error
messages.

HTH,


--
Alan Gauld
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] os.startfile?

2010-06-24 Thread Alan Gauld


"Jim Byrnes"  wrote

The os.startfile('socket-nongui.py) line was in a gui program that 
demonstrates how to start and communicate with a non-gui program. 
It is Windows specific so I looked for but could not find a drop  in 
replacement that works on Linux.


Look at the subprocess module. That is the platform independant way
to run external programs and communicate with them. There are lots of
examples in the documentation and you can find some basic info in
the OS topic of my tutorial (Python v2 only so far for that one)


--
Alan Gauld
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] finally

2010-06-24 Thread Steven D'Aprano
On Thu, 24 Jun 2010 11:06:24 pm Hugo Arts wrote:
> If you need to do some cleanup, putting it in a finally block is the
> only way to *guarantee* it will run.


To be pedantic, the finally clause will always run, *provided* the code 
in the try block itself exits (either by finishing or by raising an 
exception). If the try block never exits, the finally block never runs.

# Don't try this at home.
try:
sys.setcheckinterval(sys.maxint)
print "Can't stop this!"
while 1:
pass
finally:
print "This never prints"


The only way to exit the loop is to hit it with a hammer (kill it from 
outside Python), in which case the finally block never gets to run.




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


Re: [Tutor] finally

2010-06-24 Thread Steven D'Aprano
On Fri, 25 Jun 2010 12:29:12 am Walter Prins wrote:
> On 24 June 2010 15:08, Christopher King  wrote:
> > so if you encounter an error that you won't handle, but you still
> > to close files and save data, you could use finally?
>
> More strongly, you *should* use finally, as there's no other way to
> ensure your cleanup code will run regardless of unpredictable
> exceptions that may be thrown...

Actually, no, having learned `finally`, you should not use it and use 
`with` instead! (For some definition of "should".)

Starting from Python 2.5, Python provides a new, more flexible and 
powerful mechanism for running cleanup code: the `with` statement.

In Python 2.5 you need to import it first:

from __future__ import with_statement

In Python 2.6 and higher you don't need the import.

Then you write something like this:

with open('myfile', 'w') as f:
data = some_function()
f.write(data)

and f will be automatically closed at the end of the with block even if 
some_function raises an exception.

Of course, writing your own cleanup code using try...finally will 
continue to be supported, and it's *not* wrong to use it (especially if 
you have to support versions of Python prior to 2.5) but the with 
statement is the preferred way to do it now.

See more information about it here:

http://effbot.org/zone/python-with-statement.htm



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


Re: [Tutor] Confirm that Python 2.6 ftplib does not support Unicode file names? Alternatives?

2010-06-24 Thread Steven D'Aprano
On Fri, 25 Jun 2010 04:51:13 am Lie Ryan wrote:
> On 06/24/10 02:10, pyt...@bdurham.com wrote:
> > Can someone confirm that Python 2.6 ftplib does *NOT* support
> > Unicode file names? Or must Unicode file names be specially
> > encoded in order to be used with the ftplib module?
>
> I don't know the specifics about ftplib, however I believe in most
> file systems, file names are plain byte-strings, i.e. most file
> systems do not handle encoding, they only deal with plain bytes.

That is completely backwards. Most modern file systems use Unicode, not 
bytes.

Windows file systems use Unicode file names:
http://msdn.microsoft.com/en-us/library/aa365247(VS.85).aspx

Likewise the Macintosh HFS+ file system also uses Unicode file names:
http://developer.apple.com/mac/library/technotes/tn/tn1150.html

So do UDF, ZDF and many others:
http://en.wikipedia.org/wiki/Comparison_of_file_systems

These days, if you're a developer on a non-Linux PC who doesn't need to 
worry about legacy file systems, most file systems you come across will 
be Unicode and not bytes. It's mostly Linux developers who have to deal 
with byte file names.



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


Re: [Tutor] Use flag to exit?

2010-06-24 Thread Richard D. Moores
On Thu, Jun 24, 2010 at 17:06, Alan Gauld  wrote:
>
> "Richard D. Moores"  wrote
>
>> I think I much prefer accomplishing an exit by a function return, as I
>> do in , but if I wanted to use
>> sys.exit() in a script (but not in a function) I run inside IDLE or
>> Wing, how do I suppress the message? I'd just like to get that nailed
>> down.
>
> You can catch it like any other exception but that's not really the point.
> IDLE is not intended to run programs it's for developing them. It is set
> up to catch keyboard interrupts and sys exits deliberately because
> that's what you want in a development tool. Just run the programs
> outside IDLE and they won't get caught and you won't get error
> messages.

But I usually prefer to run the programs I write in an IDE's shell,
unless they are GUI's, of course. What are my options for running my
scripts outside of an IDE? The Windows' command line is pretty ugly
and inconvenient. Copy and pasting is a PITA. And all that changing of
directories. I did enjoy using IPython, however.

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


Re: [Tutor] Confirm that Python 2.6 ftplib does not support Unicode file names? Alternatives?

2010-06-24 Thread Sander Sweers
- Original message -
> On Fri, 25 Jun 2010 04:51:13 am Lie Ryan wrote:
> > On 06/24/10 02:10, pyt...@bdurham.com wrote:
> > > Can someone confirm that Python 2.6 ftplib does *NOT* support
> > > Unicode file names? Or must Unicode file names be specially
> > > encoded in order to be used with the ftplib module?
> > 
> > I don't know the specifics about ftplib, however I believe in most
> > file systems, file names are plain byte-strings, i.e. most file
> > systems do not handle encoding, they only deal with plain bytes.
> 
> That is completely backwards. Most modern file systems use Unicode, not 
> bytes.

-snip-

> These days, if you're a developer on a non-Linux PC who doesn't need to 
> worry about legacy file systems, most file systems you come across will 
> be Unicode and not bytes. It's mostly Linux developers who have to deal 
> with byte file names.

Linux filesystems do not store encoding information as part of the filesystem. 
It does not care if you give it an unicode filename or byte filename to write 
to disk. It is up to the userland applications to properly make sense of the 
filename.

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