Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

run_discovery_script_file accepts only non list data, flat dict #1533

Open
2 tasks done
mcMunich opened this issue Nov 12, 2024 · 2 comments
Open
2 tasks done

run_discovery_script_file accepts only non list data, flat dict #1533

mcMunich opened this issue Nov 12, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@mcMunich
Copy link

mcMunich commented Nov 12, 2024

🔍 Before submitting the issue

  • I have searched among the existing issues
  • I am using a Python virtual environment

🐞 Description of the bug

if I try to use the run_discovery_script_file to send anything other than individual string parameters, it fails. While this is slightly documented https://geometry.docs.pyansys.com/version/dev/api/ansys/geometry/core/modeler/Modeler.html#Modeler.run_discovery_script_file() it doesn't say that the dict can be exclusively flat dict. I have tried all variations (nested dictionaries, tuples, lists of strings etc) and only a key:string combination works for the script_args sent to a discovery file.
And, to make it useful in the future, it would be nice to be able to send more data to the disco launch.

📝 Steps to reproduce

run the following script to try to send a dict with two keys, the second key being a list of a single string, but it will fail with more than one also.

import tempfile
from ansys.geometry.core import launch_modeler_with_discovery
import os

version = 242

file_name = 'dummy.py'
path2dummy = os.path.join(tempfile.gettempdir(),file_name)

with open(path2dummy,'w') as f:
    f.write('#nothing')

input_dict = {'a':'1', 'b':['2']}

modeler_discovery = launch_modeler_with_discovery(product_version = version)

modeler = modeler_discovery

values = modeler.run_discovery_script_file(path2dummy, script_args = input_dict)

modeler.close()

💻 Which operating system are you using?

Windows

📀 Which ANSYS version are you using?

242 and/or 251

🐍 Which Python version are you using?

3.12

📦 Installed packages

ni
@mcMunich mcMunich added the bug Something isn't working label Nov 12, 2024
@RobPasMue
Copy link
Member

I completely agree - let me see if we can do something here to have it working.

@Alberto-DM
Copy link

The limitation is known.
I'm using this workaround to overcome the issue.

First I serialize every value in the args dict.
If args is the dictionary that I want to pass to the script, I will actually pass new_args:

new_args = copy.deepcopy(args)
for k, v in new_args.items():
    new_args[k] = json.dumps(v)

Then, inside the script that I will run, I add this lines at the beginning of the file to deserialize the arguments:

import json
keys = list(argsDict.Keys)
for k in keys:
    argsDict[k] = json.loads(argsDict[k])

In this way, the args values can be any structure that can be sterilized by json.dumps

I am actually doing this programmatically in a wrapper function of modeler.run_discovery_script_file so that I can directly pass any arg without worrying of its convertion.
You can also do the same for the result dictionary returned by the method (serialize inside the script and de-serialize outside).

@RobPasMue maybe a similar approach can be implemented natively in the method itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants