Ejemplo n.º 1
0
    def to_records(self, custom_headers=None):
        """
        Make an array of dictionaries

        It takes the first row as keys and the rest of
        the rows as values. Then zips keys and row values
        per each row. This is particularly helpful for
        database operations.
        """
        if len(self.colnames) > 0:
            if custom_headers:
                headers = custom_headers
            else:
                headers = self.colnames
            for row in self.rows():
                the_dict = compact.OrderedDict(zip(headers, row))
                yield the_dict

        elif len(self.rownames) > 0:
            if custom_headers:
                headers = custom_headers
            else:
                headers = self.rownames
            for column in self.columns():
                the_dict = compact.OrderedDict(zip(headers, column))
                yield the_dict

        else:
            raise ValueError(constants.MESSAGE_DATA_ERROR_NO_SERIES)
Ejemplo n.º 2
0
 def to_dict(self, row=False):
     """Returns a dictionary"""
     the_dict = compact.OrderedDict()
     if len(self.colnames) > 0 and row is False:
         for column in self.named_columns():
             the_dict.update(column)
     elif len(self.rownames) > 0:
         for row in self.named_rows():
             the_dict.update(row)
     else:
         raise NotImplementedError("Not implemented")
     return the_dict
Ejemplo n.º 3
0
    def __init__(self, sheets=None, filename="memory", path=None):
        """
        Book constructor

        Selecting a specific book according to filename extension

        :param sheets: a dictionary of data
        :param filename: the physical file
        :param path: the relative path or absolute path
        :param keywords: additional parameters to be passed on
        """
        self.__path = None
        self.__name_array = []
        self.filename = None
        self.__sheets = compact.OrderedDict()
        self.init(sheets=sheets, filename=filename, path=path)
Ejemplo n.º 4
0
    def load_from_sheets(self, sheets):
        """
        Load content from existing sheets

        :param dict sheets: a dictionary of sheets. Each sheet is
        a list of lists
        """
        self.__sheets = compact.OrderedDict()
        if sheets is None:
            return
        keys = sheets.keys()
        if not isinstance(sheets, compact.OrderedDict):
            # if the end user does not care about the order
            # we put alphatical order
            keys = sorted(keys)
        for name in keys:
            sheet = Sheet(sheets[name], name)
            # this sheets keep sheet order
            self.__sheets.update({name: sheet})
            # this provide the convenience of access the sheet
            self.__dict__[name] = sheet
        self.__name_array = list(self.__sheets.keys())
Ejemplo n.º 5
0
 def to_dict(self):
     """Convert the book to a dictionary"""
     the_dict = compact.OrderedDict()
     for sheet in self:
         the_dict.update({sheet.name: sheet.array})
     return the_dict