Recent

Author Topic: Interactive Tprocess?  (Read 8494 times)

Robz

  • New Member
  • *
  • Posts: 38
Interactive Tprocess?
« on: August 20, 2017, 05:15:11 am »
Hello,
I need to run python code in Ubuntu 17.04. However the python code uses hard coded variables. It is very simple to generate the variables using Lazarus with FPC. The python code typically is run in terminal mode.
For example: sudo python measureVNA.py r50
There are several questions to answer when run:
1. I need to enter the password
2. I need to answer "y" to continue.
3. I need to change a termination resistance on a hardware device then answer "Y" to continue.
In the terminal mode responding to the prompts is very simple.

However, I want to launch the python code from Lazarus.
Tprocess seems simple enough; but how do I answer the prompt questions?

I have read many examples of using Tprocess; but, none seem to target the above requirement.

Any help, suggestion, or even "hey dummy" is greatly appreciated! Please note; I know nothing about python. I have used C,C++,Pascal, even assembly; but never python. If possible do not use python lingo.

Thanks
Rob.

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Interactive Tprocess?
« Reply #1 on: August 20, 2017, 09:20:53 am »
« Last Edit: August 20, 2017, 09:30:00 am by Thaddy »
Specialize a type, not a var.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Interactive Tprocess?
« Reply #2 on: August 20, 2017, 11:55:05 pm »
Tprocess seems simple enough; but how do I answer the prompt questions?
The same way you read output from it, only use different property (input) and method (write). Don't forget to append a newline as that's what most prompt expects (Python's `input()` included) before releasing control back to the application.

Robz

  • New Member
  • *
  • Posts: 38
Re: Interactive Tprocess?
« Reply #3 on: August 21, 2017, 06:11:48 pm »
http://wiki.freepascal.org/Python_LCL_Bindings may be useful
and:
https://github.com/pyscripter/python4delphi also contains lazarus bindings

Thaddy, thank you for your reply. Is sounds like a flexible way to proceed; however I know nothing about "bindings". I have translated FORTRAN and C code to Pascal and never used the term "bindings". Many CS terms are way over my head; "servers" for example. I am not a CS engineer; but self taught. My background is EE and for the most part I program like a typical EE. Drives CS people nuts ;-). While not pure "pristine" spaghetti code, it does run fast.
The examples in your suggestion do not actually change python code to Pascal code; but rather, (just a guess) use the compiled python code to link with Pascal code. The example is not clear on the method.
Thank you
Rob

m.abudrais

  • Jr. Member
  • **
  • Posts: 52
Re: Interactive Tprocess?
« Reply #4 on: August 21, 2017, 08:54:57 pm »
LCL binding, means using Lazarus component lib from other language(other than pascal),in your case I suggest using write property  as Leledumbo suggested.
I attached a simple example.
don't forget the -i option for python interrupter.
« Last Edit: August 21, 2017, 09:57:39 pm by m.abudrais »

Almir.Bispo

  • Jr. Member
  • **
  • Posts: 91
  • CSV Comp DB is the Best NoSQL
    • CSV Comp DB (NoSQL)
Re: Interactive Tprocess?
« Reply #5 on: August 21, 2017, 10:56:19 pm »
try this
Code: Pascal  [Select][+][-]
  1. uses sysutils;
  2. procedure call_python;
  3. begin
  4. //set compiled or intepreter file name (full path)
  5. sysutils.ExecuteProcess('compiled or python interpreter file name','open',[]);
  6.  end;
  7. //how to use
  8. //call_python;
  9.  
CSV Comp DB Developer {Pascal Lover}

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: Interactive Tprocess?
« Reply #6 on: August 22, 2017, 12:55:13 am »
hello,
i have done a small python script (laztprocessTst.py ) to test interactive process :
Code: Python  [Select][+][-]
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3.  
  4. def main(args):
  5.     import getpass
  6.     pswd = getpass.getpass('my python Password:')
  7.     print('your python password is : ' + pswd)
  8.     input("Press Enter to continue...")
  9.     return 0
  10.  
  11. if __name__ == '__main__':
  12.     import sys
  13.     sys.exit(main(sys.argv))

to call this script from lazarus, i use sysutils.ExecuteProcess (Almir.Bispo post) . I launch the script from my prefered terminal (here lxterminal) :
Code: Pascal  [Select][+][-]
  1. sysutils.ExecuteProcess('/usr/bin/lxterminal',
  2. '-e sudo python3 /home/jurassic/Lazarus/laztprocessTst.py',[]);
result  in attachment.
Lazarus 1.8RC3  LUbuntu 16.04
Friendly, J.P

« Last Edit: August 22, 2017, 12:57:54 am by Jurassic Pork »
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

Edson

  • Hero Member
  • *****
  • Posts: 1301
Re: Interactive Tprocess?
« Reply #7 on: August 22, 2017, 01:11:27 am »
Maybe you can use my library: https://github.com/t-edson/UnTerminal
It let you interact with processes including the prompt detection.
Lazarus 2.2.6 - FPC 3.2.2 - x86_64-win64 on Windows 10

avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
Re: Interactive Tprocess?
« Reply #8 on: August 22, 2017, 02:52:24 am »
Maybe you can use my library: https://github.com/t-edson/UnTerminal
It let you interact with processes including the prompt detection.
Using English at least as second language would help your lib being more popular.  :D
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

Edson

  • Hero Member
  • *****
  • Posts: 1301
Re: Interactive Tprocess?
« Reply #9 on: August 22, 2017, 06:47:23 am »
Using English at least as second language would help your lib being more popular.  :D

Yes. It's just writng in english, take me a long time compared with writing in spanish.  :-[
Lazarus 2.2.6 - FPC 3.2.2 - x86_64-win64 on Windows 10

Robz

  • New Member
  • *
  • Posts: 38
Re: Interactive Tprocess?
« Reply #10 on: August 24, 2017, 02:08:45 am »
Maybe you can use my library: https://github.com/t-edson/UnTerminal
It let you interact with processes including the prompt detection.

Edson,

Thank you for your reply. Don't worry about your English. My wife is fully bi-lingual and can help. I will try your process along with suggestions from others shortly.

Es un placer recibir todas sus sugestiones, le agradesco mucho.
Rob

Robz

  • New Member
  • *
  • Posts: 38
Re: Interactive Tprocess?
« Reply #11 on: August 24, 2017, 02:43:52 am »

The same way you read output from it, only use different property (input) and method (write). Don't forget to append a newline as that's what most prompt expects (Python's `input()` included) before releasing control back to the application.

Jurassic Pork, Almir.Bispo Thank you. I will try your suggestion as I find time.
Much appreciated.


Thank you Leledumbo.
I been working on your suggestion, using Ubuntu 17.04. So far I have no problem with non-interactive python applications. No luck though with interactive python applications. The application is typically run in terminal mode, requiring user input. I planned to use a Tedit control for user input and Tmemo for application output. Since Process.Output and Process.Input are streams I think I may have messed up stream to text conversion.


Much appreciated,
Rob.

 

TinyPortal © 2005-2018