Beispiel #1
0
    def __iter__(self):

        # prefer implementation using xlutils.view as dates are automatically
        # converted
        if self.use_view:
            from petl.io import xlutils_view
            wb = xlutils_view.View(self.filename)
            if self.sheet is None:
                ws = wb[0]
            else:
                ws = wb[self.sheet]
            for row in ws:
                yield tuple(row)

        else:
            import xlrd
            with xlrd.open_workbook(filename=self.filename,
                                    on_demand=True,
                                    **self.kwargs) as wb:
                if self.sheet is None:
                    ws = wb.sheet_by_index(0)
                elif isinstance(self.sheet, int):
                    ws = wb.sheet_by_index(self.sheet)
                else:
                    ws = wb.sheet_by_name(str(self.sheet))
                for rownum in xrange(ws.nrows):
                    yield tuple(ws.row_values(rownum))
Beispiel #2
0
 def __init__(self, book, sheet, row_slice=None, col_slice=None):
     #: The workbook used by this view.
     self.book = book
     #: The sheet in the workbook used by this view.
     self.sheet = sheet
     for name, source in (('rows', row_slice), ('cols', col_slice)):
         start = 0
         stop = max_n = getattr(self.sheet, 'n' + name)
         if isinstance(source, slice):
             if source.start is not None:
                 start_val = source.start
                 if isinstance(start_val, Index):
                     start_val = start_val.__index__()
                 if start_val < 0:
                     start = max(0, max_n + start_val)
                 elif start_val > 0:
                     start = min(max_n, start_val)
             if source.stop is not None:
                 stop_val = source.stop
                 if isinstance(stop_val, Index):
                     stop_val = stop_val.__index__() + 1
                 if stop_val < 0:
                     stop = max(0, max_n + stop_val)
                 elif stop_val > 0:
                     stop = min(max_n, stop_val)
         setattr(self, name, xrange(start, stop))
Beispiel #3
0
    def __iter__(self):

        # prefer implementation using xlutils.view as dates are automatically
        # converted
        if self.use_view:
            from petl.io import xlutils_view
            wb = xlutils_view.View(self.filename)
            if self.sheet is None:
                ws = wb[0]
            else:
                ws = wb[self.sheet]
            for row in ws:
                yield tuple(row)

        else:
            import xlrd
            with xlrd.open_workbook(filename=self.filename, on_demand=True) as wb:
                if self.sheet is None:
                    ws = wb.sheet_by_index(0)
                elif isinstance(self.sheet, int):
                    ws = wb.sheet_by_index(self.sheet)
                else:
                    ws = wb.sheet_by_name(str(self.sheet))
                for rownum in xrange(ws.nrows):
                    yield tuple(ws.row_values(rownum))
Beispiel #4
0
 def __init__(self, book, sheet, row_slice=None, col_slice=None):
     #: The workbook used by this view.
     self.book = book
     #: The sheet in the workbook used by this view.
     self.sheet = sheet
     for name, source in (('rows', row_slice), ('cols', col_slice)):
         start = 0
         stop = max_n = getattr(self.sheet, 'n'+name)
         if isinstance(source, slice):
             if source.start is not None:
                 start_val = source.start
                 if isinstance(start_val, Index):
                     start_val = start_val.__index__()
                 if start_val < 0:
                     start = max(0, max_n + start_val)
                 elif start_val > 0:
                     start = min(max_n, start_val)
             if source.stop is not None:
                 stop_val = source.stop
                 if isinstance(stop_val, Index):
                     stop_val = stop_val.__index__() + 1
                 if stop_val < 0:
                     stop = max(0, max_n + stop_val)
                 elif stop_val > 0:
                     stop = min(max_n, stop_val)
         setattr(self, name, xrange(start, stop))
Beispiel #5
0
    def __iter__(self):
        nr = self.numrows
        seed = self.seed
        fields = self.fields.copy()

        # N.B., we want this to be stable, i.e., same data each time
        random.seed(seed)

        # construct header row
        hdr = tuple(text_type(f) for f in fields.keys())
        yield hdr

        # construct data rows
        for _ in xrange(nr):
            # artificial delay
            if self.wait:
                time.sleep(self.wait)
            yield tuple(fields[f]() for f in fields)
Beispiel #6
0
    def __iter__(self):
        nr = self.numrows
        seed = self.seed
        fields = self.fields.copy()

        # N.B., we want this to be stable, i.e., same data each time
        random.seed(seed)

        # construct header row
        hdr = tuple(str(f) for f in fields.keys())
        yield hdr

        # construct data rows
        for _ in xrange(nr):
            # artificial delay
            if self.wait:
                time.sleep(self.wait)
            yield tuple(fields[f]() for f in fields)
Beispiel #7
0
    def __iter__(self):

        nf = self.numflds
        nr = self.numrows
        seed = self.seed

        # N.B., we want this to be stable, i.e., same data each time
        random.seed(seed)

        # construct fields
        flds = ['f%s' % n for n in range(nf)]
        yield tuple(flds)

        # construct data rows
        for _ in xrange(nr):
            # artificial delay
            if self.wait:
                time.sleep(self.wait)
            yield tuple(random.random() for n in range(nf))
Beispiel #8
0
    def __iter__(self):

        nf = self.numflds
        nr = self.numrows
        seed = self.seed

        # N.B., we want this to be stable, i.e., same data each time
        random.seed(seed)

        # construct fields
        flds = ['f%s' % n for n in range(nf)]
        yield tuple(flds)

        # construct data rows
        for _ in xrange(nr):
            # artificial delay
            if self.wait:
                time.sleep(self.wait)
            yield tuple(random.random() for n in range(nf))