Skip to content

Commit

Permalink
tweak formatting to be closer to PEP8 compliance
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesscottbrown committed Nov 20, 2020
1 parent 83643d4 commit 7233ce1
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 48 deletions.
72 changes: 41 additions & 31 deletions demo-custom-properties-nodes-edges.py
Original file line number Diff line number Diff line change
@@ -1,64 +1,74 @@
'''
"""
About: Demo script for utilising the pyyed "Adding Custom Properties for Node and Edge objects" feature.
Version: 0.1
yEd Version: 3.20
python Version: 3.4.3
Author: bockor
Date: 16 Nov 2020
'''
"""

import pyyed

g=pyyed.Graph()
g = pyyed.Graph()

#Define Node Custom Properties
# Define Node Custom Properties
'''
scope: node
name: name of the custom property
property_type: [string|boolean|int|double]
boolean: Java keywords [true|false]
default_value: any above datatype represented as a string
'''
g.define_custom_property("node","Population", "int", "0")
g.define_custom_property("node","Unemployment", "double", "0.0")
g.define_custom_property("node","Environmental Engagements", "boolean", "false")
g.define_custom_property("node","Mayor","string", "")
g.define_custom_property("node", "Population", "int", "0")
g.define_custom_property("node", "Unemployment", "double", "0.0")
g.define_custom_property("node", "Environmental Engagements", "boolean", "false")
g.define_custom_property("node", "Mayor", "string", "")

#Define Edge Custom Properties
# Define Edge Custom Properties
'''
scope: edge
name: name of the custom property
property_type: [string|boolean|int|double]
boolean: Java keywords [true|false]
default_value: any above datatype represented as a string
'''
g.define_custom_property("edge","Distance","int", "0")
g.define_custom_property("edge","Availability","double", "100.0")
g.define_custom_property("edge","Toll Free","boolean", "true")
g.define_custom_property("edge","Year of build","string", "")
g.define_custom_property("edge", "Distance", "int", "0")
g.define_custom_property("edge", "Availability", "double", "100.0")
g.define_custom_property("edge", "Toll Free", "boolean", "true")
g.define_custom_property("edge", "Year of build", "string", "")

#Create Nodes
g.add_node('Pasta City', custom_properties={"Population": "13000","Unemployment": "13.7","Environmental Engagements": "true","Mayor" : "Genarro"})
g.add_node('Wurst Stadt', custom_properties={"Population": "25100","Unemployment": "6.2","Mayor" : "Orlowsky"})
g.add_node('Gruyereville', custom_properties={"Population": "29650","Unemployment": "11.8","Environmental Engagements": "true","Mayor" : "Delage"})
# Create Nodes
g.add_node('Pasta City',
custom_properties={"Population": "13000", "Unemployment": "13.7", "Environmental Engagements": "true",
"Mayor": "Genarro"})
g.add_node('Wurst Stadt', custom_properties={"Population": "25100", "Unemployment": "6.2", "Mayor": "Orlowsky"})
g.add_node('Gruyereville',
custom_properties={"Population": "29650", "Unemployment": "11.8", "Environmental Engagements": "true",
"Mayor": "Delage"})

#Create Edges
g.add_edge("Pasta City", "Wurst Stadt", label='N666', arrowhead="none", custom_properties={"Year of build": "1974","Distance": "356","Toll Free": "false","Availability": "85.7"})
g.add_edge("Pasta City", "Gruyereville", label='E55', arrowhead="none", custom_properties={"Year of build": "1986","Distance": "1444","Availability": "96.7"})
g.add_edge("Gruyereville", "Wurst Stadt", label='E23', arrowhead="none", custom_properties={"Year of build": "2011","Distance": "740","Toll Free": "false"})
# Create Edges
g.add_edge("Pasta City", "Wurst Stadt", label='N666', arrowhead="none",
custom_properties={"Year of build": "1974", "Distance": "356", "Toll Free": "false", "Availability": "85.7"})
g.add_edge("Pasta City", "Gruyereville", label='E55', arrowhead="none",
custom_properties={"Year of build": "1986", "Distance": "1444", "Availability": "96.7"})
g.add_edge("Gruyereville", "Wurst Stadt", label='E23', arrowhead="none",
custom_properties={"Year of build": "2011", "Distance": "740", "Toll Free": "false"})

#Write Graph
# Write Graph
g.write_graph('demo-custom-properties-nodes-edges.graphml', pretty_print=True)

print(40 * "=")
print("DONE!")
print("")
print("Open the file in yEd now. Click on the nodes and the edges, press F6 and select the 'data' tab to view the custom properties")
print("")
print("Node Custom Properties will show up into the yEd 'Structure View Window'")
print("")
print("All Custom Properties definitions should be populated into the 'Manage Custom Properties Menu'")
print("")
print("To select Nodes & Edges based on Custom Properties open yEd 'Tools Menu' -> 'Select Elements' and define criteria in the 'Nodes' and 'Edges' tabs")
print("""
DONE!
Open the file in yEd now.
Click on the nodes and the edges, press F6 and select the 'data' tab to view the custom properties.
Node Custom Properties will show up into the yEd 'Structure View Window'.
All Custom Properties definitions should be populated into the 'Manage Custom Properties Menu'
To select Nodes & Edges based on Custom Properties open yEd 'Tools Menu' -> 'Select Elements' and define criteria in the 'Nodes' and 'Edges' tabs
""")
print(40 * "=")
print("")
2 changes: 1 addition & 1 deletion demo-group-in-group.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
n121 = group12.add_node("node121")
group122 = group12.add_group("sub122")

g.write_graph('test.2.graphml')
g.write_graph('test.2.graphml')
27 changes: 12 additions & 15 deletions pyyed/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@
custom_property_types = ["string", "int", "double", "boolean"]


class CustomPropertyDefinition():
class CustomPropertyDefinition:

def __init__(self, scope, name, property_type, default_value):
'''
def __init__(self, scope, name, property_type, default_value):
"""
scope: [node|edge]
name: name of the custom property
property_type: [string|boolean|int|double]
boolean: Java keywords [true|false]
default_value: any above datatype represented as a string
'''
"""
self.scope = scope
self.name = name
self.property_type = property_type
Expand Down Expand Up @@ -275,7 +275,7 @@ def __init__(self, node_name, label=None, label_alignment="center", shape="recta
# Handle Node Custom Properties
for name, definition in Node.custom_properties_defs.items():
if custom_properties:
for k,v in custom_properties.items():
for k, v in custom_properties.items():
if k not in Node.custom_properties_defs:
raise RuntimeWarning("key %s not recognised" % k)
if name == k:
Expand Down Expand Up @@ -324,7 +324,7 @@ def convert(self):
UML.set("stereotype", stereotype)

# Node Custom Properties
for name,definition in Node.custom_properties_defs.items():
for name, definition in Node.custom_properties_defs.items():
node_custom_prop = ET.SubElement(node, "data", key=definition.id)
node_custom_prop.text = getattr(self, name)

Expand Down Expand Up @@ -378,7 +378,7 @@ def __init__(self, node1, node2, label="", arrowhead="standard", arrowfoot="none
# Handle Edge Custom Properties
for name, definition in Edge.custom_properties_defs.items():
if custom_properties:
for k,v in custom_properties.items():
for k, v in custom_properties.items():
if k not in Edge.custom_properties_defs:
raise RuntimeWarning("key %s not recognised" % k)
if name == k:
Expand Down Expand Up @@ -416,7 +416,7 @@ def convert(self):
preferredPlacement="target_on_edge", **label_color_args).text = self.target_label

# Edge Custom Properties
for name,definition in Edge.custom_properties_defs.items():
for name, definition in Edge.custom_properties_defs.items():
edge_custom_prop = ET.SubElement(edge, "data", key=definition.id)
edge_custom_prop.text = getattr(self, name)

Expand All @@ -425,7 +425,7 @@ def convert(self):
#
@classmethod
def set_custom_properties_defs(cls, custom_property):
cls.custom_properties_defs[custom_property.name] = custom_property
cls.custom_properties_defs[custom_property.name] = custom_property


class Graph:
Expand Down Expand Up @@ -509,18 +509,15 @@ def add_node(self, node_name, **kwargs):
raise RuntimeWarning("Node %s already exists" % node_name)

node = Node(node_name, **kwargs)
self.nodes[node_name] = node
self.nodes[node_name] = node
self.existing_entities[node_name] = node
return node

def add_edge(self, node1_name, node2_name, **kwargs):
# pass node names, not actual node objects

node1 = self.existing_entities.get(node1_name) or \
self.add_node(node1_name)

node2 = self.existing_entities.get(node2_name) or \
self.add_node(node2_name)
self.existing_entities.get(node1_name) or self.add_node(node1_name)
self.existing_entities.get(node2_name) or self.add_node(node2_name)

self.num_edges += 1
kwargs['edge_id'] = str(self.num_edges)
Expand Down
5 changes: 4 additions & 1 deletion tests/test_pyyed.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def test_numeric_node_ids():
g = pyyed.Graph()
g.add_node(1, label="Node1")
g.add_node(2, label="Node2")
g.add_edge(1,2)
g.add_edge(1, 2)

assert g.nodes[1].label == "Node1"
assert g.nodes[2].label == "Node2"
Expand All @@ -106,6 +106,7 @@ def test_numeric_node_ids():

assert g.get_graph()


def test_multiple_edges():
g = pyyed.Graph()
g.add_node('a', font_family="Zapfino")
Expand All @@ -131,6 +132,7 @@ def test_multiple_edges():

assert g.get_graph()


def test_node_already_there_check():

g = pyyed.Graph()
Expand Down Expand Up @@ -176,6 +178,7 @@ def test_node_already_there_check():
with pytest.raises(RuntimeWarning):
g2.add_group('a')


def test_nested_graph_edges():
g = pyyed.Graph()
g.add_edge('a', 'b')
Expand Down

0 comments on commit 7233ce1

Please sign in to comment.