hexagon logo

Simple input in python script

Hello everyone, i am learning python, and trying simple programs using python in Adams.
My problem is how to ask for input string:
 
Something like:
name=input()
 
is giving error such:
 
File "<string>", line 1, in <module>
EOFError: EOF when reading a line
 
or macros in adams python should be just script without input?
 
Jozef
  • Hi Jozef,
     
    This area needs some improvement, I believe. Here's the current way to do this:
    • Write a function in your file, perhaps like this (please excuse the formatting - it's wrong..):
     
    def make_some_parts(num_parts=10, some_prefix='part')
    <do some python stuff in here>
     
    • Then either execfile() your file or import your file on the View command line. That puts the function into your python namespace in View.
    • Now you can run your function on the python command line like: make_some_parts(99, 'my_part')
     
    Or maybe you'd type this in the python command line:
    x=99
    pre = 'my_part'
    and then call your function like:
    make_some_parts(x, pre)
     
    Hope I've understood what you're after here,
    Kent
     
     
     
  • Thanks Kent,

    I was asking how or if is possible to use keyboard input in python in adams

    Afaik there is command in python, which is working for me in jupyter notebook but not in adams:

    name=input("Enter name")

    Jozef
  • Quick update, writing in command window: help(input)
     
    gives:
     
    Help on built-in function input in module __builtin__:
     
    input(...)
    input([prompt]) -> value
     
    Equivalent to eval(raw_input(prompt)).
     
    So function is there, it is just not working for me
     
     
  • Hi Jozef,
     
    Sorry: I should have addressed your question directly: you are correct: input() should work but it doesn't.
    The interpreter shell in Adams/View isn't the "usual" Python shell that you interact with. We have to do some input processing on anything that's typed in, unfortunately. Initially multi-line commands (for, def, while, if) didn't work properly and we had to do some special things to get around that. I'll log a defect report on input() and hope that we can fix it for a future version.
     
    I think that our final goal is to provide the 'standard' Python shell that can be incorporated into another tool, like a debugger. Then everything would just work properly and we wouldn't have limitations on the command line behaviour...
     
    Thanks,
    Kent
  • Thanks for answer, also i noticed difference using command help() in interpreter shell in Adams/View and "usual" Python shell. In Adams/Views is starts and after that quits. Which is imho very confusing. I was trying to see which modules are inside Adams/View Python Shell.
     
    And it will be great to setup name for both types of pythons, for future reference by users
     
    Jozef
  • I don't understand the intention of entering values in a shell inside ADAMS and from my point of view it's rather obsolete.
     
    The usual philosophy for user interaction is to write a dialog box that asks for values and supplies them to a macro. No tool asks you to enter values in a shell/command line.
    The only way to make ADAMS ask for manual entries in the command window ist to type a command there and use return before all mandatory options have been added. Then ADAMS is asking for them.
  • I understand that using dialog box for entering values is much better, but i am just beginning with Python, i am making course on Edx and this is first method of interaction with user, i am now dealing with functions in python so maybe after that i will be able to use better way.
     
     
  • Here is a completely different way to do this that a colleague shared with me. It is so unsupported that I didn't want to share it, but it is quite interesting so here goes:
     
    ---------------------
    import qt
    i, status = qt.QInputDialog().getInt(None, "get int", "number")
    print i
    ----------------------
     
    I say this is "so unsupported" because it is using the underlying graphics toolkit (Qt) directly. We don't document this nor support this because it is our internal tool that can change from release to release. So I don't recommend using it in any kind of 'production' capacity!
     
    Thanks,
    Kent
     
  • Hey guys,
     
    I just came across this discussion.
     
    @Kent:
     
    • Is your Qt-related suggested approach still valid?
    • Have new features been developed in ADAMS 2018?
    • Or where are we now wrt. Python implementation?
    • Is there a set of propotype Python code (Alpha, beta, whatever) stuff we can nibble at?
     
    At this point, my main hurdle is how to get data or input from ADAMS to Python in a structured approach.
     
    What I do now is mayor clunky:
    1. get the user input in a AView dialog,
    2. Store them in global vars in AView and
    3. Spoon them out in a Python module.
     
    But being able to directly build Python scripts with automated AView dialog box generation (in Qt?) would be an enormous step to get me going further, and motivate me to invest more in Python in ADAMS !!!!!,
     
    At this point, motivation is - 45.6 %
     
    Hope it helps,
     
    Regards,
     
    Chris