8 ms·
Parse strings using a specification based on the Python format() syntax
- TazeTSchnitzel 10y agoSo it's C's scanf(), but for Python?
- masklinn 10y agoIt also uses the new format syntax rather than the old printf-style syntax.
- hueving 10y agoSeems to be a dead project. Several open issues and one pull request but no commits for years.
- justin_vanw 10y agoso you take %s and replace it with .*? (Checking... Yep, basically this, but with a super ugly regex) 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!"
- fixxer 10y agoWell, that isn't really fair: there are lots of people out there that would appreciate this layer of abstraction over a general approach... I wouldn't hire them to be software engineers or data sci, but they exist.
- OJFord 10y agoI've discussed wishing this existed before (without having seen this library) - I would love it to be in Python as essentially the same thing is present in other language's standard libraries, but no, I would never depend on a third-party library for this. 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. [0]: https://www.python.org/dev/peps/pep-0498/#abstract https://www.python.org/dev/peps/pep-0498/#abstract
- abecedarius 10y agoES6's template strings can do something similar: https://github.com/erights/quasiParserGenerator https://github.com/erights/quasiParserGenerator I was disappointed when Python added its f-strings instead.
- OJFord 10y ago> 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.)
- abecedarius 10y agoIn ES6 (and earlier, in E) you can write variable_name`string with ${expression} in it`, where the value of variable_name must be a function. The function gets called with an array of the substrings between the ${expression} parts, plus the values of the expressions. The function can return anything, including a non-string value. (So e.g. you might define dom so that dom`<p>Hello world` returns a new DOM node.) If you write just `string with ${e}` then there's a default for the variable_name part which does simple interpolation, like Python's f-strings. 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.
- carapace 10y agoThose f-strings are awful: "embed expressions inside string literals"!? What kind of a maniac does that!?
- OJFord 10y agoMy initial reaction was similar, though slightly tempered. However, at least for me, once you try to think about what it is that you're actually objecting to, there's nothing left when it's a language-feature and supported by your tooling. With that support, why's it worse than `'{}'.format(expr)`?
- klibertp 10y ago> Yep, basically this, but with a super ugly regex 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.
- fixxer 10y agoI never regret the three days I spent a decade ago playing regex golf.
- jp_sc 10y agoA more general and mature project is pyparsing