mapleman's ramblings

Mon, 05 Jan 2009
Winter Break

For winter break I decided to test some things that I've had in the back log for a while. These things are Google App Engine, Google Web Toolkit, ZSI.

I really like the new technologies that Google is implementing. I personally aren't a fan of javascript, and the way they have managed to hide a lot of it through the use of GWT is amazing, and can actually make you more productive.

Basically what GWT empowers you to do is build an UI pretty much like you would in AWT in Java, this is great for doing basic widgets, like those used in Web Dynpro in SAP. And you can modify their colors and characteristics with CSS aswell.

So you can mostly focus on your code and develop stuff faster.

I'm not a big fan of JSON and would like to avoid it. python-gwt-rpc looks promising.

Another tool I reviewed was Google App Engine. I can say it is amazing, I like it, just like it. Maybe I'm biased because I like python programming.I also went on to mess around a bit with ZSI, which is the python soap web service engine I suppose. Although documentation is scarce; got to add up pieces to get it to do what you need.I wanted to put a web server on python cgi, so I started writing a small wsdl:

 1  <?xml version="1.0" encoding="UTF-8"?>
 2  <wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
     xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
     xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:xs="http://www.w3.org/2001/XMLSchema"
     xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
     xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://mapleman.linuxreal.org"
     targetNamespace="http://mapleman.linuxreal.org">
 3  	<wsdl:types>
 4  		<xs:schema targetNamespace="http://mapleman.linuxreal.org" elementFormDefault="qualified"/>
 5  	</wsdl:types>
 6  	<wsdl:message name="currentRequest">
 7  		<wsdl:part name="voltage" type="xs:double"/>
 8  		<wsdl:part name="resistance" type="xs:double"/>
 9  	</wsdl:message>
10  	<wsdl:message name="currentResponse">
11  		<wsdl:part name="current" type="xs:double"/>
12  	</wsdl:message>
13  	<wsdl:portType name="CurrentServer">
14  		<wsdl:operation name="Current">
15  			<wsdl:input message="tns:currentRequest"/>
16  			<wsdl:output message="tns:currentResponse"/>
17  		</wsdl:operation>
18  	</wsdl:portType>
19  	<wsdl:binding name="CurrentServer" type="tns:CurrentServer">
20  		<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
21  		<wsdl:operation name="Current">
22  			<soap:operation soapAction="Current" style="rpc"/>
23  			<wsdl:input>
24  				<soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
                                            namespace="http://mapleman.linuxreal.org"/>
25  			</wsdl:input>
26  			<wsdl:output>
27  				<soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
                                            namespace="http://mapleman.linuxreal.org"/>
28  			</wsdl:output>
29  		</wsdl:operation>
30  	</wsdl:binding>
31  	<wsdl:service name="CurrentServer">
32  		<wsdl:port name="CurrentPort" binding="tns:CurrentServer">
33                          <soap:address
                                            location="http://mapleman.linuxreal.org/cgi/ws/current/index.cgi"/>
34  		</wsdl:port>
35  	</wsdl:service>
36  </wsdl:definitions>

Then I applied wsdl2py from a python 2.5 installation script's dir on it. From it I grabbed the output python files

Uploaded it to the location along with ZSI in a directory

#!/usr/local/bin/python -d
from CurrentServer_server import *
from ZSI import *
import sys
serialized_result = ""

try:
	ps = ParsedSoap(sys.stdin)
	curr = ps.Parse(currentRequest.typecode)	
	sw = SoapWriter(sys.stdout)
	result = currentResponse()
	result._current = curr._voltage / curr._resistance
	sw.serialize(result,currentResponse.typecode)
	sw.close()
	serialized_result = str(sw)

except ParseException, e:
	FaultFromZSIException(e).AsSOAP(OUT)
except Exception, e:
	FaultFromException(e, 1).AsSOAP()

if len(serialized_result) > 0:
	print 'Content-type: text/xml'
	print 
	print serialized_result
else:
	print 'Content-type: text/plain'
	print
	print 'This is a place holder for a webservice'

Surprisingly, that is all that is needed on the server side.

I also wrote a test client on python and on c# .net to make sure it worked correctly.

Code for C#

Added webreference using the wsdl then...

using zsitesting.linuxReal; // namespace of the webreference

private void Form1_Load(object sender, EventArgs e)
{
	c = new CurrentServer(); // form loader
}

private void button1_Click(object sender, EventArgs e)
{
	// this is just a button on a 3 textbox form
	double d1 = Double.Parse(textBox1.Text); 
	double d2 = Double.Parse(textBox2.Text);
	textBox3.Text = c.Current(d1,d2).ToString();
}

The python client requires to use wsdl2py and grab the client side files for current server.

from CurrentServer_client import *
import sys

loc = CurrentServerLocator()
port = loc.getCurrentPort(tracefile=sys.stdout)

msg = currentRequest()
msg._resistance = 1.5
msg._voltage = 2.0

rsp = port.Current(msg)
print "current: ", rsp._current

There are a few other items that I'm currently working on regarding exploration. Mainly messing with django and google app engine, the google datastore, etc.

Have been playing around with my pic16f84a and learning more about electronics, I guess that could be called a hobby of mine now a days.

posted at: 01:07 | path: /python

Wed, 02 Jul 2008
Embedding Python in C++

Adding python scripting capabilities to your code is an interesting thing to do, it can help you enhance an application without as much hard work. This is just an example on how to use code from memory as part of your program.

 1 // Embedded Py.cpp
 2 //
 3 
 4 #include "stdafx.h"
 5 #include <iostream>
 6 #undef _DEBUG /* Link with python24.lib and not python24_d.lib */
 7 #include <Python.h>
 8 
 9 
10 using namespace std;
11 
12 /* Python function code */
13 char script[] = 
14 "def doSomeStuff():\n"
15 "	import sys\n"
16 "	print sys\n"
17 "	return 3+2+2";
18 	
19 					
20 int _tmain(int argc, _TCHAR* argv[])
21 {
22 	PyObject *pName = NULL, *pModule = NULL, *pDict = NULL, *pFunc = NULL, *pCode = NULL, *pReturn = NULL;
23 	Py_Initialize();	
24 
25 	/* Compile the script defined in the array script */
26 	pCode = Py_CompileString(script, "mini-script", Py_file_input);
27 
28 	if (!pCode || PyErr_Occurred())
29 	{		
30 		PyErr_Print(); 
31 		return 0;
32 	}
33 
34 	/* Import the code compiled */
35 	pModule = PyImport_ExecCodeModule("mini-script", pCode);
36 
37 	if (!pModule || PyErr_Occurred())
38 	{
39 		PyErr_Print(); 
40 		return 0;
41 	}
42 
43 	/* Get the Dictionary of the module */
44 	pDict = PyModule_GetDict(pModule);
45 
46 	if (!pDict || PyErr_Occurred())
47 	{
48 		PyErr_Print();
49 		return 0;
50 	}
51 
52 	/* Grab the function with the name doSomeStuff() */
53 	pFunc = PyDict_GetItemString(pDict, "doSomeStuff");
54 
55 	if (!pFunc || PyErr_Occurred())
56 	{
57 		PyErr_Print();
58 		return 0;
59 	}
60     
61 	/* Call the function */
62 	pReturn = PyObject_CallObject(pFunc, NULL);
63 
64 	if (!pReturn || PyErr_Occurred())
65 	{
66 		PyErr_Print();
67 		return 0;
68 	}
69 
70 	/* Make sure return value is of int type */
71 	if (PyInt_Check(pReturn))
72 	{
73 			long returnOfFunc = PyInt_AsLong(pReturn);
74 			cout << "The return of doSomeStuff() function is: " << returnOfFunc << endl;
75 	}	
76 
77 	/* Cleanup */
78 	Py_XDECREF(pReturn);
79 	Py_XDECREF(pCode);
80 	Py_XDECREF(pFunc);
81 	Py_XDECREF(pDict);
82 	Py_XDECREF(pModule);	
83 	Py_Finalize();
84 
85 	return 0;
86 }

posted at: 17:50 | path: /python