Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/graphviz/graphs.py: 86%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

29 statements  

1r"""Assemble DOT source code objects. 

2 

3Example: 

4 >>> doctest_mark_exe() 

5 

6 >>> import graphviz 

7 >>> dot = graphviz.Graph(comment='Mønti Pythøn ik den Hølie Grailen') 

8 

9 >>> dot.node('Møøse') 

10 >>> dot.node('trained_by', 'trained by') 

11 >>> dot.node('tutte', 'TUTTE HERMSGERVORDENBROTBORDA') 

12 

13 >>> dot.edge('Møøse', 'trained_by') 

14 >>> dot.edge('trained_by', 'tutte') 

15 

16 >>> dot.node_attr['shape'] = 'rectangle' 

17 

18 >>> print(dot.source) #doctest: +NORMALIZE_WHITESPACE 

19 // Mønti Pythøn ik den Hølie Grailen 

20 graph { 

21 node [shape=rectangle] 

22 "Møøse" 

23 trained_by [label="trained by"] 

24 tutte [label="TUTTE HERMSGERVORDENBROTBORDA"] 

25 "Møøse" -- trained_by 

26 trained_by -- tutte 

27 } 

28 

29 >>> dot.render('doctest-output/m00se.gv').replace('\\', '/') 

30 'doctest-output/m00se.gv.pdf' 

31""" 

32 

33from collections.abc import Iterable, Mapping 

34 

35from .encoding import DEFAULT_ENCODING 

36from . import _tools 

37from . import dot 

38from . import jupyter_integration 

39from . import piping 

40from . import rendering 

41from . import unflattening 

42 

43__all__ = ['Graph', 'Digraph'] 

44 

45 

46class BaseGraph(dot.Dot, 

47 rendering.Render, 

48 jupyter_integration.JupyterIntegration, piping.Pipe, 

49 unflattening.Unflatten): 

50 """Dot language creation and source code rendering.""" 

51 

52 @_tools.deprecate_positional_args(supported_number=1, ignore_arg='self') 

53 def __init__(self, name: str | None = None, 

54 comment: str | None = None, 

55 filename=None, directory=None, 

56 format: str | None = None, 

57 engine: str | None = None, 

58 encoding: str | None = DEFAULT_ENCODING, 

59 graph_attr: Mapping[str, str] | None = None, 

60 node_attr: Mapping[str, str] | None = None, 

61 edge_attr: Mapping[str, str] | None = None, 

62 body: Iterable[str] | None = None, 

63 strict: bool = False, *, 

64 renderer: str | None = None, 

65 formatter: str | None = None) -> None: 

66 if filename is None and name is not None: 

67 filename = f'{name}.{self._default_extension}' 

68 

69 super().__init__(name=name, comment=comment, 

70 graph_attr=graph_attr, 

71 node_attr=node_attr, edge_attr=edge_attr, 

72 body=body, strict=strict, 

73 filename=filename, directory=directory, 

74 encoding=encoding, 

75 format=format, engine=engine, 

76 renderer=renderer, formatter=formatter) 

77 

78 @property 

79 def source(self) -> str: 

80 """The generated DOT source code as string.""" 

81 return ''.join(self) 

82 

83 

84class Graph(dot.GraphSyntax, BaseGraph): 

85 """Graph source code in the DOT language. 

86 

87 Args: 

88 name: Graph name used in the source code. 

89 comment: Comment added to the first line of the source. 

90 filename: Filename for saving the source 

91 (defaults to ``name`` + ``'.gv'``). 

92 directory: (Sub)directory for source saving and rendering. 

93 format: Rendering output format (``'pdf'``, ``'png'``, ...). 

94 engine: Layout command used (``'dot'``, ``'neato'``, ...). 

95 renderer: Output renderer used (``'cairo'``, ``'gd'``, ...). 

96 formatter: Output formatter used (``'cairo'``, ``'gd'``, ...). 

97 encoding: Encoding for saving the source. 

98 graph_attr: Mapping of ``(attribute, value)`` pairs for the graph. 

99 node_attr: Mapping of ``(attribute, value)`` pairs set for all nodes. 

100 edge_attr: Mapping of ``(attribute, value)`` pairs set for all edges. 

101 body: Iterable of verbatim lines (including their final newline) 

102 to add to the graph ``body``. 

103 strict (bool): Rendering should merge multi-edges. 

104 

105 Note: 

106 All parameters are `optional` and can be changed under their 

107 corresponding attribute name after instance creation. 

108 """ 

109 

110 @property 

111 def directed(self) -> bool: 

112 """``False``""" 

113 return False 

114 

115 

116class Digraph(dot.DigraphSyntax, BaseGraph): 

117 """Directed graph source code in the DOT language.""" 

118 

119 if Graph.__doc__ is not None: 

120 __doc__ += Graph.__doc__.partition('.')[2] 

121 

122 @property 

123 def directed(self) -> bool: 

124 """``True``""" 

125 return True