Пример #1
0
 def __init__(self, dim, data=None, fill=None, lazy=True):
     VLockable.__init__(self)
     if len(dim) < 1:
         raise TypeError('Minimum 1 dimension required')
     for d in dim:
         if d <= 0:
             raise TypeError('Each dimension must be >0')
     self._dim = tuple(dim)
     mul, m = [], 1
     for d in dim:
         mul.append(m)
         m *= d
     _len = m
     self.__mul = tuple(mul)
     if data is None:
         fill = VEntity._v_lazy(fill)
         self._data = [fill] * (_len)
     else:
         if isinstance(data, VTuple):
             self._data = list(e for e in data)
         else:
             if lazy:
                 _lazy = VEntity._v_lazy
                 self._data = self._val_type()(_lazy(e) for e in data)
             else:
                 self._data = self._val_type()(iter(data))
                 for e in self._data:
                     if not isinstance(e, VEntity):
                         raise TypeError('Invalid element type')
         if len(self._data) != _len:
             raise TypeError('Data has incorrect dimensions')
Пример #2
0
    def __getitem__(self, key):
        """Gets a dictionary value.

        :param key: dictionary key
        :type  key: :class:`versile.orb.entity.VEntity` or lazy-convertible
        :returns:   dictionary value for key
        :rtype:     :class:`versile.orb.entity.VEntity`
        :raises:    :exc:`exceptions.KeyError`

        """
        with self:
            key = VEntity._v_lazy(key)
            return self.__value[key]
Пример #3
0
 def _v_convert_for_send(cls, result):
     """Parse 'result' for sending, into VEntity-compliant format."""
     try:
         return VEntity._v_lazy(result)
     except:
         if isinstance(result, Exception):
             if isinstance(result, VException):
                 return result
             # Try to up-convert local exception types
             args = [cls._v_convert_for_send(e) for e in result.args]
             name = cls._v_python_exc._CONVERSION.get(type(result), None)
             if name:
                 return cls._v_python_exc(name, *args)
             # If this failed, generate a non-standard VPythonException
             rcls = result.__class__
             e_name = '%s.%s' % (rcls.__module__, rcls.__name__)
             return cls._v_python_exc(e_name, *args)
         else:
             return cls(result)
     else:
         return result
Пример #4
0
    def __setitem__(self, index, value):
        """Sets an element of the array.

        :param index: array index
        :type  index: (int,)
        :param value: element value
        :type  value: :class:`versile.orb.entity.VEntity` or lazy-convertible
        :raises:      :exc:`exceptions.TypeError`\ ,
                      :exc:`exceptions.IndexError`

        """
        with self:
            if len(index) != len(self._dim):
                raise IndexError('Index has incorrect length')
            for i, d in zip(index, self._dim):
                if not 0 <= i < d:
                    raise IndexError('Index out of range')
            if not isinstance(value, VEntity):
                value = VEntity._v_lazy(value)
            pos = 0
            for i, m in zip(index, self.__mul):
                pos += i * m
            self._data[pos] = value