@contextlib.contextmanager
def as_handle(handleish, mode='r', **kwargs):
 if isinstance(handleish, basestring):
  if ((sys.version_info[0] >= 3) and ('U' in mode)):
   mode = mode.replace('U', '')
  if ('encoding' in kwargs):
   with codecs.open(handleish, mode, **kwargs) as fp:
    (yield fp)
  else:
   with open(handleish, mode, **kwargs) as fp:
    (yield fp)
 else:
  (yield handleish)

@ contextlib . contextmanager def as_handle ( handleish , mode = 'r' , ** kwargs ) : if isinstance ( handleish , basestring ) : if ( ( sys . version_info [ 0 ] >= 3 ) and ( 'U' in mode ) ) : mode = mode . replace ( 'U' , '' ) if ( 'encoding' in kwargs ) : with codecs . open ( handleish , mode , ** kwargs ) as fp : ( yield fp ) else : with open ( handleish , mode , ** kwargs ) as fp : ( yield fp ) else : ( yield handleish )
context manager to ensure we are using a handle .