Wednesday, February 24, 2010

XSLT

XSLT: is a language to convert a given XML doc to some other style.
I want to learn XSLT as I want to convert different type of XML feeds into a single XML format. So I'll be removing all the noise and properly format the given XML so that the new XML doc can be used for other guys easily.
Ref:
http://www.w3schools.com/xsl/
http://www.w3schools.com/xsl/xsl_intro.asp

I have just started learning, let's see how it goes.

Labels:

Monday, June 08, 2009

mysql SELECT with autoincrement variable

I wanted to get value of a column in mysql, but with serial number.
I got the answer here: http://bytes.com/groups/mysql/74731-auto-increment-fly

mysql> select @cn:=0;
+--------+
| @cn:=0 |
+--------+
| 0 |
+--------+
1 row in set (0.00 sec)

Then do

mysql> select duration, @cn:=@cn+1 as cc from vod limit 10;
+----------+------+
| duration | cc |
+----------+------+
| 3592 | 1 |
| 1705 | 2 |
| 2966 | 3 |
| 2982 | 4 |
| 2986 | 5 |
| 3580 | 6 |
| 2970 | 7 |
| 2971 | 8 |
| 1702 | 9 |
| 2965 | 10 |
+----------+------+
10 rows in set (0.00 sec)

Labels:

Thursday, September 07, 2006

web server via python

I dicided to use rpc for that. Now I have three thing:

  1. The server which is actually a python process which is running allways somewhere.
  2. The client which connect to the server and process the data given by user, the data I get from the webserver.
  3. The webserver, which starts the client's process, when user come to my site.

So I used xmlrpc for client server communication.

Server:

>>> import SimpleXMLRPCServer as S
>>> x = S.SimpleXMLRPCServer(('', 6000))
#You need to give a object of a class which will be exposed to client.
class myfunctions:
...     def add(self, a, b): return (a+b)
...
>>> obj = myfunctions()
>>> x.register_instance(obj)
#Now just keep listening:
>>> while 1: x.handle_request()
...


Client:
>>> import xmlrpclib
#Simply connect to the server:
>>> sp = xmlrpclib.ServerProxy("http://127.0.0.1:6000")
#And call the function
>>> sp.add(10,20)
Here I wasn't able to give arguments with their name, like add(a=10, b=20) :( . WHY


So now I am able to connect to the server from outside and call it's functions. But I want the data from web.
Here I need to run a web server

$ service https start

Hay how will my client will run.
Put that in /var/www/cgi-bin/
Or just make one symbolic link to your client.py from here.
$cd /var/www/cgi-bin/
$ln -s my_tool /home/monu/client.py

If you are making any html they should be available from /var/www/html
If you are making symbolic link, then you will have to make it enable from cnfiguration file of web server:
$vim /etc/httpd/conf/httpd.conf

<Directory "/var/www/cgi-bin">
    Options FollowSymLinks
</Directory>

Same for html dir also if neede.


But it's not running :(
You will have to make it executable
$ chmod 755 client.py

and tell the computer how to run it
#!/usr/bin/python

Now how will I get data from the user?

Simple:
If a user sends data from GET you will get it in environment variable called "QUERY_STRING", and if it's POST you will get as it is in stdin.
The type of method is written in env variable "REQUEST_METHOD"
Here is my client program:

req = os.environ.get("REQUEST_METHOD", '')

print "Content-Type: text/html\r\n\r\n"
#print "Req: ", req
if req.lower() == 'get':
    query_string = os.environ.get("QUERY_STRING", '')
elif req.lower() == 'post':
    query_string = sys.stdin.read()
else:
    print "I listen only GET or POST. You are talking in %s"%req
    sys.exit()
#print "query_string: ", query_string
#To parse the query string you can use cgi function
import cgi
args = cgi.parse_qs(query_string)
#it will be a dictionary with input name as key and data as list in value.
#print "Args: ", args
#print "Hi"
try:
    ##Connect to the server
    sp = xmlrpclib.ServerProxy("http://127.0.0.1:8117")
    output = sp.html_serialize(0, '/cgi-bin/GMRF', args)

    print output
except:
    print "Exception Occured :("
    print '<br>'
    print ''.join(traceback.format_exception(*(sys.exc_info())))
    print '<br>'

#print "Hello"

Little html:

I created a small form to get the data:

<form action="/cgi-bin/GMRF" method="POST">
Title: <input name="Ti" value="" type="text"/>
<br>
Description: <textarea name="De" value="" cols=30 rows=5 > </textarea>
<br>
Keywords (separated by ;): <input name="Ke" value="" type="text" />
<br>
<input name="submit" value="Go" type="submit" />
</form>


When I click the Go button, the data goes to client as POST.
Now there when I do
args = cgi.parse_qs(query_string)
I get areg as dictionary:
{'De': ["It's a description"], 'submit': ['Go'], 'Ke': ['No kw'], 'Ti': ["It's a title"]}
And I get the data back from the server.

web server via python

I dicided to use rpc for that. Now I have three thing:

  1. The server which is actually a python process which is running allways somewhere.
  2. The client which connect to the server and process the data given by user, the data I get from the webserver.
  3. The webserver, which starts the client's process, when user come to my site.

So I used xmlrpc for client server communication.

Server:

>>> import SimpleXMLRPCServer as S
>>> x = S.SimpleXMLRPCServer(('', 6000))
#You need to give a object of a class which will be exposed to client.
class myfunctions:
...     def add(self, a, b): return (a+b)
...
>>> obj = myfunctions()
>>> x.register_instance(obj)
#Now just keep listening:
>>> while 1: x.handle_request()
...


Client:
>>> import xmlrpclib
#Simply connect to the server:
>>> sp = xmlrpclib.ServerProxy("http://127.0.0.1:6000")
#And call the function
>>> sp.add(10,20)
Here I wasn't able to give arguments with their name, like add(a=10, b=20) :( . WHY


So now I am able to connect to the server from outside and call it's functions. But I want the data from web.
Here I need to run a web server

$ service https start

Hay how will my client will run.
Put that in /var/www/cgi-bin/
Or just make one symbolic link to your client.py from here.
$cd /var/www/cgi-bin/
$ln -s my_tool /home/monu/client.py

If you are making any html they should be available from /var/www/html
If you are making symbolic link, then you will have to make it enable from cnfiguration file of web server:
$vim /etc/httpd/conf/httpd.conf

<Directory "/var/www/cgi-bin">
    Options FollowSymLinks
</Directory>

Same for html dir also if neede.


But it's not running :(
You will have to make it executable
$ chmod 755 client.py

and tell the computer how to run it
#!/usr/bin/python

Now how will I get data from the user?

Simple:
If a user sends data from GET you will get it in environment variable called "QUERY_STRING", and if it's POST you will get as it is in stdin.
The type of method is written in env variable "REQUEST_METHOD"
Here is my client program:

req = os.environ.get("REQUEST_METHOD", '')

print "Content-Type: text/html\r\n\r\n"
#print "Req: ", req
if req.lower() == 'get':
    query_string = os.environ.get("QUERY_STRING", '')
elif req.lower() == 'post':
    query_string = sys.stdin.read()
else:
    print "I listen only GET or POST. You are talking in %s"%req
    sys.exit()
#print "query_string: ", query_string
#To parse the query string you can use cgi function
import cgi
args = cgi.parse_qs(query_string)
#it will be a dictionary with input name as key and data as list in value.
#print "Args: ", args
#print "Hi"
try:
    ##Connect to the server
    sp = xmlrpclib.ServerProxy("http://127.0.0.1:8117")
    output = sp.html_serialize(0, '/cgi-bin/GMRF', args)

    print output
except:
    print "Exception Occured :("
    print '<br>'
    print ''.join(traceback.format_exception(*(sys.exc_info())))
    print '<br>'

#print "Hello"

Little html:

I created a small form to get the data:

<form action="/cgi-bin/GMRF" method="POST">
Title: <input name="Ti" value="" type="text"/>
<br>
Description: <textarea name="De" value="" cols=30 rows=5 > </textarea>
<br>
Keywords (separated by ;): <input name="Ke" value="" type="text" />
<br>
<input name="submit" value="Go" type="submit" />
</form>


When I click the Go button, the data goes to client as POST.
Now there when I do
args = cgi.parse_qs(query_string)
I get areg as dictionary:
{'De': ["It's a description"], 'submit': ['Go'], 'Ke': ['No kw'], 'Ti': ["It's a title"]}
And I get the data back from the server.

Thursday, April 06, 2006

more than one instances of a module in via swig

Put all global vars in a structure
typedef struct mod_data{
...
}

have functions like
init(mod_data*, ...)
f1(mod_data*)
finalize(mod_data*)

in interface file say

%pointer_functions(mod_data, mod_data_p)

in python wrapper have a class for the module
import swig_module as sm
class module:
def __init__(self):
self.my_mod_data_p = sm.new_mod_data_p() #this guy will malloc for the mod_data structure and returns the pointer to the structure.
sm.init(self.my_mod_data_p)
#now you can use this self.my_mod_data_p every where in the module.
#if someone will create new instanced you will have different structure for that guy.

BUT what if i couldn't put all the things in a single structure???

Wednesday, April 05, 2006

sed: don't tell me if I didn't ask

By default sed 's' print everything it doesn't match.
If you want only matching things, use p with -n option
sed -n 's///p'

Wednesday, October 05, 2005

sed done

Hayyyyyyy,
I have solved (partially) the problem.

If I am searching for a field called xxx, where fields are seperated by #<># I'll do
sed 's/.*#<>#xxx: \(.*\)#<>#yyy.*/\1/'

Where yyy is the next field.
Now if I don't know the yyy, then what ???

Socho Socho.
There is some pangaa on greedy and non greedy I tried to placed '?' some places, but it didn't work.
I 'll see for this later.
For now I am happy with this much only.

sed

I need to extract a small amount of data from each line, from a large set of lines.
HOw???

cut


No cut has bad property of having delimiter of atmost ine character :(
My friend got it using a perl script. But I need something else, some thing more smaller.
OK now I am planning to use sed

Here I got a tutorial for the same.
http://www.grymoire.com/Unix/Sed.html

I tried the same earlier also but couldn't suceed due to some greedy property of re. But this time I will do it.
Let's see.

Monday, October 03, 2005

SOAPpy

I just learned how to use soap in python.
Actually my problem is: I have a tool which works nicely on the Linux m/c. Now I want to make it's GUI part workable on the windows. So I am planning to run a server on the linux m/c which will serve the GUI client. Now it doesn't matter in which platform the client is running.

http://sandeep.weblogs.us/archives/016611.html Gives a simple and nice wxample of soap server and client on python.