update __getitem__ and __setitem__ for DataProto #1541
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Checklist Before Starting
I found nothing similar
What does this PR do?
Simplify the usage of DataProto
I modified
__getitem__
and__setitem__
so that we can:(1) use
data['input_ids']
instead ofdata.non_tensor_batch['input_ids']
ordata.batch['input_ids']
if 'input_ids' exists in data's batch, non_tensor_batch or meta_info.(2) update data by
data[1:5] = new_data[2:6]
if there's no extra keys in new_data.Specific Changes
(1) Update
__getitem__
for DataProto(2) Add
__setitem__
for DataProtoAPI
data[key]
for DataProto dataBefore:
data['key']
will raise a ValueErrorAfter: it will returns data.batch['key'] or data.non_tensor_batch['key'] or data.meta_info['key'] if key in any of them.
data[index]=other_data
for DataProto data and other_dataBefore:
data[index]=other_data
will raise an Exception as there's no__setitem__
method for DataProtoAfter: the values in rows of index (which could be a integer, ndarray or slice) will updated by other_data.
Checklist Before Submitting
[BREAKING]
to the PR title if it breaks any API.