def grok_data(self):
     # XXX when data is a csv/txt/xl file, we want to read timestamps in
     # there too
     # XXX hooks won't catch change to timestamps
     if super(NPTSImportAdapter, self).grok_data():
         return  # already processed
     numpy_array = self.grok_timestamps()
     tstamp_data = Binary()
     compressed_data = zlib.compress(pickle.dumps(numpy_array, protocol=2))
     tstamp_data.write(compressed_data)
     self.entity.cw_edited['timestamps'] = tstamp_data
     self.entity.timestamps_array = numpy_array
Example #2
0
 def test_write(self):
     b = Binary()
     b.write(b'toto')
     b.write(bytearray(b'toto'))
     b.write(memoryview(b'toto'))
     with self.assertRaises((AssertionError, TypeError)):
         # TypeError is raised by BytesIO if python runs with -O
         b.write(u'toto')
    def grok_data(self):
        """ self.data is something such as an excel file or CSV data or a pickled
        numpy array or an already processed binary.

        Ensure it's a pickle numpy array before storing object in db.

        If data seems to be already processed, return True, else return False.
        """
        entity = self.entity
        try:
            filename = entity.data.filename.lower()
        except AttributeError:
            data = entity.data
            if isinstance(data, Binary):
                return True
            # if not isinstance(data, numpy.ndarray):
            #     raise TypeError('data is neither a Binary nor a numpy array (%s)' % type(data))
            numpy_array = data
        else:
            adapter = self._cw.vreg['adapters'].select_or_none(
                'source_to_numpy_array',
                self._cw,
                entity=entity,
                filename=filename)
            if adapter is None:
                msg = self._cw._(
                    'Unsupported file type %s') % entity.data.filename
                raise ValidationError(entity.eid, {'data': msg})
            numpy_array = adapter.to_numpy_array(entity.data, filename)

        if numpy_array.ndim != 1:
            raise ValidationError(
                entity.eid, {'data': _('data must be a 1-dimensional array')})
        if numpy_array.size == 0:
            raise ValidationError(
                entity.eid, {'data': _('data must have at least one value')})
        data = Binary()
        compressed_data = zlib.compress(pickle.dumps(numpy_array, protocol=2))
        data.write(compressed_data)
        entity.cw_edited['data'] = data
        entity.array = numpy_array
        return False