Re: [Tutor] Error handling

2012-03-25 Thread Russel Winder
Michael,

On Sat, 2012-03-24 at 15:20 -0700, Michael Lewis wrote:
[...]

It is perhaps worth noting that in Python 3, the syntax has changed:

 import os, errno
 try:
 
 os.makedirs('a/b/c')
 except OSError, e:

except OSError as e :

 
 if e.errno != errno.EEXIST:
 
 raise

This as syntax works in 2.6 and 2.7 so is probably the syntax to use
unless you have to use very old versions of Python.  I think the as
syntax makes it clearer that e is a variable referring to the instance
of OSError that causes the except clause to execute if it does.

-- 
Russel.
=
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder


signature.asc
Description: This is a digitally signed message part
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] getUncPath(mappedDrive)

2012-03-25 Thread Albert-Jan Roskam






 From: Tim Golden m...@timgolden.me.uk
To: Albert-Jan Roskam fo...@yahoo.com 
Cc: Python Mailing List tutor@python.org 
Sent: Saturday, March 24, 2012 11:25 PM
Subject: Re: [Tutor] getUncPath(mappedDrive)
 
On 24/03/2012 21:29, Albert-Jan Roskam wrote:
     Thanks! This seems a feasible approach. I have found this Python
     project that exposes some of the functions of mpr.dll:
     http://sourceforge.net/projects/wnetconnect/ WNetGetConnection is
     not among the functions, but the code will help. I have to read up
     on ctypes.Structure though as I never really understood this.

This particular function call doesn't require too much work
in fact. Something like the following code -- error-handling
mostly omitted -- should do the trick:

code
import ctypes
#
# Get the ANSI version of the function from its DLL
#
WNetGetConnection = ctypes.windll.mpr.WNetGetConnectionA

ERROR_MORE_DATA = 234

#
# Set up the drive name to map back from
# and an empty buffer with zero length.
#
local_name = Z:
length = ctypes.c_long (0)
remote_name = ctypes.create_string_buffer ()

#
# Call the function, expecting to receive an ERROR_MORE_DATA
# result, which indicates that the buffer is too small and
# which populates the length field with the right length.
#
result = WNetGetConnection (
  local_name,
  remote_name,
  ctypes.byref (length)
)
#
# Assuming we did get that error, recreate the buffer and
# call again with the supplied length. This isn't strictly
# necessary (you could probably get away with hard-coding
# 2048 or whatever) but it does save you having to guess.
#
if result == ERROR_MORE_DATA:
  remote_name = ctypes.create_string_buffer (length.value)
  result = WNetGetConnection (
    local_name,
    remote_name,
    ctypes.byref (length)
  )

#
# If the result of either call was an error, raise an Exception
#
if result != 0:
  raise RuntimeError (Error %d % result)

print Remote name is, remote_name.value

/code

TJG

Hi Tim,

Thank you so much for this! I think this would also be a valuable addition to 
os.path (where I'd expect it to be).
You call WNetGetConnection twice: one time with a 'dummy' string buffer, and 
one time with a buffer of the exact required length. If I'd run a function 
getUncPath() on a gazillion paths, wouldn't it be more effcient to hard-code 
the length to 2048 as you suggest in your comment?

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


Re: [Tutor] getUncPath(mappedDrive)

2012-03-25 Thread Tim Golden

On 25/03/2012 09:12, Albert-Jan Roskam wrote:

Thank you so much for this! I think this would also be a valuable
addition to os.path (where I'd expect it to be).
You call WNetGetConnection twice: one time with a 'dummy' string
buffer, and one time with a buffer of the exact required length. If
I'd run a function getUncPath() on a gazillion paths, wouldn't it be
more effcient to hard-code the length to 2048 as you suggest in your
comment?


The fail-and-resize-the-buffer dance is fairly common on
Windows. The advantage of the technique is that
it will cope with any path length. If you hardcode to 2048
(or whatever length) then the time will come when you'll
encounter a longer path and then you'll have to rewrite your
code with 4096 or 8192 etc. Obviously, if you have control
over all the paths you're interested in, and you know that
they can't grow beyond MAXLEN characters, then by all means
use MAXLEN as the buffer size and shorten your code.

In terms of efficiency, I imagine you'd have to run the function
on an unusually high number of paths -- and you've only got
26 possible drive letters in the first place -- so it looks
to me like a premature optimisation!

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


Re: [Tutor] Question about input

2012-03-25 Thread Joel Goldstick
On Sun, Mar 25, 2012 at 1:31 AM, Asif Kazmi akazmi...@gmail.com wrote:
 Hello,

 I'm going through Python Programming for the Absolute Beginner, 3rd edition,
 on a Mac with Python 3.2.

 In the second chapter, the book gives sample code that shows how a logical
 error can occur:

 # Trust Fund Buddy - Bad
 # Demonstrates a logical error

 print(
 
             Trust Fund Buddy

 Totals your monthly spending so that your trust fund doesn't run out
 (and you're forced to get a real job).

 Please enter the requested, monthly costs.  Since you're rich, ignore
 pennies
 and use only dollar amounts.

 
 )

 car = input(Lamborghini Tune-Ups: )
 rent = input(Manhattan Apartment: )
 jet = input(Private Jet Rental: )
 gifts = input(Gifts: )
 food = input(Dining Out: )
 staff = input(Staff (butlers, chef, driver, assistant): )
 guru = input(Personal Guru and Coach: )
 games = input(Computer Games: )

 total = car + rent + jet + gifts + food + staff + guru + games

 print(\nGrand Total:, total)

 input(\n\nPress the enter key to exit.)


 This program should show the inputted numbers as a concatenation rather than
 a sum, I understand that is the mistake in the code. However, when I run it,
 it shows:

 Grand Total: 111Manhattan Apartment: 111Private Jet Rental: 111Gifts:
 111Dining Out: 111Staff (butlers, chef, driver, assistant): 111Personal Guru
 and Coach: 111Computer Games: 111

 It appears to be adding the input prompt as part of the variables? except
 for car? What am I missing?

 Thanks,
 Asif

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

Input returns a string in python 3.  So you are doing something like
'1' + '1' ... etc which will concatinate the strings.  Test it out by
typing in something other than a number for your inputs.

Once you get that worked out, see what your code produces.


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


Re: [Tutor] Question about input

2012-03-25 Thread Asif Kazmi
The odd thing is that the same code runs perfectly in the Mac terminal, but
incorrectly in Komodo Edit. In the latter, the input prompts are somehow
being included  into the variables themselves somehow due to the way Komodo
works.

Thanks for the help. I've just taken to running the code in the Mac
terminal which is working great.

Asif

On Sun, Mar 25, 2012 at 9:45 AM, Joel Goldstick joel.goldst...@gmail.comwrote:

 On Sun, Mar 25, 2012 at 1:31 AM, Asif Kazmi akazmi...@gmail.com wrote:
  Hello,
 
  I'm going through Python Programming for the Absolute Beginner, 3rd
 edition,
  on a Mac with Python 3.2.
 
  In the second chapter, the book gives sample code that shows how a
 logical
  error can occur:
 
  # Trust Fund Buddy - Bad
  # Demonstrates a logical error
 
  print(
  
  Trust Fund Buddy
 
  Totals your monthly spending so that your trust fund doesn't run out
  (and you're forced to get a real job).
 
  Please enter the requested, monthly costs.  Since you're rich, ignore
  pennies
  and use only dollar amounts.
 
  
  )
 
  car = input(Lamborghini Tune-Ups: )
  rent = input(Manhattan Apartment: )
  jet = input(Private Jet Rental: )
  gifts = input(Gifts: )
  food = input(Dining Out: )
  staff = input(Staff (butlers, chef, driver, assistant): )
  guru = input(Personal Guru and Coach: )
  games = input(Computer Games: )
 
  total = car + rent + jet + gifts + food + staff + guru + games
 
  print(\nGrand Total:, total)
 
  input(\n\nPress the enter key to exit.)
 
 
  This program should show the inputted numbers as a concatenation rather
 than
  a sum, I understand that is the mistake in the code. However, when I run
 it,
  it shows:
 
  Grand Total: 111Manhattan Apartment: 111Private Jet Rental: 111Gifts:
  111Dining Out: 111Staff (butlers, chef, driver, assistant): 111Personal
 Guru
  and Coach: 111Computer Games: 111
 
  It appears to be adding the input prompt as part of the variables? except
  for car? What am I missing?
 
  Thanks,
  Asif
 
  ___
  Tutor maillist  -  Tutor@python.org
  To unsubscribe or change subscription options:
  http://mail.python.org/mailman/listinfo/tutor
 
 Input returns a string in python 3.  So you are doing something like
 '1' + '1' ... etc which will concatinate the strings.  Test it out by
 typing in something other than a number for your inputs.

 Once you get that worked out, see what your code produces.


 --
 Joel Goldstick

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


Re: [Tutor] Error handling

2012-03-25 Thread Mark Lawrence

On 25/03/2012 08:22, Russel Winder wrote:

Michael,

On Sat, 2012-03-24 at 15:20 -0700, Michael Lewis wrote:
[...]

It is perhaps worth noting that in Python 3, the syntax has changed:


import os, errno
try:

 os.makedirs('a/b/c')
except OSError, e:


except OSError as e :



 if e.errno != errno.EEXIST:

 raise


This as syntax works in 2.6 and 2.7 so is probably the syntax to use
unless you have to use very old versions of Python.  I think the as
syntax makes it clearer that e is a variable referring to the instance
of OSError that causes the except clause to execute if it does.




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


It's worth noting that PEP 3151 has been implemented in Python 3.3 see 
http://docs.python.org/dev/whatsnew/3.3.html#pep-3151-reworking-the-os-and-io-exception-hierarchy


Quoting from the above link.

Thanks to the new exceptions, common usages of the errno can now be 
avoided. For example, the following code written for Python 3.2:


from errno import ENOENT, EACCES, EPERM

try:
with open(document.txt) as f:
content = f.read()
except IOError as err:
if err.errno == ENOENT:
print(document.txt file is missing)
elif err.errno in (EACCES, EPERM):
print(You are not allowed to read document.txt)
else:
raise

can now be written without the errno import and without manual 
inspection of exception attributes:


try:
with open(document.txt) as f:
content = f.read()
except FileNotFoundError:
print(document.txt file is missing)
except PermissionError:
print(You are not allowed to read document.txt)


--
Cheers.

Mark Lawrence.

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


Re: [Tutor] Question about input

2012-03-25 Thread Joel Goldstick
On Sun, Mar 25, 2012 at 11:52 AM, Asif Kazmi akazmi...@gmail.com wrote:
 The odd thing is that the same code runs perfectly in the Mac terminal, but
 incorrectly in Komodo Edit. In the latter, the input prompts are somehow
 being included  into the variables themselves somehow due to the way Komodo
 works.

 Thanks for the help. I've just taken to running the code in the Mac terminal
 which is working great.

 Asif

Are you sure that you are running python 3.x in each environment?  I'm
not familiar with Komodo, so I can't help you there, but in python
v2.x input attempts to evaluate the values you enter.  raw_input, on
the other hand returns your data as a string.  In python 3, input was
removed and raw_input's name was changed to input.

See if you have two different versions of python on your system.

 On Sun, Mar 25, 2012 at 9:45 AM, Joel Goldstick joel.goldst...@gmail.com
 wrote:

 On Sun, Mar 25, 2012 at 1:31 AM, Asif Kazmi akazmi...@gmail.com wrote:
  Hello,
 
  I'm going through Python Programming for the Absolute Beginner, 3rd
  edition,
  on a Mac with Python 3.2.
 
  In the second chapter, the book gives sample code that shows how a
  logical
  error can occur:
 
  # Trust Fund Buddy - Bad
  # Demonstrates a logical error
 
  print(
  
              Trust Fund Buddy
 
  Totals your monthly spending so that your trust fund doesn't run out
  (and you're forced to get a real job).
 
  Please enter the requested, monthly costs.  Since you're rich, ignore
  pennies
  and use only dollar amounts.
 
  
  )
 
  car = input(Lamborghini Tune-Ups: )
  rent = input(Manhattan Apartment: )
  jet = input(Private Jet Rental: )
  gifts = input(Gifts: )
  food = input(Dining Out: )
  staff = input(Staff (butlers, chef, driver, assistant): )
  guru = input(Personal Guru and Coach: )
  games = input(Computer Games: )
 
  total = car + rent + jet + gifts + food + staff + guru + games
 
  print(\nGrand Total:, total)
 
  input(\n\nPress the enter key to exit.)
 
 
  This program should show the inputted numbers as a concatenation rather
  than
  a sum, I understand that is the mistake in the code. However, when I run
  it,
  it shows:
 
  Grand Total: 111Manhattan Apartment: 111Private Jet Rental: 111Gifts:
  111Dining Out: 111Staff (butlers, chef, driver, assistant): 111Personal
  Guru
  and Coach: 111Computer Games: 111
 
  It appears to be adding the input prompt as part of the variables?
  except
  for car? What am I missing?
 
  Thanks,
  Asif
 
  ___
  Tutor maillist  -  Tutor@python.org
  To unsubscribe or change subscription options:
  http://mail.python.org/mailman/listinfo/tutor
 
 Input returns a string in python 3.  So you are doing something like
 '1' + '1' ... etc which will concatinate the strings.  Test it out by
 typing in something other than a number for your inputs.

 Once you get that worked out, see what your code produces.


 --
 Joel Goldstick





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


[Tutor] Error Handling

2012-03-25 Thread Michael Lewis
In the below block, why is the if statement e.errno != errno.EEXIST?
Why can the errno be both before and after the .?

import os, errno

try:


os.makedirs('a/b/c')

except OSError, e:


if e.errno != errno.EEXIST:


raise



-- 
Michael J. Lewis

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


Re: [Tutor] Error Handling

2012-03-25 Thread Joel Goldstick
On Sun, Mar 25, 2012 at 12:54 PM, Michael Lewis mjole...@gmail.com wrote:
 In the below block, why is the if statement e.errno != errno.EEXIST?
 Why can the errno be both before and after the .?



 import os, errno
 try:
     os.makedirs('a/b/c')
 except OSError, e:
     if e.errno != errno.EEXIST:
         raise

errno is a dictionary of standard errors.  You can learn about it in
the python shell by importing errno then typing help(errno) or
dir(errno).  So, errno.EEXIST is just the number 17 as it turns out

e is an exception object.  It has an attribute called errno which
identifies what the error number is.  So this is just comparing what
your actual error is to the EEXIST number.  If it isn't that, then you
'raise'




 --
 Michael J. Lewis

 mjole...@gmail.com
 415.815.7257


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




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


[Tutor] Permissions Error

2012-03-25 Thread Michael Lewis
Hi everyone,

If I've created a folder, why would I receive a permissions error when
trying to copy the file. My source code is here:
http://pastebin.com/1iX7pGDw

When I check the properties/security of the file in question, the system
says I have full control.

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


Re: [Tutor] Permissions Error

2012-03-25 Thread Evert Rol
 Hi everyone,
 
 If I've created a folder, why would I receive a permissions error when trying 
 to copy the file. My source code is here:
 http://pastebin.com/1iX7pGDw

What's the permission error you get? Can't you copy the file, or not create the 
destination directory?
Or you may not be allowed to remove the file in the destination directory if it 
already exists.

Giving the traceback could be useful.


 When I check the properties/security of the file in question, the system says 
 I have full control.

I think that copying file depends on the permissions of the folder, not the 
file. Not sure though, and it may depend on the OS in question.


Cheers,

  Evert

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


Re: [Tutor] Permissions Error

2012-03-25 Thread Steven D'Aprano

Michael Lewis wrote:

Hi everyone,

If I've created a folder, why would I receive a permissions error when
trying to copy the file. My source code is here:
http://pastebin.com/1iX7pGDw


The usual answer to why would I receive a permissions error is that you 
don't actually have permission to access the file.


What is the actual error you get?



When I check the properties/security of the file in question, the system
says I have full control.


This does not sound like a Python problem, but an operating system problem. 
What OS are you using?


You should check the permissions on the folder, not just the file. Also, if 
you OS supports it, check any extended permissions and ACLs that might apply. 
Can you copy the file using another language, e.g. using powershell, bash or 
applescript? Also check that you are running the Python script as the same 
user you used when creating the file.




--
Steven

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