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

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

×