Capturing errors raised by other scripts ?

2010-02-19 Thread northof40
I'm using the subroutine module to run run python script A.py from
B.py (this is on windows fwiw).

A.py is not my script and it may raise arbitary errors before exiting.
How can I determine what's happened before A.py exited ?

To simulate this I've got this script (which is meant to simulate
A.py):

class customError(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)

try:
raise customError(2*2)
except customError as e:
print 'Custom exception occurred, value:', e.value


I then run my A.py like this :

 fdOut, fOut = tempfile.mkstemp(suffix='.txt', prefix='AOut-')
 fdErr, fErr = tempfile.mkstemp(suffix='.txt', prefix='AErr-')
 try:
... pathtojob=python.exe A.py
... p = subprocess.Popen(pathtojob, stderr=fdErr, stdout=fdOut)
... except:
... print bad stuff happened
...

When I do this I the exception handler is not fired and the text
Custom exception occurred, value: 4 ends up in the stdout file.

I'd really like it to end up in stderr because then I could say
anything in stderr ? then ignore the output and flag an error.

I don't want to have to parse the stdout for error like messages and I
can't make changes to A.py.

I'm sure there's a better way to do this - can anyone offer some
advice ?

thanks

Richard.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Capturing errors raised by other scripts ?

2010-02-19 Thread northof40
On Feb 20, 4:13 pm, MRAB pyt...@mrabarnett.plus.com wrote:
 northof40 wrote:
  I'm using the subroutine module to run run python script A.py from
  B.py (this is on windows fwiw).

  A.py is not my script and it may raise arbitary errors before exiting.
  How can I determine what's happened before A.py exited ?

  To simulate this I've got this script (which is meant to simulate
  A.py):

  class customError(Exception):
     def __init__(self, value):
             self.value = value
     def __str__(self):
             return repr(self.value)

  try:
     raise customError(2*2)
  except customError as e:
     print 'Custom exception occurred, value:', e.value

  I then run my A.py like this :

  fdOut, fOut = tempfile.mkstemp(suffix='.txt', prefix='AOut-')
  fdErr, fErr = tempfile.mkstemp(suffix='.txt', prefix='AErr-')
  try:
  ...     pathtojob=python.exe A.py
  ...     p = subprocess.Popen(pathtojob, stderr=fdErr, stdout=fdOut)
  ... except:
  ...     print bad stuff happened
  ...

  When I do this I the exception handler is not fired and the text
  Custom exception occurred, value: 4 ends up in the stdout file.

  I'd really like it to end up in stderr because then I could say
  anything in stderr ? then ignore the output and flag an error.

  I don't want to have to parse the stdout for error like messages and I
  can't make changes to A.py.

  I'm sure there's a better way to do this - can anyone offer some
  advice ?

  thanks

 A.py is printing the error message.

 The 'print' statement prints to stdout unless you direct it elsewhere
 with '', for example:

      print  my_file, 'message'

 If you don't want error messages to go to stdout, then don't print them
 to stdout, it's as simple as that!

Thanks for your reply. I take your point about print - perhaps I
hadn't really thought that through. Unfortunately I don't have control
of the real script that's being run so if the programmer of that
script has used print there's nothing I can do about it.

Perhaps I could modify the question. If A.py terminates due an
unhandled exception is there anyway for B.py to know that's happened
(without A.py's cooperation ?).

thanks

Richard.


If A.py terminates due to an unhandled exception
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to timeout when waiting for raw_input from user ?

2009-12-05 Thread northof40
On Dec 5, 2:44 pm, Maxim Khitrov mkhit...@gmail.com wrote:
 On Fri, Dec 4, 2009 at 6:55 PM, northof40 shearich...@gmail.com wrote:
  On Dec 5, 12:52 pm, northof40 shearich...@gmail.com wrote:
  Hi - I'm writing a *very* simple program for my kids. It asks the user
  to give it the answer to a maths question and says right or wrong

  They now want a timed version where they would only get so long to
  respond to the question.

  I'm thinking of some logic where a raw_input call is executed and then
  if more than X seconds elapses before the prompt is replied to the
  process writes a message Sorry too slow (or similar).

  I can't see the wood for the trees here - what's the best way to do
  this given the rather simple environment it's needed within.

  Regards

  richard.

  Sorry I should said that based upon other answers I've seen to similar
  questions this needs to run on a windows machine (other answers
  suggest this is more difficult than running on *nix)

 Simplest solution I could come up with. This is indeed much easier on
 *nix (just use select.select on sys.stdin with a timeout).

 ---
 from msvcrt import getch, kbhit, putch
 from time import sleep, time

 ans = ''
 end = time() + 5

 print('2 + 2 = ?')

 while True:
         while time()  end:
                 if kbhit():
                         break
                 else:
                         sleep(0.001)
         else:
                 ans = None
                 break

         char = getch()
         if char == '\r':
                 print('')
                 break
         ans += char
         putch(char)

 if ans is None:
         print('\nSorry too slow')
 else:
         try:
                 print('right' if int(ans) == 4 else 'wrong')
         except:
                 print('not a number')
 ---

 - Max

That's really great - thanks. I've never looked at the whole msvcrt
module before - it looks like it could be useful ... at least for
windows programmes.

Thanks again.

R.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to timeout when waiting for raw_input from user ?

2009-12-05 Thread northof40
On Dec 5, 6:23 pm, Paul Rubin no.em...@nospam.invalid wrote:
 northof40 shearich...@gmail.com writes:
  I'm thinking of some logic where a raw_input call is executed and then
  if more than X seconds elapses before the prompt is replied to the
  process writes a message Sorry too slow (or similar).

 The simplest way to do this is with the alarm function and a signal
 handler.  See the docs for the signal module.

Hi Paul - Thanks for your reply. Unfortunately it seems like the bit
of the signal module I would need for this is not implemented for
windows (AttributeError: 'module' object has no attribute 'SIGALRM').
Still no matter when I asked the question I couldn't even figure out
what module might provide this functionality for any platform so it
was useful knowledge for the future. Thanks again.

regards

Richard.
-- 
http://mail.python.org/mailman/listinfo/python-list


How to timeout when waiting for raw_input from user ?

2009-12-04 Thread northof40
Hi - I'm writing a *very* simple program for my kids. It asks the user
to give it the answer to a maths question and says right or wrong

They now want a timed version where they would only get so long to
respond to the question.

I'm thinking of some logic where a raw_input call is executed and then
if more than X seconds elapses before the prompt is replied to the
process writes a message Sorry too slow (or similar).

I can't see the wood for the trees here - what's the best way to do
this given the rather simple environment it's needed within.

Regards

richard.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to timeout when waiting for raw_input from user ?

2009-12-04 Thread northof40
On Dec 5, 12:52 pm, northof40 shearich...@gmail.com wrote:
 Hi - I'm writing a *very* simple program for my kids. It asks the user
 to give it the answer to a maths question and says right or wrong

 They now want a timed version where they would only get so long to
 respond to the question.

 I'm thinking of some logic where a raw_input call is executed and then
 if more than X seconds elapses before the prompt is replied to the
 process writes a message Sorry too slow (or similar).

 I can't see the wood for the trees here - what's the best way to do
 this given the rather simple environment it's needed within.

 Regards

 richard.

Sorry I should said that based upon other answers I've seen to similar
questions this needs to run on a windows machine (other answers
suggest this is more difficult than running on *nix)

-- 
http://mail.python.org/mailman/listinfo/python-list


What file is foo in package bar in ?

2009-08-19 Thread northof40
Hi - I think this is a pretty basic question but it's never worried me
before.

To improve my skills I'm reading the source code of a library written
by someone else.

I've come across a problem doing that.

Commonly a function is called like this:

thepackage.theclass.foo

The problem is that 'theclass' is implemented in a file called
nothingliketheclass.py so finding the implemention of foo is a bit
boring.

Now I can grep for foo and depending on how common the method name is
that helps ... or not but I'm sure there must be a better way.

Given an arbitary package is there some programmatic way to 'ask' what
file the method/function is implemented in ?

thanks

R.




-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What file is foo in package bar in ?

2009-08-19 Thread northof40
On Aug 20, 11:06 am, Christian Heimes li...@cheimes.de wrote:
 northof40 wrote:
  Given an arbitary package is there some programmatic way to 'ask' what
  file the method/function is implemented in ?

 Indeed, the inspect module contains several useful functions for the
 job, for examplehttp://docs.python.org/library/inspect.html#inspect.getfile

 Christian

That's really great thanks - just what I was after.

regards

R.

-- 
http://mail.python.org/mailman/listinfo/python-list


Python 2.6 in shared/VPS environment

2009-07-31 Thread northof40
Hi - I'd really like to have access to Python 2.6 to try something
out. It needs to be on Linux/Unix machine. I don't mind paying but
whichever way I turn I find Python 2.4 is the standard installation.

Anyone know of anyone who offers this out of the box ? Or got any
smart way of achieving it ?

thanks

R.



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python 2.6 in shared/VPS environment

2009-07-31 Thread northof40
Ignore this question. Managed to get Amazon Web Services going and
have installed Python 2.6 on there. Thanks for your eyeballs time.

On Jul 31, 7:09 pm, northof40 shearich...@gmail.com wrote:
 Hi - I'd really like to have access to Python 2.6 to try something
 out. It needs to be on Linux/Unix machine. I don't mind paying but
 whichever way I turn I find Python 2.4 is the standard installation.

 Anyone know of anyone who offers this out of the box ? Or got any
 smart way of achieving it ?

 thanks

 R.

-- 
http://mail.python.org/mailman/listinfo/python-list