Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_gen_multidim(self):
d = {'md_nml': {'x': [[1, 2, 3], [4, 5, 6], [7, 8, 9]]}}
nml = f90nml.Namelist(d)
out = StringIO()
print(nml, file=out)
# TODO: Check output
out.close()
def test_dict_assign(self):
test_nml = f90nml.Namelist()
test_nml['dict_group'] = {'a': 1, 'b': 2}
try:
test_nml.write('tmp.nml')
finally:
os.remove('tmp.nml')
def test_namelist_patch(self):
nml = f90nml.Namelist({
'a_nml': {
'x': 1,
'y': 2,
}
})
# Check overwriting values
nml.patch({'a_nml': {'x': 3}})
self.assertEqual(nml['a_nml']['x'], 3)
self.assertEqual(nml['a_nml']['y'], 2)
# Check appending values doesn't remove previous
nml.patch({'a_nml': {'z': 5}})
self.assertEqual(nml['a_nml']['x'], 3)
def test_gen_dtype(self):
d = {'dtype_nml': {'a': [{'b': 1, 'c': 2}, {'b': 3, 'c': 4}]}}
nml = f90nml.Namelist(d)
out = StringIO()
print(nml, file=out)
# TODO: Check output
out.close()
def test_namelist_default_index(self):
d = {'x_nml': {'x': [1, 2, 3]}}
test_nml = f90nml.Namelist(d, default_start_index=1)
# TODO: Check value
def write_namelist(namelist: dict, path: str) -> None:
logger.info(f'writing namelist to {path}')
nml = f90nml.Namelist(namelist)
nml.indent = 0
nml.write(path, force=True)
def add_structure(structure, f90namelist, cards):
""" Modifies f90namelist and cards according to structure """
from . import Card
from f90nml import Namelist as F90Namelist
from quantities import bohr_radius
if 'system' not in f90namelist:
f90namelist['system'] = F90Namelist()
for key in ['a', 'b', 'c', 'cosab', 'cosac', 'cosbc', 'celldm', 'nat', 'ntyp']:
f90namelist['system'].pop(key, None)
f90namelist['system'].pop(key.upper(), None)
f90namelist['system']['ibrav'] = 0
f90namelist['system']['celldm'] = float(structure.scale.rescale(bohr_radius))
f90namelist['system']['nat'] = len(structure)
f90namelist['system']['ntyp'] = len(set([u.type for u in structure]))
card_dict = {card.name: card for card in cards}
if 'cell' not in card_dict:
cell = Card('cell_parameters')
cards.append(cell)
else:
cell = card_dict['cell']
cell.subtitle = 'alat'
try:
data_mnc_nml = self.read_namelist(data_mnc_path)
data_mnc_nml['mnc_01']['mnc_outdir_str'] = mnc_header
data_mnc_nml.write(data_mnc_path, force=True)
except IOError as exc:
if exc.errno == errno.ENOENT:
mnc_01_grp = {
'mnc_use_outdir': True,
'mnc_use_name_ni0': True,
'mnc_outdir_str': mnc_header,
'mnc_outdir_date': True,
'monitor_mnc': True
}
data_mnc_nml = f90nml.Namelist(mnc_01=mnc_01_grp)
data_mnc_nml.write(data_mnc_path)
else:
raise
# Read the input file
if input_fname:
if input_fmt in ('json', 'yaml'):
if input_fmt == 'json':
with open(input_fname) as input_file:
input_data = json.load(input_file)
elif input_ext == '.yaml':
with open(input_fname) as input_file:
input_data = yaml.safe_load(input_file)
else:
input_data = f90nml.read(input_fname)
else:
input_data = {}
input_data = f90nml.Namelist(input_data)
# Construct the update namelist
update_nml = {}
if args.variable:
if not args.group:
# Use the first available group
grp = list(input_data.keys())[0]
warnings.warn(
'f90nml: warning: Assuming variables are in group \'{g}\'.'
''.format(g=grp)
)
else:
grp = args.group
update_nml_str = '&{0} {1} /\n'.format(grp, ', '.join(args.variable))
update_io = StringIO(update_nml_str)
def __init__(self, dictionary=None):
from f90nml import Namelist as F90Namelist
super(HasTraits, self).__init__()
self.__inputs = F90Namelist()
if dictionary is not None:
for key, value in dictionary.items():
setattr(self, key, value)