Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

the f"" prefix is not supported (python3.6) #1239

Closed
philiparvidsson opened this issue Feb 26, 2017 · 8 comments · Fixed by #1749
Closed

the f"" prefix is not supported (python3.6) #1239

philiparvidsson opened this issue Feb 26, 2017 · 8 comments · Fixed by #1749
Labels

Comments

@philiparvidsson
Copy link

if the backend is python 3.6, the f prefix for strings should be supported. see this for more information.

example:

>>> name = "Fred"
>>> f"He said his name is {name}."
'He said his name is Fred.'
>>> width = 10
>>> precision = 4
>>> value = decimal.Decimal("12.34567")
>>> f"result: {value:{width}.{precision}}"  # nested fields
'result:      12.35'
(setv my-var "hello world")
(print f"my-var contains: {my-var}")
@philiparvidsson philiparvidsson changed the title the f"" prefix is not supported the f"" prefix is not supported (python3.6) Feb 26, 2017
@refi64
Copy link
Contributor

refi64 commented Feb 26, 2017

It would really be nice if Hy supported this other Python versions, too...it seems like it wouldn't he too hard to compile...

@Kodiologist
Copy link
Member

It seems that the replacement can be most any Python expression (possibly followed by format specifiers):

>>> f"{(lambda p: p + 'b')('a') if 1 else 2 !r:5}"
"'ab' "

For Hy, It would perhaps make more sense to allow a Hy expression instead of a Python expression.

@philiparvidsson
Copy link
Author

yes, we definitely want to allow hy expressions:

(setv a 3 b 3)
(print f"the sum of {a} and {b} is {(+ a b)}")

I think we have the advantage of being to bring this feature back to python 2.7 (by generating old code), ie:

a = 2
b = 3
print "the sum of {} and {} is {}".format(a, b, a + b)

it's very close to the code that needs to be generated for python 3.6:

a = 2
b = 2
print(f"the sum of {a} and {b} is {a + b}")

that is, we just insert the generated code in the {} in the string and replace the .format call with an f prefix.

@gilch
Copy link
Member

gilch commented Feb 28, 2017

The main advantage of f-strings seems to be the format specifiers. I'm not sure how that should look in Hy. If you don't need that part, you can get pretty close with a map and join.

=> (setv a 2  b 3)
a = 2
b = 3
(a, b)
(2, 3)
=> (defn f [&rest xs] (.join "" (map str xs)))
from hy.core.language import map

def f(*xs):
    return ''.join(map(str, xs))
=> (f "the sum of "a" and "b" is "(+ a b)"")
f('the sum of ', a, ' and ', b, ' is ', (a + b), '')
'the sum of 2 and 3 is 5'

It doesn't look quite as nice in the Python translation, but HyExpressions don't use comma separators.

@Kodiologist
Copy link
Member

I figured that f"{1 + 1 !r:5}" would be written in Hy as f"{(+ 1 1) !r:5}" or something.

@refi64
Copy link
Contributor

refi64 commented Mar 4, 2017

FWIW I've started work in this, but it'll be a while before it's done...

@Kodiologist
Copy link
Member

@kirbyfan64 You might try implementing only forms like {foo !r:5} first, where the expression has to be an identifier, since that could be easier to implement but will cover most uses.

@refi64 refi64 added the feature label Mar 13, 2017
@Kodiologist
Copy link
Member

Kodiologist commented Jul 1, 2017

It occurs to me that you could get the same effect on any version of Python with a pretty simple macro:

(deftag f [string-literal]
  (import re)
  (setv exprs [])
  (setv string-literal (re.sub :flags re.VERBOSE
    r"\{
      (?P<expr> [^}!:]+)
      (?P<suffix> [!:] [^}]+)?
      \}"
    (fn [m]
      (.append exprs (read-str (.group m "expr")))
      (+ "{" (or (.group m "suffix") "") "}"))
    string-literal))
  `(.format ~string-literal ~@exprs))

(setv a 3   b 2000)
(print #f"The sum of {a} and {b} is {(+ a b):,}.")

It would take work to make this permit things like nested fields and backslash-escaped :s and !s, but it could probably be done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants