Re: [Tutor] working with c_byte?

2012-03-24 Thread Alan Gauld

On 24/03/12 03:51, Alex Hall wrote:


Of course, 9 means the battery is high and charging, but how do I
interpret an arbitrary integer as the sum of its flags? Is there a
binary trick I can use?


Dave has given the immediate answer which is a subset of a general 
technique known as bitwise masking.


I discuss this with some other examples in the Operating System topic of 
my tutorial. You might find the write up interesting.


Another useful bitwise operator is xor which can be used to tell is any 
flags have changed value since the last time you looked:


oldflags = readFlags()

while True:   # look for a change
   newflags = readFlags()
   if newflags ^ oldflags:
   break # there's been a change

# process newflags.

And finally, the bitwise OR can be used to set a particular flag

HiBattery = 8

flags = flags | HiBattery  # sets HiBattery, preserving the rest.


HTH,

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

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


Re: [Tutor] working with c_byte?

2012-03-24 Thread Dave Angel

On 03/24/2012 04:14 AM, Alan Gauld wrote:

On 24/03/12 03:51, Alex Hall wrote:


Of course, 9 means the battery is high and charging, but how do I
interpret an arbitrary integer as the sum of its flags? Is there a
binary trick I can use?


Dave has given the immediate answer which is a subset of a general 
technique known as bitwise masking.


I discuss this with some other examples in the Operating System topic 
of my tutorial. You might find the write up interesting.


Another useful bitwise operator is xor which can be used to tell is 
any flags have changed value since the last time you looked:


oldflags = readFlags()

while True:   # look for a change
   newflags = readFlags()
   if newflags ^ oldflags:
   break # there's been a change

# process newflags.

And finally, the bitwise OR can be used to set a particular flag

HiBattery = 8

flags = flags | HiBattery  # sets HiBattery, preserving the rest.


HTH,



There are many useful purposes for xor, but your example could have just 
used  the != operator.  A better example is if you want to know which 
bit(s) changed, you xor the old with the new.



One other technique which I forgot about when composing my earlier reply 
is to fetch individual bits.  If you just want one bit at a time, and 
usually there's just a single one, it can be useful to have an algorithm 
which simply gives you the lowest nonzero bit.


For any nonzero value, if you AND that value with -value, you'll get a 
single bit value.  (only on twos-complement machines)


so if you have a dict called flag_meanings, you could do something like:

   flag = flags  (-flags)
   meaning = flag_meanings[ flag ]

And if flag_meanings has an entry for zero as well as for 1,2, 4, etc., 
then this will always give you a useful meaning. Now if you apply your 
xor technique here, you can do something like:


flags ^= flag

to clear out the flag you've processed, and repeat to process the rest.  
Something like:


while flags:
 flag = flags  (-flags)
 print flag_meanings[flag]
 flags ^= flag

This loop will behave similarly to the for-loop I suggested earlier.  
But it will loop around fewer times, and waste no cycles once the flags 
have been processed.  This can be especially useful if you have hundreds 
of bits in your flags, rather than just 8.


(above code fragments untested;  it's late here)



--

DaveA

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


[Tutor] getUncPath(mappedDrive)

2012-03-24 Thread Albert-Jan Roskam
Hi,

Is there a function that takes a file path with a mapped drive (z:\blah) and 
returns the associated UNC path (\\server\share\ding\dang\dong\blah)? I looked 
in os.path, but it doesn't seem to have this. The link below seems to be a 
solution (code in the bottom of the page), but I can't install win32com.client 
in the office :-( Is there any built-in function?

 http://stackoverflow.com/questions/2244767/python-check-network-map

Thanks!

Regards,
Albert-Jan


~~
All right, but apart from the sanitation, the medicine, education, wine, public 
order, irrigation, roads, a 
fresh water system, and public health, what have the Romans ever done for us?
~~ ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] getUncPath(mappedDrive)

2012-03-24 Thread Tim Golden

On 24/03/2012 20:13, Albert-Jan Roskam wrote:

Hi,

Is there a function that takes a file path with a mapped drive
(z:\blah) and returns the associated UNC path
(\\server\share\ding\dang\dong\blah)? I looked in os.path, but it
doesn't seem to have this. The link below seems to be a solution
(code in the bottom of the page), but I can't install win32com.client
in the office :-( Is there any built-in function?

http://stackoverflow.com/questions/2244767/python-check-network-map


There's nothing built-in. The easiest function to emulate
through ctypes is probably WNetGetConnection:

http://msdn.microsoft.com/en-us/library/windows/desktop/aa385453%28v=vs.85%29.aspx

(this is available from pywin32 via the win32wnet package
but I assume you can't install that either)

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


Re: [Tutor] getUncPath(mappedDrive)

2012-03-24 Thread Albert-Jan Roskam




From: Tim Golden m...@timgolden.me.uk
To: 
Cc: Python Mailing List tutor@python.org 
Sent: Saturday, March 24, 2012 9:22 PM
Subject: Re: [Tutor] getUncPath(mappedDrive)
 
On 24/03/2012 20:13, Albert-Jan Roskam wrote:
 Hi,

 Is there a function that takes a file path with a mapped drive
 (z:\blah) and returns the associated UNC path
 (\\server\share\ding\dang\dong\blah)? I looked in os.path, but it
 doesn't seem to have this. The link below seems to be a solution
 (code in the bottom of the page), but I can't install win32com.client
 in the office :-( Is there any built-in function?

 http://stackoverflow.com/questions/2244767/python-check-network-map

There's nothing built-in. The easiest function to emulate
through ctypes is probably WNetGetConnection:

http://msdn.microsoft.com/en-us/library/windows/desktop/aa385453%28v=vs.85%29.aspx

(this is available from pywin32 via the win32wnet package
but I assume you can't install that either)

TJG
___
Hi Tim,

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.

Cheers,
Albert-Jan


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


[Tutor] Error handling

2012-03-24 Thread Michael Lewis
Hi everyone,

I am having a bit of trouble understanding what is going on below. What
does the e in except OSError, e: do?
Any other help you can provide regarding errno would be extremely
appreciated. I've done help() and dir() on it, but I am not really
understanding what's going on with e.errno != errno.EEXIST:

Thanks.

import os, errno
try:

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

if e.errno != errno.EEXIST:

raise

I am in the process of writing a script to move files from one
directory to another. I am supplying both the source and destination
directories at runtime. I want to create the destination file on the
fly; however, if it already exists, I want to handle that error/ignore
that error. Also, if the the same file exists in both the source and
destination directory, I'd like to override the one in the destination
directory with the one in the source directory.

I am having trouble with the error handling portion. Here is a
pastebin for my source code:

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


Re: [Tutor] getUncPath(mappedDrive)

2012-03-24 Thread Tim Golden

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
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Error handling

2012-03-24 Thread Colton Myers
 I am having a bit of trouble understanding what is going on below. What does 
 the e in except OSError, e: do? 
 Any other help you can provide regarding errno would be extremely 
 appreciated. I've done help() and dir() on it, but I am not really 
 understanding what's going on with e.errno != errno.EEXIST:
 
 
 

Basically, that `except` block is catching all exceptions of type OSError, and 
storing the exception in variable `e`.  This variable does not have to be 
called `e`, but that's the most commonly-used variable name.

Once you have the exception stored (in this case in the variable `e`), you can 
then see what type of exception, using the `errno` property of the exception.  
You can read about the different types here:

http://docs.python.org/library/errno.html
 import os, errno try: os.makedirs('a/b/c') except OSError, e: if 
 e.errno != errno.EEXIST: raise
 
 
 
 
 
 

In this particular section, it's catching any OSError, and then if it turns out 
that the error was File Exists, it is raising that exception again, to be 
either caught by an encapsulating try block, or which will bring the program to 
a halt with an exception shown by the interpreter.

Is that the behavior you are going for?  Any more confusion?

--
Colton Myers

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


Re: [Tutor] Library of Module for Analyzing Answer Cards

2012-03-24 Thread bob gailer

  
  
Remember to always reply-all so a copy goes to the list.

On 3/24/2012 7:49 AM, Khalid Al-Ghamdi wrote:

  thanks a lot that was extremely helpful.

On Fri, Mar 23, 2012 at 3:58 AM, bob
  gailer bgai...@gmail.com
  wrote:
  

   On 3/22/2012 2:45 PM, Khalid Al-Ghamdi
wrote:

  Hi All,


I work in in academic testing environment and
  we employ expensive machines to scan answer sheets
  (the ones where you blacken the letter of the
  correct multiple choice answer). Anyway, I was
  thinking if there was a way we could use regular
  old scanners to scan the sheets than analyze the
  images to score the tests.

  


  
  This is not a Python solution - but www.cardiff-teleform.com/

offers a product called Teleform that does
  exactly what you want. I used it a while ago for a project
  where we scanned over 100,000 copies of 4 different forms.
  Worked like a charm.
  -- 
Bob Gailer
919-636-4239
Chapel Hill NC

  


  



-- 
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] Error handling

2012-03-24 Thread Michael Lewis
On Sat, Mar 24, 2012 at 3:51 PM, Colton Myers colton.my...@gmail.comwrote:

  I am having a bit of trouble understanding what is going on below. What
 does the e in except OSError, e: do?
 Any other help you can provide regarding errno would be extremely
 appreciated. I've done help() and dir() on it, but I am not really
 understanding what's going on with e.errno != errno.EEXIST:

 Basically, that `except` block is catching all exceptions of type OSError,
 and storing the exception in variable `e`.  This variable does not have to
 be called `e`, but that's the most commonly-used variable name.

 Once you have the exception stored (in this case in the variable `e`), you
 can then see what type of exception, using the `errno` property of the
 exception.  You can read about the different types here:

 http://docs.python.org/library/errno.html

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

 In this particular section, it's catching any OSError, and then if it
 turns out that the error was File Exists, it is raising that exception
 again, to be either caught by an encapsulating try block, or which will
 bring the program to a halt with an exception shown by the interpreter.

 Is that the behavior you are going for?  Any more confusion?


Why wouldn't it be errno.e instead of e.errno?


 --
 Colton Myers




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


[Tutor] Question about input

2012-03-24 Thread Asif Kazmi
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


Re: [Tutor] Error handling

2012-03-24 Thread Sithembewena Lloyd Dube
That is because 'errno' is a property on the exception object called 'e',
not the other way around.

On Sun, Mar 25, 2012 at 7:12 AM, Michael Lewis mjole...@gmail.com wrote:



 On Sat, Mar 24, 2012 at 3:51 PM, Colton Myers colton.my...@gmail.comwrote:

  I am having a bit of trouble understanding what is going on below. What
 does the e in except OSError, e: do?
 Any other help you can provide regarding errno would be extremely
 appreciated. I've done help() and dir() on it, but I am not really
 understanding what's going on with e.errno != errno.EEXIST:

 Basically, that `except` block is catching all exceptions of type
 OSError, and storing the exception in variable `e`.  This variable does not
 have to be called `e`, but that's the most commonly-used variable name.

 Once you have the exception stored (in this case in the variable `e`),
 you can then see what type of exception, using the `errno` property of the
 exception.  You can read about the different types here:

 http://docs.python.org/library/errno.html

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

 In this particular section, it's catching any OSError, and then if it
 turns out that the error was File Exists, it is raising that exception
 again, to be either caught by an encapsulating try block, or which will
 bring the program to a halt with an exception shown by the interpreter.

 Is that the behavior you are going for?  Any more confusion?


 Why wouldn't it be errno.e instead of e.errno?


 --
 Colton Myers




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




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