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

[ENHANCEMENT] Interface for adding multiple tags at a time #29

Open
mtfishman opened this issue Feb 10, 2025 · 2 comments
Open

[ENHANCEMENT] Interface for adding multiple tags at a time #29

mtfishman opened this issue Feb 10, 2025 · 2 comments
Labels
enhancement New feature or request

Comments

@mtfishman
Copy link
Member

Currently with the new "named tags" design based on a dictionary, you can add or update a tag using its name with:

i = Index(2)
j = settag(i, "site", "1")

and delete a tag using its name with:

k = unsettag(j, "site")
k == i # true

It might be nice to have a syntax for adding multiple tags at once. Currently, settags is used as an internal function for setting all the tags at once, with existing ones getting removed:

i = Index(2)
j = settag(i, "site", "1")
k = settags(j, Dict(["link" => "3"])) # This is currently only defined in `IndexName` but this is for demonstration purposes
hastag(k, "site") # false

so it is meant to be low-level, and I think it is a good name for that.

Maybe we can use the syntax mergetags, i.e.:

i = Index(2)
j = settag(i, "site", "1")
k = mergetags(j, Dict(["link" => "3"]))
gettag(k, "site") == "1" # true
gettag(k, "link") == "3" # true

following the usage of Base.merge on Base.Dict:

julia> merge(Dict("foo" => 0.0, "bar" => 42.0), Dict("baz" => 17, "bar" => 4711))
Dict{String, Float64} with 3 entries:
  "bar" => 4711.0
  "baz" => 17.0
  "foo" => 0.0
@mtfishman mtfishman added the enhancement New feature or request label Feb 10, 2025
@mtfishman
Copy link
Member Author

My current thoughts on this is that we should define settags to set multiple tags, while preserving existing tags:

i = Index(2)
i = settag(i, "x", "xtag")

# Verbose interface
i = settags(i, Dict(["site" => "2", "sitetype" => "S=1/2"]))
gettag(i, "x") == "xtag" # true
gettag(i, "site") == "2" # true
gettag(i, "sitetype") == "S=1/2" # true

# Possible shorthands (inspired by Dict constructor notation)
i = settags(i, ["site" => 2, "sitetype" => "S=1/2"])
i = settags(i, "site" => 2, "sitetype" => "S=1/2")

Then, we can have a lower level internal function resettags that removes existing tags and sets the tags to the ones that are input:

i = Index(2)
i = settag(i, "x", "xtag")
i = resettags(i, Dict(["site" => "2", "sitetype" => "S=1/2"]))
hastag(i, "x") # false
gettag(i, "site") == "2" # true
gettag(i, "sitetype") == "S=1/2" # true

resettags(i) could be the interface for removing all tags.

@emstoudenmire
Copy link

The second proposal would make sense to me. I think the idea would be that settags could semantically mean something similar to how setindex! works for a dictionary, where it creates new entries but does not delete different pre-existing entries in the dictionary. Then resettags would more explicitly warn the user that other pre-existing tags will be deleted unless they are specified.

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

No branches or pull requests

2 participants