Skip to content

Commit 0631985

Browse files
committed
Add try/except to catch error.
1 parent 50eee06 commit 0631985

1 file changed

Lines changed: 27 additions & 62 deletions

File tree

fields/working_with_fields.ipynb

Lines changed: 27 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,11 @@
3737
{
3838
"cell_type": "code",
3939
"execution_count": 1,
40-
"metadata": {
41-
"collapsed": false
42-
},
40+
"metadata": {},
4341
"outputs": [],
4442
"source": [
4543
"import numpy as np\n",
46-
"from landlab import RasterModelGrid\n",
44+
"from landlab import RasterModelGrid, FieldError\n",
4745
"from landlab.components import LinearDiffuser\n",
4846
"\n",
4947
"mg = RasterModelGrid((3, 4), 1.)"
@@ -59,9 +57,7 @@
5957
{
6058
"cell_type": "code",
6159
"execution_count": 2,
62-
"metadata": {
63-
"collapsed": false
64-
},
60+
"metadata": {},
6561
"outputs": [
6662
{
6763
"data": {
@@ -83,9 +79,7 @@
8379
{
8480
"cell_type": "code",
8581
"execution_count": 3,
86-
"metadata": {
87-
"collapsed": false
88-
},
82+
"metadata": {},
8983
"outputs": [
9084
{
9185
"data": {
@@ -131,9 +125,7 @@
131125
{
132126
"cell_type": "code",
133127
"execution_count": 4,
134-
"metadata": {
135-
"collapsed": false
136-
},
128+
"metadata": {},
137129
"outputs": [
138130
{
139131
"name": "stdout",
@@ -173,34 +165,27 @@
173165
{
174166
"cell_type": "code",
175167
"execution_count": 6,
176-
"metadata": {
177-
"collapsed": false
178-
},
168+
"metadata": {},
179169
"outputs": [
180170
{
181-
"ename": "FieldError",
182-
"evalue": "field__number_one: already exists",
183-
"output_type": "error",
184-
"traceback": [
185-
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
186-
"\u001b[0;31mFieldError\u001b[0m Traceback (most recent call last)",
187-
"\u001b[0;32m<ipython-input-6-2aabe11f9114>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mno_1c\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmg\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0madd_field\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'node'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'field__number_one'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minput_array\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcopy\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mFalse\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0munits\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'm'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
188-
"\u001b[0;32m/Users/danhobley/git_landlab/landlab/field/grouped.pyc\u001b[0m in \u001b[0;36madd_field\u001b[0;34m(self, *args, **kwds)\u001b[0m\n\u001b[1;32m 716\u001b[0m \u001b[0;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'number of arguments must be 2 or 3'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 717\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 718\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mgroup\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0madd_field\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mvalue_array\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwds\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 719\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 720\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mset_units\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mgroup\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mname\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0munits\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
189-
"\u001b[0;32m/Users/danhobley/git_landlab/landlab/field/scalar_data_fields.pyc\u001b[0m in \u001b[0;36madd_field\u001b[0;34m(self, name, value_array, units, copy, noclobber, **kwds)\u001b[0m\n\u001b[1;32m 376\u001b[0m \"\"\"\n\u001b[1;32m 377\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mnoclobber\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0mname\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 378\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mFieldError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'{name}: already exists'\u001b[0m\u001b[0;34m.\u001b[0m \u001b[0mformat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 379\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 380\u001b[0m \u001b[0mvalue_array\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0masarray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mvalue_array\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
190-
"\u001b[0;31mFieldError\u001b[0m: field__number_one: already exists"
171+
"name": "stdout",
172+
"output_type": "stream",
173+
"text": [
174+
"ERROR: The field already exists.\n"
191175
]
192176
}
193177
],
194178
"source": [
195-
"no_1c = mg.add_field('node', 'field__number_one', input_array, copy=False, units='m')"
179+
"try:\n",
180+
" no_1c = mg.add_field('node', 'field__number_one', input_array, copy=False, units='m')\n",
181+
"except FieldError:\n",
182+
" print('ERROR: The field already exists.')"
196183
]
197184
},
198185
{
199186
"cell_type": "code",
200187
"execution_count": 7,
201-
"metadata": {
202-
"collapsed": false
203-
},
188+
"metadata": {},
204189
"outputs": [
205190
{
206191
"name": "stdout",
@@ -220,9 +205,7 @@
220205
{
221206
"cell_type": "code",
222207
"execution_count": 8,
223-
"metadata": {
224-
"collapsed": false
225-
},
208+
"metadata": {},
226209
"outputs": [
227210
{
228211
"name": "stdout",
@@ -254,9 +237,7 @@
254237
{
255238
"cell_type": "code",
256239
"execution_count": 9,
257-
"metadata": {
258-
"collapsed": false
259-
},
240+
"metadata": {},
260241
"outputs": [
261242
{
262243
"data": {
@@ -283,9 +264,7 @@
283264
{
284265
"cell_type": "code",
285266
"execution_count": 10,
286-
"metadata": {
287-
"collapsed": false
288-
},
267+
"metadata": {},
289268
"outputs": [
290269
{
291270
"data": {
@@ -312,9 +291,7 @@
312291
{
313292
"cell_type": "code",
314293
"execution_count": 11,
315-
"metadata": {
316-
"collapsed": false
317-
},
294+
"metadata": {},
318295
"outputs": [
319296
{
320297
"data": {
@@ -334,9 +311,7 @@
334311
{
335312
"cell_type": "code",
336313
"execution_count": 12,
337-
"metadata": {
338-
"collapsed": false
339-
},
314+
"metadata": {},
340315
"outputs": [],
341316
"source": [
342317
"mg.at_node.clear() # delete all fields at nodes"
@@ -345,9 +320,7 @@
345320
{
346321
"cell_type": "code",
347322
"execution_count": 13,
348-
"metadata": {
349-
"collapsed": false
350-
},
323+
"metadata": {},
351324
"outputs": [
352325
{
353326
"data": {
@@ -367,9 +340,7 @@
367340
{
368341
"cell_type": "code",
369342
"execution_count": 14,
370-
"metadata": {
371-
"collapsed": false
372-
},
343+
"metadata": {},
373344
"outputs": [
374345
{
375346
"data": {
@@ -389,9 +360,7 @@
389360
{
390361
"cell_type": "code",
391362
"execution_count": 15,
392-
"metadata": {
393-
"collapsed": false
394-
},
363+
"metadata": {},
395364
"outputs": [
396365
{
397366
"data": {
@@ -411,9 +380,7 @@
411380
{
412381
"cell_type": "code",
413382
"execution_count": 16,
414-
"metadata": {
415-
"collapsed": false
416-
},
383+
"metadata": {},
417384
"outputs": [
418385
{
419386
"data": {
@@ -440,9 +407,7 @@
440407
{
441408
"cell_type": "code",
442409
"execution_count": 17,
443-
"metadata": {
444-
"collapsed": false
445-
},
410+
"metadata": {},
446411
"outputs": [
447412
{
448413
"data": {
@@ -484,9 +449,9 @@
484449
"name": "python",
485450
"nbconvert_exporter": "python",
486451
"pygments_lexer": "ipython2",
487-
"version": "2.7.11"
452+
"version": "2.7.13"
488453
}
489454
},
490455
"nbformat": 4,
491-
"nbformat_minor": 0
456+
"nbformat_minor": 1
492457
}

0 commit comments

Comments
 (0)