This functionality MUST be accessible with a function or a method. (?)

As already mentioned, building up a pattern string and then using that on
the LHS cannot work:

  >>> pattern = "{a:d} {b:d}"
  >>> pattern += " {c:s}"
  >>> pattern = "12 34 ef"

This is the way Parse works; you call parse() and it returns a Result:

  >>> r = parse("The {} who say {}", "The knights who say Ni!")
  >>> print(r)
  <Result ('knights', 'Ni!') {}>
  >>> print(r.fixed)
  ('knights', 'Ni!')

https://github.com/r1chardj0n3s/parse#result-and-match-objects :

```rst
Result and Match Objects
------------------------

The result of a ``parse()`` and ``search()`` operation is either ``None``
(no match), a
``Result`` instance or a ``Match`` instance if ``evaluate_result`` is False.

The ``Result`` instance has three attributes:

``fixed``
   A tuple of the fixed-position, anonymous fields extracted from the input.
``named``
   A dictionary of the named fields extracted from the input.
``spans``
   A dictionary mapping the names and fixed position indices matched to a
   2-tuple slice range of where the match occurred in the input.
   The span does not include any stripped padding (alignment or width).

The ``Match`` instance has one method:

``evaluate_result()``
   Generates and returns a ``Result`` instance for this ``Match`` object.
```

Similar functionality in the standard library could return e.g.
Union[tuple, namedtuple] and expect users to call namedtuple.asdict() when
there are template field names specified; but the parse.Result object does
support .spans ("A dictionary mapping the names and fixed position indices
matched to a 2-tuple slice range of where the match occurred in the
input.").

On Fri, Sep 18, 2020 at 9:34 AM Ricky Teachey <ri...@teachey.org> wrote:

> On Fri, Sep 18, 2020, 9:16 AM Chris Angelico <ros...@gmail.com> wrote:
>
>> On Fri, Sep 18, 2020 at 11:04 PM Ricky Teachey <ri...@teachey.org> wrote:
>> >
>> > On Fri, Sep 18, 2020, 8:34 AM Chris Angelico <ros...@gmail.com> wrote:
>> >>
>> >> On Fri, Sep 18, 2020 at 10:26 PM Ricky Teachey <ri...@teachey.org>
>> wrote:
>> >> >
>> >> > On Fri, Sep 18, 2020, 8:17 AM Ricky Teachey <ri...@teachey.org>
>> wrote:
>> >> >>
>> >> >>
>> >> >> Why not just grow a parse method on str that returns a dict and do
>> it this way?
>> >> >>
>> >> >>
>> >> >> q = "{a} {b}"
>> >> >> p = "1 2"
>> >> >> (a, b) = q.parse(p)
>> >> >
>> >> >
>> >> > Sorry that should have been:
>> >> >
>> >> > (a, b) = q.parse(p).values()
>> >> >
>> >>
>> >> You're using a dictionary as if it were a tuple. That's going to cause
>> >> a LOT of pain when someone does something like:
>> >>
>> >> a, b = "{b} {a}".parse(p).values()
>> >>
>> >> and they come out in the wrong order. Bad bad bad idea. Don't have
>> >> names if they're going to be pure lies.
>> >>
>> >> ChrisA
>> >
>> >
>> > I'm not sure I understand the point you are making here since
>> dictionaries have preserved order since python 3.6...?
>> >
>> > The same problem exists here:
>> >
>> > a, b  = 2, 1
>> > assert a == 1. # whoops got the order wrong
>> >
>>
>> But you don't have any sort of lie in the RHS about the name mapping.
>> It's just a sequence, so people will expect it to be a sequence. If
>> the parser incorporates names, people will expect it to use those
>> names. Why have it return a dictionary if you're going to assume and
>> mandate that it be a sequence?
>>
>> ChrisA
>>
>
> I see your point now. Missed it before: the lie inherent to using the
> names not the order.
>
> But that seems to me to be an argument for dictionaries, not against. You
> can at least consult the dictionary to find out what name was used to grab
> the value.
>
> And again, similar things can happen in other contexts and it's just
> people's responsibility to get it right.
>
> d = dict(b=1, a=2)
> a1, b1, a2, b2 = {**d, **d}.values()
>
> _______________________________________________
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-ideas@python.org/message/P4CRHIBTNMPV6ZLVI2AFIFAVIND7YBIN/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/HLCFY7ROQAOOUCCOGX3OCAQ7MUBMEW5U/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to