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

[WIP] Add stringformat #83

Open
alanjds opened this issue Oct 6, 2018 · 4 comments
Open

[WIP] Add stringformat #83

alanjds opened this issue Oct 6, 2018 · 4 comments
Labels
imported PR Pull Request imported from google/grumpy

Comments

@alanjds
Copy link

alanjds commented Oct 6, 2018

google#185 opened on Jan 21, 2017 by @S-YOU

  • Still finding way to monkey patch properly.
  • Temporarily put the file in pypy, please do not merge.
% make run
import stringformat
print ['{a:5} {} {:d} {:<10}'.format("abcd", 1234, "a", a=42)]
['   42 abcd 1234 a         ']
@alanjds
Copy link
Author

alanjds commented Oct 6, 2018

Comment by trotterdylan
Saturday Jan 21, 2017 at 16:08 GMT


This is cool. I can think of a couple different options for integrating this off the top of my head:

  1. Add some code to the code generator that imports stringformat automatically as part of main()
  2. Support a code generator mode where code will assume it's part of the core grumpy package and include the generated code directly into the runtime build

The nice thing about 2. is that we could do something similar for the __builtin__ module so that we could implement some builtins in Python as has been discussed in some PRs.

The downside is that the code generator gets more complicated and it needs to hardcode a small set of imports that it will pull from sys.modules and not import the associated Go package.

@alanjds
Copy link
Author

alanjds commented Oct 6, 2018

Comment by S-YOU
Sunday Jan 22, 2017 at 05:01 GMT


I guess with 1., we cannot use that feature internally, like in third_party modules, unless manually importing it.

@alanjds
Copy link
Author

alanjds commented Oct 6, 2018

Comment by trotterdylan
Sunday Jan 22, 2017 at 21:42 GMT


I was thinking the code would run before any other Python code, so it should work for third_party modules. But 1. does seem a lot more hacky 2.

There are a couple parts of 2. that would need to be implemented:

a. Special casing some modules in the compiler so that they're pulled directly from sys.modules but no corresponding Go package is imported
b. Supporting a code generation mode where the module's Code object is called something different and grumpy API calls are not prefixed with πg.

I'm still not convinced this is the best approach (there may be others we haven't thought of) but it does seem doable.

@alanjds alanjds added the imported PR Pull Request imported from google/grumpy label Oct 6, 2018
@alanjds
Copy link
Author

alanjds commented Oct 6, 2018

Actual master state:

$ python -c 'print ["""{a:5} {} {:d} {:<10}""".format("abcd", 1234, "a", a=42)]'
['   42 abcd 1234 a         ']
$ grumpy -c 'print ["""{a:5} {} {:d} {:<10}""".format("abcd", 1234, "a", a=42)]'
Traceback (most recent call last):
  File "/Users/alanjds/src/git/grumpy/grumpy-tools-src/__main__.py", line 1, in <module>
AttributeError: 'str' object has no attribute 'format'
exit status 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
imported PR Pull Request imported from google/grumpy
Projects
None yet
Development

No branches or pull requests

1 participant