Skip to content

Commit

Permalink
fixes related to imports
Browse files Browse the repository at this point in the history
  • Loading branch information
Vipul-Cariappa committed Oct 18, 2023
1 parent dbf0456 commit 08b176e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
9 changes: 7 additions & 2 deletions automata/base/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
Literal,
Set,
Tuple,
TypeAlias,
TypeVar,
Union,
)
Expand All @@ -30,6 +31,10 @@
_visual_imports = False
else:
_visual_imports = True
finally:
# create a type for type checker
# irrespective of whether the imports were successful
GraphT: TypeAlias = "pgv.AGraph"


LayoutMethod = Literal["neato", "dot", "twopi", "circo", "fdp", "nop"]
Expand Down Expand Up @@ -81,7 +86,7 @@ def create_graph(
reverse_orientation: bool = False,
fig_size: Union[Tuple[float, float], Tuple[float], None] = None,
state_separation: float = 0.5,
) -> pgv.AGraph:
) -> GraphT:
"""Creates and returns a graph object
Args:
- horizontal (bool, optional): Direction of node layout. Defaults
Expand Down Expand Up @@ -118,7 +123,7 @@ def create_graph(


def save_graph(
graph: pgv.AGraph,
graph: GraphT,
path: Union[str, os.PathLike],
) -> None:
"""Write `graph` to file given by `path`. PNG, SVG, etc.
Expand Down
10 changes: 9 additions & 1 deletion automata/fa/fa.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
import coloraide
import pygraphviz as pgv
except ImportError:
pass
_visual_imports = False
else:
_visual_imports = True


FAStateT = AutomatonStateT
Expand Down Expand Up @@ -73,6 +75,12 @@ def show_diagram(
AGraph corresponding to the given automaton.
"""

if not _visual_imports:
raise ImportError(
"Missing visualization packages; "
"please install coloraide and pygraphviz."
)

# Defining the graph.
graph = create_graph(
horizontal, reverse_orientation, fig_size, state_separation
Expand Down
10 changes: 9 additions & 1 deletion automata/pda/pda.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
import coloraide
import pygraphviz as pgv
except ImportError:
pass
_visual_imports = False
else:
_visual_imports = True

PDAStateT = AutomatonStateT
PDATransitionsT = AutomatonTransitionsT
Expand Down Expand Up @@ -134,6 +136,12 @@ def show_diagram(
AGraph corresponding to the given automaton.
"""

if not _visual_imports:
raise ImportError(
"Missing visualization packages; "
"please install coloraide and pygraphviz."
)

# Defining the graph.
graph = create_graph(
horizontal, reverse_orientation, fig_size, state_separation
Expand Down

0 comments on commit 08b176e

Please sign in to comment.