NBT: TAG_List now accepts item assignment using slices.

e.g. `list_tag[:] = [nbt.TAG_Double(3), nbt.TAG_Double(5)]`

Also, changed the names of a few method args to be more consistent between implementations.
This commit is contained in:
David Vierra 2012-10-31 15:16:12 -10:00
parent 30cfb1d380
commit 3e4a9b8ef6
2 changed files with 25 additions and 17 deletions

View File

@ -273,12 +273,16 @@ cdef class _TAG_List(TAG_Value):
raise TypeError("Invalid type %s for TAG_List(%s)" % (value.__class__, tag_classes[self.list_type]))
# --- collection methods ---
def __getitem__(self, key):
return self.value[key]
def __getitem__(self, index):
return self.value[index]
def __setitem__(self, key, val):
self.check_tag(val)
self.value[key] = val
def __setitem__(self, index, value):
if isinstance(index, slice):
for tag in value:
self.check_tag(tag)
else:
self.check_tag(value)
self.value[index] = value
def __iter__(self):
return iter(self.value)
@ -286,13 +290,13 @@ cdef class _TAG_List(TAG_Value):
def __len__(self):
return len(self.value)
def insert(self, idx, val):
def insert(self, index, tag):
if len(self.value) == 0:
self.list_type = val.tagID
self.list_type = tag.tagID
else:
self.check_tag(val)
self.check_tag(tag)
self.value.insert(idx, val)
self.value.insert(index, tag)
def __delitem__(self, key):
del self.value[key]

20
nbt.py
View File

@ -456,22 +456,26 @@ class TAG_List(TAG_Value, collections.MutableSequence):
def __iter__(self):
return iter(self.value)
def __contains__(self, key):
return key in self.value
def __contains__(self, tag):
return tag in self.value
def __getitem__(self, i):
return self.value[i]
def __getitem__(self, index):
return self.value[index]
def __len__(self):
return len(self.value)
def __setitem__(self, index, value):
self.check_tag(value)
value.name = ""
if isinstance(index, slice):
for tag in value:
self.check_tag(tag)
else:
self.check_tag(value)
self.value[index] = value
def __delitem__(self, i):
del self.value[i]
def __delitem__(self, index):
del self.value[index]
def insert(self, index, value):
if len(self) == 0: