Created
May 13, 2020 19:37
-
-
Save hsolbrig/b9412e9557b47189bfcfba40089faf2b to your computer and use it in GitHub Desktop.
rdflib turtle serializer with 4.x behavior
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from rdflib import plugin | |
from rdflib.plugins.serializers.turtle import TurtleSerializer | |
from rdflib.serializer import Serializer | |
class Cornucopia: | |
""" | |
An iterator that claims to contain everything | |
""" | |
def __iter__(self): | |
return self | |
def __contains__(self, item): | |
return True | |
class TurtleWithPrefixes(TurtleSerializer): | |
""" A turtle serializer that always emits prefixes """ | |
def __init__(self, store): | |
super().__init__(store) | |
self.roundtrip_prefixes = Cornucopia() | |
# You have to call register to make this plugin active | |
def register(): | |
plugin.register('tortoise', Serializer, 'pyshex.utils.tortoise', 'TurtleWithPrefixes') | |
# g.serialize(format="tortoise") has the same behavior as "turtle" w/ exception that all declared prefixes are emitted |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment