Python

Class Selector

Q How do I choose between one of several classes using Python?

A Here's a small Python program that uses match to select a class.

Python Implementation

As you know, every Python object is a class. A class has several attributes, including a name found using __class__. However, when a class is nested in other object, that name can become mangled. __qualname__ helps to untangle nested class names.

We use __qualname__ in this example to differentiate different MicroStation helper classes …

#   Where text is found
TEXT_SOURCE_PRIMITIVE = "text element"
TEXT_SOURCE_CELL      = "cell"
TEXT_SOURCE_NODE      = "text node"
TEXT_SOURCE_TABLE     = "text table"
TEXT_SOURCE_UNKNOWN   = "unknown"

def DescribeTextSource (handler: Handler)->str:
    # A text element may be found stand-alone or embedded in a text node, cell or text table.
    # Some applications may find it useful to know the source of a text element.
    match handler.__class__.__qualname__:
        case TextElemHandler.__qualname__:
            return TEXT_SOURCE_PRIMITIVE
        case TextNodeHandler.__qualname__:
            return TEXT_SOURCE_NODE
        case TextTableHandler.__qualname__:
            return TEXT_SOURCE_TABLE
        case NormalCellHeaderHandler.__qualname__:
            return TEXT_SOURCE_CELL
        case _:
            return TEXT_SOURCE_UNKNOWN

The above code is not intended to be used stand-alone. It's expected to be used in a larger program, such as our Text Exporter.

Python Manager

Use MicroStation's Python Manager to find and execute the script.

Questions

Post questions about MicroStation programming to the MicroStation Programming Forum.