@contextmanager
def file_or_url_context(resource_name):
 if is_url(resource_name):
  (_, ext) = os.path.splitext(resource_name)
  try:
   with tempfile.NamedTemporaryFile(delete=False, suffix=ext) as f:
    u = urlopen(resource_name)
    f.write(u.read())
   (yield f.name)
  finally:
   os.remove(f.name)
 else:
  (yield resource_name)

@ contextmanager def file_or_url_context ( resource_name ) : if is_url ( resource_name ) : ( _ , ext ) = os . path . splitext ( resource_name ) try : with tempfile . NamedTemporaryFile ( delete = False , suffix = ext ) as f : u = urlopen ( resource_name ) f . write ( u . read ( ) ) ( yield f . name ) finally : os . remove ( f . name ) else : ( yield resource_name )
yield name of file from the given resource .