-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Consistency of read() and write() functions. #14608
Comments
Thanks. I wonder whether |
Thanks @samoconnor, this is a very good design review. I still kind of like the idea of |
@JeffBezanson, |
Following up on what I proposed above, the new methods would be... read(io::IO) = readbytes(io)
readstring(io::IO) = readall(io)
write(filename::AbstractString, d...) = open(io->write(io, d...), filename, "w")
read!(filename::AbstractString, a) = open(io->read!(io, a), filename)
read(filename::AbstractString, t...) = open(io->read(io, t...), filename)
readuntil(filename::AbstractString, c...) = open(io->readuntil(io, c...), filename)
readline(filename::AbstractString) = open(readline, filename)
readlines(filename::AbstractString) = open(readlines, filename)
readstring(filename::AbstractString) = open(readstring, filename) Unless anyone objects, I'll close #14546 and open a new PR with these methods. |
|
Yes The "core" output functions seem to be those that write data to a stream at the current position. From there, I often want to either write a whole file in one go (like you), or get the output as a string or byte vector. I feel like there should be a standard way to add those behaviors, so that you don't need to rely on every I/O function adding a "whole file" method.
|
I think the abstractions are great as a tool for keeping the API implementation simple and efficient, but I think the API should have concrete functions that let you tell the computer to do what you want it to. As it stands if I say "Computer, put my data in a .CSV file", it says "done!", Another thing, it occurs to me that part of the unnecisary verboseness of |
What about merging create(filename::String) = open(filename, "w+")
create(f::Function, filename::String) = open(f, filename, "w+")
create(filename::String, f::Function, data...) = create(io->f(io, data...), filename)
write(filename::String, data...) = create(filename, write, data...)
create("/tmp/foo.csv", writecsv, results)
create("/tmp/foo.png", write, plot(results)) But I think I still prefer to do... writecsv("/tmp/foo.csv", results)
write("/tmp/foo.png", plot(results)) |
All good points. I agree the I agree a certain set of convenience functions is unavoidable. I'm just a bit leery of expecting every I/O function
|
If we had some sort of scoped open/close construct this would be sufficiently concise that it might not even be necessary to define the |
replace readall() with readstring() replace readbytes() with read() readbytes! returns nb whereas read! returns array, so they can't trivially be merged. Fix test/replutil.jl test that matches output of method error message that has changed due to introduction of readbytes(::AbstractString).
Any opinions about whether This would give a much more consistent API. More generally, this would mean |
Many of the specify-an-output-type API's take the type first, or maybe second after the output array for in-place methods? We've got a few possibly-overlapping conventions for first arguments. |
Indeed, this is similar to the discussion at #14412. Given the conclusion there, I would say it's better to keep the output type as second argument here too. |
replace readall() with readstring() replace readbytes() with read() readbytes! returns nb whereas read! returns array, so they can't trivially be merged. Fix test/replutil.jl test that matches output of method error message that has changed due to introduction of readbytes(::AbstractString). add: +eachline(filename::AbstractString) = EachLine(open(stream), close)
I like the idea of specifying the output type as a type rather than encoding it in the function names. |
replace readall() with readstring() replace readbytes() with read() readbytes! returns nb whereas read! returns array, so they can't trivially be merged. Fix test/replutil.jl test that matches output of method error message that has changed due to introduction of readbytes(::AbstractString). add: +eachline(filename::AbstractString) = EachLine(open(stream), close)
replace readall() with readstring() replace readbytes() with read() readbytes! returns nb whereas read! returns array, so they can't trivially be merged. Fix test/replutil.jl test that matches output of method error message that has changed due to introduction of readbytes(::AbstractString). add: +eachline(filename::AbstractString) = EachLine(open(stream), close) revert mistaken docstring change -- read! vs readbytes! update base/docs/helpdb/Base.jl update rst docs, BBEditTextWrangler-julia.plist replace missing chomp() in getmetabranch() doc tweaks make && make julia-genstdlib more doc tweaks whitespace tweak doc: read!(stream or filename, ...)
Consistency of read() and write() functions (per #14608)
Now that #14699 and #14660 are merged, most of what this issue addresses is resolved. A quick scan of the comment history yields the following loose ends...
I'll leave it to others to create new issues for the other items as they see fit. |
Actually, I think it can be changed immediately to |
Below is review of current
write
anread
variants inBase
.I propose:
readbytes
andreadall
, replace withread
andreadstring
respectivelyRationale:
write
is notwritebytes
soreadbytes
should just beread
.readall
is named as "read_[how much]" but behaves like "read[as type]_" so it should bereadstring
.Deprecation and addition indicated by
strikethroughandbold
below.Filename = has
read(filename::AbstractString...)
method.write
writecsv
writedlm
writemime
read(io,T)
T
readavailable
Array{UInt8}
readuntil
String
readline
String
readall
String
readbytes
Array{UInt8}
read
Array{UInt8}
readstring
String
readlines
Array{String}
readcsv
Array{T}
readdlm
Array{T}
readdir
Array{String}
readlink
String
readchomp(x)
= chomp(readall(x))
Related:
14546 write(filename::AbstractString, data)
The text was updated successfully, but these errors were encountered: