| Total Complexity | 5 |
| Total Lines | 41 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | import itertools |
||
| 2 | import string |
||
| 3 | import urllib.request |
||
| 4 | from collections.abc import Iterable |
||
| 5 | |||
| 6 | import graphinate |
||
| 7 | |||
| 8 | |||
| 9 | def word_model(title: str, words: Iterable[str]) -> graphinate.GraphModel: |
||
| 10 | graph_model = graphinate.model(name=title) |
||
| 11 | |||
| 12 | @graph_model.edge() |
||
| 13 | def edge(): |
||
| 14 | for w1, w2 in itertools.pairwise(words): |
||
| 15 | yield {'source': w1, 'target': w2} |
||
| 16 | |||
| 17 | return graph_model |
||
| 18 | |||
| 19 | |||
| 20 | def words(url: str): |
||
| 21 | data = urllib.request.urlopen(url) |
||
| 22 | for line in data: # files are iterable |
||
| 23 | for w in line.decode().strip().split(): |
||
| 24 | word = w.strip().lower().translate(str.maketrans('', '', string.punctuation)) |
||
| 25 | yield word |
||
| 26 | |||
| 27 | |||
| 28 | if __name__ == '__main__': |
||
| 29 | great_expectations = 'https://www.gutenberg.org/cache/epub/1400/pg1400.txt' |
||
| 30 | moby_dick = 'https://www.gutenberg.org/cache/epub/2701/pg2701.txt' |
||
| 31 | alice_in_wonderland = 'https://www.gutenberg.org/cache/epub/11/pg11.txt' |
||
| 32 | |||
| 33 | model = word_model('alice_in_wonderland', itertools.islice(words(alice_in_wonderland), 5000)) |
||
| 34 | builder = graphinate.builders.GraphQLBuilder(model) |
||
| 35 | |||
| 36 | # to create a Strawberry GraphQL schema |
||
| 37 | schema = builder.build() |
||
| 38 | |||
| 39 | # and serve it using Uvicorn web server |
||
| 40 | graphinate.graphql.server(schema) |
||
| 41 |