def __init__(self, iterable, *, shared_memory_context): self.shared_memory_context = shared_memory_context if isinstance(iterable, (list, tuple)): # Skip creation of short-lived duplicate list. shared_array = SharedNDArray.copy(np.array(iterable)) else: shared_array = SharedNDArray.copy(np.array(list(iterable))) self.allocated_size = -1 # TODO: overallocate more than needed self.replace_held_shared_array(shared_array, destroy_old=False)
def insert(self, position, value): arr_of_one = np.array([value], dtype=self.array.dtype) shared_array = SharedNDArray.copy( np.concatenate( [self.array[:position], arr_of_one, self.array[position:]])) self.allocated_size = -1 # TODO: overallocate more than needed self.replace_held_shared_array(shared_array)
def pop(self, position=None): # TODO: implement use of position retval = self.array[-1] # TODO: fix problem when shrinking to length of 0 shared_array = SharedNDArray.copy(self.array[:-1]) self.allocated_size = -1 # TODO: overallocate more than needed self.replace_held_shared_array(shared_array) return retval
def remove(self, value): position = self.index(value) shared_array = SharedNDArray.copy( np.concatenate([self.array[:position], self.array[position + 1:]])) self.allocated_size = -1 # TODO: overallocate more than needed self.replace_held_shared_array(shared_array)
def extend(self, values): arr_of_multiple = np.array(values, dtype=self.array.dtype) shared_array = SharedNDArray.copy( np.concatenate([self.array, arr_of_multiple])) self.allocated_size = -1 # TODO: overallocate more than needed self.replace_held_shared_array(shared_array)
def clear(self): shared_array = SharedNDArray.copy(np.array([], dtype=self.array.dtype)) self.allocated_size = -1 # TODO: overallocate more than needed self.replace_held_shared_array(shared_array)
def append(self, value): arr_of_one = np.array([value], dtype=self.array.dtype) shared_array = SharedNDArray.copy( np.concatenate([self.array, arr_of_one])) self.allocated_size = -1 # TODO: overallocate more than needed self.replace_held_shared_array(shared_array)
def attach_object(self): "Attach to existing object in shared memory segment." self.shared_array = SharedNDArray(*(self._callmethod('_getstate')))