Skip to content

Commit

Permalink
NBT: TAG_List now accepts item assignment using slices.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
codewarrior0 committed Nov 1, 2012
1 parent 30cfb1d commit 3e4a9b8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
22 changes: 13 additions & 9 deletions _nbt.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -273,26 +273,30 @@ 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)

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]
Expand Down
20 changes: 12 additions & 8 deletions nbt.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,22 +456,26 @@ def check_tag(self, value):
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:
Expand Down

0 comments on commit 3e4a9b8

Please sign in to comment.