Recent

Author Topic: Running .py scripts using Python4Lazarus ?  (Read 6822 times)

docwat

  • New Member
  • *
  • Posts: 40
Running .py scripts using Python4Lazarus ?
« on: September 01, 2018, 05:24:46 pm »
Hi,

I have installed 'Python4Lazarus' to explore it's ability to embed in Lazarus a Python Script 'PS1.py' which collects data from a Wireless Network via a Cloud RestAPI and
dumps it to a .CSV file. P4L works fine on simple example scripts on my macine.

PS1.py runs successfully from my Windows 8.1 C:Prompt as:
 
        > c:\Code    python PS1.py  user   password   module    -- that is with three arguments which enable the script to access a Cloud account and to select the desired data node.

Hypothetically I would like to use the PythonEngine by entering the above sequence with it's arguments.
 
I imagine using the  PythonGUIInputOutput1 component to feed transitory data back into my FP environment to drive quasi real time displays, whilst the complete data passes to a shared SSD Solid State Drive within the host computer, to be extracted into Excel for subsequent scientific analysis(The data is feed weight consumed by cattle in a research environment).

As an octogenarian, hardware engineer, I am making heavy weather of deploying P4L to accept a Python script file of the form shown and would welcome any help from specialists in the application of P4L who might have accomplished running of a script file as above.

Very many thanks,

docwat

docwat

  • New Member
  • *
  • Posts: 40
Re: Running .py scripts using Python4Lazarus ?
« Reply #1 on: September 02, 2018, 02:16:15 pm »
Having experimented with the P4L python1 engine I conclude that trying to run the PS1.py is riddled with problems - maybe due to my Python ignorance.

The simplistic approach may be to simply load the PS1.py script into a TMEMO and to edit the values dependent upon the .py arguments. This would allow my main Free Pascal program to address the script and to manage the insertion of the arguments by text editing of the TMemo strings in lines  previously set by the three arguments.

There may be problems with Pascal strings replacing Python strings however since the Python script can be read in NOTEPAD I would suspect that the string compatability
problem is non-existent.

More experienced Python writers may have a much better solution ?

Thanks,

Docwat

Thaddy

  • Hero Member
  • *****
  • Posts: 14210
  • Probably until I exterminate Putin.
Re: Running .py scripts using Python4Lazarus ?
« Reply #2 on: September 02, 2018, 02:43:01 pm »
Python strings are PChars, not Pascal strings. FPC can handle both, but not Python. A cast to PChar usually helps, but not when the Pascal strings contain zero's in the middle:that's something Python simply can't handle. Be Aware, VERY Aware what the types really mean in the different languages.
« Last Edit: September 02, 2018, 02:45:21 pm by Thaddy »
Specialize a type, not a var.

docwat

  • New Member
  • *
  • Posts: 40
Re: Running .py scripts using Python4Lazarus ?
« Reply #3 on: September 02, 2018, 05:43:32 pm »
Thanks Thaddy,

Yes, a wholly relevant caution ! I will take account.

Docwat

docwat

  • New Member
  • *
  • Posts: 40
Re: Running .py scripts using Python4Lazarus ?
« Reply #4 on: September 02, 2018, 05:59:51 pm »
Another reality is that the line offsets of the .py script will have to be preserved. The script file therefore should have no extraneous spaces - in hope rather than expectation!

Docwat

Thaddy

  • Hero Member
  • *****
  • Posts: 14210
  • Probably until I exterminate Putin.
Re: Running .py scripts using Python4Lazarus ?
« Reply #5 on: September 02, 2018, 06:52:32 pm »
Another reality is that the line offsets of the .py script will have to be preserved. The script file therefore should have no extraneous spaces - in hope rather than expectation!

Docwat
In my experience Freepascal or Delphi strings casted to PChar never add or drop whitespace.
I also use Python mixed with FPC code and never ran into trouble. In both, a line break doesn't count to whitespace but has special meaning.
Specialize a type, not a var.

docwat

  • New Member
  • *
  • Posts: 40
Re: Running .py scripts using Python4Lazarus ?
« Reply #6 on: September 03, 2018, 12:07:11 am »
Looks as though the TMemo method for running my PS1.py  will work. At this stage P4L   PythonEngine1.ExecStrings(memo1.lines); Runs the script only showing the absence of expected arguments as the expected error.

I will work on this over the next few days and return the example as it may interest others trying P4L for the first time.

Thanks for your help Thaddy !

Thaddy

  • Hero Member
  • *****
  • Posts: 14210
  • Probably until I exterminate Putin.
Re: Running .py scripts using Python4Lazarus ?
« Reply #7 on: September 03, 2018, 07:09:00 am »
Note Python strings are unicode. Make sure you pass them as unicode.
Specialize a type, not a var.

scons

  • Full Member
  • ***
  • Posts: 141
Re: Running .py scripts using Python4Lazarus ?
« Reply #8 on: September 03, 2018, 12:48:52 pm »

I will work on this over the next few days and return the example as it may interest others trying P4L for the first time.


Did you manage to get it working with python 3.7 ?
Windows 10-64bit Lazarus 2.0.12 + FPC 3.2.0

AlexTP

  • Hero Member
  • *****
  • Posts: 2387
    • UVviewsoft
Re: Running .py scripts using Python4Lazarus ?
« Reply #9 on: September 03, 2018, 05:23:59 pm »
Python for Lazarus (package) was fixed by me for Python 3.7 about month ago. I have merged Py 3.7 fixes from Python4Delphi github.

scons

  • Full Member
  • ***
  • Posts: 141
Re: Running .py scripts using Python4Lazarus ?
« Reply #10 on: September 04, 2018, 08:37:57 am »
Ah, ok, grabbed it from github and it works fine, thanks !
Windows 10-64bit Lazarus 2.0.12 + FPC 3.2.0

docwat

  • New Member
  • *
  • Posts: 40
Re: Running .py scripts using Python4Lazarus ?
« Reply #11 on: September 05, 2018, 12:38:03 am »
The perceived problem of calling PS3.py and three arguments was easily circumvented by installing the arguments in the script and dumping the  original argparse routines:

# Program start
def main():
    # External Variables
    # These four parameters are the original .py arguments
    account = 'XXXXX@XXXX.com'
    password = 'xxxxxxxxxxx'
    device = '$101$0-0-0-db93e8690'
    startdate = '2018-01-25'

    deviceList = []
    deviceList.append(device)

    day = datetime.datetime.now().strftime("%d")
    ISOstartdate = ISOdateformatter(startdate)
    year = datetime.datetime.now().strftime("%Y")
    month = int(datetime.datetime.now().strftime("%m")) - 1
    ISOstartdate = "{}-{}-{}T00:00:00".format(year, month, day)
    tomorrow = str(int(day) + 1)
    ISOenddate = "{}{}T00:00:00".format(datetime.datetime.now().strftime("%Y-%m-"), tomorrow)

    # Pull metadata
    findmetadata(deviceList, ISOstartdate, ISOenddate, account, password)

# if run by command line, run main()
if __name__ == '__main__':
    main()

...
...

This allowed the script to run using TMemo components as in the simple Github Memo example format:

type

  { TP4L_TEST }

  TP4L_TEST = class(TForm)
    PythonEngine1: TPythonEngine;
    PythonGUIInputOutput1: TPythonGUIInputOutput;
    RS_Button: TButton;
    LS_Button: TButton;
    Memo1: TMemo;
    Memo2: TMemo;
    procedure LS_ButtonClick(Sender: TObject);
    procedure RS_ButtonClick(Sender: TObject);
  private

  public

  end;

var
  P4L_TEST: TP4L_TEST;

implementation

{$R *.lfm}

{ TP4L_TEST }

procedure TP4L_TEST.LS_ButtonClick(Sender: TObject);  #LOAD SCRIPT

var DatFileName:string;

begin
    DatFileName:= 'c:\Code\PS3.py';
    Memo1.Lines.LoadFromFile(DatFileName);
end;

procedure TP4L_TEST.RS_ButtonClick(Sender: TObject); #EXECUTE SCRIPT
begin
    PythonEngine1.ExecStrings(memo1.lines);
end;


The TPythonGUIInputOutput; component enable the scrip print() outputs to be displayed   The arguments are only used during system
commissioning and it is simple to set them directly iin Mem and to save thisback to the PS3.py .  This satisfies my requirements but
I would certainly be interested in seeing a better way !   It proves to me that P4L runs fine in Python 3.7 and in my own Win8.1
environment. The returned Memo2 data is read but it may require conversion from u' before parsing in the windows environment.

Docwat


 

TinyPortal © 2005-2018