0

I'm using third-party modules and figting with error raised while calling those modules. Here is what compiler is showing:

C:\Users\Dmitry\AppData\Local\Enthought\Canopy\edm\envs\User\lib\site-    packages\backtrader\feeds\csvgeneric.py in _loadline(self, linetokens)
    148                 # get it from the token
    149                 csvfield = linetokens[csvidx]
--> 150                 print(csvidx)
    151 
    152             if csvfield == '':
IndexError: list index out of range

I deliberately added print(csvidx) to see the value of csvidx, but it's not showing up on console. What am I doing wrong? Thanks a lot.

Here is the code:

  def _loadline(self, linetokens):
        # Datetime needs special treatment
        dtfield = linetokens[self.p.datetime]
        if self._dtstr:
            dtformat = self.p.dtformat

            if self.p.time >= 0:
                # add time value and format if it's in a separate field
                dtfield += 'T' + linetokens[self.p.time]
                dtformat += 'T' + self.p.tmformat

            dt = datetime.strptime(dtfield, dtformat)
        else:
            dt = self._dtconvert(dtfield)

        if self.p.timeframe >= TimeFrame.Days:
            # check if the expected end of session is larger than parsed
            if self._tzinput:
                dtin = self._tzinput.localize(dt)  # pytz compatible-ized
            else:
                dtin = dt

            dtnum = date2num(dtin)  # utc'ize

            dteos = datetime.combine(dt.date(), self.p.sessionend)
            dteosnum = self.date2num(dteos)  # utc'ize

            if dteosnum > dtnum:
                self.lines.datetime[0] = dteosnum
            else:
                # Avoid reconversion if already converted dtin == dt
                self.l.datetime[0] = date2num(dt) if self._tzinput else dtnum
        else:
            self.lines.datetime[0] = date2num(dt)

        # The rest of the fields can be done with the same procedure
        for linefield in (x for x in self.getlinealiases() if x != 'datetime'):
            # Get the index created from the passed params
            csvidx = getattr(self.params, linefield)

            if csvidx is None or csvidx < 0:
                # the field will not be present, assignt the "nullvalue"
                csvfield = self.p.nullvalue
            else:
                # get it from the token
                print(csvidx)
                csvfield = linetokens[csvidx]


            if csvfield == '':
                # if empty ... assign the "nullvalue"
                csvfield = self.p.nullvalue

            # get the corresponding line reference and set the value
            line = getattr(self.lines, linefield)
            line[0] = float(float(csvfield))

        return True
  • 1
    Try to `print(csvidx)` before defining `csvfield`. – FabienP Oct 26 '17 at 21:13
  • Fabien, thanks for taking care of me. But no, the same result. `C:\Users\Dmitry\AppData\Local\Enthought\Canopy\edm\envs\User\lib\site-packages\backtrader\feeds\csvgeneric.py in _loadline(self, linetokens) 147 else: 148 # get it from the token --> 149 print(csvidx) 150 csvfield = linetokens[csvidx] 151 IndexError: list index out of range `. – Ирина Павлова Oct 30 '17 at 21:03
  • Then could you show the code that ends with this error? It would help to see what are `csvfiled`, `linetokens` and `csvidx` (and how they are defined). – FabienP Oct 30 '17 at 22:29
  • This is really what debuggers like `pdb` are for. I often find [remote_pdb](https://pypi.python.org/pypi/remote-pdb) useful in situations where the application interferes with console output. – larsks Nov 01 '17 at 21:04

1 Answers1

0
        csvidx = getattr(self.params, linefield)

        if csvidx is None or csvidx < 0:
            # the field will not be present, assignt the "nullvalue"
            csvfield = self.p.nullvalue
        else:
            # get it from the token
            print(csvidx)
            csvfield = linetokens[csvidx]

csvidx is being sought in self.params and it's obviously being found.

And it seems to be neither None nor < 0, so it seems to have a numeric value >= 0

The IndexError: list index out of range is clearly indicating that linetokens doesn't contain as many items as csvidxexpects.

Since the name self.params seems to indicate user input, it would seem that whatever value you have given is greater than the actual number of tokens available in linetokens

The code seems to be executed in one of those canned Python environments

--> 150                 print(csvidx)

Because that's for sure not the usual Python console output. If that canned environment (and not the 3rd party packages) really allows you to print to the console, it would seem advisable to actually do it a lot sooner, as in:

    for linefield in (x for x in self.getlinealiases() if x != 'datetime'):
        # Get the index created from the passed params
        csvidx = getattr(self.params, linefield)
        print('linefield {} -> csvidx {}'.format(linefield, csvidx)

        if csvidx is None or csvidx < 0:
            # the field will not be present, assignt the "nullvalue"
            csvfield = self.p.nullvalue
        else:
            # get it from the token
            csvfield = linetokens[csvidx]

You should see each relationship linefield -> csvidx before the exception is triggered.

If your environment allows it, run everything with python -u which uses unbuffered output. (Strongly recommended under Windows where flushing on newline is known to either not work or have problems)

mementum
  • 3,153
  • 13
  • 20
  • Thank you very much for your input. Unfortunately your approach to print() not working either. Seems like console is being affected somehow by this application. It is canned environment (Canopy) but switching to original didn't change a thing for me =( Anyway thank you very much for such detailed answer. – Ирина Павлова Nov 05 '17 at 19:33
  • You can find messages that invite the developer to manually *flush* the output when running under Canopy. See: https://stackoverflow.com/questions/18709659/enthought-canopy-doesnt-print-right-away-when-statement-occurs – mementum Nov 06 '17 at 14:57