How to transfer python dict to CINT?

I have some python scripts to read logs. I want to transfer the logs read from Python to CINT. Here are the detail story.

Data file (something like these):

# Gamma 1: E_γ = 0.847 MeV
# Gamma 1: E_γ = 1.238 MeV

C1C1-PH: [ 61, 272, 124], [273, 3840]
C1C2-PH: [ 79, 121,  88], [150, 3842], [1450.37, 6.33774], [2359.08, 5.25428], [3418.48, 4.87569]
C1C3-PH: [100, 128, 111], [250, 3833], [1478.59, 6.28460], [2392.18, 4.50688], [3457.03, 5.22970]

Python script to handle them:

def valid(a):
    if len(a)>0 and a[0] != "#": return True

def readrlog(fn):
    ss = [l.strip() for l in open(fn,"r").readlines()]
    records = {}
    for aa in filter(valid, ss):
        key,v = aa.split(":")
        try: records[key] = eval(v)
        except: print "Invalid record:",key,'--',v
    return records

logs = readrlog("myfile.log")

I want to use the result “logs” in CINT. How to implement this? TPythong::Bind(…)?
Please give me some hints. Thanks!

Hi,

instead of a python dict, create a dictionary for and use an std::mapstd::string,std::string (or: std.map(str,str) in python style) and pass that to CINT (see: http://root.cern.ch/root/html526/TPython.html for passing objects that have dictionaries associated with them).

Cheers,
Wim

It looks sophisticated. Python is good for handling dicts, lists, etc. If these data structures can be passed easily to CINT, it will be very charming.

Hi,

it probably wouldn’t be all that nice if python dicts could be passed. As said, you’d have a PyObject* on the CINT side. This object should then be manipulated with the Python C API, which isn’t all that pretty. The reason that dicts and lists are nice in python, is b/c the language supports it. They are not that nice in C++, as that language does not offer an equivalent support.

Cheers,
Wim