Skip to content

Commit

Permalink
add __setitem__ to interface (#29)
Browse files Browse the repository at this point in the history
* remove wheel test step of build workflow (too unreliable)

* add `__setitem__` to interface
  • Loading branch information
zacharyburnettNOAA authored Oct 26, 2020
1 parent eeaba5d commit 3266c6f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 45 deletions.
30 changes: 0 additions & 30 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,33 +35,3 @@ jobs:
TWINE_USERNAME: __token__
TWINE_PASSWORD: ${{ secrets.PYPI_TOKEN }}
run: twine upload dist/* --verbose
install:
needs: deploy
strategy:
matrix:
os: [ ubuntu-latest, windows-latest ]
python-version: [ 3.8 ]
name: test wheel in Python ${{ matrix.python-version }} on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Install Python
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Restore cached dependencies
uses: actions/cache@v2
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ runner.python-version }}-${{ hashFiles('setup.py') }}
restore-keys: ${{ runner.os }}-pip-${{ runner.python-version }}-
- name: Update pip
run: python -m pip install --upgrade pip
- name: Retrieve version from repository
uses: mtkennerly/dunamai-action@v1
with:
env-var: VERSION
- name: Install wheel
run: pip install nemspy==$VERSION
shell: bash
10 changes: 10 additions & 0 deletions nemspy/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,16 @@ def __getitem__(self, model_type: str) -> ModelEntry:
raise KeyError(f'"{model_type}" not in {model_types}')
return self.__sequence[ModelType(model_type.upper())]

def __setitem__(self, model_type: str, model: ModelEntry):
if not isinstance(model_type, str) and not isinstance(model_type, ModelType):
raise ValueError(
f'model type must be {str} or {ModelType}, not {type(model_type)}'
)
model_types = [model_type.value.upper() for model_type in ModelType]
if model_type.upper() not in model_types:
raise KeyError(f'"{model_type}" not in {model_types}')
self.__sequence[ModelType(model_type.upper())] = model

def __contains__(self, model_type: str) -> bool:
if not isinstance(model_type, str) and not isinstance(model_type, ModelType):
raise ValueError(
Expand Down
23 changes: 14 additions & 9 deletions tests/test_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def test_interface(self):
atmospheric_mesh = AtmosphericMeshEntry(ATMOSPHERIC_MESH_FILENAME)
wave_mesh = WaveMeshEntry(WAVE_MESH_FILENAME)
ocean_model = ADCIRCEntry(11)
hydrological_model = NationalWaterModelEntry(769, Verbosity=ModelVerbosity.MAX)

nems = ModelingSystem(
start_time,
Expand All @@ -39,27 +40,31 @@ def test_interface(self):
ocn=ocean_model,
)

self.assertIs(nems['ATM'], atmospheric_mesh)
self.assertIs(nems['WAV'], wave_mesh)
self.assertIs(nems['OCN'], ocean_model)
self.assertIs(atmospheric_mesh, nems['ATM'])
self.assertIs(wave_mesh, nems['WAV'])
self.assertIs(ocean_model, nems['OCN'])

with self.assertRaises(KeyError):
nems['HYD']
with self.assertRaises(KeyError):
nems['nonexistent']

self.assertEqual(nems.interval, interval)
self.assertEqual(nems.attributes['Verbosity'], 'off')
nems['HYD'] = hydrological_model

self.assertIs(hydrological_model, nems['HYD'])

self.assertEqual(interval, nems.interval)
self.assertEqual('off', nems.attributes['Verbosity'])

new_interval = timedelta(minutes=30)
nems.interval = new_interval

self.assertEqual(nems.interval, new_interval)
self.assertEqual(new_interval, nems.interval)

nems.attributes = {'Verbosity': ModelVerbosity.MAX}
nems.attributes['Verbosity'] = ModelVerbosity.LOW

self.assertEqual(nems.attributes['Verbosity'], 'max')
self.assertEqual('max', nems.attributes['Verbosity'])

def test_connection(self):
start_time = datetime(2020, 6, 1)
Expand All @@ -80,7 +85,7 @@ def test_connection(self):
with self.assertRaises(KeyError):
nems.connect('WAV', 'OCN', 'nonexistent')

self.assertEqual(nems.connections, ['WAV -> OCN :remapMethod=redist'])
self.assertEqual(['WAV -> OCN :remapMethod=redist'], nems.connections)

def test_mediation(self):
start_time = datetime(2020, 6, 1)
Expand Down Expand Up @@ -117,7 +122,6 @@ def test_mediation(self):
nems.connect('WAV', 'OCN', 'nonexistent')

self.assertEqual(
nems.connections,
[
'ATM -> MED :remapMethod=redist\n'
'MED MedPhase_prep_ice\n'
Expand All @@ -128,6 +132,7 @@ def test_mediation(self):
'MED MedPhase_prep_ocn\n' 'MED -> OCN :remapMethod=redist',
'OCN -> MED :remapMethod=redist',
],
nems.connections,
)

def test_sequence(self):
Expand Down
12 changes: 6 additions & 6 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ def test_processors(self):
model_1.next = model_2
model_2.next = model_3

self.assertIs(model_1.start_processor, None)
self.assertIs(model_1.end_processor, None)
self.assertIs(model_2.start_processor, None)
self.assertIs(model_2.end_processor, None)
self.assertIs(model_3.start_processor, None)
self.assertIs(model_3.end_processor, None)
self.assertIs(None, model_1.start_processor)
self.assertIs(None, model_1.end_processor)
self.assertIs(None, model_2.start_processor)
self.assertIs(None, model_2.end_processor)
self.assertIs(None, model_3.start_processor)
self.assertIs(None, model_3.end_processor)

model_1.start_processor = 0

Expand Down

0 comments on commit 3266c6f

Please sign in to comment.