From 3e4a9b8ef6e7210dc66d6e12df68abc158e1e02b Mon Sep 17 00:00:00 2001 From: David Vierra Date: Wed, 31 Oct 2012 15:16:12 -1000 Subject: [PATCH] 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. --- _nbt.pyx | 22 +++++++++++++--------- nbt.py | 20 ++++++++++++-------- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/_nbt.pyx b/_nbt.pyx index 5eb8cff..9c9e407 100644 --- a/_nbt.pyx +++ b/_nbt.pyx @@ -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] diff --git a/nbt.py b/nbt.py index ddacf16..3e76dad 100644 --- a/nbt.py +++ b/nbt.py @@ -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: