-1

Say I have a dictionary of sets of paths:

my_dict['some_key'] = {'abc/hi/you','xyz/hi/you','jkl/hi/you'}

I want to see if a path appears is in this set. If I have the whole path I simply would do the following:

str = 'abc/hi/you'
if str in my_dict['some_key']:
    print(str)

But what if I don't know b is what comes in between a and c. What if it could be literally anything. If I was lsing in a shell I'd just put * and call it a day.

What I want to be able to do is have str be a regx:

regx = '^a.*c/hi/you$' #just assume this is the ideal regex. Doesn't really matter.
if regx in my_dict['some_key']:
    print('abc/hi/you') #print the actual path, not the regx

What is a clean and fast way to implement something like this?

0

2 Answers 2

0

You need to loop through the set rather than a simple in call.

To avoid setting up the whole dictionary of sets for the example I have abstracted it as simply my_set.

import re
my_set = {'abc/hi/you','xyz/hi/you','jkl/hi/you'}
regx  = re.compile('^a.*c/hi/you$')
for path in my_set:
    if regx.match(path):
        print(path)

I chose to compile instead of simply re.match() because the set could have 1 million plus elements in the actual implementation.

Sign up to request clarification or add additional context in comments.

6 Comments

While needing to print the actual path and not the regex breaks the typical "formula" of if x in y: print(x) it is important to note this doesn't change the fact that I don't think it is possible to even do if regx in y: print(regx)
no need to compile, re uses a recently used cache of regex strings
Why do you answer your own question within 1 minute? And give a comment on the answer?
regarding the compiling, I was under the impression that compiling once was slightly faster for very large number of cache checks? Maybe I was mistaken. Also, due to the nature of the broader program (outside of what is shown in the question) it makes a bit more sense to compile from a readability perspective imo.
|
0

You can subclass the set class and implement the a in b operator

import re
from collections import defaultdict

class MySet(set):
  def __contains__(self, regexStr):
    regex = re.compile(regexStr)
    for e in self:
      if regex.match(e):
        return True
    return False

my_dict = defaultdict(MySet)

my_dict['some_key'].add('abc/hi/you')

regx = '^a.*c/hi/you$'
if regx in my_dict['some_key']:
    print('abc/hi/you')

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.