Aunsiels / pyformlang

A python library to manipulate formal languages and various automata
https://pypi.org/project/pyformlang/
MIT License
43 stars 10 forks source link

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

Closed parthokr closed 3 years ago

parthokr commented 3 years ago
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 commented 3 years ago

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