Recent

Author Topic: Running Pyrhon Script  (Read 1632 times)

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 535
  • Professional amateur ;-P
Re: Running Pyrhon Script
« Reply #15 on: April 15, 2021, 09:43:50 am »
Hey Trev,

@Gus - I dunno.

Results of your 1 + 2 options:

Quote
'--param1="car dealer" dealership sale sales manager'
"--param1='car dealer' dealership sale sales manager"

cf:

Quote
--param1=\"car dealer\" dealership sale sales manager

Something, definitely, got lost in translation ;)
Can you please elaborate on the point you're trying to make?
I think I'm too dumb, or too sleep deprived to make sense of it :) Really, really sorry mate :)

But none the less, have you never had to run php code on the command line and had to use double and single quotes to make it work?
Code: Bash  [Select][+][-]
  1. php -r 'echo "This is a string that can have expanded {$variables}";'
OR
Code: Bash  [Select][+][-]
  1. php -r "echo 'This is a string that cannot have expanded variables';"

That's what I was getting at, and maybe fumbled it on the code example.

Cheers,
Gus
Lazarus 2.1.0(trunk) FPC 3.3.1(trunk) Ubuntu 21.04 64b Dark Theme
Lazarus 2.0.12(stable) FPC 3.2.2(stable) Ubuntu 21.04 64b Dark Theme
http://github.com/gcarreno

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1475
  • Former Delphi 1-7, 10.2 user
Re: Running Pyrhon Script
« Reply #16 on: April 15, 2021, 09:58:18 am »
@Gus - the parameters were for a search engine. Your quoting looks sus to me cf engkin and note marcov's comment re Windows.
Lazarus 2.1 r65061 FPC 3.3.1 r49223 macOS 10.14.6 Xcode 11.3.1
Lazarus 2.1 r65182 FPC 3.3.1 r49223 macOS 11.4 aarch64 Xcode 12.4
Lazarus 2.1 r61574 FPC 3.3.1 r42318 FreeBSD 12.1 amd64 VMware VM
Lazarus 2.1 r61574 FPC 3.0.4 Ubuntu 20.04 Parallels VM
Lazarus 2.0.10 FPC 3.2.0 Win10 Parallels VM

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 9340
  • FPC developer.
Re: Running Pyrhon Script
« Reply #17 on: April 15, 2021, 10:14:39 am »
I've seen a comment that doubling quotes may help on windows, bu-

Code: Pascal  [Select][+][-]
  1.   arguments.add('long argument with "" quotes in it ');

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 535
  • Professional amateur ;-P
Re: Running Pyrhon Script
« Reply #18 on: April 15, 2021, 10:23:55 am »
Hey Trev,

@Gus - the parameters were for a search engine. Your quoting looks sus to me cf engkin and note marcov's comment re Windows.

Okydokes, got it, well 99%. I'm still defeated by the abbreviation "cf", that's the only thing confusing me now. Sorry  :-[

And you're right, I guess I was adding a tip on conjugating double and single quotes and probably not answering the OP  :-[

Cheers,
Gus
Lazarus 2.1.0(trunk) FPC 3.3.1(trunk) Ubuntu 21.04 64b Dark Theme
Lazarus 2.0.12(stable) FPC 3.2.2(stable) Ubuntu 21.04 64b Dark Theme
http://github.com/gcarreno

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1475
  • Former Delphi 1-7, 10.2 user
Re: Running Pyrhon Script
« Reply #19 on: April 15, 2021, 11:12:34 am »
I'm still defeated by the abbreviation "cf", that's the only thing confusing me now. Sorry  :-[

cf. An abbreviation meaning “compare.” It is short for the Latin word confer and instructs the reader to compare one thing with another.
Lazarus 2.1 r65061 FPC 3.3.1 r49223 macOS 10.14.6 Xcode 11.3.1
Lazarus 2.1 r65182 FPC 3.3.1 r49223 macOS 11.4 aarch64 Xcode 12.4
Lazarus 2.1 r61574 FPC 3.3.1 r42318 FreeBSD 12.1 amd64 VMware VM
Lazarus 2.1 r61574 FPC 3.0.4 Ubuntu 20.04 Parallels VM
Lazarus 2.0.10 FPC 3.2.0 Win10 Parallels VM

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 535
  • Professional amateur ;-P
Re: Running Pyrhon Script
« Reply #20 on: April 15, 2021, 11:23:01 am »
Hey Trev,

cf. An abbreviation meaning “compare.” It is short for the Latin word confer and instructs the reader to compare one thing with another.

Thanks Trev, appreciate the rather complete explanation. All is clear now!!

Cheers,
Gus
Lazarus 2.1.0(trunk) FPC 3.3.1(trunk) Ubuntu 21.04 64b Dark Theme
Lazarus 2.0.12(stable) FPC 3.2.2(stable) Ubuntu 21.04 64b Dark Theme
http://github.com/gcarreno

tatamata

  • Hero Member
  • *****
  • Posts: 757
    • ZMSQL - SQL enhanced in-memory database
Re: Running Pyrhon Script
« Reply #21 on: April 15, 2021, 03:57:59 pm »
I've seen a comment that doubling quotes may help on windows, bu-

Code: Pascal  [Select][+][-]
  1.   arguments.add('long argument with "" quotes in it ');
I tried with doubling quotes, instead "\" escaping, but it doesn't work too...
Code: Pascal  [Select][+][-]
  1.         //When search has a quotation mark, escape it with a backslash, AND surround it with quotation marks.
  2.         if ContainsText(vSearchPhrase, '"') then begin
  3.            {$IFDEF MSWINDOWS}
  4.            //UTF8StringReplace(vSearchPhrase, '"', '\"',[rfReplaceAll,rfIgnoreCase],''); //Alternative1 - not working
  5.            UTF8StringReplace(vSearchPhrase, '"', '""',[rfReplaceAll,rfIgnoreCase],''  //Alternative2 - not working
  6.            vSearchPhrase:='--search_phrase=' + vSearchPhrase;  //This does not work!
  7.            //vSearchPhrase:='"' + '--search_phrase=' + vSearchPhrase + '"';  //This does not work!
  8.            //vSearchPhrase:='--search_phrase=' + '"' + vSearchPhrase + '"';   //This does not works!
  9.            {$ELSE}
  10.            UTF8StringReplace(vSearchPhrase, '"', '\"',[rfReplaceAll,rfIgnoreCase],'');
  11.            vSearchPhrase:='--search_phrase=' + vSearchPhrase;
  12.            {$ENDIF}
  13.         end
  14.         else begin
  15.           vSearchPhrase:='--search_phrase=' + vSearchPhrase;
  16.         end;
  17.  
So, I am stucked for Windows...any other idea?

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 9340
  • FPC developer.
Re: Running Pyrhon Script
« Reply #22 on: April 15, 2021, 04:45:56 pm »
The Windows quote char turns out to be ^. Here are my test programs

parent.dpr:
Code: Pascal  [Select][+][-]
  1.  
  2. {$mode delphi}
  3. uses process;
  4. var s : string;
  5.  
  6. begin
  7.   runcommand('child.exe',['test^"^"123','bla^"bla'],s);
  8. writeln(s);
  9. end.

child.dpr:
Code: Pascal  [Select][+][-]
  1. {$mode delphi}
  2.  
  3. var i : Integer;
  4. begin
  5.   writeln(paramcount,'  parameters:');
  6.   for i:=0 to paramcount do
  7.       writeln(i:5,' "',paramstr(i),'"');
  8. end.

output:

Quote
2  parameters:
    0 "D:\Testing\process\child.exe"
    1 "test^^123"
    2 "bla^bla"

P.s. always try to reduce the program to as minimal as possible. Codedumps like the one you are posted are pretty useless.

engkin

  • Hero Member
  • *****
  • Posts: 2819
Re: Running Pyrhon Script
« Reply #23 on: April 15, 2021, 05:09:17 pm »
Attached is a project with a sample script. The script prints the parameters it receives. I tested it with Python 3.8.1.

Python executable is in the PATH. The parameters are in the memo. The output on my side is:
Quote from: txt
Just a simple test.
['--param1="car dealer" dealership sale sales manager',
'--param2=car dealer dealership sale sales manager']

The script I used is:
Code: Python  [Select][+][-]
  1. import sys
  2. print("Just a simple test.")
  3. print(sys.argv[1:])

« Last Edit: April 15, 2021, 05:25:28 pm by engkin »

tatamata

  • Hero Member
  • *****
  • Posts: 757
    • ZMSQL - SQL enhanced in-memory database
Re: Running Pyrhon Script
« Reply #24 on: April 16, 2021, 11:55:09 am »
Hey guys, I am sorry for confusion, it was my amazingly stupid error, I forgot to assign replaced value to the original variable vSearchPhrase:=UTF8StringReplace(vSearchPhrase, '"', '\"',[rfReplaceAll,rfIgnoreCase],'');

Bottom line: escaping double quotes with \" works for both Linux and Windows. In both systems, parameter string does not need to be additionally enclosed by quotes!
On Windows, escaping double quotes, by doubling the double quotes "" also works!

engkin

  • Hero Member
  • *****
  • Posts: 2819
Re: Running Pyrhon Script
« Reply #25 on: April 16, 2021, 06:53:13 pm »
One thing to be aware of, unintentionally escaping the escape character. For instance:
Quote
--param1=\"car dealer\" sales

if there is a backslash after dealer, it will escape the escape character:
Quote
--param1=\"car dealer\\" sales

more about this problem from Microsoft. (not everything is relevant)

In summary, make sure there are odd number of escape characters before the quotation mark.
« Last Edit: April 16, 2021, 06:55:06 pm by engkin »

 

TinyPortal © 2005-2018