Parse strings using a specification based on the Python format() syntax
github.com
github.com
This library should include the tagline "why learn something useful like regular expressions, when you can use some custom library with 1/10th the power!"
There are occasions when it's a much nicer idea than the `re` hoops we need to jump through instead though; I wonder if PEP498 f-strings [0] mightn't be laying the groundwork for this to exist.
In Python 3.6 it will be possible to do:
knights_say = 'Ni!'
dialogue = f"""
ARTHUR: Knights of Ni, we are but simple travelers who seek the enchanter who lives beyond these woods.
HEAD KNIGHT: {knights_say}
"""
Perhaps future Python will enable `parse`-like: dialogue = """
ARTHUR: Knights of Ni, we are but simple travelers who seek the enchanter who lives beyond these woods.
HEAD KNIGHT: Ni!
"""
dialogue.<some_future_method>(f'KNIGHT: {knights_say}')
print(f'Knights have been known to say {knights_say!r}.')
# "Knights have been known to say 'Ni!'." is printed
Or perhaps I'm dreaming.I was disappointed when Python added its f-strings instead.
> I was disappointed when
> Python added its
> f-strings instead.
What's the difference, other than syntax? (I've only a basic familiarity with JS.)So it's a powerful way of embedding other languages in the host language, where f-strings are not. The quasiParserGenerator library I linked to shows some of the potential.
With that support, why's it worse than `'{}'.format(expr)`?
But those are two different things, and the former drives the latter, of necessity.
We (humans, the core devs, draw your circle, it wasn't God nor aliens) chose to add this BS to the standard language.
Now, everyone who wants to make tools that work with python have to be written to deal with this additional syntax-within-a-syntax. How does it look in the grammar? Pretty?
Your linters, your type analyzers, your documentation tools, and whatever else, all have to be (re)written to deal with this.
For what? What is the huge pain point with putting your expressions in the call to format() or even in the "logic" code? Mixing source code and string literals is just stupid.
It's one more example of how Python 3 is drawing the stupid away from Python 2. As a 2 syntax partisan, I'm sad to see 3 degenerate, but at least 2 is still off the "innovators'" radar.
> those are two different things, and the former drives
> the latter, of necessity
Absolutely, I wasn't suggesting they were the same - I joined them with 'and'. > tools that work with python have to be written to deal
> with this additional syntax-within-a-syntax
That's the point though, it's not "syntax-within-a-syntax", any more than is, say, a `lambda` - it's just "the [new] syntax".https://docs.python.org/3.6/reference/lexical_analysis.html#...
It is a mess and I pity the fools who have to implement it.
And where, if you don't mind me asking, have you ever seen a pretty regex?
> why learn something useful like regular expressions, when you can use some custom library with 1/10th the power!
Regular expressions are not that powerful anyway, but they compensate this by being highly unreadable and error-prone.
In very many cases you don't need 90% of the power of regexes - you just want to extract a certain portion of a string, without worrying about greediness, character classes, double-triple-quadruple quotation with backslashes and so on. Which is where `parse` shines.
And if you see that simpler tool like `parse` is not enough, my advice is to jump over regexes and go straight for `pyparsing`. Regexes are a mess, no matter how careful you are when constructing them. A proper parser is going to be a better choice every single time.