
Parse strings using a specification based on the Python format() syntax - trueduke
https://github.com/r1chardj0n3s/parse
======
hueving
Seems to be a dead project. Several open issues and one pull request but no
commits for years.

------
fixxer
I never regret the three days I spent a decade ago playing regex golf.

------
TazeTSchnitzel
So it's C's scanf(), but for Python?

~~~
masklinn
It also uses the new format syntax rather than the old printf-style syntax.

------
jp_sc
A more general and mature project is pyparsing

------
justin_vanw
so 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
Well, 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
I'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
ES6'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

        > 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
In 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.

