On Mon, Nov 12, 2018 at 7:47 PM Martin Blais <bl...@furius.ca> wrote:

> On Mon, Nov 12, 2018 at 4:47 PM <shreedharhardi...@gmail.com> wrote:
>
>> Hi,
>>
>> Just noticed that the implementation of the grep function in bean-query
>> doesn't make sense to me from the description:
>>
>> class Grep(query_compile.EvalFunction):
>>     "Match a group against a string and return only the matched portion."
>>     __intypes__ = [str, str]
>>
>>
>>     def __init__(self, operands):
>>         super().__init__(operands, str)
>>
>>
>>     def __call__(self, context):
>>         args = self.eval_args(context)
>>         match = re.search(args[0], args[1])
>>         if match:
>>             return match.group(0)
>>
>> According to the description I think it should do:
>>         if match:
>>             # Get the first matched group; group(0) matches entire string
>>             return match.group(1)
>>
>>
>> or even:
>>         if match:
>>             # Get the last matched group or entire string if there are
>> no groups
>>             return match.group(len(match.groups))
>>
>> Reference: https://docs.python.org/3/library/re.html#match-objects
>>
>
> bergamot [hg|default]:~/p/invest/options$ python3
> Python 3.7.0 (default, Jul 30 2018, 01:44:42)
> [GCC 7.3.0] on linux
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import re
> >>> re.search('a+', 'cccaadd').group(0)
> 'aa'
> >>>
>
> 'aa' is the matched portion.
> WAI
>
>
Ah, so you had no use for the subgroups - which are captured using the
parentheses?


>
>
> If it is implemented as intended, I suppose it would be nice to have an
>> overloaded grep() function that takes a 3rd parameter of type int, for the
>> group id. I can send a patch for that if you prefer that, although I think
>> the second implementation should work for both styles:
>>
>> >>> import re
>> >>> m = re.search('a (b) c', 'asda b c')
>> >>> m.group(len(m.groups()))
>>
>> 'b'
>> >>> m = re.search('a b c', 'asda b c')
>>
>> >>> m.group(len(m.groups()))
>> 'a b c'
>>
>> Thanks,
>> Shreedhar
>>
>
> Patches welcome.
>
>
>
Anyway, the one-line change I suggested would work for your scenario also.
Basically, if there're no parent groups, it'll just return the matched
portion (as it does now) and otherwise it'll return the last group in the
pattern string. Not sure it makes sense to add an integer selector - since
GREP returns only one string so why would have a pattern with multiple
subgroups.

Anyway, I've attached a patch with some tests.

- Shreedhar



>
>
> --
>> You received this message because you are subscribed to the Google Groups
>> "Beancount" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to beancount+unsubscr...@googlegroups.com.
>> To post to this group, send email to beancount@googlegroups.com.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/beancount/5b9484fc-1bd0-4e8c-81b4-f6caa2877cea%40googlegroups.com
>> <https://groups.google.com/d/msgid/beancount/5b9484fc-1bd0-4e8c-81b4-f6caa2877cea%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Beancount" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to beancount+unsubscr...@googlegroups.com.
> To post to this group, send email to beancount@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/beancount/CAK21%2BhPBsqHP_9DSC8jnSTDKMiEkhtYuJcmG9_KaStfqBxSpVw%40mail.gmail.com
> <https://groups.google.com/d/msgid/beancount/CAK21%2BhPBsqHP_9DSC8jnSTDKMiEkhtYuJcmG9_KaStfqBxSpVw%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Beancount" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to beancount+unsubscr...@googlegroups.com.
To post to this group, send email to beancount@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/beancount/CAAY9sD-n0rkK_HDgbchwDzQaDJRT3c9tJu_skGHGdnJj0oahVg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Attachment: grep-subgroup.patch
Description: Binary data

Reply via email to