• Skip to main content
  • Skip to search
  • Skip to footer
Cadence Home
  • This search text may be transcribed, used, stored, or accessed by our third-party service providers per our Cookie Policy and Privacy Policy.

  1. Community Forums
  2. Adams Car
  3. Problem with python code in macro called from Adams/Car...

Following Cadence Design Systems’ acquisition of the Design & Engineering (D&E) business, the D&E Community Forum on the Nexus Community Center has been migrated in the Cadence Physical System Simulation Community. For more details and if you have any questions, please refer to the FAQ.

Stats

  • State Not Answered
  • Replies 7
  • Subscribers 1
  • Views 2
  • Members are here 0
More Content

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

1ondrej.petergac
1ondrej.petergac over 6 years ago
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
  • Sign in to reply
  • Cancel
  • Kent West
    0 Kent West over 6 years ago
    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
     
     
    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • 1ondrej.petergac
    0 1ondrej.petergac over 6 years ago
    Hi Kent,
    thanks a lot for your answer it helped a lot.
    Both your ways are working:
    1) read python file method
    file python read &
      file_name = (eval(getenv("TOPDIR")//"/python_code/rm_files.py))
     
    2) run_python_code method
    !--- Run PYTHON code
    variable set variable_name=$_self.imp    integer_value=(eval(run_python_code("import glob, os, os.path")))
    variable set variable_name=$_self.dir    integer_value=(eval(run_python_code("dir = os.getcwd()")))
    variable set variable_name=$_self.files  integer_value=(eval(run_python_code("filelist = glob.glob(os.path.join(dir, 'tmp_*'))")))
    variable set variable_name=$_self.frmv   integer_value=(eval(run_python_code("junk = [os.remove(f) for f in filelist]")))
     Thanks for your help. 
    It is quite unfortunate that there is a limitation for single line python commads.
     
    Is MSC working on an update to run more sophisticated multiline python codes?
     
    Best regards,
    Ondrej
    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • MK202610123211
    0 MK202610123211 over 6 years ago
    Why should someone take the burden and write Python code in command language instead of putting it in a file and edit this with an editor with syntax highlighting or even a more complex environment like IDLE, Anaconda,Spyder, PYCharm, Eclipse or whatever ?
    Especially since there is the command:
    file python read file = "xxx.py"
     
    Nevertheless:
    Have you tried to enter that code when switching the command window to Py-input ?
     
     
    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • 1ondrej.petergac
    0 1ondrej.petergac over 6 years ago
    Hello Martin,
     
    it was just 5 lines of code and I wanted to avoid the python file.
     
    Best regards,
    Ondrej
    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • MK202610123211
    0 MK202610123211 over 6 years ago
    Another workaround with four additional lines would be to dump the desired content into an intermediate python file and simply read/cleanup after creation.
     
    file text open file="test.py" open_mode = overwrite
    file text write format="import glob, os, os.path"
    file text write format="cwd = os.getcwd()"
    file text write format="filelist = glob.glob(os.path.join(cwd, 'tmp_*'))"
    file text write format="for f in filelist:"
    file text write format="  os.remove(f)"
    file text close file="test.py"
    file python read file = "test.py"
    var set var = DeleteIt int = (eval(REMOVE_FILE("test.py"))
     
    It's still using a python file, but saves the administrative work of having files installed & found on the target machine (especially TOPDIR is one of the top-forbidden zones in our IT)
    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
>
Cadence Guidelines

Community Guidelines

The Cadence Design Communities support Cadence users and technologists interacting to exchange ideas, news, technical information, and best practices to solve problems and get the most from Cadence technology. The community is open to everyone, and to provide the most value, we require participants to follow our Community Guidelines that facilitate a quality exchange of ideas and information. By accessing, contributing, using or downloading any materials from the site, you agree to be bound by the full Community Guidelines.

© 2026 Cadence Design Systems, Inc. All Rights Reserved.

  • Terms of Use
  • Privacy
  • Cookie Policy
  • US Trademarks
  • Do Not Sell or Share My Personal Information