how to read stdout without blocking

2009-12-22 Thread Hamish McKenzie
I need to run a cmd line app from python whose run time can vary from a 
fraction of a second to minutes.  I need to be able to tell whether the process 
is still waiting, or whether it has stalled - and this is farily easily done by 
keeping an eye on whether its writing to stdout or not.  The process under 
working conditions will write to stdout fairly regularly.

So I want to bail out waiting for the process if it is no longer writing to 
stdout, but whenever I read stdout the script blocks and doesn't return until 
the process has finished.

This is under windows, python 2.5.  Can anyone help?  The code I'm using is 
below.


import subprocess
import time


TIMEOUT_PERIOD = 5  #timeout period in seconds

def run( *args ):
cmdStr = 'somePotentiallyLengthyCmdHere'

try:
proc = subprocess.Popen( cmdStr, shell=True, 
stdout=subprocess.PIPE, stderr=subprocess.PIPE )
except OSError:
return False

startTime = time.clock()
stdoutAccum = []
stderrAccum = []
hasTimedOut = False
while True:
ret = proc.poll()
newStdout = proc.stdout.read()
newStderr = proc.stderr.read()
print 'waiting...'

stdoutAccum += newStdout
stderrAccum += newStderr

#if the proc has terminated, deal with 
returning appropriate data
if ret is not None:
if hasTimedOut:
if callable( 
RETURNED_CALLBACK ):

try: RETURNED_CALLBACK( *args )

except: pass

return stdoutAccum + stderrAccum

#if there has been new output, the proc is 
still alive so reset counters
if newStderr or newStdout:
startTime = time.clock()

#make sure we haven't timed out
curTime = time.clock()
if curTime - startTime > TIMEOUT_PERIOD:
hasTimedOut = True
-- 
http://mail.python.org/mailman/listinfo/python-list


getting caller

2009-02-12 Thread Hamish McKenzie
so I'm trying to wrap some functionality around execfile but don't want to 
modify the way it works.  specifically when you call execfile, it automatically 
grabs globals and locals from the frame the execfile exists in.

so if I wrap execfile in a function, I need a way to reliably get at the 
calling frame.

what I want to know is - is it *ALWAYS* true that:
inspect.getouterframes( inspect.currentframe() )[ 1 ][ 0 ]

will return me the frame that called execfile in the first place?

I can't think of a way that this wouldn't be the case, but I'm not 100% sure 
that assertion is valid.
--
http://mail.python.org/mailman/listinfo/python-list


RE: type conversion

2009-01-02 Thread Hamish McKenzie
I actually have no idea what ur talking about...  aren't conversations threaded 
by subject?




-Original Message-
From: python-list-bounces+hamish=valvesoftware@python.org 
[mailto:python-list-bounces+hamish=valvesoftware@python.org] On Behalf Of r
Sent: Friday, January 02, 2009 12:12 PM
To: python-list@python.org
Subject: Re: type conversion

On Jan 2, 1:46 pm, Robert Kern  wrote:
> r wrote:
> > On Jan 1, 4:40 pm, Robert Kern  wrote:
> >> Hamish McKenzie wrote:
> >>> sometimes I want to be able to initialize an instance with a variety of 
> >>> different data types.
> >>> as an obvious example I might want to initialize a 4x4 matrix with either 
> >>> 16 floats, a list/tuple or 16 floats, another matrix or a quaternion.
> >>> is there any other way to do it other than putting case statements in the 
> >>> __init__ method of the class, or having a Matrix.FromQuaternion( quat )?
> >> I recommend keeping the __init__() as dumb as possible. Ideally, it should 
> >> just
> >> assign to attributes. I would add a from() classmethod for each  
> >> that
> >> I wanted to support. If I really wanted an all-singing, all-dancing
> >> initialization method, I would add another classmethod that would just 
> >> dispatch
> >> to the appropriate type-specific classmethod. I prefer classmethods to 
> >> plain
> >> functions because I can subclass.
>
> >> --
> >> Robert Kern
>
> >> "I have come to believe that the whole world is an enigma, a harmless 
> >> enigma
> >>   that is made terrible by our own mad attempt to interpret it as though 
> >> it had
> >>   an underlying truth."
> >>-- Umberto Eco
>
> > Why are you busting into this thread?
>
> Are you talking to me, or Hamish, who (presumably unintentionally) responded 
> to
> a message instead of making a new thread?
>
> If you want to suggest that one should have more care about starting new
> threads, that's fine, but please do not jump to the conclusion that there is
> malintent. And don't chastise people for not being as rude as you.
>
> --
> Robert Kern
>
> "I have come to believe that the whole world is an enigma, a harmless enigma
>   that is made terrible by our own mad attempt to interpret it as though it 
> had
>   an underlying truth."
>-- Umberto Eco

I am not so much chastising Hamish as i am Steven, Steven knows better
than this BS, and has preached to so many people about off topic post
that my ears are bleeding from his incessant rambling about it. Now,
he goes and does what he complains about s much. I'm just calling
him on it thats all. I can't speak for the other responders because i
know of none of them.
--
http://mail.python.org/mailman/listinfo/python-list
--
http://mail.python.org/mailman/listinfo/python-list


RE: type conversion

2009-01-01 Thread Hamish McKenzie

>>  You could also use a dict with type:method key/value pairings. This is 
>> closer to a switch/case than an if...elif chain is.

of course, that's a great idea...

thanks.









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


type conversion

2008-12-31 Thread Hamish McKenzie
sometimes I want to be able to initialize an instance with a variety of 
different data types.

as an obvious example I might want to initialize a 4x4 matrix with either 16 
floats, a list/tuple or 16 floats, another matrix or a quaternion.

is there any other way to do it other than putting case statements in the 
__init__ method of the class, or having a Matrix.FromQuaternion( quat )?

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


python bug when subclassing list?

2008-11-06 Thread Hamish McKenzie
I want to write a Vector class and it makes the most sense to just subclass 
list.  I also want to be able to instantiate a vector using either:

Vector( 1, 2, 3 )
OR
Vector( [1, 2, 3] )


so I have this:

class Vector(list):
  def __new__( cls, *a ):
try:
  print a
  return list.__new__(cls, a)
except:
  print 'broken'
  return list.__new__(cls, list(a))


doing Vector( 1, 2, 3 ) on this class results in a TypeError - which doesn't 
seem to get caught by the try block (ie "broken" never gets printed, and it 
never tries to

I can do pretty much the exact same code but inheriting from tuple instead of 
list and it works fine.

is this a python bug?  or am I doing something wrong?

thanks,
-h.


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


python bug when subclassing list?

2008-11-06 Thread Hamish McKenzie
I want to write a Vector class and it makes the most sense to just subclass 
list.  I also want to be able to instantiate a vector using either:

Vector( 1, 2, 3 )
OR
Vector( [1, 2, 3] )


so I have this:

class Vector(list):
def __new__( cls, *a ):
try:
print a
return list.__new__(cls, a)
except:
print 'broken'
return list.__new__(cls, list(a))


doing Vector( 1, 2, 3 ) on this class results in a TypeError - which doesn't 
seem to get caught by the try block (ie "broken" never gets printed, and it 
never tries to

I can do pretty much the exact same code but inheriting from tuple instead of 
list and it works fine.

is this a python bug?  or am I doing something wrong?

thanks,
-h.
--
http://mail.python.org/mailman/listinfo/python-list


inheritance question...

2008-06-20 Thread Hamish McKenzie

I have this class:

class Vector(object):
 TOL = 1e-5
 def __eq__( self, other, tolerance=TOL ):
   print tolerance


shortened for clarity obviously.  so I want to subclass this class like
so:

class BigVector(Vector)
 TOL = 100


for example if I was working with large vectors which I knew would never
be very close hence the large tolerance.  this doesn't work however -
the TOL class variable, while overridden in BigVector, is still using
the Vector.TOL variable in the __eq__ method.


which kinda makes sense to a certain degree, but how do I get the
behaviour where doing:

BigVector().__eq__( otherVec )


prints 100 instead of 1e-5?

does this question make sense?  not sure how clearly I'm phrasing my
question...  any of you guys python experts?


I *could* do this, but its ugly:

class Vector(object):
 TOL = 1e-5
 def __eq__( self, other, tolerance=None ):
   if tolerance is None: tolerance = self.TOL
   print tolerance


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


str class inheritance prob?

2008-04-16 Thread Hamish McKenzie
so I'm trying to create a class that inherits from str, but I want to
run some code on the value on object init.  this is what I have:

 

 

class Path(str):

def __init__( self, path ):

clean = str(path).replace('\\','/')

while clean.find('//') != -1:

clean = clean.replace('//','/')

 

print 'cleaned on init:\t',clean

self = clean

 

 

so clearly the clean variable is what I want value of the string to be,
but that's decidedly not the case.  so running this:

 

a=Path('path///with\\nasty/crap_in_it/')

print a

 

 

gives me this:

 

cleaned on init:  path/with/nasty/crap_in_it/

path///with\nasty/crap_in_it/

 

 

what gives?  what am I doing wrong, and can I do what I'm trying to
here?

thanks.

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