joheinze/PriNCe

No file loading on import

Closed this issue · 3 comments

Currently the prince_config.py loads that may trigger a unhandled exception. If this occurs, docs don't build. In general, file loads on import should be avoided.

You mean these lines of code?

PriNCe/prince_config.py

Lines 176 to 179 in c5c6381

#: Dictionary containing particle properties, like mass, charge
#: lifetime or branching ratios
spec_data = pickle.load(
open(path.join(config["data_dir"], "particle_data.ppo"), "rb"))

As part of pull request #13, the spec data was moved to prince.data, since I though that it will make more sense there. However, I guess this may still cause the same problem.

Yes, meant these lines.

The simplest solution is to distribute the really essential data with the code. This seems one of the few files that are needed to just import the code. Or, if this becomes part of the "nuclear database" then it will become part of the requirements.txt.

The simplest fix seems a simple protection, like:

_spec_data = None
@property
def spec_data():
    global _spec_data
    if _spec_data is None:
         _spec_data = .... load file ....
    return _spec_data

I'll do a pull request on this

:) This won't work, because one can not declare functions as properties on module level. Only for classes and the workarounds are awful. I think the best way will be to integrate these data into particletools and then migrate to it in the next iterations when moving to PDG indices. But without proper tests it will be hell.