hexagon logo

Problem with python code in macro called from Adams/Car 2018

Hello,
 
I am trying to run python code from the macro in the Adams/Car 2018.
 
The python code is following:
 
!--- Run PYTHON code
variable set variable_name=$_self.intTmp integer_value=(eval(run_python_code("import glob, os, os.path")))
variable set variable_name=$_self.intTmp integer_value=(eval(run_python_code("cwd = os.getcwd()")))
variable set variable_name=$_self.intTmp integer_value=(eval(run_python_code("filelist = glob.glob(os.path.join(cwd, 'tmp_*'))")))
variable set variable_name=$_self.intTmp integer_value=(eval(run_python_code("for f in filelist:")))
variable set variable_name=$_self.intTmp integer_value=(eval(run_python_code("   os.remove(f)")))
!
 
When the python code is called in Adams/Car 2018 then I get following error message in the log file:
 
!
! ---------------------------------------------------
!  File "<string>", line 1
!    for f in filelist:
!                     ^
! SyntaxError: unexpected EOF while parsing
!  File "<string>", line 1
!    os.remove(f)
!    ^
! IndentationError: unexpected indent
 
When I run the same python code from DOS shell with command: adams2018 python ***.py then the code runs without any problem.
 
What could be the problem?
 
Can a python for loop be started in macro with (eval(run_python_code(…)))?
 
Thanks and best regards,
Ondrej
Parents
  • Hi Ondrej!
     
    I think that you have hit a limitation of the RUN_PYTHON_CODE() function: it only accepts single-line commands.
     
    Two possible ways to go:
     
    1. Usually you'd try to get your .py commands into a .py file. Then you just exec() the Python file (or use the cmd language "file python read file_name = ...." ). This can be a bit of a pain, though, if you're not sure where your .py file is. You might need to add to sys.path, or similar, to get the PATH setup to find your .py file.
    2. Here's another Pythonic way, using a list comprehension:
     
    Python code, test this out:
     
    junk=[print(item) for item in range(4)]
     
    I've not tried with your exact code, but I think it is this:
     
    var set var=pydummy int=(eval(run_python_code("junk = [os.remove(f) for f in filelist]")))
     
     
    HTH,
    Kent
     
     
Reply
  • Hi Ondrej!
     
    I think that you have hit a limitation of the RUN_PYTHON_CODE() function: it only accepts single-line commands.
     
    Two possible ways to go:
     
    1. Usually you'd try to get your .py commands into a .py file. Then you just exec() the Python file (or use the cmd language "file python read file_name = ...." ). This can be a bit of a pain, though, if you're not sure where your .py file is. You might need to add to sys.path, or similar, to get the PATH setup to find your .py file.
    2. Here's another Pythonic way, using a list comprehension:
     
    Python code, test this out:
     
    junk=[print(item) for item in range(4)]
     
    I've not tried with your exact code, but I think it is this:
     
    var set var=pydummy int=(eval(run_python_code("junk = [os.remove(f) for f in filelist]")))
     
     
    HTH,
    Kent
     
     
Children
No Data