Skip to content

Must import as "from pythonfmu3 import ...", i.e. cannot just "import pythonfmu3" #6

@steve-biggs-fox

Description

@steve-biggs-fox

The documentation examples suggest to import pythonfmu3 as, for example:

from pythonfmu3 import Fmi3Causality, Fmi3Slave, Boolean, Int32, Float64, String

However, one may wish to import like this:

import pythonfmu3 as pyfmu3

Then, later in the code, do something like this:

class MyFMU(pyfmu3.Fmi3Slave):
    ...

However, when compiling the FMU, this will fail with the following error message:

  File "E:\Pegasus\sensors\fmu_venv\lib\site-packages\pythonfmu3\builder.py", line 27, in get_class_name
    return re.search(r'class (\w+)\(\s*Fmi3Slave\s*\)\s*:', data).group(1)
AttributeError: 'NoneType' object has no attribute 'group'

So, it appears that the pythonFMU3 code relies on the import style used in the documentation examples, because it searches for class declarations that look like this:

class PythonSlave(Fmi3Slave):
    ....

and not like the MyFMU case above.

Users should be able to import the code either way.

Incidentally, is searching for regular expressions in the code the best way to find classes that extend Fmi3Slave? Would it not be better to instantiate classes, test if they are an instance of Fmi3Slave (which will be True for all subclasses), then get the name via reflection? e.g. type(subclass_instance).__name__

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions