Ejemplo n.º 1
0
def test_Serializer():
    data = {
        'field1': 10,
        'field2': 100,
        'field3': 1000,
    }

    s = Serializer()
    tmp = tempfile.NamedTemporaryFile('r+',
                                      suffix='.yaml',
                                      dir='/tmp',
                                      delete=False)
    try:
        s.save_yaml(filename=tmp.name, data=data)
        nose.tools.eq_(data, s.load_yaml(filename=tmp.name))
    finally:
        os.unlink(tmp.name)

    tmp = tempfile.NamedTemporaryFile('r+',
                                      suffix='.cpickle',
                                      dir='/tmp',
                                      delete=False)
    try:
        s.save_cpickle(filename=tmp.name, data=data)
        nose.tools.eq_(data, s.load_cpickle(filename=tmp.name))
    finally:
        os.unlink(tmp.name)

    tmp = tempfile.NamedTemporaryFile('r+',
                                      suffix='.json',
                                      dir='/tmp',
                                      delete=False)
    try:
        s.save_json(filename=tmp.name, data=data)
        nose.tools.eq_(data, s.load_json(filename=tmp.name))
    finally:
        os.unlink(tmp.name)

    tmp = tempfile.NamedTemporaryFile('r+',
                                      suffix='.msgpack',
                                      dir='/tmp',
                                      delete=False)
    try:
        s.save_msgpack(filename=tmp.name, data=data)
        nose.tools.eq_(data, s.load_msgpack(filename=tmp.name))
    finally:
        os.unlink(tmp.name)

    tmp = tempfile.NamedTemporaryFile('r+',
                                      suffix='.marshal',
                                      dir='/tmp',
                                      delete=False)
    try:
        s.save_marshal(filename=tmp.name, data=data)
        nose.tools.eq_(data, s.load_marshal(filename=tmp.name))
    finally:
        os.unlink(tmp.name)
Ejemplo n.º 2
0
    def load(self, filename=None):
        """Load file

        Parameters
        ----------
        filename : str, optional
            File path
            Default value filename given to class constructor

        Raises
        ------
        ImportError:
            Error if file format specific module cannot be imported

        IOError:
            File does not exists or has unknown file format

        Returns
        -------
        self

        """

        if filename:
            self.filename = filename
            self.detect_file_format()
            self.validate_format()

        if self.exists():
            # File exits
            from dcase_util.files import Serializer
            dict.clear(self)

            if self.format == FileFormat.YAML or self.format == FileFormat.META:
                data = Serializer.load_yaml(filename=self.filename)
                dict.update(self, data)

            elif self.format == FileFormat.CPICKLE:
                dict.update(self,
                            Serializer.load_cpickle(filename=self.filename))

            elif self.format == FileFormat.MARSHAL:
                dict.update(self,
                            Serializer.load_marshal(filename=self.filename))

            elif self.format == FileFormat.MSGPACK:
                dict.update(self,
                            Serializer.load_msgpack(filename=self.filename))

            elif self.format == FileFormat.JSON:
                dict.update(self, Serializer.load_json(filename=self.filename))

            elif self.format == FileFormat.TXT:
                with open(self.filename, 'r') as f:
                    lines = f.readlines()
                    dict.update(self, dict(zip(range(0, len(lines)), lines)))

            elif self.format == FileFormat.CSV:
                data = {}
                delimiter = self.delimiter()
                with open(self.filename, 'r') as f:
                    csv_reader = csv.reader(f, delimiter=delimiter)
                    for row in csv_reader:
                        if len(row) == 2:
                            data[row[0]] = row[1]

                dict.update(self, data)

            else:
                message = '{name}: Unknown format [{format}]'.format(
                    name=self.__class__.__name__, format=self.filename)
                self.logger.exception(message)
                raise IOError(message)

        else:
            message = '{name}: File does not exists [{file}]'.format(
                name=self.__class__.__name__, file=self.filename)
            self.logger.exception(message)
            raise IOError(message)

        # Check if after load function is defined, call if found
        if hasattr(self, '_after_load'):
            self._after_load()

        return self