language: support setting slices of data in mutate_dataset

This commit is contained in:
Sebastien Bourdeauducq 2016-06-12 12:56:12 +08:00
parent 3b63c39d2f
commit 7dff446932
2 changed files with 14 additions and 2 deletions

View File

@ -286,7 +286,12 @@ class HasEnvironment:
a given position in a NumPy array) a given position in a NumPy array)
If the dataset was created in broadcast mode, the modification is If the dataset was created in broadcast mode, the modification is
immediately transmitted.""" immediately transmitted.
If the index is a tuple of integers, it is interpreted as
``slice(*index)``.
If the index is a tuple of tuples, each sub-tuple is interpreted
as ``slice(*sub_tuple)`` (multi-dimensional slicing)."""
self.__dataset_mgr.mutate(key, index, value) self.__dataset_mgr.mutate(key, index, value)
def get_dataset(self, key, default=NoDefault): def get_dataset(self, key, default=NoDefault):

View File

@ -1,3 +1,4 @@
from operator import setitem
from collections import OrderedDict from collections import OrderedDict
import importlib import importlib
import logging import logging
@ -194,7 +195,13 @@ class DatasetManager:
target = self.broadcast[key][1] target = self.broadcast[key][1]
if target is None: if target is None:
raise KeyError("Cannot mutate non-existing dataset") raise KeyError("Cannot mutate non-existing dataset")
target[index] = value
if isinstance(index, tuple):
if isinstance(index[0], tuple):
index = tuple(slice(*e) for e in index)
else:
index = slice(*index)
setitem(target, index, value)
def get(self, key): def get(self, key):
if key in self.local: if key in self.local: