Skip to content

Commit d672194

Browse files
authored
Merge pull request #61 from jyucsiro/nclddump_py3
minor edits to nclddump to be python3 compatible
2 parents 57d7bad + c50b1df commit d672194

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

ncldDump/ncldDump.py

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from __future__ import print_function
2+
from six import string_types, PY2
23

34
import argparse
45
import jinja2
@@ -9,6 +10,8 @@
910
import re
1011
import sys
1112

13+
import pprint
14+
import traceback
1215

1316
def parseArgs(args):
1417
'''
@@ -100,9 +103,7 @@ def parseType(obj):
100103
#
101104
result = '?'
102105

103-
if True == isinstance(obj, unicode):
104-
result = ''
105-
elif True == isinstance(obj, str):
106+
if True == isinstance(obj, string_types):
106107
result = ''
107108
elif True == isinstance(obj, numpy.int8):
108109
result = 'b'
@@ -135,24 +136,27 @@ def parseType(obj):
135136
def convertToStringHook(item, ignoreDicts = False):
136137
'''
137138
This function is passed to the json load function as an object hook. It
138-
converts any unicode strings into ASCII strings.
139+
converts any string_types strings into ASCII strings.
139140
140141
item [in] An item passed in for processing.
141142
ignoreDicts [in] If this is set to True ignore any dict objects passed in.
142-
returns Items with any unicode strings converted to ASCII.
143+
returns Items with any string_types strings converted to ASCII.
143144
'''
144145

145-
# If this is a unicode string, convert it. If this is a list, convert any
146-
# contained unicode strings. If this is a dict and it hasn't been converted
147-
# already, convert any contained unicode strings. Otherwise, leave the item
146+
# If this is a string_types string, convert it. If this is a list, convert any
147+
# contained string_types strings. If this is a dict and it hasn't been converted
148+
# already, convert any contained string_types strings. Otherwise, leave the item
148149
# alone.
149150
#
150-
if isinstance(item, unicode):
151-
result = item.encode('utf-8')
151+
if isinstance(item, string_types):
152+
if PY2:
153+
result = item.encode('utf-8')
154+
else:
155+
result = item
152156
elif isinstance(item, list):
153157
result = [ convertToStringHook(element, True) for element in item ]
154158
elif isinstance(item, dict) and not ignoreDicts:
155-
result = { convertToStringHook(key, True) : convertToStringHook(value, True) for key, value in item.iteritems() }
159+
result = { convertToStringHook(key, True) : convertToStringHook(value, True) for key, value in item.items() }
156160
else:
157161
result = item
158162

@@ -294,7 +298,7 @@ def resolveValue(name, value, aliasDict):
294298

295299
# If the value is not a string, get a string representation.
296300
#
297-
if False == isinstance(value, str) and False == isinstance(value, unicode):
301+
if False == isinstance(value, str) and False == isinstance(value, string_types):
298302
value = str(value)
299303

300304
# If the value starts with 'http', interpret the entire string as a
@@ -379,7 +383,7 @@ def parseAttributes(ncObj, aliasDict):
379383

380384
# If the value is a string, wrap it in '"' characters.
381385
#
382-
if True == isinstance(attrValue, str) or True == isinstance(attrValue, unicode):
386+
if True == isinstance(attrValue, str) or True == isinstance(attrValue, string_types):
383387
attrValue = '"' + str(attrValue) + '"'
384388

385389
valueEntry = { 'element' : attrValue }
@@ -424,7 +428,7 @@ def parseGroup(ncObj, aliasDict):
424428
dimList = []
425429

426430
try:
427-
for dimName, dimObj in ncObj.dimensions.iteritems():
431+
for dimName, dimObj in ncObj.dimensions.items():
428432
dimEntry = {'name' : dimName }
429433

430434
if True == dimObj.isunlimited():
@@ -445,7 +449,7 @@ def parseGroup(ncObj, aliasDict):
445449
varList = []
446450

447451
try:
448-
for varName, varObj in ncObj.variables.iteritems():
452+
for varName, varObj in ncObj.variables.items():
449453
varType = parseDtype(varObj.dtype)
450454

451455
varEntry = {'name' : varName, 'type' : varType}
@@ -480,6 +484,8 @@ def parseGroup(ncObj, aliasDict):
480484

481485
varList.append(varEntry)
482486
except:
487+
#type_, value_, traceback_ = sys.exc_info()
488+
#tb = traceback.format_tb(traceback_)
483489
pass
484490

485491
if 0 < len(varList):
@@ -522,7 +528,7 @@ def parseDataset(ncObj, aliasDict):
522528

523529
# If there are any other groups, add them as well.
524530
#
525-
for groupName, groupObj in ncObj.groups.iteritems():
531+
for groupName, groupObj in ncObj.groups.items():
526532
groupEntry = parseGroup(groupObj, aliasDict)
527533

528534
groupEntry['groupName'] = groupName

0 commit comments

Comments
 (0)