Skip to content

Instantly share code, notes, and snippets.

@hsolbrig
Created May 13, 2020 19:37
Show Gist options
  • Save hsolbrig/b9412e9557b47189bfcfba40089faf2b to your computer and use it in GitHub Desktop.
Save hsolbrig/b9412e9557b47189bfcfba40089faf2b to your computer and use it in GitHub Desktop.
rdflib turtle serializer with 4.x behavior
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