GrandMoff100 / RegexFactory

Dynamically construct regex patterns with native python representations
https://regexfactory.readthedocs.io
GNU General Public License v3.0
7 stars 4 forks source link

Adding __hash__ operator to support set operation for RegexPattern #12

Closed jeffrey82221 closed 1 year ago

jeffrey82221 commented 1 year ago

Hi @GrandMoff100 ,

I wonder if you can add hash operator to support set operation for RegexPattern.

The use case is as follows:

from regexfactory.chars import ANY, DIGIT
from regexfactory.pattern import escape
set([ANY, ANY, DIGIT, DIGIT, escape('apple'), escape('apple')]) 
>> {<RegexPattern '.'>, <RegexPattern '\d'>, <RegexPattern 'apple'>}

Without hash, the above situation will raise:

TypeError: unhashable type: 'RegexPattern'.

A simple additional __hash__ method to the RegexPattern class may solve the problem:

    def __hash__(self):
         return hash(self.regex)

I am willing to make this contribute, too. Is it possible to add this new feature to the near future release (maybe v1.0.1)?

GrandMoff100 commented 1 year ago

I love this idea. You bring up a valid point. We can definitely include it in new release. Would you like to ship it with the rest of your contributions and would you rather we release in a separate minor release before the rest of your contributions?