python4delphi 设置syspath
详细看demo25的代码
These techniques are demonstrated in Demo25 in the examples folder of your Python for Delphi distribution.
The old vs. the new ways.
Because Delphi 6 has custom variants, they can point to specific smart proxies for python objects. Before Delphi 6, you could have an oleVariant pointing to a python instance, but you couldn't do the smart things like know that it was a list type etc.
The following examples assume you have a module called LoginMgr.py in the python path or in the same folder as your application .exe that you have built in Delphi. Note that the python file LoginMgr.py contains within it a python class called LoginMgr which has a method called somemethod.
Old way (see my basic tutorial for more info
on the AndyDelphiPy functions.)
New way (requires Delphi 6)
// Drop a TPythonAtomEngine onto your form
// or datamodule and name it PE
// You also need to drop a pythonDelphiVar
// component and call it pdv
uses AndyDelphiPy;
var
obj : olevariant;
begin
AndyDelphiPy.PyExeFile('LoginMgr.py', PE);
obj := AndyDelphiPy.PyClass('LoginMgr()', pdv, PE);
obj.somemethod() // call the method
// Drop a TPythonAtomEngine or TPythonEngine
// onto your form or datamodule.
uses VarPyth;
var
mymodule, obj: variant;
begin
mymodule := Import('LoginMgr');
obj := mymodule.LoginMgr();
obj.somemethod() // call the method
Note that it it possible to slightly mix the old and new way, so that if you use the AndyDelphiPy.PyExeFile('LoginMgr.py', PE); to import the module then you can then switch to the new way, declare an obj: variant; then instantiate an instance using obj := MainModule.LoginMgr(); However you still need Delphi 6 and so you might as well just use the new way properly.
Widestrings
Declare your delphi strings widestrings if you want to get more than 255 chars back from calls to python methods that return strings. e.g.
var
s : widestring;
begin
s := mypythonclassinstance.somemethod() ;
showmessage(s) ;
Booleans
If your python method call returns 1 or 0 and this is supposed to be interpreted as a boolean, then cast it inside Delphi e.g.
if Boolean( mypythonclassinstance.somemethod()) then
....
Accessing syspath directly
Here is a function that accesses a global variable called SysModule and access the syspath directly.
This function also calls VarIsPythonSequence which tests to see if the parameter passed is a list or not.
procedure TForm1.Button2Click(Sender: TObject);
const
LIB = 'E:\\ZopeWebSite\\bin\\lib';
LIBDLL = 'E:\\ZopeWebSite\\bin\\DLLs';
var
re : variant;
m : variant;
begin
memo1.lines.Add('SysModule.path is ' + SysModule.path);
memo1.lines.Add('');
Assert(VarIsPythonSequence(SysModule.path));
displaySysPath(ListBox1);
if not Boolean(SysModule.path.Contains(LIB)) then
SysModule.path.insert(0,LIB);
SysModule.path.append(LIBDLL);
memo1.lines.Add('SysModule.path now is ' + \
SysModule.path + #13#13);
displaySysPath(ListBox1);
fixSysPath;
re := Import('re');
showmessage(re);
m := Import('xml.dom.minidom');
showmessage(m);
end;
Playing with sys paths
This is an example of how to set the python system path as seen by delphi's instance of the python interpreter (as represented by the pythonEngine component). Note that it is imperative that you have \\ as the slashed in your path as otherwise things like \fred will actually be interpreted as \f (whatever that escaped character is) plus 'red'.
Technique 1
procedure TForm1.fixSysPath;
const
LIB = 'E:\\ZopeWebSite\bin\\lib';
LIBDLL = 'E:\\ZopeWebSite\\bin\\DLLs';
begin
// this is illegal
// SysModule.path.Clear;
// this will work with latest python for delphi components OK.
//SysModule.path := NewPythonList;
// this is a boring but effective solution as well.
while SysModule.path.Length > 1 do
SysModule.path.pop;
SysModule.path.append(LIBDLL);
SysModule.path.append(LIB);
end;
Technique 2
procedure TForm1.btnClearSyspathToJustLibClick(Sender: TObject);
var
currdir, libdir : string;
begin
currdir := ExtractFilePath( Application.ExeName );
NOTE: Simply putting a window path as the currdir will ultimately fail since the paths returned by Delphi have single slashes and python needs wither unix slashes or \\ slashes. See here for an algorithm to handle this.
libdir := EnsurePathHasDoubleSlashes(libdir);
libdir := currdir + 'Lib';
SysModule.path := NewPythonList;
// Relies on Jan 2002 install of python for Delphi components
SysModule.path.append(currdir);
SysModule.path.append(libdir);
end;
NOTE: See the python for delphi deployment section for a more in-depth discussion of paths.
Supplimentary utility to display the python syspath in a delphi gui control.
procedure TForm1.btnDisplaySysPathClick(Sender: TObject);
begin
ListBox1.clear;
displaySysPath(ListBox1);
end;
Writing a Delphi function that uses a python function to do the hard work.
Here is an example of writing a delphi utility function that takes a string, and splits it up (delimited by comma) and puts the result into a delphi list box. We are using python split function to do the splitting - cool eh?
procedure TForm1.splitAstring(str:string; lstbox: TListBox);
var
s, lzt : variant;
i : integer;
begin
s := VarPythonCreate(str);
// convert normal string into a python string.
lzt := s.split(',');
for i := 0 to lzt.Length-1 do
lstbox.Items.Add(lzt.GetItem(i))
end;
Displaying the python syspath in a delphi listbox
Even though we have a pointer to a python list object (via a Delphi variant), we still have to call .GetItem(i) on a python list rather than the python syntax of lzt[i] - why? Because we are in Delphi and thus we cannot use python syntax.
procedure TForm1.displaySysPath(lstbox: TListBox);
var
lzt : variant;
i : integer;
begin
Assert(VarIsPythonSequence(SysModule.path));
lzt := SysModule.path;
for i := 0 to lzt.Length-1 do
lstbox.Items.Add(lzt.GetItem(i));
lstbox.Items.Add('----------------------------------');
end;
Loading python base64 and minidom module and processing XML in Delphi
procedure TForm1.minidomLoadClick(Sender: TObject);
var
m, doc, top : variant;
s : string;
begin
fixSysPath;
displaySysPath(ListBox1);
m := Import('base64');
showmessage(m);
s := m.encodestring('this is some text which I am going to encode then decode again.');
showmessage(s + #13+#13 + m.decodestring(s));
m := Import('xml.dom.minidom');
doc := m.Document();
showmessage(doc);
top := doc.createElement( 'Workspace' );
top.setAttribute('Version', '1.1 beta4');
doc.appendChild(top);
s := doc.toxml();
showmessage('doc.toxml()' + #13+#13 + s);
end;
Importing your own class
Ensure you have a TPythonAtomEngine or TPythonEngine onto your form or datamodule.
var
mymodule, obj: variant;
begin
mymodule := Import('LoginMgr');
obj := mymodule.LoginMgr();
obj.somemethod() // call the method
python4delphi 设置syspath的更多相关文章
- Linux命令总结大全,包含所有linux命令
使用说明:此文档包含所有的Linux命令,只有你想不到的没有你看不到的,此文档共计10万余字,有8400多行,预计阅读时间差不多需要3个小时左右,所以要给大家说一说如何阅读此文档 为了方便大家阅读,我 ...
- python4delphi 使用
Python 开发桌面程序, 之前写过一个使用IronPython的博客. 下面这个方案使用 delphi 作为主开发语言,通过 python4delphi 控件包将 python 作为 script ...
- python4delphi 安装
环境搭建: 目前p4d已经可以支持到XE7,可惜googlecode即将关闭,不知道作者是否会在github上继续更新. 因为此开源项目历史较久远,拿到源代码后可能还需要手动修改相关的文件引用,毕竟需 ...
- 利用API设置桌面背景
实现效果: 知识运用: API函数SystemParametersInfo 实现代码: [DllImport("user32.dll", EntryPoint = "Sy ...
- 【.net 深呼吸】设置序列化中的最大数据量
欢迎收看本期的<老周吹牛>节目,由于剧组严重缺钱,故本节目无视频无声音.好,先看下面一个类声明. [DataContract] public class DemoObject { [Dat ...
- LINUX篇,设置MYSQL远程访问实用版
每次设置root和远程访问都容易出现问题, 总结了个通用方法, 关键在于实用 step1: # mysql -u root mysql mysql> Grant all privileges o ...
- Visual Studio Code 代理设置
Visual Studio Code (简称 VS Code)是由微软研发的一款免费.开源的跨平台文本(代码)编辑器,在十多年的编程经历中,我使用过非常多的的代码编辑器(包括 IDE),例如 Fron ...
- myeclipse学习总结一(在MyEclipse中设置生成jsp页面时默认编码为utf-8编码)
1.每次我们在MyEclispe中创建Jsp页面,生成的Jsp页面的默认编码是"ISO-8859-1".在这种情况下,当我们在页面中编写的内容存在中文的时候,就无法进行保存.如下图 ...
- Linux scp 设置nohup后台运行
Linux scp 设置nohup后台运行 1.正常执行scp命令 2.输入ctrl + z 暂停任务 3.bg将其放入后台 4.disown -h 将这个作业忽略HUP信号 5.测试会话中断,任务继 ...
随机推荐
- get与post
1. get是从服务器上获取数据,post是向服务器传送数据.2. get是把参数数据队列加到提交表单的ACTION属性所指的URL中,值和表单内各个字段一一对应,在URL中可以看到.post是通过H ...
- AJAX(二)AJAX框架
上文(AJAX(一)AJAX的简介和基础)对ajax异步请求服务器做了详细的介绍和基础应用,可以看出,ajax的一些过程是相对不变的.不必要每次发送请求都写一遍发送代码,一些ajax开发人员已经把他们 ...
- WampServer集成环境安装与配置
实习到了第三个礼拜了,原来我们小组是以开发php为主的,我们项目的服务器也是用php做的,因此我觉得很有必要学一下php的相关知识,首先当然是搭建环境了,写篇博客分享下经验. 目录: 一.软件下载 二 ...
- ASP.NET MVC实现POST方式的Redirect
我们知道,在ASP.NET MVC中,要从一个Action跳转到另一个Action,通常是用一系列以“Redirect”开头的方法 Redirect RedirectToAction Redirect ...
- JMeter工具的使用-ForEach
1,Add Thread group this detail information about this panel as below link http://jmeter.apache.org/u ...
- Java基础-序列化
Java序列化是将一个对象编码成一个字节流,反序列化将字节流编码转换成一个对象. 序列化是Java中实现持久化存储的一种方法: 为数据传输提供了线路级对象表示法. Java的序列化机制是通过在运行时判 ...
- 解决vs2015使用fopen、fprintf等函数报错的问题
出现错误提示: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable ...
- 14.Android之Layout布局学习
Android布局主要有5种,接下来学习总结下. 1) 最常见的线性布局 LinearLayout 线性布局是Android布局中最简单的布局,也是最常用,最实用的布局. android:orient ...
- BZOJ1452 [JSOI2009]Count
Description Input Output Sample Input Sample Output 1 2 HINT 正解:二维树状数组 解题报告: 这是一道送肉题.二维树状数组直接维护每种颜色的 ...
- Idea 添加lib文件夹,并添加至项目Libary
在WEB-INF文件夹下新建lib文件夹,在lib文件夹上右键选择Add as Libary...,然后填写library名称,选择作用级别,选择作用项目,OK 注意:lib文件夹下需要有jar包后才 ...