Skip to content

compat

Utility functions and objects to ease Python 2/3 compatibility, and different versions of support libraries.

Functions:

Attributes:

long_type module-attribute

long_type = int

r_unicodeEscape module-attribute

r_unicodeEscape = compile('(\\\\u[0-9A-Fa-f]{4}|\\\\U[0-9A-Fa-f]{8})')

ascii

ascii(stream)
Source code in rdflib/compat.py
def ascii(stream):
    return codecs.getreader("ascii")(stream)

bopen

bopen(*args, **kwargs)
Source code in rdflib/compat.py
def bopen(*args, **kwargs):
    # type error: No overload variant of "open" matches argument types "Tuple[Any, ...]", "str", "Dict[str, Any]"
    return open(*args, mode="rb", **kwargs)  # type: ignore[call-overload]

cast_bytes

cast_bytes(s, enc='utf-8')
Source code in rdflib/compat.py
def cast_bytes(s, enc="utf-8"):
    if isinstance(s, str):
        return s.encode(enc)
    return s

decodeStringEscape

decodeStringEscape(s)
Source code in rdflib/compat.py
def decodeStringEscape(s):  # noqa: N802
    warnings.warn(
        DeprecationWarning(
            "rdflib.compat.decodeStringEscape() is deprecated, "
            "it will be removed in rdflib 7.0.0. "
            "This function is not used anywhere in rdflib anymore "
            "and the utility that it does provide is not implemented correctly."
        )
    )
    r"""
    s is byte-string - replace \ escapes in string
    """

    s = s.replace("\\t", "\t")
    s = s.replace("\\n", "\n")
    s = s.replace("\\r", "\r")
    s = s.replace("\\b", "\b")
    s = s.replace("\\f", "\f")
    s = s.replace('\\"', '"')
    s = s.replace("\\'", "'")
    s = s.replace("\\\\", "\\")

    return s

decodeUnicodeEscape

decodeUnicodeEscape(escaped: str) -> str
Source code in rdflib/compat.py
def decodeUnicodeEscape(escaped: str) -> str:  # noqa: N802
    if "\\" not in escaped:
        # Most of times, there are no backslashes in strings.
        return escaped
    return _turtle_escape_pattern.sub(_turtle_escape_subber, escaped)

sign

sign(n)
Source code in rdflib/compat.py
def sign(n):
    if n < 0:
        return -1
    if n > 0:
        return 1
    return 0