Skip to content

compat

python engine compatibility module.

Examples:

from pyrevit.compat import IRONPY2711
from pyrevit.compat import safe_strtype

Attributes

PY2 = sys.version_info[0] == 2 module-attribute

PY3 = sys.version_info[0] == 3 module-attribute

IRONPY = '.net' in sys.version.lower() module-attribute

IRONPY2 = PY2 and IRONPY module-attribute

IRONPY3 = PY3 and IRONPY module-attribute

NETCORE = System.Environment.Version.Major >= 8 module-attribute

NETFRAMEWORK = not NETCORE module-attribute

NO_REVIT = -1 module-attribute

REVIT_NETCORE_VERSION = 2025 module-attribute

safe_strtype = str module-attribute

Functions

get_elementid_value_func()

Returns the ElementId value extraction function based on the Revit version.

Follows API changes in Revit 2024.

Returns:

Name Type Description
function

A function returns the value of an ElementId.

Examples:

get_elementid_value = get_elementid_value_func()
sheet_revids = {get_elementid_value(x) for x in self.revit_sheet.GetAllRevisionIds()}
add_sheet_revids = {get_elementid_value(x) x in self.revit_sheet.GetAdditionalRevisionIds()}
Source code in pyrevitlib/pyrevit/compat.py
def get_elementid_value_func():
    """Returns the ElementId value extraction function based on the Revit version.

    Follows API changes in Revit 2024.

    Returns:
        function: A function returns the value of an ElementId.

    Examples:
        ```python
        get_elementid_value = get_elementid_value_func()
        sheet_revids = {get_elementid_value(x) for x in self.revit_sheet.GetAllRevisionIds()}
        add_sheet_revids = {get_elementid_value(x) x in self.revit_sheet.GetAdditionalRevisionIds()}
        ```
    """
    def get_value_post2024(item):
        return item.Value

    def get_value_pre2024(item):
        return item.IntegerValue

    return get_value_post2024 if _get_revit_version() > 2023 else get_value_pre2024

urlopen(url)

Urlopen wrapper.

Parameters:

Name Type Description Default
url str

request url

required
Source code in pyrevitlib/pyrevit/compat.py
def urlopen(url):
    """Urlopen wrapper.

    Args:
        url (str): request url
    """
    if PY3:
        return urllib.request.urlopen(url)
    return urllib2.urlopen(url)

make_request(url, headers, data)

Urlopen wrapper to create and send a request.

Parameters:

Name Type Description Default
url str

request url

required
headers dict[str, str]

headers

required
data bytes | None

request data

required
Source code in pyrevitlib/pyrevit/compat.py
def make_request(url, headers, data):
    """Urlopen wrapper to create and send a request.

    Args:
        url (str): request url
        headers (dict[str, str]): headers
        data (bytes | None): request data
    """
    if PY3:
        req = urllib.request.Request(url, headers, data)
        urllib.request.urlopen(req).close()
        return

    req = urllib2.Request(url, headers, data)
    urllib2.urlopen(req).close()