def mlab_tempfile(dir=None):
 valid_name = re.compile(u'^\\w+$')
 for n in range(100):
  f = tempfile.NamedTemporaryFile(suffix=u'.m', prefix=u'tmp_matlab_', dir=dir)
  fname = os.path.splitext(os.path.basename(f.name))[0]
  if valid_name.match(fname):
   break
  f.close()
 else:
  raise ValueError(u'Could not make temp file after 100 tries')
 return f

def mlab_tempfile ( dir = None ) : valid_name = re . compile ( u'^\\w+$' ) for n in range ( 100 ) : f = tempfile . NamedTemporaryFile ( suffix = u'.m' , prefix = u'tmp_matlab_' , dir = dir ) fname = os . path . splitext ( os . path . basename ( f . name ) ) [ 0 ] if valid_name . match ( fname ) : break f . close ( ) else : raise ValueError ( u'Could not make temp file after 100 tries' ) return f
returns a temporary file-like object with valid matlab name .