1

I am trying to use finviz package (written for python 3) in my codes in Python 2. When importing I get SyntaxError:

    values = f'tickers: {tuple(self._tickers)}\n' \
                                                ^
SyntaxError: invalid syntax

The code in question looks like this in the library:

def __repr__(self):
    """ Returns a string representation of the parameter's values. """

    values = f'tickers: {tuple(self._tickers)}\n' \
             f'filters: {tuple(self._filters)}\n' \
             f'rows: {self._rows}\n' \
             f'order: {self._order}\n' \
             f'signal: {self._signal}\n' \
             f'table: {self._table}\n' \
             f'table: {self._custom}'

    return values

finviz's API is here and this part is in screener.py

3
  • 2
    Why are you writing new code for Python 2? It's going to be end-of-life as of the end of this year; no patches, even for security. Can you write everything in Python 3 and avoid this? 3to2 is a thing, but a package designed purely for Python 3 is unlikely to backport to Python 2 without some manual intervention. Point is, even if we solved this specific problem for you, the code is likely littered with other Python 3-only things (some of which won't be so obvious as to trigger SyntaxErrors, they might just silently misbehave). Commented May 22, 2019 at 0:23
  • I already have many codes in Python 2 and for now I am testing few things and not ready to migrate everything to Python 3. I tried 3to2 with no success. Commented May 22, 2019 at 0:27
  • Okay, but what I'm trying to tell you is that migrating 3.x-only packages back to Python 2 is usually going to be at least as much, if not more work, than migrating 2.x packages to Python 3. SyntaxErrors are obvious; subtleties in library usage, exception hierarchies, text/binary mismatches, etc. may not be caught at all, and spending your effort backporting to Python 2 rather than porting forward to Python 3 is a waste of limited resources. Either port to Python 3, or don't use finviz until you do. Commented May 22, 2019 at 0:54

2 Answers 2

2

Python's f strings weren't introduced until 3.6.

A 2.x compatible way of interpolating strings with similar syntax is using str.format().

s = '{greeting}, {place}!'.format(greeting='Hello', place='World')
print s
>>> 'Hello, World!'

Or, here's what str.format might look like with your example:

values = ('tickers: {tickers}\n'
          'filters: {filters}\n'
          'rows: {rows}\n'
          'order: {order}\n'
          'signal: {signal}\n'
          'table: {table}\n'
          'table: {custom}'
)

values.format(
    tickers=tuple(self._tickers),
    filters=tuple(self._filters),
    rows=self._rows,
    order=self._order,
    signal=self._signal,
    table=self._table,
    custom=self._custom,
)

Here's the documentation for str.format()

Sign up to request clarification or add additional context in comments.

Comments

2

You can replace f-strings with format like so:

f'{x} {y}' == '{}, {}!'.format(x, y)

But please don't! Python 2's life is over. It will receive no more updates, even for security, after 2020. It's better just to rewrite whatever you have in Python 2 to Python 3.

1 Comment

It was useful to know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.