How To Use PyLCGDict

Overview

PyLCGDict is a Python extension module that allows the user to interact with any C++ class for which the LCG Dictionary has been generated. With this extension module there is not need to generate specialized Python bindings or wrapper code to interact with C++ classes.

Currently the PyLCGDict module supports the following features:

Generating the LCG Dictionary

This is the first step and only requirement. Note that the dictionary is not specific for Python. Dictionaries previously generated to fulfill persistency requirements (POOL) should be sufficient as long as the method information was not stripped off (--pool option). Follow the instructions to know how to generate the dictionary. For example, for the following class (examples/MyClass.h)

#include <iostream>

namespace Example {
  class MyClass {
  public:
    MyClass() : m_value(0) {}
    MyClass(const MyClass& m ) : m_value(m.m_value) {}
    ~MyClass() {}
    int doSomething(const std::string& something ) {
      std::cout << "I am doing something with " << something << std::endl;
      return something.size();
    }
    int value() { return m_value; }
    void setValue(int v) { m_value = v; }
  private:
    int m_value;
  public:
    float fprop;
    std::string sprop;
  };
}

To generate the dictionary using the lcgdict command followed by the compilation and  linking of the resulting XXXX_dict.cpp file. In order to do this a correct environment is needed. This can be achieved following easy steps making use of LCG scripts:

Now we are ready to process the header file:

> lcgdict MyClass.h
Parsing file MyClass.h with GCC_XML OK
Generating LCG Dictionary
class Example::MyClass
> g++ -o libMyClassDict.so -fpic -shared MyClass_dict.cpp 
  -I<...>/SEAL_x_y_z/include
  -L<...>/<architecture>/lib -llcg_ReflectionBuilder

Running an example

Assuming that the environment is setup correctly (LD_LIBRARY_PATH and PYTHONPATH are required) the user can start interacting with the MyClass directly from the Python shell (MyClass.py).

> python
Python 2.3.4 (#1, Jun 7 2004, 11:39:13) [GCC 3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pylcgdict
>>> pylcgdict.loadDictionary('MyClassDict')
>>> g = pylcgdcit.Namespace('')
>>> m = g.Example.MyClass()
>>> dir(m)
['Example','__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__', '__hash__', 
'__init__', '__module__', '__new__', '__nonzero__', '__reduce__', '__repr__', 
'__setattr__', '__str__', '__weakref__', '__zero__', '_theObject', 'doSomething', 
'fprop', 'setValue', 'sprop', 'value']
>>> m.doSomething('care')
I am doing something with care
4
>>> m.setValue(99)
>>> print m.value()
99
>>> n = g.Example.MyClass(m) # copy constructor
>>> print n.value()
99
>>> n.fprop = 1.2
>>> n.sprop = 'String property'
>>> print n.sprop, n.fprop
String property 1.2

Other Examples

We provide other examples of usage of PyLCGDict module. These examples are located in the PyLCGDict/examples directory.

dumpdict.py

This python module uses PyLCGDict to dump the contents and check consistency any LCG dictionary. Its is used as follows

> dumpdict.py --help
Usage:
 dumpdict.py dictionary [options]

Options:
-c , --check_completeness
  In addition to dump the contents, it checks its completeness
-h, --help
  Prints this help
> dumpdict.py MyClassDict
class Example::MyClass
      double fprop
      int m_value
      std::basic_string<char> sprop

 

 

f