Skip to content

Commit b1ca8dc

Browse files
authored
Upgrade to Python 3.8 syntax (#159)
* upgrade to python 3.8+ syntax * remove six dependency * upgrade notebooks to Python 3.8+ syntax * add news fragment [skip ci]
1 parent d2af40c commit b1ca8dc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+89
-109
lines changed

news/159.misc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Removed dependency on ``six`` since we don't support Python 2 and updated code
2+
to use Python 3.8+ syntax.
3+

notebooks/cem.ipynb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@
275275
"cem.get_grid_spacing(grid_id, out=spacing)\n",
276276
"\n",
277277
"print('The grid has %d rows and %d columns' % (shape[0], shape[1]))\n",
278-
"print('The spacing between rows is %f m and between columns is %f m' % (spacing[0], spacing[1]))"
278+
"print('The spacing between rows is {:f} m and between columns is {:f} m'.format(spacing[0], spacing[1]))"
279279
]
280280
},
281281
{
@@ -399,7 +399,7 @@
399399
"# read in the csv file of bedload measurements in the Rhine River, the Netherlands\n",
400400
"# these data were collected over different days over a season in 2004, at nearby locations.\n",
401401
"# plot how river discharge controls bedload; Q (x-axis) and Qb (y-axis) data. \n",
402-
"# label both axes\n"
402+
"# label both axes"
403403
]
404404
},
405405
{
@@ -425,7 +425,7 @@
425425
"outputs": [],
426426
"source": [
427427
"# extrapolate this relationship and calculate how much river discharge, Q, \n",
428-
"# would be needed to transport the model specification Qb of 1250 kg/s\n"
428+
"# would be needed to transport the model specification Qb of 1250 kg/s"
429429
]
430430
},
431431
{

notebooks/child.ipynb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
"metadata": {},
2424
"outputs": [],
2525
"source": [
26-
"from __future__ import print_function\n",
27-
"\n",
2826
"# Some magic to make plots appear within the notebook\n",
2927
"%matplotlib inline\n",
3028
"\n",

notebooks/hydrotrend.ipynb

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
"source": [
6161
"# To start, import numpy and matplotlib.\n",
6262
"import matplotlib.pyplot as plt\n",
63-
"import numpy as np\n"
63+
"import numpy as np"
6464
]
6565
},
6666
{
@@ -237,8 +237,7 @@
237237
"# compare with the last year\n",
238238
"plt.plot(q[-366:-1],'grey')\n",
239239
"plt.title('HydroTrend simulation of first and last year discharge, Waiapaoa River')\n",
240-
"plt.show()\n",
241-
"\n"
240+
"plt.show()"
242241
]
243242
},
244243
{
@@ -269,7 +268,7 @@
269268
"metadata": {},
270269
"outputs": [],
271270
"source": [
272-
"# your code goes here\n"
271+
"# your code goes here"
273272
]
274273
},
275274
{
@@ -288,7 +287,7 @@
288287
"metadata": {},
289288
"outputs": [],
290289
"source": [
291-
"# here you can calculate the maximum river discharge.\n"
290+
"# here you can calculate the maximum river discharge."
292291
]
293292
},
294293
{
@@ -337,7 +336,7 @@
337336
"# your code goes here\n",
338337
"# you will have to sum all days of the individual years, to get the annual loads, then calculate the mean over the 100 years.\n",
339338
"# one possible trick is to use the .reshape() method\n",
340-
"# plot a graph of the 100 years timeseries of the total annual loads \n"
339+
"# plot a graph of the 100 years timeseries of the total annual loads "
341340
]
342341
},
343342
{
@@ -346,7 +345,7 @@
346345
"metadata": {},
347346
"outputs": [],
348347
"source": [
349-
"# take the mean over the 100 years\n"
348+
"# take the mean over the 100 years"
350349
]
351350
},
352351
{
@@ -430,8 +429,7 @@
430429
"# your code that prints out the mean river discharge, the mean sediment load and the mean bedload goes here\n",
431430
"\n",
432431
"\n",
433-
"# print out these same parameters for the basecase for comparison\n",
434-
"\n"
432+
"# print out these same parameters for the basecase for comparison"
435433
]
436434
},
437435
{

notebooks/hydrotrend_Ganges.ipynb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@
306306
"z = np.polyfit(q_y_vals.flatten(), q_mean_rows_GQ0.flatten(), 1)\n",
307307
"p = np.poly1d(z)\n",
308308
"plt.plot(q_y_vals, p(q_y_vals), \"r--\")\n",
309-
"plt.suptitle(\"y=%.6fx+%.6f\" % (z[0], z[1]), y=0.8)\n",
309+
"plt.suptitle(\"y={:.6f}x+{:.6f}\".format(z[0], z[1]), y=0.8)\n",
310310
"plt.show()"
311311
]
312312
},
@@ -359,7 +359,7 @@
359359
"z = np.polyfit(qs_y_vals.flatten(), qs_mean_rows_GQ0.flatten(), 1)\n",
360360
"p = np.poly1d(z)\n",
361361
"plt.plot(qs_y_vals, p(qs_y_vals), \"r--\")\n",
362-
"plt.suptitle(\"y=%.6fx+%.6f\" % (z[0], z[1]), y=0.85)\n",
362+
"plt.suptitle(\"y={:.6f}x+{:.6f}\".format(z[0], z[1]), y=0.85)\n",
363363
"plt.show()"
364364
]
365365
},
@@ -394,29 +394,29 @@
394394
"outputs": [],
395395
"source": [
396396
"print(\n",
397-
" \"Mean Water Discharge = {0} {1}\".format(\n",
397+
" \"Mean Water Discharge = {} {}\".format(\n",
398398
" q_GQ0.mean(),\n",
399399
" hydrotrend_GQ0.get_var_units(\"channel_exit_water__volume_flow_rate\"),\n",
400400
" )\n",
401401
")\n",
402402
"print(\n",
403-
" \"Mean Bedload Discharge = {0} {1}\".format(\n",
403+
" \"Mean Bedload Discharge = {} {}\".format(\n",
404404
" qb_GQ0.mean(),\n",
405405
" hydrotrend_GQ0.get_var_units(\n",
406406
" \"channel_exit_water_sediment~bedload__mass_flow_rate\"\n",
407407
" ),\n",
408408
" )\n",
409409
")\n",
410410
"print(\n",
411-
" \"Mean Suspended Sediment Discharge = {0} {1}\".format(\n",
411+
" \"Mean Suspended Sediment Discharge = {} {}\".format(\n",
412412
" qs_GQ0.mean(),\n",
413413
" hydrotrend_GQ0.get_var_units(\n",
414414
" \"channel_exit_water_sediment~suspended__mass_flow_rate\"\n",
415415
" ),\n",
416416
" )\n",
417417
")\n",
418418
"print(\n",
419-
" \"Mean Suspended Sediment Concentration = {0} {1}\".format(\n",
419+
" \"Mean Suspended Sediment Concentration = {} {}\".format(\n",
420420
" cs_GQ0.mean(),\n",
421421
" hydrotrend_GQ0.get_var_units(\n",
422422
" \"channel_exit_water_sediment~suspended__mass_concentration\"\n",

notebooks/sedflux3d.ipynb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
"metadata": {},
2424
"outputs": [],
2525
"source": [
26-
"from __future__ import print_function\n",
27-
"\n",
2826
"# Some magic to make plots appear within the notebook\n",
2927
"%matplotlib inline\n",
3028
"\n",

notebooks/subside.ipynb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@
4343
"metadata": {},
4444
"outputs": [],
4545
"source": [
46-
"from __future__ import print_function\n",
47-
"\n",
4846
"%matplotlib inline\n",
4947
"\n",
5048
"import matplotlib.pyplot as plt"

pymt/cmd/cmt_config.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import socket
44
import sys
55

6-
import six
76
import yaml
87
from model_metadata.find import find_model_data_files
98

@@ -17,19 +16,19 @@ def __init__(self, stdout=None, stderr=None):
1716
self._err = stderr or sys.stderr
1817

1918
def __enter__(self):
20-
if isinstance(self._out, six.string_types):
19+
if isinstance(self._out, str):
2120
sys.stdout = open(self._out, "w")
2221
else:
2322
sys.stdout = self._out
24-
if isinstance(self._err, six.string_types):
23+
if isinstance(self._err, str):
2524
sys.stderr = open(self._err, "w")
2625
else:
2726
sys.stderr = self._err
2827

2928
def __exit__(self, type_, value, traceback):
30-
if isinstance(self._out, six.string_types):
29+
if isinstance(self._out, str):
3130
sys.stdout.close()
32-
if isinstance(self._err, six.string_types):
31+
if isinstance(self._err, str):
3332
sys.stderr.close()
3433

3534
sys.stdout = self._stdout

pymt/component/component.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
"""
5858
import warnings
5959

60-
import six
6160
import yaml
6261

6362
from ..events.chain import ChainEvent
@@ -146,7 +145,7 @@ def __init__(
146145
if provides is None:
147146
provides = set()
148147

149-
if isinstance(port, six.string_types):
148+
if isinstance(port, str):
150149
if name is None:
151150
name = port
152151
self._port = services.instantiate_component(port, name)
@@ -419,7 +418,7 @@ def from_path(cls, path):
419418
Component
420419
A newly-created component.
421420
"""
422-
with open(path, "r") as yaml_file:
421+
with open(path) as yaml_file:
423422
return cls._from_yaml(yaml_file.read())
424423

425424
@classmethod

pymt/component/model.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import os
22

3-
import six
43
import yaml
54

65
from .component import Component
@@ -30,7 +29,7 @@ def get_exchange_item_mapping(items):
3029
"""
3130
mapping = []
3231
for item in items:
33-
if isinstance(item, six.string_types):
32+
if isinstance(item, str):
3433
item_map = (item, item)
3534
else:
3635
try:
@@ -83,7 +82,7 @@ def duration(self, duration):
8382
def go(self, filename=None):
8483
"""Start the model."""
8584
if filename:
86-
with open(filename, "r") as f:
85+
with open(filename) as f:
8786
model = yaml.safe_load(f.read())
8887
self._driver, self._duration = (model["driver"], model["duration"])
8988

@@ -158,7 +157,7 @@ def from_file(cls, filename):
158157
model : Model
159158
A newly-created model.
160159
"""
161-
with open(filename, "r") as fp:
160+
with open(filename) as fp:
162161
return cls.load(fp)
163162

164163
@classmethod

0 commit comments

Comments
 (0)