0

I'm trying to find the permutations of a regex (s) that are also regexes, this is what I have so far:

def permutations(s):
    leaves = ['1', '2', '0', 'e']
    possible = []

    if len(s) == 3 and s[1] in leaves:
        return s
    elif len(s) > 3:
        for i in range(len(s)):
            current_character = s[i]
            other_characters = s[:i] + s[i+1:]
            for possible_regexes in permutations(other_characters):
                if is_regex(possible_regexes):
                    possible = [[current_character] + [other_characters]]
        return possible

    for regexes in possible:
        print(regexes)

However I'm getting an error telling me that I can't iterate over a string, I'm pretty sure this is the problem here: possible = [[current_character] + [other_characters]]. How would I return it so I could iterate over every permutation, then determine if that permutation is valid?

EDIT This is the error TypeError: 'NoneType' object is not iterable

EDIT2 Full traceback: File "regex_functions.py", line 86, in <module> print (all_regex_permutations('(1.2)')) File "regex_functions.py", line 68, in all_regex_permutations for possible_regexes in all_regex_permutations(other_characters): File "regex_functions.py", line 68, in all_regex_permutations for possible_regexes in all_regex_permutations(other_characters): TypeError: 'NoneType' object is not iterable

5
  • Don't say that you're getting an error, show it. That's especially true here because you say the error message tells you that you can't iterate over a string, but you can iterate over a string. Moreover, the line you point to doesn't do any iteration. Please post the full traceback.
    – DSM
    Mar 23, 2014 at 18:25
  • @DSM Sorry, check my edit!
    – Amon
    Mar 23, 2014 at 18:30
  • Not every code path returns a value.... do something about it. Mar 23, 2014 at 18:41
  • @JeffMercado I've tried everything, I can't seem to wrap my head around this
    – Amon
    Mar 23, 2014 at 19:03
  • Did you? Step through the code, line by line. If you had, you'd see the problem. Mar 23, 2014 at 20:28

1 Answer 1

2

The traceback you posted references a different function name than the function you posted, but it appears that it was just renamed. I will assume that is the case.

The trace item you should be concerned about is the last one in the traceback.

 File "regex_functions.py", line 68, in all_regex_permutations
   for possible_regexes in all_regex_permutations(other_characters):
 TypeError: 'NoneType' object is not iterable

The the error is indicating that the NoneType is not iterable. That would mean that the result of calling all_regex_permutations(other_characters) returned None at some point. You need to analyze your code and figure out at what point does it actually return None.

I hinted that not every code path returns a value which is the ultimate cause of this error (there are other possible errors I see but that's a different question). You should be asking yourself these questions: "What are all the code paths for this function?" and "Am I returning the correct result for every one?"

I've highlighted what the code paths are in that function.

enter image description here

  1. The orange path will return whatever s is. And assuming s is a list or other expected type, then that will work.
  2. The green path will return whatever possible is. possible is set at two different points, the initial value at the top of the function, and the new result of the assignment made in the loop body.
  3. The blue path will fall through to the end of the function. In Python, if a function doesn't explicitly return a value, it is understood to return None.

The blue path is the problem. You didn't return anything there. That needs to be corrected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.