Thanks to visit codestin.com
Credit goes to github.com

Skip to content

function "VarPyToStrings" #497

Closed
Closed
@LucaBertoncini

Description

@LucaBertoncini

Hi, today I started experimenting with your amazing library. I am using FreePascal version 3.3.1 and this is my OS: Linux Mint21 5.15.0-136-generic.

This is a simple test I did.

program TestPython1;

{$mode objfpc}{$H+}

uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
Classes, PythonEngine, VarPyth;

var
PyEngine : TPythonEngine;
Code : String;
Items : TStringList;

begin
PyEngine := TPythonEngine.Create(nil);
PyEngine.LoadDll;

MainModule.aList := VarPythonCreate(['First element', 'Second element', 'Third element'], stList);

Code := 'aList.append("Py first element!")' + sLineBreak +
'aList.append("Py second element!")' + sLineBreak +
'aList = sorted(aList)' + sLineBreak;

PyEngine.ExecString(Code);

if VarIsPythonList(MainModule.aList) then
begin
Items := TStringList.Create;
VarPyToStrings(MainModule.aList, Items);
writeln(Items.Text);
Items.Free;
end;

PyEngine.Free;
end.

I noticed a strange behavior of the "VarPyToStrings" function. I had to modify it in this way to get the desired output.

procedure VarPyToStrings(const AValue : Variant; const AStrings: TStrings);
var
V: Variant;
begin
for V in VarPyIterate(AValue) do
AStrings.Add(VarPythonAsString(V));
end;

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions