Friday, March 14, 2014

Passing Parameters in From C to Pyhton.

Hi all,

Once again i come with some C and Python. Dont think its very new things. It just an upgraded post of the last post "Embedding Python in C".

In the last post you learned "how to Embed the Python code in C". Now i am going to explain how we can send some parameters from Python to C.

Example:

mypython.py

import sys

ca_one = str(sys.argv[1])
ca_two = str(sys.argv[2])
print "My command line args are " + ca_one + " and " + ca_two

call.c
#include <Python.h>
#include <stdio.h>

int main(int argc,char *argv[])
{
    FILE* file;

    Py_SetProgramName(argv[0]);
    Py_Initialize();
    PySys_SetArgv(argc, argv);
    file = fopen("mypython.py","r");
    PyRun_SimpleFile(file, "mypython.py");
    Py_Finalize();

    return 0;
}
  
Now compile your c program and pass some paramters *for mypython.py ( above sample code) you have to send two parameters.

Input:
./call cool dharma


Output:
My command line args are cool and dharma


Yeah..! You did this, like this you can pass your parameters from your C program to python code. :)


Regards,
cooldharma06..:)



Monday, March 10, 2014

Embedding python in C.

Hi all,

Python is developing now a days. So as a developer we have to change based on the technology.

Now i am going to explain about how we can compile the python programs with(in) c.

Example python program:

addition.py

def add(a,b):
    c=a+b
    print "You did it and Added result is ..",c


And my C program:

samlepython.c

#include <Python.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
  Py_SetProgramName(argv[0]);  /* optional but recommended */
  Py_Initialize();
  PyRun_SimpleString("import addition\naddition.add(4, 2)\n");  /* here we are calling the python                                                                                                                                                 things */
  Py_Finalize();
  return 0;
}


Now you have to compile your c program:

gcc samlepython.c -o sample_python

it will show the following error:
"mainpgm.c:1:20: fatal error: Python.h: No such file or directory"

For this you have add your python installed location to your gcc.

gcc -I/usr/include/python2.7 -lpython2.7 samplepython.c -o sample_python

now it compiled and if you are running the ./sample_python means; you will get the foollowing error.

Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named addition


It means the program is not able to find the imported "addition" location. for this you have to add the path folder to PYTHONPATH variable.

Suppose i stored my program under the /home/cool/docs/

export PYTHONPATH="/home/cool/docs/"

then again compile and once again run your program,
Output:
You did it and Added result is .. 6


Yeah you compiled your python program with c. Congratz...:)



Regards,
cooldharma06..:)



Thursday, March 6, 2014

Xml file in XEN-4.3.1

hi all,

As it of opensource, i just want to share some things which i knows:

Most of the beginners are not aware of xml file creation in XEN.

Please refer the following things if you have the above questions in your mind..

XML file is created by some programs which is under the xen source tree as Node.

it will create the XML file and it will passed into libvirt for VM creation.


Flow of Xml file creation is:

instance.xml is produced by the node. And it will get under the xsl translation with libvirt.xsl file to produce the instance-libvirt.xml.

That is passed for the VM creation in XEN...:)


That's all i think..:)

If you have any suggestions kindly google or ask the XEN community or ask here..:)

Regards,
cooldharma06.. :)

Raw image creation by dd command

hi all,

If you want to create the raw file image using the dd command just simply run the following command:

   dd if=/dev/zero of=windows.img bs=1 count=0 seek=10G

It will create the raw image in current directory. Before running this command check the memory is available in the folder or not.

If you want to change the size means just change the seek value to 1M or 1024K.

then do

   ls -l --block-size=G

You did it, Congratz...!!! Enjoy with your image.

regards,
cooldharma06..:)