erdantic.core¶
DEFAULT_EDGE_ATTR
module-attribute
¶
DEFAULT_EDGE_ATTR = (('dir', 'both'))
Default edge attributes passed to Graphviz.
DEFAULT_GRAPH_ATTR
module-attribute
¶
DEFAULT_GRAPH_ATTR = (
("nodesep", "0.5"),
("ranksep", "1.5"),
("rankdir", "LR"),
(
"label",
f"Created by erdantic v{__version__} <https://github.com/drivendataorg/erdantic>",
),
(
"fontname",
"Times New Roman,Times,Liberation Serif,serif",
),
("fontsize", "9"),
("fontcolor", "gray66"),
)
Default graph attributes passed to Graphviz.
DEFAULT_NODE_ATTR
module-attribute
¶
DEFAULT_NODE_ATTR = (
(
"fontname",
"Times New Roman,Times,Liberation Serif,serif",
),
("fontsize", 14),
("shape", "plain"),
)
Default node attributes passed to Graphviz.
Cardinality
¶
Bases: Enum
Enumeration of possible cardinality values for a relationship between two data model classes. Cardinality measures the maximum number of associations.
Source code in erdantic/core.py
302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
|
to_dot
¶
to_dot() -> str
Returns the DOT language specification for the arrowhead styling associated with the cardinality value.
Source code in erdantic/core.py
311 312 313 314 315 |
|
Edge
¶
Bases: BaseModel
Hold information about a relationship between two data model classes. These represent directed edges in the entity relationship diagram.
Attributes:
Name | Type | Description |
---|---|---|
source_model_full_name |
FullyQualifiedName
|
Fully qualified name of the source model, i.e., the model that contains a field that references the target model. |
source_field_name |
str
|
Name of the field on the source model that references the target model. |
target_model_full_name |
FullyQualifiedName
|
Fully qualified name of the target model, i.e., the model that is referenced by the source model's field. |
target_cardinality |
Cardinality
|
Cardinality of the target model in the relationship,
e.g., if the relationship is one (source) to many (target), this value will be
|
target_modality |
Modality
|
Modality of the target model in the relationship, e.g., if the
relationship is one (source) to zero (target), meaning that the target is optional,
this value will be |
source_cardinality |
Optional[Cardinality]
|
Cardinality of the source model in the relationship. This will never be set for Edges created by erdantic, but you can set it manually to notate an externally known cardinality. |
source_modality |
Optional[Modality]
|
Modality of the source model in the relationship. This will never be set for Edges created by erdantic, but you can set it manually to notate an externally known modality. |
Source code in erdantic/core.py
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 |
|
key
property
¶
key: str
Returns the key used to identify this instance of Edge in the
EntityRelationshipDiagram.edges mapping. This value is a hyphenated string of the fields
source_model_full_name
, source_field_name
, and target_model_full_name
.
from_field_info
classmethod
¶
Constructor method to create a new instance from a target model instance and a source model's FieldInfo.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
target_model |
type
|
Target model class. |
required |
source_field_info |
FieldInfo
|
FieldInfo instance for the field on the source model that references the target model. |
required |
Returns:
Type | Description |
---|---|
Self
|
New instance of Edge. |
Source code in erdantic/core.py
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 |
|
source_dot_arrow_shape
¶
source_dot_arrow_shape() -> str
Arrow shape specification in Graphviz DOT language for this edge's tail (the end at the source model). See Graphviz docs as a reference. Shape returned is based on crow's foot notation for the relationship's cardinality and modality.
Returns:
Type | Description |
---|---|
str
|
DOT language specification for arrow shape of this edge's tail |
Source code in erdantic/core.py
437 438 439 440 441 442 443 444 445 446 447 |
|
target_dot_arrow_shape
¶
target_dot_arrow_shape() -> str
Arrow shape specification in Graphviz DOT language for this edge's head (the end at the target model). See Graphviz docs as a reference. Shape returned is based on crow's foot notation for the relationship's cardinality and modality.
Returns:
Type | Description |
---|---|
str
|
DOT language specification for arrow shape of this edge's head |
Source code in erdantic/core.py
425 426 427 428 429 430 431 432 433 434 435 |
|
EntityRelationshipDiagram
¶
Bases: BaseModel
Holds information about an entity relationship diagram for a set of data model classes and their relationships, and provides methods to render the diagram.
Attributes:
Name | Type | Description |
---|---|---|
models |
SortedDict[str, ModelInfo]
|
Mapping of ModelInfo instances for models included in the diagram. Each key is the string representation of the fully qualified name of the model. |
edges |
SortedSet[Edge]
|
Set of edges representing relationships between the models. |
Source code in erdantic/core.py
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 |
|
add_model
¶
add_model(model: type, recurse=True)
Add a data model class to the diagram.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
type
|
Data model class to add to the diagram. |
required |
recurse |
bool
|
Whether to recursively add models referenced by fields of the given model. Defaults to True. |
True
|
Raises:
Type | Description |
---|---|
UnknownModelTypeError
|
If the model is not recognized as a data model class type that is supported by registered plugins. |
UnresolvableForwardRefError
|
If the model contains a forward reference that cannot be automatically resolved. |
Source code in erdantic/core.py
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 |
|
draw
¶
draw(
out: Union[str, PathLike],
graph_attr: Optional[Mapping[str, Any]] = None,
node_attr: Optional[Mapping[str, Any]] = None,
edge_attr: Optional[Mapping[str, Any]] = None,
**kwargs
)
Render entity relationship diagram for given data model classes to file. The file format can be inferred from the file extension. Typical formats include '.png', '.svg', and '.pdf'.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
out |
str | PathLike
|
Output file path for rendered diagram. |
required |
graph_attr |
Mapping[str, Any] | None
|
Override any graph attributes on
the |
None
|
node_attr |
Mapping[str, Any] | None
|
Override any node attributes for all
nodes on the |
None
|
edge_attr |
Mapping[str, Any] | None
|
Override any edge attributes for all
edges on the |
None
|
**kwargs |
Additional keyword arguments to
|
{}
|
Source code in erdantic/core.py
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 |
|
to_dot
¶
to_dot(
graph_attr: Optional[Mapping[str, Any]] = None,
node_attr: Optional[Mapping[str, Any]] = None,
edge_attr: Optional[Mapping[str, Any]] = None,
) -> str
Generate Graphviz DOT language representation of entity relationship diagram for given data model classes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
graph_attr |
Mapping[str, Any] | None
|
Override any graph attributes on
the |
None
|
node_attr |
Mapping[str, Any] | None
|
Override any node attributes for all
nodes on the |
None
|
edge_attr |
Mapping[str, Any] | None
|
Override any edge attributes for all
edges on the |
None
|
Returns:
Type | Description |
---|---|
str
|
DOT language representation of diagram |
Source code in erdantic/core.py
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 |
|
to_graphviz
¶
to_graphviz(
graph_attr: Optional[Mapping[str, Any]] = None,
node_attr: Optional[Mapping[str, Any]] = None,
edge_attr: Optional[Mapping[str, Any]] = None,
) -> AGraph
Return pygraphviz.AGraph
instance for diagram.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
graph_attr |
Mapping[str, Any] | None
|
Override any graph attributes on
the |
None
|
node_attr |
Mapping[str, Any] | None
|
Override any node attributes for all
nodes on the |
None
|
edge_attr |
Mapping[str, Any] | None
|
Override any edge attributes for all
edges on the |
None
|
Returns:
Type | Description |
---|---|
AGraph
|
graph object for diagram |
Source code in erdantic/core.py
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 |
|
FieldInfo
¶
Bases: BaseModel
Holds information about a field of an analyzed data model class.
Attributes:
Name | Type | Description |
---|---|---|
model_full_name |
FullyQualifiedName
|
Fully qualified name of the data model class that the field belongs to. |
name |
str
|
Name of the field. |
type_name |
str
|
String representation of the field's type. |
Source code in erdantic/core.py
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
|
key
property
¶
key: str
Returns the key used to identify this instance of FieldInfo in the ModelInfo.fields mapping. This value is the value in the 'name' field.
raw_type
property
¶
raw_type: type
Returns the raw type annotation of the field. This is a cached property. If the raw type is not already known, it will attempt to import the data model class and reextract the field's type annotation.
Raises:
Type | Description |
---|---|
FieldNotFoundError
|
description |
UnknownModelTypeError
|
description |
Returns:
Type | Description |
---|---|
type
|
Type annotation. |
from_raw_type
classmethod
¶
from_raw_type(
model_full_name: FullyQualifiedName,
name: str,
raw_type: type,
) -> Self
Constructor method to create a new instance from a raw type annotation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_full_name |
FullyQualifiedName
|
Fully qualified name of the data model class that the field belongs to. |
required |
name |
str
|
Name of field. |
required |
raw_type |
type
|
Type annotation. |
required |
Returns:
Type | Description |
---|---|
Self
|
description |
Source code in erdantic/core.py
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
|
to_dot_row
¶
to_dot_row() -> str
Returns the DOT language "HTML-like" syntax specification of a row detailing this field
that is part of a table describing the field's parent data model. It is used as part the
label
attribute of data model's node in the graph's DOT representation.
Returns:
Type | Description |
---|---|
str
|
DOT language for table row |
Source code in erdantic/core.py
187 188 189 190 191 192 193 194 195 |
|
FullyQualifiedName
¶
Bases: BaseModel
Holds the fully qualified name components (module and qualified name) of a Python object. This is used to uniquely identify an object, can be used to import it.
Attributes:
Name | Type | Description |
---|---|---|
module |
str
|
Name of the module that the object is defined in. |
qual_name |
str
|
Qualified name of the object. |
Source code in erdantic/core.py
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
|
from_object
classmethod
¶
from_object(obj: Any) -> Self
Constructor method to create a new instance from a Python object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj |
Any
|
Python object. |
required |
Returns:
Type | Description |
---|---|
Self
|
Fully qualified name of the object. |
Source code in erdantic/core.py
64 65 66 67 68 69 70 71 72 73 74 |
|
import_object
¶
import_object() -> Any
Imports the object from the module and returns it.
Returns:
Type | Description |
---|---|
Any
|
Object referenced by this FullyQualifiedName instance. |
Source code in erdantic/core.py
82 83 84 85 86 87 88 89 90 91 92 |
|
Modality
¶
Bases: Enum
Enumeration of possible modality values for a relationship between two data model classes. Modality measures the minimum number of associations.
Source code in erdantic/core.py
325 326 327 328 329 330 331 332 333 334 335 336 337 338 |
|
to_dot
¶
to_dot() -> str
Returns the DOT language specification for the arrowhead styling associated with the modality value.
Source code in erdantic/core.py
334 335 336 337 338 |
|
ModelInfo
¶
Bases: BaseModel
, Generic[_ModelType]
Holds information about an analyzed data model class.
Attributes:
Name | Type | Description |
---|---|---|
full_name |
FullyQualifiedName
|
Fully qualified name of the data model class. |
name |
str
|
Name of the data model class. |
fields |
Dict[str, FieldInfo]
|
A mapping to FieldInfo instances for each field of the data model class. |
description |
str
|
Docstring or other description of the data model class. |
Source code in erdantic/core.py
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
|
key
property
¶
key: str
Returns the key used to identify this instance of ModelInfo in the
EntityRelationshipDiagram.models mapping. This value is the string representation of the
full_name
field.
raw_model
property
¶
raw_model: _ModelType
Returns the raw data model class. This is a cached property. If the raw model is not already known, it will attempt to import the data model class.
from_raw_model
classmethod
¶
from_raw_model(raw_model: _ModelType) -> Self
Constructor method to create a new instance from a raw data model class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
raw_model |
type
|
Data model class. |
required |
Returns:
Type | Description |
---|---|
Self
|
New instance of ModelInfo. |
Source code in erdantic/core.py
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
|
to_dot_label
¶
to_dot_label() -> str
Returns the DOT language "HTML-like" syntax specification of a table for this data
model. It is used as the label
attribute of data model's node in the graph's DOT
representation.
Returns:
Type | Description |
---|---|
str
|
DOT language for table |
Source code in erdantic/core.py
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
|