I want to use the Delphi 4 Python components from here https://github.com/pyscripter/python4delphi
but I don't want to drop the components on a form, I want everything in code , my code goes like this :
var
PythonEngine_netA: TPythonEngine;
PythonInputOutput_netA: TPythonInputOutput;
begin
PythonEngine_netA := TPythonEngine.Create(Self);
PythonInputOutput_netA := TPythonInputOutput.Create(Self);
try
/// configure the components
PythonEngine_netA.DllName:='python39.dll';
PythonEngine_netA.IO := PythonInputOutput_netA;
PythonEngine_netA.UseLastKnownVersion := True;
PythonInputOutput_netA.OnSendUniData := PythonInputOutput_SendUniData;
PythonInputOutput_netA.UnicodeIO := True;
PythonInputOutput_netA.RawOutput := True;
/// execute the script
PythonEngine_netA.ExecString(UTF8Encode(mmo_pythoncode.text));
finally
PythonEngine_netA.free;
PythonInputOutput_netA.free;
end;
execution of this code fails, error msg : "Python is not properly initialized",
what did I miss to use this code ?
One quick look at PythonEngine.pas (or even better: always search all files for the error message to find out where and why an error is returned) tells me you missed calling PythonEngine_netA.Initialize().
Also note that /Demos describes:
Demo34 Dynamically creating, destroying and recreating PythonEngine. Uses PythonVersions
So please have a look at /Demos/Demo34/Unit1.pas how it is done there with (almost) no components. Or run the whole project in general, preferably in debug mode single stepping thru it be aware which method does what.
You just forgot to load the Dll:
PythonEngine_netA.UseLastKnownVersion:= True;
//PythonEngine_netA.opendll(PYDLL)
PythonEngine_netA.LoadDll;
PythonEngine_netA.IO:= PythonInputOutput_netA;
Related
I am learning XGBoost. I want to finish a demonstration with XGBoost python api.
when I use the function "xgboost.DMatrix" that data is set a file,silent is set True. However, the function "xgboost.DMatrix" always output some message "[23:28:44] 1441x10 matrix with 11528 entries loaded from file_name". was I setting error parameters? reference
This is interesting. The silent value is taken and passed to the Wrapper, but it seems like the wrapper doesn't really use it !
This shows the appropriate code https://github.com/dmlc/xgboost/blob/master/src/c_api/c_api.cc#L202
Which says:
int XGDMatrixCreateFromFile(const char *fname,
int silent,
DMatrixHandle *out) {
API_BEGIN();
if (rabit::IsDistributed()) {
LOG(CONSOLE) << "XGBoost distributed mode detected, "
<< "will split data among workers";
}
*out = new std::shared_ptr<DMatrix>(DMatrix::Load(fname, false, true));
API_END();
}
i.e. even though silent is a argument, it is not used in the function anywhere ... (very weird)
So, it seems right now, if you're using any of the wrappers (Python, R, julia, etc) the silent functionality for DMatrix won't work.
Is there any equivalent of DocTest for Delphi. I use DUnit but I like the Python DocTest idea. I saw some answer like here but I think that, for simple functions, a DocTest like could be OK.
My goal is to define my tests in the comment header when I write the function.
Like :
function Plus(i1, i2 : integer) : integer;
//>>> Check( Plus(1, 3) = 4)
begin
result := i1 + i2;
end;
The idea is that you can use a "console" to output some results to testing code, then compare the output text content to an expected value.
Take a look for instance at the regression tests available with the great DWSScript Open Source project. You'll find out some .pas files and some related .txt files.
For instance abs.pas:
var vf = 1.5;
var vi = 2;
var i : Integer := Abs(-vi);
PrintLn(i);
PrintLn(Abs(vi));
var f : Float := Abs(-vf);
PrintLn(f);
PrintLn(Abs(vf));
And the corresponding abs.txt content:
2
2
1.5
1.5
AFAIK there is no already existing solution by now integrate in the Delphi world.
Writing the test in the comment will lack for IDE auto-completion, and somewhat break the object pascal design. It would be something easy with DWS, but require to call the command-line Delphi compiler. Honestly, I do not see what is wrong having your own set of units dedicated to tests. A small piece of code with a for..to loop with fixed and random values will have a much better test coverage than a fixed set of parameters.
I am developing automated test cases for my app using selenium RC in python 2.7. When I am using wait_for_time_to_load(time) is throwing error as the timeout is variable in my app. Can anyone suggest me any other alternative for the function "wait_for_page_to_load" which does not take time as a parameter.
Thanks
Just to add to #rs79's code
int iteration = 0;
//checks the presence of element till a given no of iterations(say 20) to avoid infinite loop
while(!(selenium.isElementPresent("yourelement")) && iteration < 20){
Thread.sleep(1000);
iteration++;
}
again this is in java, hopefully you can apply same logic in python.
You could check for the absence of an expected element and keep waiting till it appears.
while (!(selenium.isElementPresent("your_element_identifier")==true)) {
selenium.setSpeed("10");
Thread.sleep(10);
}
Evidently, the code above is in Java, but applying the same login in Python should be trivial.
I am using Python4Delphi to embed Python in a Delphi program. Versions: Python 2.6.4, Delphi 2009, Windows XP.
The Delphi program crashes with EInvalidOp when importing json. I tracked it to the line
NaN, PosInf, NegInf = float('nan'), float('inf'), float('-inf')
in json.decoder.
Sure enough, the command float('nan') raises an EInvalidOp when run inside Python embedded in the Delphi program. When executed in the command line Python (same installation) it simply returns nan.
Any idea what is the difference between the Python standard startup and that of the embedded one that may result in such an error?
This is most likely that Python uses a different 8087 control word (CW) setting than Delphi.
Try this kind of code:
var
OldControlWord: Word;
begin
OldControlWord := Get8087CW();
Set8087CW($133F);
try
// perform your Python code here
finally
Set8087CW(OldControlWord);
end;
end;
See my blog article on the 8087 CW in Delphi for a more detailed explanation of the $133F value.
It needs the JCL for the T8087Precision type (which is in the Jcl8087 unit).
--jeroen
I use the following:
$1332 is the delphi default.
$1232 is the value to deal with Python Issue 9980.
procedure MaskFPUExceptions(ExceptionsMasked : boolean);
begin
// if ExceptionsMasked then
// Set8087CW($1332 or $3F)
// else
// Set8087CW($1332);
if ExceptionsMasked then
Set8087CW($1232 or $3F)
else
Set8087CW($1232);
end;
I am having an intermittent error causing my Python module to crash, and I'm assuming it's because of a memory error occurring by not getting the refcounts correct in the c code. I have a bit of code that gets a response at a random time from a remote location. Based on the data received, it needs to update a data variable which I should have access to in Python. What's the best way to accomplish this? The following code runs most of the time, and it works correctly when it does, but when it doesn't it crashes Python (bringing up the visual studio debug box). Thanks.
if (event == kResponseEvent) {
list = PyList_New(0);
for (i = 0; i < event->count; i++) {
PyList_Append(list, Py_BuildValue("{s:i, s:s}",
"id", event->id,
"name", event->name));
}
PyModule_AddObject(module, "names", list);
}
PyModule_AddObject() steals a reference. As such, you should not be decrefing list after.
PyList_New() can return NULL to indicate an error, which you aren't checking for. Py_BuildValue() can return NULL to indicate an error, which you aren't checking for. PyList_Append() can return -1 to indicate an error, which you're also not checking for. PyList_Append() doesn't steal the reference, so you're leaking the reference to the dict returned by Py_BuildValue(). The latter may be causing you to run out of memory, which can cause Py_BuildValue() or PyList_Append() to fail, and your failure to handle the error can cause a crash.
(Something else can also cause Py_BuildValue() or PyList_Append() to fail, but that's hard to guess at from just this snippet.)