|
[yoda-svn] yoda: Reinstate writing to Python file objects (without zipping)YODA Mercurial yoda at projects.hepforge.orgThu May 31 17:15:02 BST 2018
details: https://yoda.hepforge.org/hg/yoda/rev/9f91fa4b7b15 branches: release-1-7 changeset: 1459:9f91fa4b7b15 user: Andy Buckley <andy at insectnation.org> date: Thu May 31 17:04:29 2018 +0100 description: Reinstate writing to Python file objects (without zipping) diffs (truncated from 129 to 50 lines): --- a/pyext/yoda/include/IO.pyx Wed May 23 14:39:32 2018 +0100 +++ b/pyext/yoda/include/IO.pyx Thu May 31 17:04:29 2018 +0100 @@ -58,29 +58,29 @@ # cdef void _make_iss(c.istringstream &iss, string s): # iss.str(s) -# ## Read a file's contents as a returned string -# ## The file argument can either be a file object, filename, or special "-" reference to stdin -# def _str_from_file(file_or_filename): -# if hasattr(file_or_filename, 'read'): -# s = file_or_filename.read() -# elif file_or_filename == "-": -# s = sys.stdin.read() -# else: -# with open(file_or_filename, "r") as f: -# s = f.read() -# return s +## Read a file's contents as a returned string +## The file argument can either be a file object, filename, or special "-" reference to stdin +def _str_from_file(file_or_filename): + if hasattr(file_or_filename, 'read'): + s = file_or_filename.read() + elif file_or_filename == "-": + s = sys.stdin.read() + else: + with open(file_or_filename, "r") as f: + s = f.read() + return s -# ## Write a string to a file -# ## The file argument can either be a file object, filename, or special "-" reference to stdout -# def _str_to_file(s, file_or_filename): -# s = s.decode('utf-8') -# if hasattr(file_or_filename, 'write'): -# file_or_filename.write(s) -# elif file_or_filename == "-": -# sys.stdout.write(s) -# else: -# with open(file_or_filename, "w") as f: -# f.write(s) +## Write a string to a file +## The file argument can either be a file object, filename, or special "-" reference to stdout +def _str_to_file(s, file_or_filename): + s = s.decode('utf-8') + if hasattr(file_or_filename, 'write'): + file_or_filename.write(s) + elif file_or_filename == "-": + sys.stdout.write(s) + else: + with open(file_or_filename, "w") as f:
More information about the yoda-svn mailing list |