Commit 9d9e4017 authored by Klaus Zimmermann's avatar Klaus Zimmermann
Browse files

Simplify string formatting (closes #109)

parent 213ab9c9
......@@ -18,8 +18,8 @@ def prepare_environment(args):
def parse_args():
parser = argparse.ArgumentParser(
description=('An editor for a climate index thing, version {}.'
''.format(climix.__version__)))
description=(f'An editor for a climate index thing, '
f'version {climix.__version__}.'))
parser.add_argument('-o', '--output')
parser.add_argument('master_table')
return parser.parse_args()
......
......@@ -42,9 +42,9 @@ def tr_inputs(no_inputs,
def tr_relops(no_thresholds, relop_string):
relops = split_parts(no_thresholds, relop_string)
d = [{'var_name': 'relop_{}'.format(i),
d = [{'var_name': f'relop_{i}',
'kind': 'operator',
'operator': '"{}"'.format(relop)}
'operator': f'"{relop}"'}
for i, relop in enumerate(relops)
]
return d
......
......@@ -122,7 +122,7 @@ class FirstOccurrence:
for i, representative_date in enumerate(time.cells()):
year = representative_date.point.year
start_date = datetime(year, period.first_month_number, 1)
units = Unit('days since {}-01-01'.format(year), calendar=calendar)
units = Unit(f'days since {year}-01-01', calendar=calendar)
offsets[i] = units.date2num(start_date)
collapsed_cube.data = (collapsed_cube.core_data()
+ offsets[:, None, None])
......@@ -169,7 +169,7 @@ class LastOccurrence:
for i, representative_date in enumerate(time.cells()):
year = representative_date.point.year
start_date = datetime(year, period.first_month_number, 1)
units = Unit('days since {}-01-01'.format(year), calendar=calendar)
units = Unit(f'days since {year}-01-01', calendar=calendar)
offsets[i] = units.date2num(start_date)
collapsed_cube.data = (collapsed_cube.core_data()
+ offsets[:, None, None])
......
......@@ -26,8 +26,7 @@ MISSVAL = 1.0e20
def parse_args():
parser = argparse.ArgumentParser(
description=('A climate index thing, version {}.'
''.format(climix.__version__)))
description=(f'A climate index thing, version {climix.__version__}.'))
parser.add_argument('-s', '--sliced-mode', action='store_true',
help='activate calculation per period to avoid memory '
'problems')
......@@ -71,15 +70,15 @@ def build_parameters(parameters_metadata):
elif kind == 'operator':
op = md['operator']
if op not in SUPPORTED_OPERATORS:
raise ValueError('Unknown operator <{}>'.format(op))
raise ValueError(f'Unknown operator <{op}>')
parameter = op
elif kind == 'reducer':
red = md['reducer']
if red not in SUPPORTED_REDUCERS:
raise ValueError('Unknown reducer <{}>'.format(red))
raise ValueError(f'Unknown reducer <{red}>')
parameter = red
else:
raise ValueError('Unknown parameter kind <{}>'.format(kind))
raise ValueError(f'Unknown parameter kind <{kind}>')
parameters[name] = parameter
return parameters
......@@ -89,14 +88,16 @@ def build_index_function(spec):
candidates = list(pkg_resources.iter_entry_points('climix.index_functions',
name=name))
if len(candidates) == 0:
raise ValueError('No implementation found for index_function <{}>'
''.format(name))
raise ValueError(f'No implementation found for index_function <{name}>')
elif len(candidates) > 1:
msg = ('Found several implementations for index_function <{}>. '
'Please make sure only one is installed at any time. '
'The implementation came from the distributions {}'
''.format(name, 'placeholder'))
raise ValueError(msg)
distributions = [candidate.dist for candidate in candidates]
raise ValueError(
f'Found several implementations for index_function <{name}>. '
f'Please make sure only one is installed at any time. '
f'The implementations come from the distributions {distributions}')
candidate = candidates[0]
logging.info(f'Found implementation for index_function <{name}> '
f'from distribution <{candidate.dist}>')
index_function_factory = candidates[0].load()
parameters = build_parameters(spec['parameters'])
index_function = index_function_factory(**parameters)
......@@ -149,10 +150,10 @@ def guess_output_template(datafiles):
bases, starts, ends = zip(*files)
unique_bases = set(bases)
if len(unique_bases) == 1:
base = unique_bases.pop()
start = min(starts)
end = max(ends)
output_template = '{{var_name}}_{base}_{{frequency}}_{start}-{end}.nc'.format(
base=unique_bases.pop(), start=start, end=end)
output_template = f'{{var_name}}_{base}_{{frequency}}_{start}-{end}.nc'
return output_template
......@@ -184,14 +185,14 @@ def main():
args = parse_args()
cluster = setup_cluster()
client = Client(cluster)
print("Cluster ready; starting main program.")
print('Cluster ready; starting main program.')
try:
start = time.time()
do_main(args.indices, args.datafiles,
args.output_template, args.sliced_mode)
end = time.time()
input("Calculation took {:.4f} seconds.\n"
"Press enter to close the cluster ".format(end-start))
input(f'Calculation took {end-start:.4f} seconds.\n'
'Press enter to close the cluster ')
finally:
client.close()
cluster.close()
......
......@@ -72,10 +72,7 @@ class Season(Period):
def long_label(self):
first_month = self.MONTHS[self.first_month_number-1]
last_month = self.MONTHS[self.last_month_number-1]
long_label = '{}-{}'.format(
first_month,
last_month,
)
long_label = f'{first_month}-{last_month}'
return long_label
def add_coord_categorisation(self, cube):
......@@ -98,7 +95,7 @@ def build_period(period_spec):
try:
Period = PERIODS[period_spec.type]
except KeyError:
raise ValueError("Unknown period specification <{}>".format(period_spec))
raise ValueError("Unknown period specification <{period_spec}>")
if period_spec.parameters is None:
period = Period()
else:
......
......@@ -8,7 +8,7 @@ def dict_diffs(dict_one, dict_two, label=None):
if label is None:
label_template = "{}"
else:
label_template = "{}['{{}}']".format(label)
label_template = f"{label}['{{}}']"
for key in set(dict_one) - common_keys:
attr = label_template.format(key)
diffs[attr] = (dict_one[key], "None")
......@@ -60,15 +60,15 @@ def coord_diffs(coord1, coord2):
diffs[attr] = ('MISSING', 'EXISTS')
else:
if array1.shape != array2.shape:
diffs[attr] = ('Shape 1={}'.format(array1.shape),
'Shape 2={}'.format(array2.shape))
diffs[attr] = (f'Shape 1={array1.shape}',
f'Shape 2={array2.shape}')
elif np.any(array1 != array2):
arry = np.array([array1.ravel(), array2.ravel()])
adif = np.abs(np.diff(arry, axis=0)).ravel()
dabs = np.max(adif)
drel = np.max(adif/np.min(arry, axis=0))
diffs[attr] = ('AbsDiff={}'.format(dabs),
'RelDiff={}'.format(drel))
diffs[attr] = (f'AbsDiff={dabs}',
f'RelDiff={drel}')
if array1 is not None and array1.dtype != array2.dtype:
diffs[attr] = (array1.dtype, array2.dtype)
return diffs
......@@ -102,14 +102,14 @@ def cube_diffs(cube1, cube2):
cdiff = {}
for key in set(coord1) - coords:
attr = "coordinate['{}']".format(key)
attr = f"coordinate['{key}']"
cdiff[attr] = ("EXISTS", "MISSING")
for key in set(coord2) - coords:
attr = "coordinate['{}']".format(key)
attr = f"coordinate['{key}']"
cdiff[attr] = ('MISSING', 'EXISTS')
for key in coords:
attr = "coordinate['{}']".format(key)
attr = f"coordinate['{key}']"
c = coord_diffs(cube1.coords(key), cube2.coords(key))
if c != {}:
cdiff[attr] = c
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment