{"action":"created","issue":{"url":"https://api.github.com/repos/marshallward/f90nml/issues/25","repository_url":"https://api.github.com/repos/marshallward/f90nml","labels_url":"https://api.github.com/repos/marshallward/f90nml/issues/25/labels{/name}","comments_url":"https://api.github.com/repos/marshallward/f90nml/issues/25/comments","events_url":"https://api.github.com/repos/marshallward/f90nml/issues/25/events","html_url":"https://github.com/marshallward/f90nml/issues/25","id":137446788,"number":25,"title":"File or string APIs","user":{"login":"ExpHP","id":1411280,"avatar_url":"https://avatars.githubusercontent.com/u/1411280?v=3","gravatar_id":"","url":"https://api.github.com/users/ExpHP","html_url":"https://github.com/ExpHP","followers_url":"https://api.github.com/users/ExpHP/followers","following_url":"https://api.github.com/users/ExpHP/following{/other_user}","gists_url":"https://api.github.com/users/ExpHP/gists{/gist_id}","starred_url":"https://api.github.com/users/ExpHP/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ExpHP/subscriptions","organizations_url":"https://api.github.com/users/ExpHP/orgs","repos_url":"https://api.github.com/users/ExpHP/repos","events_url":"https://api.github.com/users/ExpHP/events{/privacy}","received_events_url":"https://api.github.com/users/ExpHP/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":6,"created_at":"2016-03-01T02:32:35Z","updated_at":"2016-03-23T14:58:37Z","closed_at":"2016-03-23T14:58:37Z","body":"Feature request:\r\n\r\nCurrently `f90nml` only accepts filenames for its input and output files.  This generally restricts the usage of `f90nml` in comparison to other serialization libraries in Python, which usually have methods for working with arbitrary file-like objects and string data (E.g. `json` and `pickle` have `dump/load` for file-like objects and `dumps/loads` for strings. `xml.etree` has `parse` which accepts both filenames and file-like objects, and `fromstring` for strings.)\r\n\r\nFor some examples of how these APIs are useful:  Currently, it is not possible to parse an NML file directly from `sys.stdin` or to write one to `sys.stdout`, because these are already-open files with no path.  It is similarly difficult to apply multiple patches in a row (whereas with a string api it would just be `for patch in patches: s = f90nml.patchstr(s, patch)`).  Currently I work around these limitations using `NamedTemporaryFile`, which has a fair number of quirks about it and is something I generally consider to be a last resort.\r\n\r\nSome thoughts:\r\n * If it's too much to add both, either one will do.  It's easy for client code to write file-based wrappers around string functions (by calling `f.read()`, `f.write()`), or to write string-based wrappers around file functions (using `StringIO`).\r\n * because there is already `patch` which takes two files, I feel that the API would be cleanest if `read`, `write`, and `patch` were modified to detect if their arguments are paths or a file-like object (e.g. with `hasattr(infile, 'read')`).  This way a user could mix and match, e.g. read from a file object and write to a path. In any case, though, strings will require their own functions (e.g. `readstr(s)`, `writestr(nml)`, and `patchstr(s, patch)` or however you want to name them).\r\n * I suppose there is also the question of whether `patchstr` should return just a string, or if it should return `(string, nml)` (since `patch` returns an NML).\r\n * One slight hurdle is that, unlike a path-based API, a file-based API should not call `close`.  I bring this up because there are several calls to `close()` currently embedded inside the parsing functions."},"comment":{"url":"https://api.github.com/repos/marshallward/f90nml/issues/comments/200382594","html_url":"https://github.com/marshallward/f90nml/issues/25#issuecomment-200382594","issue_url":"https://api.github.com/repos/marshallward/f90nml/issues/25","id":200382594,"user":{"login":"ExpHP","id":1411280,"avatar_url":"https://avatars.githubusercontent.com/u/1411280?v=3","gravatar_id":"","url":"https://api.github.com/users/ExpHP","html_url":"https://github.com/ExpHP","followers_url":"https://api.github.com/users/ExpHP/followers","following_url":"https://api.github.com/users/ExpHP/following{/other_user}","gists_url":"https://api.github.com/users/ExpHP/gists{/gist_id}","starred_url":"https://api.github.com/users/ExpHP/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ExpHP/subscriptions","organizations_url":"https://api.github.com/users/ExpHP/orgs","repos_url":"https://api.github.com/users/ExpHP/repos","events_url":"https://api.github.com/users/ExpHP/events{/privacy}","received_events_url":"https://api.github.com/users/ExpHP/received_events","type":"User","site_admin":false},"created_at":"2016-03-23T14:58:37Z","updated_at":"2016-03-23T14:58:37Z","body":"I'll close this now that v0.18 has file APIs"}}