erivlis /
graphinate
| 1 | import graphinate |
||
| 2 | |||
| 3 | |||
| 4 | def caffeine_graph_model(): |
||
| 5 | """ |
||
| 6 | Create a graph model for the caffeine molecule (C8H10N4O2). |
||
| 7 | |||
| 8 | Returns: |
||
| 9 | GraphModel: A graph model representing the caffeine molecule. |
||
| 10 | """ |
||
| 11 | graph_model = graphinate.model(name="Caffeine Molecule") |
||
| 12 | |||
| 13 | # Define atoms |
||
| 14 | atoms = [ |
||
| 15 | ('C1', 'C'), ('C2', 'C'), ('C3', 'C'), ('C4', 'C'), ('C5', 'C'), ('C6', 'C'), |
||
| 16 | ('C7', 'C'), ('C8', 'C'), ('H1', 'H'), ('H2', 'H'), ('H3', 'H'), ('H4', 'H'), |
||
| 17 | ('H5', 'H'), ('H6', 'H'), ('H7', 'H'), ('H8', 'H'), ('H9', 'H'), ('H10', 'H'), |
||
| 18 | ('N1', 'N'), ('N2', 'N'), ('N3', 'N'), ('N4', 'N'), ('O1', 'O'), ('O2', 'O') |
||
| 19 | ] |
||
| 20 | |||
| 21 | # Define bonds |
||
| 22 | bonds = [ |
||
| 23 | ('C1', 'C2'), ('C1', 'N1'), ('C1', 'H1'), ('C2', 'C3'), ('C2', 'N2'), ('C3', 'C4'), |
||
| 24 | ('C6', 'C7'), ('C7', 'C8'), ('C7', 'H4'), ('C8', 'O1'), ('C8', 'O2'), ('N1', 'H5'), |
||
| 25 | ('N2', 'H6'), ('N3', 'H7'), ('N4', 'H8'), ('O1', 'H9'), ('O2', 'H10') |
||
| 26 | ] |
||
| 27 | |||
| 28 | # Add nodes (atoms) |
||
| 29 | @graph_model.node(lambda x: x[1], key=lambda x: x[0], label=lambda x: x[1]) |
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||
| 30 | def atom(): |
||
| 31 | yield from atoms |
||
| 32 | |||
| 33 | @graph_model.edge() |
||
| 34 | def bond(): |
||
| 35 | for bond in bonds: |
||
| 36 | yield {'source': bond[0], 'target': bond[1]} |
||
| 37 | |||
| 38 | return graph_model |
||
| 39 | |||
| 40 | |||
| 41 | if __name__ == '__main__': |
||
| 42 | model = caffeine_graph_model() |
||
| 43 | schema = graphinate.builders.GraphQLBuilder(model).build() |
||
| 44 | graphinate.graphql.server(schema) |
||
| 45 |