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 

33import typing 

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: typing.Optional[str] = None, 

54 comment: typing.Optional[str] = None, 

55 filename=None, directory=None, 

56 format: typing.Optional[str] = None, 

57 engine: typing.Optional[str] = None, 

58 encoding: typing.Optional[str] = DEFAULT_ENCODING, 

59 graph_attr=None, node_attr=None, edge_attr=None, 

60 body=None, 

61 strict: bool = False, *, 

62 renderer: typing.Optional[str] = None, 

63 formatter: typing.Optional[str] = None) -> None: 

64 if filename is None and name is not None: 

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

66 

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

68 graph_attr=graph_attr, 

69 node_attr=node_attr, edge_attr=edge_attr, 

70 body=body, strict=strict, 

71 filename=filename, directory=directory, 

72 encoding=encoding, 

73 format=format, engine=engine, 

74 renderer=renderer, formatter=formatter) 

75 

76 @property 

77 def source(self) -> str: 

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

79 return ''.join(self) 

80 

81 

82class Graph(dot.GraphSyntax, BaseGraph): 

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

84 

85 Args: 

86 name: Graph name used in the source code. 

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

88 filename: Filename for saving the source 

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

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

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

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

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

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

95 encoding: Encoding for saving the source. 

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

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

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

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

100 to add to the graph ``body``. 

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

102 

103 Note: 

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

105 corresponding attribute name after instance creation. 

106 """ 

107 

108 @property 

109 def directed(self) -> bool: 

110 """``False``""" 

111 return False 

112 

113 

114class Digraph(dot.DigraphSyntax, BaseGraph): 

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

116 

117 if Graph.__doc__ is not None: 

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

119 

120 @property 

121 def directed(self) -> bool: 

122 """``True``""" 

123 return True