Best practices to avoid long lines when using eval()

The new eval() is quite useful and handy, but I’m wondering what would be the recommended/best approach when using it with longer commands such as those used to collect versions of Python libraries? e.g. "python -c 'import long.library.namespace; print(long.library.namespace.__version__)'"

You could move the string into a helper function:

def pythonVersionCmd() {
  "python -c 'import long.library.namespace; print(long.library.namespace.__version__)'"
}

process PROC {
  output:
  eval( pythonVersionCmd() )

  // ...
}

Or you can use an env() output which is slightly more verbose but pretty close:

process PROC {
  output:
  env('PYTHON_VERSION')

  script:
  """
  export PYTHON_VERSION=$(python -c 'import long.library.namespace; print(long.library.namespace.__version__)')
  """
}

Thank you @bentsherman. The latter seems more reasonable, particularly if you have many modules in need of this version (so you don’t end up multiplying the pythonVersionCmd() function, or having to go the more complex route of the utils subworkflow).

You can have a module that just defines helper functions like pythonVersionCmd() that are included by other modules. You don’t have to have a subworkflow. That is just an nf-core convention that I personally find to be too restrictive, especially for cases like this one.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.