Read namelist into Python object #138
Replies: 5 comments
-
I've thought about that as well, since typing out the dict syntax can be tedious, although there are people who consider it bad practice. It can lead to namespace collisions with existing properties, for example. It could go under (I think the netcdf4python project does something like this? I would need to check) In any case, it could be doable with some |
Beta Was this translation helpful? Give feedback.
-
FYI: just for fun, I hacked together something based on these posts:
By adding these methods to def __getattr__(self, name):
if name in self:
return self[name]
else:
#default behavior:
return OrderedDict.__getattr__(self, name)
def __setattr__(self, name, value):
if name in self:
self[name] = value
else:
#default behavior:
return OrderedDict.__setattr__(self, name, value) Example run: import f90nml
n = f90nml.read('test/comment_patch.nml')
n.comment_nml.v_cmt_in_str
Out[3]: 'This token ! is not a comment'
n.comment_nml.v_cmt_in_str = '2'
n.comment_nml.v_cmt_in_str
Out[5]: '2'
n['comment_nml']['v_cmt_in_str']
Out[6]: '2' Seems to work for this one example (I haven't done any further testing). I have no idea if this is a good solution or not, or if there are some other side-effects that might crop up. |
Beta Was this translation helpful? Give feedback.
-
Looks good, I am in favour of this. But some way to prevent interference with the configuration flags ( The whole configuration method has not even been documented, so there is plenty of space to restructure it. |
Beta Was this translation helpful? Give feedback.
-
I might tinker with it some more. Maybe there is a way to get the |
Beta Was this translation helpful? Give feedback.
-
I have been playing around with this, and it's actually much safer than I had originally thought. I wonder if the It might still be good to introduce some sort of warning if there's a collision. It could get confusing if the types are the same. Honestly the whole thing still strikes me as reckless, but I have to admit it is super convenient. |
Beta Was this translation helpful? Give feedback.
-
I was wondering if a good feature might be to be able to read the namelist into a Python object, rather than a dictionary. So, for example, rather than accessing the variables like:
we could use:
That would be pretty slick, I think.
Beta Was this translation helpful? Give feedback.
All reactions