CATIA-Systems / FMPy

Simulate Functional Mockup Units (FMUs) in Python
Other
429 stars 118 forks source link

dump function does not correctly format output for long variable names #672

Open james-thunes opened 4 months ago

james-thunes commented 4 months ago

If a FMU has a variable with a long name, the columns output in the variables block of the dump command are not correctly justified.

To reproduce:

from fmpy import *
fmu = 'fmuWithLongVariableNames.fmu'

dump(fmu)

the variables block in the returned output (variable names changed for privacy reasons):

Variables (input, output)

  Name               Causality              Start Value  Unit     Description
  aaaaaaaaaaaaaaaaa01 input                         0.55
  bbbbbbbbbbbbbbbbb0 input                     200000.0
  ccccccccccccccccc0123 input                          0.5
  ddddddddddddddddd012 input                     200000.0
  eeeeeeeeeeeeeeeee01234567 input                       319.82
  fffffffffffffffff0123456789 input                        285.5
  ggggggggggggggggg01234567890 output
  hhhhhhhhhhhhhhhhh012345678 output

The issue appears to be on ln698 of util.py:

        l.append('  {:18} {:10} {:>23}  {:8} {}'.format(*args))

The column width could either be set to the max variable name length (in which case the code would need to loop over the variables to find the maximum length followed by adjustment to ln687 in that same file) or the variable names would need to be truncated. I would suggest that truncation may be the most straightforward of solutions. It has the potential to be confusing if the variable names differ only after character 18, but having a fixed width table printed is preferable in my opinion.

The line could be changed to this:

        l.append('  {:.18} {:10} {:>23}  {:8} {}'.format(*args))

The output with that change:

Variables (input, output)

  Name               Causality              Start Value  Unit     Description
  aaaaaaaaaaaaaaaaaa input                         0.55
  bbbbbbbbbbbbbbbbbb input                     200000.0
  cccccccccccccccccc input                          0.5
  dddddddddddddddddd input                     200000.0
  eeeeeeeeeeeeeeeeee input                       319.82
  ffffffffffffffffff input                        285.5
  gggggggggggggggggg output
  hhhhhhhhhhhhhhhhhh output