Skip to content
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

New versions of create_root, new_child, etc with attributes and inner text #35

Open
315234 opened this issue Oct 20, 2015 · 7 comments
Open

Comments

@315234
Copy link

315234 commented Oct 20, 2015

This is a feature request to add methods for creating new nodes with an attribute dictionary, like this:

function create_root( xdoc::XMLDocument
                    , name::AbstractString
                    , attrs::Dict{ASCIIString,ASCIIString} )
    xroot = create_root(xdoc, name)
    set_attributes(xroot, attrs)
    xroot
end
function new_child( parent::XMLElement
                  , name::AbstractString
                  , attrs::Dict{ASCIIString,ASCIIString} )
    child = new_child(parent, name)
    set_attributes(child, attrs)
    child
end
function new_child( parent::XMLElement
                  , name::AbstractString
                  , attrs::Dict{ASCIIString,ASCIIString}
                  , text::ASCIIString )
    child = new_child2(parent, name, attrs)
    add_text(child, text)
    child
end

This would remove the need for additional calls to set_attributes or add_text and make the creation of new elements much more streamlined, for example:

xgeom = new_child(xgrid, "Geometry", Dict("Type"=>"VXVYVZ"))
xxdat = new_child(xgeom, "DataItem", Dict("Dimensions"=>"4 2", "Format"=>"HDF", "Precision"=>"8"), xspacedata)
@tkelman
Copy link
Contributor

tkelman commented Oct 20, 2015

Could be additional methods using keyword arguments, I suppose. I'd look over a pull request.

@315234
Copy link
Author

315234 commented Oct 20, 2015

Would there be an issue with having them defined as methods with positional arguments, rather than keyword arguments, and letting dispatch choose the appropriate one?

I will try and submit a pull request if I can figure out how AbstractString and friends are actually supposed to work, and if I can get it working for both 0.3 and 0.4.

@tkelman
Copy link
Contributor

tkelman commented Oct 20, 2015

The issue with positional arguments is whether the particular order is a clear API. Sometimes it's clearer to have named inputs. For example with attributes and text, what if you want to add text but not have any attributes? I also lean towards separating functionality out into smaller, more composable pieces, so I'm not sure what trying to put everything into the same list of arguments for a single function call accomplishes.

@nalimilan
Copy link
Member

Wouldn't it make sense to pass all attributes as keyword arguments, like this?

function create_root(xdoc::XMLDocument,
                     name::AbstractString; # Notice the semicolon
                     attrs...)

@315234 See Compat.jl to get things working on both 0.3 and 0.4.

@tkelman
Copy link
Contributor

tkelman commented Oct 20, 2015

That would only work for assigning to attributes whose names are valid Julia identifiers (so no keywords, no punctuation), but otherwise would be consistent with one of the signatures of set_attributes

@nalimilan
Copy link
Member

Right. There could be an alternative method taking a Dict{AbstractString, AbstractString} for corner cases. (At least it could be added later if somebody requests it.)

@nalimilan
Copy link
Member

Actually, doing f(symbol("/")=>1) works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants