Skip to content
This repository has been archived by the owner on May 31, 2020. It is now read-only.

Extend Builtin Format function #816

Merged
merged 26 commits into from
Apr 25, 2020
Merged

Extend Builtin Format function #816

merged 26 commits into from
Apr 25, 2020

Conversation

staujd02
Copy link
Contributor

Format was implemented into types where format(, "") is the only accepted string format specifier

Format can now be implemented on more types.

PR Checklist:

  • [ x] All new features have been tested
  • [ x] All new features have been documented
  • [ x] I have read the CONTRIBUTING.md file
  • [ x] I will abide by the code of conduct

Signed-off-by: Joel Stauffer <altosax13@gmail.com>
Signed-off-by: Joel Stauffer <altosax13@gmail.com>
Signed-off-by: Joel Stauffer <altosax13@gmail.com>
Signed-off-by: Joel Stauffer <altosax13@gmail.com>
Signed-off-by: Joel Stauffer <altosax13@gmail.com>
Signed-off-by: Joel Stauffer <altosax13@gmail.com>
Signed-off-by: Joel Stauffer <altosax13@gmail.com>
Signed-off-by: Joel Stauffer <altosax13@gmail.com>
Signed-off-by: Joel Stauffer <altosax13@gmail.com>
Signed-off-by: Joel Stauffer <altosax13@gmail.com>
@martica
Copy link
Contributor

martica commented Jun 12, 2019

Howdy @staujd02. Thanks for working on this!

The eslint check in CI is broken currently, so I'm stuck enforcing it manually at the moment. Can you replace the template strings with concatenation? Our .eslintrc currently doesn't allow them. :( You can refer to it in the top level of the repo.

Signed-off-by: Joel <staujd02@students.ipfw.edu>
Signed-off-by: Joel <staujd02@students.ipfw.edu>
@staujd02
Copy link
Contributor Author

Sure thing @martica, Done! Took care of the auto linter too...

@martica
Copy link
Contributor

martica commented Jun 12, 2019

I'll take a closer soon, but moving to calling the __format__ methods on types is a great step in the right direction here.

@martica
Copy link
Contributor

martica commented Jun 12, 2019

Looks like there are a couple more errors that Brutus is grumbling about.

Signed-off-by: Joel Stauffer <altosax13@gmail.com>
@alexjdw
Copy link
Contributor

alexjdw commented Jun 14, 2019

Looking at this briefly, I think most of the functionality already exists for you in Str.format. Could you do something like this?

s = "{" + formatSpecifier + "}"
return callables.call_method(s, 'format', [this], {}) # Batavia equivalent of s.format(this)

Does that make sense as a boilerplate?

@staujd02
Copy link
Contributor Author

Yep, that makes sense.

@martica
Copy link
Contributor

martica commented Jun 14, 2019

We do need to keep in mind that eventually str.format should be calling __format__ on the types that are being formatted. For now, having the various __format__ methods call StrUtils._new_subsitute would get a lot of reuse quickly.

For a long-term goal, it feels like we need to find a seem in StrUtils._new_subsitute between its parsing of the full format strings and its formatting of individual values and __format__ in the types could call into the latter part.

@staujd02
Copy link
Contributor Author

I'm trying to redirect the formatting to string.format() and in effect StringUtils.__new_substitute(). But the arguments are being converted into a string types and therefore type conversions valid for the Boolean type are being rejected since they don't apply to strings...

@martica
Copy link
Contributor

martica commented Jun 14, 2019

That sounds frustrating. I can try to help this evening (PDT) if you are still stuck.

@alexjdw
Copy link
Contributor

alexjdw commented Jun 14, 2019

@martica so if I'm understanding right, the end result should actually be Str.format calls something like <type>.__format__(arg, format_style) for each substitution? That's going to be a very, very thick commit as the current format is about 1500 lines of switch/case choose your own adventure.

@martica
Copy link
Contributor

martica commented Jun 14, 2019 via email

@martica
Copy link
Contributor

martica commented Jun 15, 2019

I took a look @staujd02. It looks like the problem comes in here: https://github.com/beeware/batavia/blob/master/batavia/types/StrUtils.js#L1173

Bool is actually an outlier in that if you ask for string formatting, it shows the english word, but any numeric formatting treats it as 0 or 1. Seems like that chain of ifs needs a case for Bool, and because it depends on the format type (empty string or 's' vs. all the other types) you'll need to move the format parsing before the arg gets set. Look at https://github.com/beeware/batavia/blob/master/batavia/types/StrUtils.js#L796

Fixing the current string.format() is a major refactoring. Don't worry about that. Having the format builtin call string.format directly should get us a lot without making the future refactor any harder.

@staujd02
Copy link
Contributor Author

@martica that sounds like a plan. I'll bump the PR if something else crops up.

Joel Stauffer added 4 commits June 17, 2019 10:55
Signed-off-by: Joel Stauffer <jstauffer@apterainc.com>
Signed-off-by: Joel Stauffer <jstauffer@apterainc.com>
Signed-off-by: Joel Stauffer <jstauffer@apterainc.com>
Joel Stauffer added 2 commits June 17, 2019 12:55
Signed-off-by: Joel Stauffer <jstauffer@apterainc.com>
Signed-off-by: Joel Stauffer <jstauffer@apterainc.com>
@staujd02
Copy link
Contributor Author

Thanks for the help @martica and @awildtechno ! Hope this is close to what you are looking for...

@alexjdw
Copy link
Contributor

alexjdw commented Jun 17, 2019

I'm not really qualified to review but I wanted to say Thanks for sticking it out with this. I just got started here a few weeks ago with some modifications to Str.format and I know how nitpicky writing and passing tests for the formatter can be.

@phildini phildini merged commit 57a1a54 into beeware:master Apr 25, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants