Skip to content

Access attributes with dot operator with python fhicl module #6

@knoepfel

Description

@knoepfel

This issue has been migrated from https://cdcvs.fnal.gov/redmine/issues/24091 (FNAL account required)
Originally created by @ikatza on 2020-02-26 10:22:45


python/fhiclmodule.cc defines a python module to be able to open a fcll file and put the contents on a python dict.

Accessing parameters can become cumbersome:

pset['parameter_table']['parameter_value']

A less cumbersome and more familiar way would be to do:

pset.parameter_table.parameter_value

In Python this is easily achieved by defining __getattr__() function.

What I've done is:

On my python script define:

class dotDict(dict):
    def __getattr__(self,val):
        return self[val]

then feed this class with the python dict:

fcl_params = fhicl.make_pset('file_with_parameters.fcl')
pset = dotDict(fcl_params)
par = pset.parameter_table.parameter_value

Unfortunately I don't understand how to construct this code on the C++ implementation of this module.

Would this be something you would like to add to the module?

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureAn enhancement to the project

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions