Skip to main content

A graph structure that can take arbitrary properties.

Project description

prop-graph

A graph structure that can take arbitrary properties.

Usage

In the examples that follow, I'll be working with a graph that looks as follows:

                a                               g
            /       \                       /       \
        b               c               h               i
    /       \       /       \       /
d               e               f

Build graph

graph = Graph([
    ("a", "b"),
    ("a", "c"),
    ("b", "d"),
    ("b", "e"),
    ("c", "e"),
    ("c", "f"),
    ("g", "h"),
    ("g", "i"),
    ("h", "f"),
])

graph.print()
# a 
#         b 
#                 d 
#                 e 
#         c 
#                 e 
#                 f 
# g 
#         h 
#                 f 
#         i

Add properties

props = {
    "c": "interesting",
    "g": "interesting",
}
graph.add_property("type", props)

Finding nodes

parent_ids = graph.find_by(lambda n: len(n.children) > 0)
print(parent_ids)
# ['a', 'b', 'c', 'g', 'h']

root_ids = graph.find_by(lambda n: len(n.parents) == 0)
print(root_ids)
# ['a', 'g']

interesting_ids = graph.find_by(lambda n: n["type"] == "interesting")
print(interesting_ids)
# ['c', 'g']

Deleting nodes

graph.delete_by(lambda n: n["type"] == "interesting")
graph.print()
# a 
#         b 
#                 d 
#                 e 
#         e 
#         f 
# h 
#         f 
# i

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

propgraph-0.1.1.tar.gz (4.1 kB view hashes)

Uploaded Source

Built Distribution

propgraph-0.1.1-py2.py3-none-any.whl (3.2 kB view hashes)

Uploaded Python 2 Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page