[Tutor] Standard Library Performance (3.4.1)

2014-10-24 Thread Adam Jensen
I'm tinkering this evening and I've noticed that math.factorial() is
much faster than my plain python implementations.


import math

def factorial(n):
temp = 1
for k in range(0,n):
temp = temp * (n - k)
return(temp)

def fac(n):
return 1 if (n == 0) else n * fac(n-1)


>From IPython:

In [21]: %timeit factorial(9)
10 loops, best of 3: 5.31 µs per loop

In [22]: %timeit fac(9)
10 loops, best of 3: 6.86 µs per loop

In [23]: %timeit math.factorial(9)
100 loops, best of 3: 416 ns per loop


Is this kind of performance difference typical of the standard library
functions (compared to plain python user implementations)?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Standard Library Performance (3.4.1)

2014-10-24 Thread Alan Gauld

On 24/10/14 02:45, Adam Jensen wrote:

I'm tinkering this evening and I've noticed that math.factorial() is
much faster than my plain python implementations.




Is this kind of performance difference typical of the standard library
functions (compared to plain python user implementations)?


Yes, its often the case that the library functions are written in C and 
compiled into libraries. That makes them much faster than your Python 
code being executed within the Python interpreter.


In addition the library code has typically been fine tuned and optimised 
over many releases so its about as fast as it can get.

(Your Python code could be optimised a bit but it would still
not be as fast as a C implementation.)

Not all library modules are C based however so it doesn't
always apply. But they are usually optimised and thoroughly
debugged so it is still worth using them rather than building
your own.

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

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


Re: [Tutor] Standard Library Performance (3.4.1)

2014-10-24 Thread Stefan Behnel
Alan Gauld schrieb am 24.10.2014 um 13:03:
> Not all library modules are C based however so it doesn't
> always apply. But they are usually optimised and thoroughly
> debugged so it is still worth using them rather than building
> your own.

It's worth stressing this point a bit more. Lots of people have been using
this code, and some found bugs in it that are now fixed. This means that
whatever implementation of similar functionality you can come up with
yourself will most likely have more bugs and be less generally versatile.
And even if the standard library code doesn't fit your needs, start by
taking a deep look at what the Python Package Index (PyPI) offers instead
of writing your own.

Developer time is much better spent reusing other people's code and helping
to squash the remaining bugs in it than having everyone write their own
buggy code over and over.

Stefan


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


[Tutor] Code critique

2014-10-24 Thread Bo Morris
Hello all,

May I please get a little instructional criticism. The code below works. It
logs into 9 different Linux computers, runs a couple commands, and then
transfers a file back to the server. I want to become a better Python
coder; therefore, I was hoping for some ways to make the below code better,
more efficient, or if I am doing something incorrectly, a correct way of
doing it.  Thanks

#!/usr/bin/python

import paramiko

l = ['ip-1', 'ip-2', 'ip-3', 'ip-4', 'ip-5', 'ip-6', 'ip-7', 'ip-8', 'ip-9']

def connect(ip):
user = 'user'
passwd = ''
command = 'echo $HOSTNAME'
s = paramiko.SSHClient()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
s.connect(ip,22,user,passwd,timeout=4)
stdin, stdout, stderr = s.exec_command('echo $HOSTNAME')
out = stdout.read()
if '3102EHD-Lanka-1108' in out:
s.exec_command('export DISPLAY=:0.0; cd /Downloads/Hourly/win.sh')
sftp = s.open_sftp()
sftp.get('/Downloads/Hourly/3102EHD-01108/3102EHD-01108.png',
'/Downloads/Hourly/3102EHD-01108.png')
sftp.close()
print 'file recieved'
elif '3102EHD-01109' in out:
s.exec_command('export DISPLAY=:0.0; cd /Downloads/Hourly/win.sh')
sftp = s.open_sftp()
sftp.get('/Downloads/Hourly/3102DHD-01109/3102DHD-01109.png',
'/Downloads/Hourly/3102DHD-01109.png')
sftp.close()
print 'file recieved'
elif '3102EHD-MUTV-1082' in out:
s.exec_command('export DISPLAY=:0.0; cd /Downloads/Hourly/win.sh')
sftp = s.open_sftp()
sftp.get('/Downloads/Hourly/3102EHD-01082/3102EHD-01082.png',
'/Downloads/Hourly/3102EHD-01082.png')
sftp.close()
print 'file recieved'
elif '3102DHD-MUTV-1033' in out:
s.exec_command('export DISPLAY=:0.0; cd /Downloads/Hourly/win.sh')
sftp = s.open_sftp()
sftp.get('/Downloads/Hourly/3102DHD-01033/3102DHD-01033.png',
'/Downloads/Hourly/3102DHD-01033.png')
sftp.close()
print 'file recieved'
elif 'Encoder' in out:
s.exec_command('export DISPLAY=:0.0; cd /Downloads/Hourly/win.sh')
sftp = s.open_sftp()
sftp.get('/Downloads/Hourly/3102EHD-01302/3102EHD-01302.png',
'/Downloads/Hourly/3102EHD-01302.png')
sftp.close()
print 'file recieved'
elif '3102DHD-01149' in out:
s.exec_command('export DISPLAY=:0.0; cd /Downloads/Hourly/win.sh')
sftp = s.open_sftp()
sftp.get('/Downloads/Hourly/3102DHD-01149/3102DHD-01149.png',
'/Downloads/Hourly/3102DHD-01149.png')
sftp.close()
print 'file recieved'
elif '3102EHD-01125' in out:
s.exec_command('export DISPLAY=:0.0; cd /Downloads/Hourly/win.sh')
sftp = s.open_sftp()
sftp.get('/Downloads/Hourly/3102EHD-01125/3102EHD-01125.png',
'/Downloads/Hourly/3102EHD-01125.png')
sftp.close()
print 'file recieved'
elif '3102DHD-01144' in out:
s.exec_command('export DISPLAY=:0.0; cd /Downloads/Hourly/win.sh')
sftp = s.open_sftp()
sftp.get('/Downloads/Hourly/3102DHD-01144/3102DHD-01144.png',
'/Downloads/Hourly/3102DHD-01144.png')
sftp.close()
print 'file recieved'
elif '3102EHD-01105' in out:
s.exec_command('export DISPLAY=:0.0; cd /Downloads/Hourly/win.sh')
sftp = s.open_sftp()
sftp.get('/Downloads/Hourly/3102EHD-01105/3102EHD-01105.png',
'/Downloads/Hourly/3102EHD-01105.png')
sftp.close()
print 'file recieved'

con = map(connect, l)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Code critique

2014-10-24 Thread Stefan Behnel
Bo Morris schrieb am 24.10.2014 um 14:03:
> May I please get a little instructional criticism. The code below works. It
> logs into 9 different Linux computers, runs a couple commands, and then
> transfers a file back to the server. I want to become a better Python
> coder; therefore, I was hoping for some ways to make the below code better,
> more efficient, or if I am doing something incorrectly, a correct way of
> doing it.  Thanks

A quick comment, not related to coding style: take a look at fabric. It's
made for doing these things.

http://www.fabfile.org/

Regarding your program, instead of writing long sequences of repetitive if
conditions, I would write one function for each of the different operations
and store them in a dict, mapping each host name to a function (and
multiple host names may map to the same function). Then, look up the host
name in the dict and call the corresponding function to run the right
operations on that host.

Using functions will make it easier to factor out similar code. If you look
at the different operations that you do on the different hosts, you will
notice that most of them do the same thing, just with different files.
Instead of duplicating your code for each host, extract the file names that
each host needs and then pass it into the function that reads the file from
the host. The function will then be the same for all hosts, only the input
arguments change.

Stefan


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


[Tutor] Problem by installing Python 3.4.2 64 Bit

2014-10-24 Thread Friedhelm Peters


Dear Mr. and Mrs.,

 

Sorry, I'm from Germany and my English isn't so good. By installing python
3.4.2 (64bit) I get the following error message:

 

"There is a problem with this Windows Installer package. A program required
for this install the complete could not be run. Contact your support
personnel or package vendor."

 

My system: Windows 7 Ultimate x64 SP1, Intel i7 3770, 16 GB RAM

 

The Version 3.4.1150.1013 is installed

 

Greetings

 

Friedhelm Peters

 

Bockholter Str. 29

 

45659 Recklinghausen

 

Tel.:+49 (171) 823 899 1

Fax:   +49 (2361) 905 326

Mail:
friedhelm.peters.1...@epost.de

 
friedhelm-pet...@arcor.de 

 

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


Re: [Tutor] Code critique

2014-10-24 Thread Bo Morris
"...Regarding your program, instead of writing long sequences of repetitive
if
conditions, I would write one function for each of the different operations
and store them in a dict, mapping each host name to a function (and
multiple host names may map to the same function). Then, look up the host
name in the dict and call the corresponding function to run the right
operations on that host..."

Thank you Stefan for your input. Would please be willing to provide an
example?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Code critique

2014-10-24 Thread Peter Otten
Bo Morris wrote:

> "...Regarding your program, instead of writing long sequences of
> repetitive if
> conditions, I would write one function for each of the different
> operations and store them in a dict, mapping each host name to a function
> (and multiple host names may map to the same function). Then, look up the
> host name in the dict and call the corresponding function to run the right
> operations on that host..."
> 
> Thank you Stefan for your input. Would please be willing to provide an
> example?

Start small. Look at this

> if '3102EHD-Lanka-1108' in out:
> s.exec_command('export DISPLAY=:0.0; cd /Downloads/Hourly/win.sh')
> sftp = s.open_sftp()
> sftp.get('/Downloads/Hourly/3102EHD-01108/3102EHD-01108.png',
> '/Downloads/Hourly/3102EHD-01108.png')
> sftp.close()
> print 'file recieved'
> elif '3102EHD-01109' in out:
> s.exec_command('export DISPLAY=:0.0; cd /Downloads/Hourly/win.sh')
> sftp = s.open_sftp()
> sftp.get('/Downloads/Hourly/3102DHD-01109/3102DHD-01109.png',
> '/Downloads/Hourly/3102DHD-01109.png')
> sftp.close()
> print 'file recieved'
> 

and try to move the code in the if-suite into a function

> if '3102EHD-Lanka-1108' in out:
  process()

Then try to determine the argument(s) needed to make the same process() 
function work for both the if and the elif suite:

> if '3102EHD-Lanka-1108' in out:
  process(?)
> elif '3102EHD-01109' in out:
  process(?)

Once that works see if you can preprocess `out` so that it only contains the 
hostname. Do you still need the if...elif...elif...? 

Come back for more hints if you think you do.

Random remarks:

> command = 'echo $HOSTNAME'

The sooner you remove unused code, the better.

> cd /Downloads/Hourly/win.sh

That looks like you are cd-ing into a shell script.

> con = map(connect, l)

That should be a for loop; also, use a descriptive variable name instead of 
`l`.

PS: a dict would come into play once you want to treat machines differently. 
Say, your computers are called one, two, three, four, ..., and you have a 
default procedure

def process_default(hostname): ...

and some special treatment for three, seven, and eight:

def process_three(hostname): ...
def process_seven_or_eight(hostname): ...

Then you can build a lookup table

lookup_process = {
"three": process_three,
"seven": process_seven_or_eight,
"eight": process_seven_or_eight,
}

and find the applicable function with

hostname = ...
process = lookup_process.get(hostname, process_default)
process(hostname)


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


Re: [Tutor] Standard Library Performance (3.4.1)

2014-10-24 Thread Adam Jensen

On 10/24/2014 08:01 AM, Stefan Behnel wrote:
> Alan Gauld schrieb am 24.10.2014 um 13:03:
>> Not all library modules are C based however so it doesn't
>> always apply. But they are usually optimised and thoroughly
>> debugged so it is still worth using them rather than building
>> your own.
> 
> It's worth stressing this point a bit more. Lots of people have been using
> this code, and some found bugs in it that are now fixed. This means that
> whatever implementation of similar functionality you can come up with
> yourself will most likely have more bugs and be less generally versatile.
> And even if the standard library code doesn't fit your needs, start by
> taking a deep look at what the Python Package Index (PyPI) offers instead
> of writing your own.

If the standard library is thoughtfully designed, tested, tuned and
released in a stable way then I agree, it's worth spending development
time to search and explore the library for potentially useful components
then examine the component's interface, decipher the documentation and
tinker with the component to discover its characteristics. There is some
cost in doing this (developer time and energy). Sometimes it seems like
there will be less expense in just writing a few lines of pure python
but a ~10x difference in performance settles that - the library route is
probably worth the effort.

> Developer time is much better spent reusing other people's code and helping
> to squash the remaining bugs in it than having everyone write their own
> buggy code over and over.

I haven't studied the PyPI engineering process yet but my limited
experience with just installing third party packages (e.g., pip install
scipy) makes me a bit wary about naively using these packages as
foundational components.

BTW - I did manage to get all of the scipy dependencies built and
installed but damn, that was a grueling process. I'm surprised it
wasn't automated and thoroughly shocked by the number of compilation
warnings. (I once heard someone describe the current state of software
engineering as being like "building a skyscraper out of bananas").
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Code critique

2014-10-24 Thread Japhy Bartlett
out = stdout.read()
if '3102EHD-Lanka-1108' in out:
s.exec_command('export DISPLAY=:0.0; cd /Downloads/Hourly/win.sh')
sftp = s.open_sftp()
sftp.get('/Downloads/Hourly/3102EHD-01108/3102EHD-01108.png',
'/Downloads/Hourly/3102EHD-01108.png')
sftp.close()
print 'file recieved'
elif '3102EHD-01109' in out:
s.exec_command('export DISPLAY=:0.0; cd /Downloads/Hourly/win.sh')
sftp = s.open_sftp()
sftp.get('/Downloads/Hourly/3102DHD-01109/3102DHD-01109.png',
'/Downloads/Hourly/3102DHD-01109.png')
sftp.close()
print 'file recieved'
...


could be something like:


params = {
'3102EHD-Lanka-1108': '3102EHD-01108',
'3102EHD-01109': '3102DHD-0119',
# ... etc, for each of those elif choices
}

# one function with a variable for the parts that change
function dosomething(identifier):
s.exec_command('export DISPLAY=:0.0; cd /Downloads/Hourly/win.sh')
sftp = s.open_sftp()
sftp.get('/Downloads/Hourly/{}/{}.png'.format(identifier,identifier),
'/Downloads/Hourly/{}.png'.format(identifier))
sftp.close()
print 'file recieved'

# do this as usual
out = stdout.read()

# then call the function with the variable
dosomething(params[out])





with a bit of work, it seems like that could be simplified even more, but
the general idea is that the elif blocks with lots of boilerplate repeated
over and over is generally a sign of bad/lazy code.


On Fri, Oct 24, 2014 at 1:50 PM, Bo Morris  wrote:

> "...Regarding your program, instead of writing long sequences of
> repetitive if
> conditions, I would write one function for each of the different operations
> and store them in a dict, mapping each host name to a function (and
> multiple host names may map to the same function). Then, look up the host
> name in the dict and call the corresponding function to run the right
> operations on that host..."
>
> Thank you Stefan for your input. Would please be willing to provide an
> example?
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Is there a convenient table of Python 3.4 exceptions?

2014-10-24 Thread Cameron Simpson

On 24Oct2014 15:50, Ben Finney  wrote:

Is this a hurdle for newcomers? Yes, and that's why the standard library
API documentation is not a tutorial. We have separate tutorial
documentation for that.

It's not a problem with the documentation of ‘signal.getsignal’ that it
doesn't have an exhaustive list of all exception classes that might be
raised. This is Python, not Java; [...]


Further to this, in particular WRT to Java and other strongly typed static 
languages, in Java one must define what exceptions a function may throw or the 
code won't even compile. This is just great in one sense: it lets you write 
interface definitions with complete confidence about the types of failure the 
caller must accomodate.


However, this only works rigorously because the same information is available 
on everything the function itself calls, courtesy of the same requirements and 
the staticly typed nature of the language.


With Python, this goes out the window. Because _names_ are untyped (they may be 
bound to any object) you can't make the same guarentees. Newcomers will find 
this disorienting; I did.


Because of this, not only can't one document everything a function might throw, 
one can return to writing relevant documents that says what a function does, 
what it expected to receive, and specific exceptions representing important 
failure modes special to the operation. Pass it something else, and 
undocumented badness may ensue (GIGO).


One thing the newcomer should keep in mind is that with library wrapper 
functions (such as almost everything in the "os" module), the python 
documentation says what the function does and what underlying library function 
is involved; detailed failure modes often are to be found in the external 
library documentation.



exceptions are used prolifically as a
flow control method, for code to let its call stack know something
unusual occurred.


This, again, is surprising to newcomers, who will often think of exceptions 
only in "something went wrong" scenarios.


Raising an exception, particularly StopIteration from iterators, frees one from 
choosing a sentinel value for every single function to indicate some value not 
in the legitimate domain.


Consider an "iteration" in C where the values from from an external call. It 
might naturally be written like this:


  while ( (value=function()) != -1) {
loop body here...
  }

Here, "-1" is a sentinel value returned by function() when there are no more 
values for the iteration. It needs to be some naturally "invalid" return value, 
and in C is often -1 for intergers and NULL for pointers including strings.  
Regardless, a special value needs to be chosen and exposed in the loop 
condition.


Comared with almost every Python iteration loop:

  for value in generator_function():
loop body here...

Here the generator (usually automaticly) raises StopIteration, and the Python 
"for" loop implementation automatically catches that exception and exits the 
loop. No sentinel values required.


Again with sentinel values, for non-iteration: Compare with, say, POSIX system 
calls in C. They mostly return -1 on error, and every piece of code calling 
them has to look like this:


  if (unlink(some_pathname) == -1) {
consult errno, recover or complain, maybe return or abort, etc ...
  else:
... continue after successful operation...

With exceptions one can often write clumps of operations all of which success 
without explicit checking verbiage:


  os.unlink(some_pathname)
  os.rename(this, that)

because a failed operation will raise an exception.

Cheers,
Cameron Simpson 

Mike was a sportbike rider, He'd say "El grande numero one!"
With a 'No Fear' sticker on his zx11, he'd lightem'up just for fun...
- Mike Hardcore DoD#5010 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Code critique

2014-10-24 Thread Bo Morris
Thank you all for the helpful criticism. I wish I was able to catch on to
what you are suggesting more quickly.

Based on your recommendations, I have come up with the following so far,
however I just dont see it as easily as I did while using the if/elif
statements.

This is what I have so far. I can not figure out how to iterate through the
dictionary inserting each value where "png_file" should be and execute the
code for each ip address in the list. I was able to do it using the if/elif
statements, but I am afraid I am lost trying to do it another way.

ipList = ['ip-1', 'ip-2', 'ip-3', 'ip-4', 'ip-5', 'ip-6', 'ip-7', 'ip-8',
'ip-9']

host = {'3102EHD-01108':'3102EHD-01108.png',
'3102EHD-01109':'3102DHD-01109.png',
'3102EHD-MUTV-1082':'3102EHD-01082.png',
'3102DHD-01033':'3102DHD-MUTV-1033.png',
'Encoder':'3102EHD-01302.png',
'3102DHD-01149':'3102DHD-01149.png',
'3102EHD-01125':'3102EHD-01125.png',
'3102DHD-01144':'3102DHD-01144.png',
'3102EHD-01105':'3102EHD-01105.png'}

# iterate through the dictionary inserting the png file
def get_png_file(?):
process = get_png_file.get(hostname, png_file)
s.exec_command('export DISPLAY=:0.0; /Downloads/Hourly/win.sh')
sftp = s.open_sftp()
sftp.get('/Downloads/Hourly/'png_file,'/Downloads/Hourly/'png_file)
sftp.close()
print 'file recieved'

user = 'user'
passwd = 'password'
s = paramiko.SSHClient()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())

# iterate through the list and do the below for each
for ip in ipList:
s.connect(ip,22,user,passwd,timeout=4)
since I have all the hostnames in the dic, and I am removing
the if/elif statments, do I need the below command 'echo $HOSTNAME'?
stdin, stdout, stderr = s.exec_command('echo $HOSTNAME')
out = stdout.read()
get_png_file(?)


**original code below**

#connect to each machine and retrieve the image#

l = ['ip-1', 'ip-2', 'ip-3', 'ip-4', 'ip-5', 'ip-6', 'ip-7', 'ip-8', 'ip-9']

def connect(ip):
user = 'user'
passwd = 'password'
command = 'echo $HOSTNAME'
s = paramiko.SSHClient()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
s.connect(ip,22,user,passwd,timeout=4)
stdin, stdout, stderr = s.exec_command('echo $HOSTNAME')
out = stdout.read()
if '3102EHD-Lanka-1108' in out:
s.exec_command('export DISPLAY=:0.0; /Downloads/Hourly/win.sh')
sftp = s.open_sftp()
sftp.get('/Downloads/Hourly/3102EHD-01108/3102EHD-01108.png',
'/Downloads/Hourly/3102EHD-01108.png')
sftp.close()
elif '3102EHD-01109' in out:
s.exec_command('export DISPLAY=:0.0; /Downloads/Hourly/win.sh')
sftp = s.open_sftp()
sftp.get('/Downloads/Hourly/3102DHD-01109/3102DHD-01109.png',
'/Downloads/Hourly/3102DHD-01109.png')
sftp.close()
elif '3102EHD-MUTV-1082' in out:
s.exec_command('export DISPLAY=:0.0; /Downloads/Hourly/win.sh')
sftp = s.open_sftp()
sftp.get('/Downloads/Hourly/3102EHD-01082/3102EHD-01082.png',
'/Downloads/Hourly/3102EHD-01082.png')
sftp.close()
elif '3102DHD-MUTV-1033' in out:
s.exec_command('export DISPLAY=:0.0; /Downloads/Hourly/win.sh')
sftp = s.open_sftp()
sftp.get('/Downloads/Hourly/3102DHD-01033/3102DHD-01033.png',
'/Downloads/Hourly/3102DHD-01033.png')
sftp.close()
elif 'Encoder' in out:
s.exec_command('export DISPLAY=:0.0; /Downloads/Hourly/win.sh')
sftp = s.open_sftp()
sftp.get('/Downloads/Hourly/3102EHD-01302/3102EHD-01302.png',
'/Downloads/Hourly/3102EHD-01302.png')
sftp.close()
elif '3102DHD-01149' in out:
s.exec_command('export DISPLAY=:0.0; /Downloads/Hourly/win.sh')
sftp = s.open_sftp()
sftp.get('/Downloads/Hourly/3102DHD-01149/3102DHD-01149.png',
'/Downloads/Hourly/3102DHD-01149.png')
sftp.close()
elif '3102EHD-01125' in out:
s.exec_command('export DISPLAY=:0.0; /Downloads/Hourly/win.sh')
sftp = s.open_sftp()
sftp.get('/Downloads/Hourly/3102EHD-01125/3102EHD-01125.png',
'/Downloads/Hourly/3102EHD-01125.png')
sftp.close()
elif '3102DHD-01144' in out:
s.exec_command('export DISPLAY=:0.0; /Downloads/Hourly/win.sh')
sftp = s.open_sftp()
sftp.get('/Downloads/Hourly/3102DHD-01144/3102DHD-01144.png',
'/Downloads/Hourly/3102DHD-01144.png')
sftp.close()
elif '3102EHD-01105' in out:
s.exec_command('export DISPLAY=:0.0; /Downloads/Hourly/win.sh')
sftp = s.open_sftp()
sftp.get('/Downloads/Hourly/3102EHD-01105/3102EHD-01105.png',
'/Downloads/Hourly/3102EHD-01105.png')
sftp.close()

con = map(connect, l)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Is there a convenient table of Python 3.4 exceptions?

2014-10-24 Thread boB Stepp
On Thu, Oct 23, 2014 at 10:57 PM, Danny Yoo  wrote:
>
> > I have so far been unable to find a list of these class/subclass names. Of
> > course I can force an error to occur in the interpreter and see what comes
> > up for each type of error I wish to catch. Is there such a table or list?
> >
>
> Hi Bob,
>
> You can find the ones used in the Standard Library here:
>
> https://docs.python.org/3.4/library/exceptions.html
>
Thanks, Danny. That is exactly what I was looking for. Not being
familiar with exception-related vocabulary yet, I was not typing in
the search strings to get me what I wanted. I wound up in "The Python
Language Reference", which is where the quote I gave came from.
Apparently my answers were in the Standard Library Reference. Sigh.

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


Re: [Tutor] Is there a convenient table of Python 3.4 exceptions?

2014-10-24 Thread boB Stepp
On Thu, Oct 23, 2014 at 11:04 PM, Ben Finney  wrote:
> boB Stepp  writes:
>
[...]
>
>> I have so far been unable to find a list of these class/subclass
>> names.
>
> The standard library documentation's chapter on exceptions
> https://docs.python.org/3/library/exceptions.html> shows
> https://docs.python.org/3/library/exceptions.html#exception-hierarchy>.
>

As with Danny, thanks Ben. This is what I was trying to find

[...]
>
> More importantly, though, you should consider *why* you're attempting to
> catch a lot of exception classes. Will you be meaningfully handling
> every one of those situations? That's rather doubtful.
>
> Instead, you should consider which exceptional states your code can
> meaningfully handle, discover what excpetion classes are raised for
> those few situations, and catch *only* those classes.
>

In the programs I have been dabbling in at work, I am often
"surprised" by the situations my users stumble into that I did not
have sufficient imagination to consider up front. And in line with
your statements I have often wondered if I had done enough error
checking, or whether in some instances I was doing too much. Trying to
imagine what the user might do is often a difficult exercise,
especially as my programs have been becoming more lengthy and complex.
I suppose this gets easier with more knowledge and experience?

> Any other exception that gets raised isn't something you can do anything
> useful with, so it should propagate back up to higher levels, either to
> be handled or to exit with a useful error message.
>
As a user of software I have often wished that the error messages
generated were understandable and help me to avoid the condition which
triggered that message in the future.


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


[Tutor] How to anticipate and deal with unexpected exceptions (was: Is there a convenient table of Python 3.4 exceptions?)

2014-10-24 Thread Ben Finney
boB Stepp  writes:

> In the programs I have been dabbling in at work, I am often
> "surprised" by the situations my users stumble into that I did not
> have sufficient imagination to consider up front.

This is a good thing to focus on. Improving the robustness of your code
is a matter of experience, but you can learn from the experience (i.e.
mistakes) of the several decades of programmers who have come before
you, and the approaches that have arisen in response to those mistakes.

> And in line with your statements I have often wondered if I had done
> enough error checking, or whether in some instances I was doing too
> much.

It's interesting that you raise that dilemma. The Python style
definitely leans away from LBYL (Look Before You Leap, i.e. lots of
checking for errors before performing the task at hand), and much more
toward EAFP (it is Easier to Ask Forgiveness than Permission).

EAFP means that if you're going to perform a task in your code, you make
a clear clean API, document that API, and then trust that the other
programmers using that API will get the preconditions and parameters
correct. If they don't, allow errors to propagate all the way back to
that code and let the user of your API handle the errors.

So, when you're using the Python standard library, you're expected not
to know every possible error that can occur; but rather, to read and
understand the API you're writing to, and set appropriate preconditions
and parameters. If you don't, you can rely on getting an exception
object that tells you about the problem.

> Trying to imagine what the user might do is often a difficult
> exercise, especially as my programs have been becoming more lengthy
> and complex. I suppose this gets easier with more knowledge and
> experience?

It also gets easier if you record every bug-fix as a unit test, and
automate a full run of all the existing unit tests after changing your
program behaviour. That way, you:

* Don't have to keep re-discovering bugs you thought you'd fixed.

* Continually ratchet up the robustness of your code.

* Never get to the point where you feel like you can't trust the old
  code.

* Begin to internalise the habit of anticipating what possible
  conditions you should test for, and write those tests *first*.

Once you're comfortable with always writing a battery of unit tests for
any change you make, and accumulating a suite of unit tests that run
before every release (and ideally much more often than that), and never
release any code that breaks any of the tests, you will gain a
confidence in making changes that is priceless.

-- 
 \  “Very few things happen at the right time, and the rest do not |
  `\ happen at all. The conscientious historian will correct these |
_o__)  defects.” —Mark Twain, _A Horse's Tale_ |
Ben Finney

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


Re: [Tutor] Problem by installing Python 3.4.2 64 Bit

2014-10-24 Thread Steven D'Aprano
On Fri, Oct 24, 2014 at 04:31:02PM +0200, Friedhelm Peters wrote:

> 
> Sorry, I'm from Germany and my English isn't so good. By installing python
> 3.4.2 (64bit) I get the following error message:
> 
> "There is a problem with this Windows Installer package. A program required
> for this install the complete could not be run. Contact your support
> personnel or package vendor."

Which installer package are you using?


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