Jump to content
JGMS

Arrays from Delphi to Python and vice versa

Recommended Posts

I studied Demo 17 to use it in my application.
The following routine is what I made from it:

Function TPyForm.VertaalMetPython(Var Arr : Variant; TaalCode : String; TaalNr : integer) : Boolean;
 VAR
   Mem   : TStringList;
   
  begin
    Result := False;

   // Arr is created upfront with: Arr   := VarArrayCreate([0, FDQ_comps.RecordCount-1, 0, 5], VarVariant),
   // and populated with variant data in a for loop: ... VarArrayPut(Arr, FDQ_comps.Fieldbyname('nederlands').AsVariant, [I, 0]) ...;
   // PythonModule1.Engine     := PythonEngine1;
   // PythonModule1.ModuleName := 'Vertaal';

   PythonModule1.SetVarFromVariant( 'PArr', Arr );

   TRY
    Mem := TStringList.Create;
    With Mem DO
    begin
      Add('from deep_translator import GoogleTranslator');

      Add('taalcode = "' + taalcode +'"');
      Add('taalnr = ' + TaalNr.toString );
      Add('for j in range(len(Vertaal.PArr)):');
      Add('  translated = GoogleTranslator(source="nl", target=taalcode).translate(text= Vertaal.PArr[j, 0])' ); 
      Add('  Vertaal.PArr[j, taalnr] = translated');
    end;
     TRY
       PythonEngine1.ExecString( ansiString( Mem.text ) );
       Arr :=  PythonModule1.GetVarAsVariant( 'PArr' );
       Result := True;
     Except
       Result := False;
     END;
     FINALLY
       Mem.Free;
     END;
  end;

 

The input array for Python is populated correctly with data, but Python doesn't give any sign of action. The array is returned empty and the Result value is set to True.

 

What do I wrong? Please help.

 

Share this post


Link to post

Don't you get an error when you run your code?

 

Vertaal.PArr[j, taalnr] = translated

should be 

Vertaal.PArr[j][taalnr] = translated

 

Sample code:

 

  var VArr: Variant := VarArrayCreate([0, 1, 0, 1], VarVariant);
  VArr[0, 0] := 'I';
  VArr[1, 0] := 'You';

  PythonModule1.SetVarFromVariant( 'arr', VArr);

image.thumb.png.168ccff46cb3139967a894ae886a99c8.png

 

After you execute the code:

 

  var VArr := PythonModule1.GetVarAsVariant('arr');
  ShowMessage(Varr[1][1]);

 

image.png

 

Note that what you get with GetVarAsVariant is not a two-dimensional Variant array, but rather an one-dimensional Variant array containing one-dimensional variant arrays.

 

Before you rush to criticize my French, I know that I is Je in French :classic_wink:.

Edited by pyscripter

Share this post


Link to post

Thank you very much indeed @pyscripter!
You put me on the right track. The modulename had to be removed too, I understood from your example.
Now the function works like a charm:


 

   PythonModule1.SetVarFromVariant( 'Varr', Arr );

    Mem := TStringList.Create;
    With Mem DO
    begin
      Add('from deep_translator import GoogleTranslator');
      Add('from delphi_module import Varr');
      Add('taalcode = "' + taalcode +'"');
      Add('taalnr = ' + I2S(TaalNr) );
      Add('for j in range(len(Varr)):');
      Add('  translated = GoogleTranslator(source="nl", target=taalcode).translate(text=Varr[j][0])' );
      Add('  Varr[j][taalnr]=translated');
    end;
     TRY
 
    GetPythonEngine.Execstring(UTF8Encode(mem.text));
    Mem.Free;

    Arr :=  PythonModule1.GetVarAsVariant( 'Varr' ); // VAR parameter returned to calling function.
    

I am very happy now. Many thanks again and best wishes.
 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×