Skip to content

Commit 3e1a040

Browse files
committed
refactoring some bits to be python3 compatible
1 parent e2f40bd commit 3e1a040

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

ncldDump/ncldDump.py

Lines changed: 22 additions & 14 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
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,7 +103,7 @@ def parseType(obj):
100103
#
101104
result = '?'
102105

103-
if True == isinstance(obj, unicode):
106+
if True == isinstance(obj, string_types):
104107
result = ''
105108
elif True == isinstance(obj, str):
106109
result = ''
@@ -135,24 +138,27 @@ def parseType(obj):
135138
def convertToStringHook(item, ignoreDicts = False):
136139
'''
137140
This function is passed to the json load function as an object hook. It
138-
converts any unicode strings into ASCII strings.
141+
converts any string_types strings into ASCII strings.
139142
140143
item [in] An item passed in for processing.
141144
ignoreDicts [in] If this is set to True ignore any dict objects passed in.
142-
returns Items with any unicode strings converted to ASCII.
145+
returns Items with any string_types strings converted to ASCII.
143146
'''
144147

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
148+
# If this is a string_types string, convert it. If this is a list, convert any
149+
# contained string_types strings. If this is a dict and it hasn't been converted
150+
# already, convert any contained string_types strings. Otherwise, leave the item
148151
# alone.
149152
#
150-
if isinstance(item, unicode):
151-
result = item.encode('utf-8')
153+
if isinstance(item, string_types):
154+
if sys.version_info[0] < 3:
155+
result = item.encode('utf-8')
156+
else:
157+
result = item
152158
elif isinstance(item, list):
153159
result = [ convertToStringHook(element, True) for element in item ]
154160
elif isinstance(item, dict) and not ignoreDicts:
155-
result = { convertToStringHook(key, True) : convertToStringHook(value, True) for key, value in item.iteritems() }
161+
result = { convertToStringHook(key, True) : convertToStringHook(value, True) for key, value in item.items() }
156162
else:
157163
result = item
158164

@@ -294,7 +300,7 @@ def resolveValue(name, value, aliasDict):
294300

295301
# If the value is not a string, get a string representation.
296302
#
297-
if False == isinstance(value, str) and False == isinstance(value, unicode):
303+
if False == isinstance(value, str) and False == isinstance(value, string_types):
298304
value = str(value)
299305

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

380386
# If the value is a string, wrap it in '"' characters.
381387
#
382-
if True == isinstance(attrValue, str) or True == isinstance(attrValue, unicode):
388+
if True == isinstance(attrValue, str) or True == isinstance(attrValue, string_types):
383389
attrValue = '"' + str(attrValue) + '"'
384390

385391
valueEntry = { 'element' : attrValue }
@@ -424,7 +430,7 @@ def parseGroup(ncObj, aliasDict):
424430
dimList = []
425431

426432
try:
427-
for dimName, dimObj in ncObj.dimensions.iteritems():
433+
for dimName, dimObj in ncObj.dimensions.items():
428434
dimEntry = {'name' : dimName }
429435

430436
if True == dimObj.isunlimited():
@@ -445,7 +451,7 @@ def parseGroup(ncObj, aliasDict):
445451
varList = []
446452

447453
try:
448-
for varName, varObj in ncObj.variables.iteritems():
454+
for varName, varObj in ncObj.variables.items():
449455
varType = parseDtype(varObj.dtype)
450456

451457
varEntry = {'name' : varName, 'type' : varType}
@@ -480,6 +486,8 @@ def parseGroup(ncObj, aliasDict):
480486

481487
varList.append(varEntry)
482488
except:
489+
#type_, value_, traceback_ = sys.exc_info()
490+
#tb = traceback.format_tb(traceback_)
483491
pass
484492

485493
if 0 < len(varList):
@@ -522,7 +530,7 @@ def parseDataset(ncObj, aliasDict):
522530

523531
# If there are any other groups, add them as well.
524532
#
525-
for groupName, groupObj in ncObj.groups.iteritems():
533+
for groupName, groupObj in ncObj.groups.items():
526534
groupEntry = parseGroup(groupObj, aliasDict)
527535

528536
groupEntry['groupName'] = groupName

0 commit comments

Comments
 (0)