Skip to content

# `` compat

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

Functions:

- `ascii` –
- `bopen` –
- `cast_bytes` –
- `decodeStringEscape` –
- `decodeUnicodeEscape` –
- `sign` –

Attributes:

- `long_type` –
- `r_unicodeEscape` –

## `` 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`

    20
    21

    def ascii(stream):
        return codecs.getreader("ascii")(stream)

## `` bopen

    bopen(*args, **kwargs)

Source code in `rdflib/compat.py`

    24
    25
    26

    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`

    14
    15
    16
    17

    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`

    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69

    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`

     98
     99
    100
    101
    102

    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`

    32
    33
    34
    35
    36
    37

    def sign(n):
        if n < 0:
            return -1
        if n > 0:
            return 1
        return 0