Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeError: module 'pyformlang' has no attribute 'regular_expression' #8

Closed
parthokr opened this issue Jun 24, 2021 · 1 comment

Comments

@parthokr
Copy link

parthokr commented Jun 24, 2021

from pyformlang.finite_automaton import EpsilonNFA, State, Symbol, Epsilon

enfa = EpsilonNFA()
state0 = State(0)
state1 = State(1)
symb_a = Symbol("0")
symb_b = Symbol("1")
enfa.add_start_state(state0)
enfa.add_final_state(state1)
enfa.add_transition(state0, symb_a, state0)
enfa.add_transition(state1, symb_b, state0)
enfa.add_transition(state1, symb_b, state1)

# Turn a finite automaton into a regex...
regex = enfa.to_regex()
# And turn it back into an epsilon non deterministic automaton
enfa2 = regex.to_epsilon_nfa()

Source: Here

This code generates AttributeError: module 'pyformlang' has no attribute 'regular_expression

But importing Regex from pyformlang.regular_expression fixes that issue.

Why a method call to a library requires explicit import?

Importing Regex from pyformlang.regular_expression at epsilon_nfa.py causes circular import.
So importing Regex at runtime from to_regex() may resolve this. Like

    def to_regex(self) -> "Regex":
        from pyformlang import regular_expression
        ...
        ...
        ...
        return regular_expression.Regex(res)

at epsilon_nfa.py

@Aunsiels
Copy link
Owner

Thank you for noticing that! Circular imports are a pain... I used your solution with a local import.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants