| 464 | |
| 465 | |
| 466 | class GraphMLWriter(GraphML): |
| 467 | def __init__( |
| 468 | self, |
| 469 | graph=None, |
| 470 | encoding="utf-8", |
| 471 | prettyprint=True, |
| 472 | infer_numeric_types=False, |
| 473 | named_key_ids=False, |
| 474 | edge_id_from_attribute=None, |
| 475 | ): |
| 476 | self.construct_types() |
| 477 | from xml.etree.ElementTree import Element |
| 478 | |
| 479 | self.myElement = Element |
| 480 | |
| 481 | self.infer_numeric_types = infer_numeric_types |
| 482 | self.prettyprint = prettyprint |
| 483 | self.named_key_ids = named_key_ids |
| 484 | self.edge_id_from_attribute = edge_id_from_attribute |
| 485 | self.encoding = encoding |
| 486 | self.xml = self.myElement( |
| 487 | "graphml", |
| 488 | { |
| 489 | "xmlns": self.NS_GRAPHML, |
| 490 | "xmlns:xsi": self.NS_XSI, |
| 491 | "xsi:schemaLocation": self.SCHEMALOCATION, |
| 492 | }, |
| 493 | ) |
| 494 | self.keys = {} |
| 495 | self.attributes = defaultdict(list) |
| 496 | self.attribute_types = defaultdict(set) |
| 497 | |
| 498 | if graph is not None: |
| 499 | self.add_graph_element(graph) |
| 500 | |
| 501 | def __str__(self): |
| 502 | from xml.etree.ElementTree import tostring |
| 503 | |
| 504 | if self.prettyprint: |
| 505 | self.indent(self.xml) |
| 506 | s = tostring(self.xml).decode(self.encoding) |
| 507 | return s |
| 508 | |
| 509 | def attr_type(self, name, scope, value): |
| 510 | """Infer the attribute type of data named name. Currently this only |
| 511 | supports inference of numeric types. |
| 512 | |
| 513 | If self.infer_numeric_types is false, type is used. Otherwise, pick the |
| 514 | most general of types found across all values with name and scope. This |
| 515 | means edges with data named 'weight' are treated separately from nodes |
| 516 | with data named 'weight'. |
| 517 | """ |
| 518 | if self.infer_numeric_types: |
| 519 | types = self.attribute_types[(name, scope)] |
| 520 | |
| 521 | if len(types) > 1: |
| 522 | types = {self.get_xml_type(t) for t in types} |
| 523 | if "string" in types: |
no outgoing calls