Skip to content

Commit 284b189

Browse files
committed
Use print() function in both Python 2 and Python 3
Legacy __print__ statements are syntax errors in Python 3 but __print()__ function works as expected in both Python 2 and Python 3.
1 parent a0b0a10 commit 284b189

File tree

10 files changed

+68
-61
lines changed

10 files changed

+68
-61
lines changed

misc/wiki-scripts/update-matrix.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#must be ran from scl/build_matrix
77

8+
from __future__ import print_function
89
from xml.etree import ElementTree as ET
910
import os
1011
from datetime import date
@@ -49,7 +50,7 @@ def find_xml():
4950
if str(date.today().year) in dirname:
5051
i += 1
5152
if i > 1:
52-
print "Too many directories, exiting"
53+
print("Too many directories, exiting")
5354
exit(1)
5455
xml = os.path.join("Testing", dirname, "Test.xml")
5556
return xml
@@ -58,31 +59,31 @@ def find_wiki():
5859
#find wiki and matrix file, issue 'git pull'
5960
wikipath = os.path.abspath("../../wiki-scl")
6061
if not os.path.isdir(os.path.join(wikipath,".git")):
61-
print "Can't find wiki or not a git repo"
62+
print("Can't find wiki or not a git repo")
6263
exit(1)
6364
p = subprocess.call(["git", "pull", "origin"], cwd=wikipath)
6465
if not p == 0:
65-
print "'git pull' exited with error"
66+
print("'git pull' exited with error")
6667
exit(1)
6768
matrix = os.path.join(wikipath, "Schema-build-matrix.md")
6869
if not os.path.isfile(matrix):
69-
print "Matrix file doesn't exist or isn't a file"
70+
print("Matrix file doesn't exist or isn't a file")
7071
exit(1)
7172
return wikipath,matrix
7273

7374
def git_push(path,f):
7475
p = subprocess.call(["git", "add", f], cwd=path)
7576
if not p == 0:
76-
print "'git add' exited with error"
77+
print("'git add' exited with error")
7778
exit(1)
7879
msg = date.today().__str__() + " - schema matrix updated by update-matrix.py"
7980
p = subprocess.call(["git", "commit", "-m", msg ], cwd=path)
8081
if not p == 0:
81-
print "'git commit' exited with error"
82+
print("'git commit' exited with error")
8283
exit(1)
8384
p = subprocess.call(["git", "push", "origin"], cwd=path)
8485
if not p == 0:
85-
print "'git push' exited with error"
86+
print("'git push' exited with error")
8687
exit(1)
8788

8889

@@ -98,8 +99,8 @@ def read_tests(xml):
9899
# read all <Test>s in xml, create mixed html/markdown
99100
try:
100101
tree = ET.parse(xml)
101-
except Exception, inst:
102-
print "Unexpected error opening %s: %s" % (xml, inst)
102+
except Exception as inst:
103+
print("Unexpected error opening %s: %s" % (xml, inst))
103104
return
104105

105106
root = tree.getroot()

src/exp2python/python/SCL/AggregationDataTypes.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,9 @@ class ARRAY(BaseType.Type, BaseType.Aggregate):
126126
"""
127127
def __init__( self , bound_1 , bound_2 , base_type , UNIQUE = False, OPTIONAL=False, scope = None):
128128
BaseType.Type.__init__(self, base_type, scope)
129-
if not type(bound_1)==int:
129+
if not isinstance(bound_1, int):
130130
raise TypeError("ARRAY lower bound must be an integer")
131-
if not type(bound_2)==int:
131+
if not isinstance(bound_2, int):
132132
raise TypeError("ARRAY upper bound must be an integer")
133133
if not (bound_1 <= bound_2):
134134
raise AssertionError("ARRAY lower bound must be less than or equal to upper bound")
@@ -235,17 +235,17 @@ class LIST(BaseType.Type, BaseType.Aggregate):
235235
"""
236236
def __init__( self , bound_1 , bound_2 , base_type , UNIQUE = False, scope = None):
237237
BaseType.Type.__init__(self, base_type, scope)
238-
if not type(bound_1)==int:
238+
if not isinstance(bound_1, int):
239239
raise TypeError("LIST lower bound must be an integer")
240240
# bound_2 can be set to None
241241
self._unbounded = False
242242
if bound_2 == None:
243243
self._unbounded = True
244-
elif not type(bound_2)==int:
244+
elif not isinstance(bound_2, int):
245245
raise TypeError("LIST upper bound must be an integer")
246246
if not bound_1>=0:
247247
raise AssertionError("LIST lower bound must be greater of equal to 0")
248-
if (type(bound_2)==int and not (bound_1 <= bound_2)):
248+
if (isinstance(bound_2, int) and not (bound_1 <= bound_2)):
249249
raise AssertionError("ARRAY lower bound must be less than or equal to upper bound")
250250
# set up class attributes
251251
self._bound_1 = bound_1
@@ -282,14 +282,14 @@ def get_loindex(self):
282282

283283
def get_hibound(self):
284284
hibound = self._bound_2
285-
if type(hibound)==int:
285+
if isinstance(hibound, int):
286286
return INTEGER(hibound)
287287
else:
288288
return hibound
289289

290290
def get_lobound(self):
291291
lobound = self._bound_1
292-
if type(lobound)==int:
292+
if isinstance(lobound, int):
293293
return INTEGER(lobound)
294294
else:
295295
return lobound
@@ -409,17 +409,17 @@ class BAG(BaseType.Type, BaseType.Aggregate):
409409
"""
410410
def __init__( self , bound_1 , bound_2 , base_type , scope = None):
411411
BaseType.Type.__init__(self, base_type, scope)
412-
if not type(bound_1)==int:
412+
if not isinstance(bound_1, int):
413413
raise TypeError("LIST lower bound must be an integer")
414414
# bound_2 can be set to None
415415
self._unbounded = False
416416
if bound_2 == None:
417417
self._unbounded = True
418-
elif not type(bound_2)==int:
418+
elif not isinstance(bound_2, int):
419419
raise TypeError("LIST upper bound must be an integer")
420420
if not bound_1>=0:
421421
raise AssertionError("LIST lower bound must be greater of equal to 0")
422-
if (type(bound_2)==int and not (bound_1 <= bound_2)):
422+
if (isinstance(bound_2, int) and not (bound_1 <= bound_2)):
423423
raise AssertionError("ARRAY lower bound must be less than or equal to upper bound")
424424
# set up class attributes
425425
self._bound_1 = bound_1
@@ -462,14 +462,14 @@ def get_loindex(self):
462462

463463
def get_hibound(self):
464464
hibound = self._bound_2
465-
if type(hibound)==int:
465+
if isinstance(hibound, int):
466466
return INTEGER(hibound)
467467
else:
468468
return hibound
469469

470470
def get_lobound(self):
471471
lobound = self._bound_1
472-
if type(lobound)==int:
472+
if isinstance(lobound, int):
473473
return INTEGER(lobound)
474474
else:
475475
return lobound
@@ -527,17 +527,17 @@ class SET(BaseType.Type, BaseType.Aggregate):
527527
"""
528528
def __init__( self , bound_1 , bound_2 , base_type , scope = None):
529529
BaseType.Type.__init__(self, base_type, scope)
530-
if not type(bound_1)==int:
530+
if not isinstance(bound_1, int):
531531
raise TypeError("LIST lower bound must be an integer")
532532
# bound_2 can be set to None
533533
self._unbounded = False
534534
if bound_2 == None:
535535
self._unbounded = True
536-
elif not type(bound_2)==int:
536+
elif not isinstance(bound_2, int):
537537
raise TypeError("LIST upper bound must be an integer")
538538
if not bound_1>=0:
539539
raise AssertionError("LIST lower bound must be greater of equal to 0")
540-
if (type(bound_2)==int and not (bound_1 <= bound_2)):
540+
if (isinstance(bound_2, int) and not (bound_1 <= bound_2)):
541541
raise AssertionError("ARRAY lower bound must be less than or equal to upper bound")
542542
# set up class attributes
543543
self._bound_1 = bound_1
@@ -581,14 +581,14 @@ def get_loindex(self):
581581

582582
def get_hibound(self):
583583
hibound = self._bound_2
584-
if type(hibound)==int:
584+
if isinstance(hibound, int):
585585
return INTEGER(hibound)
586586
else:
587587
return hibound
588588

589589
def get_lobound(self):
590590
lobound = self._bound_1
591-
if type(lobound)==int:
591+
if isinstance(lobound, int):
592592
return INTEGER(lobound)
593593
else:
594594
return lobound

src/exp2python/python/SCL/BaseType.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from __future__ import print_function
12
# Copyright (c) 2011, Thomas Paviot (tpaviot@gmail.com)
23
# All rights reserved.
34

@@ -43,10 +44,10 @@ def get_scope(self):
4344
return self._scope
4445

4546
def get_type(self):
46-
if type(self._typedef) == str:
47+
if isinstance(self._typedef, str):
4748
if self._scope == None:
4849
raise AssertionError('No scope defined for this type')
49-
elif vars(self._scope).has_key(self._typedef):
50+
elif self._typedef in vars(self._scope):
5051
return vars(self._scope)[self._typedef]
5152
else:
5253
raise TypeError("Type '%s' is not defined in given scope"%self._typedef)
@@ -65,5 +66,5 @@ class Aggregate:
6566
class line:
6667
pass
6768
new_type = Type('lie',scp)
68-
print new_type.get_type()
69+
print(new_type.get_type())
6970

src/exp2python/python/SCL/ConstructedDataTypes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class SELECT(object):
6666
"""
6767
def __init__(self,*kargs,**args):
6868
# first defining the scope
69-
if args.has_key('scope'):
69+
if 'scope' in args:
7070
self._scope = args['scope']
7171
else:
7272
self._scope = None

src/exp2python/python/SCL/Model.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from __future__ import print_function
12
# Copyright (c) 2011-2012, Thomas Paviot (tpaviot@gmail.com)
23
# All rights reserved.
34

@@ -33,7 +34,7 @@ class Model(objet):
3334
""" The container for entity instances
3435
"""
3536
def __init_(self):
36-
print "Model initialized"
37+
print("Model initialized")
3738
self._instances = []
3839

3940
def add_instance(self, entity_instance):

src/exp2python/python/SCL/SimpleDataTypes.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"""
3333
Docstrings are courtesy of ISO 10303-11:1994(E)
3434
"""
35+
from __future__ import print_function
3536

3637
class NUMBER:
3738
"""
@@ -198,19 +199,19 @@ def __init__(self, value, width=-1, fixed=False):
198199

199200

200201
if __name__=="__main__":
201-
print "Creating REAL from float value"
202+
print("Creating REAL from float value")
202203
a = REAL(1.5)
203-
print a*2
204-
print "Creating REAL from string value"
204+
print(a*2)
205+
print("Creating REAL from string value")
205206
a = REAL("1.2")
206-
print a*3
207-
print "Creating INTEGER from int value"
207+
print(a*3)
208+
print("Creating INTEGER from int value")
208209
b = INTEGER(2)
209210
c = INTEGER(3)
210-
print b+c
211-
print "Creating INTEGER from string value"
211+
print(b+c)
212+
print("Creating INTEGER from string value")
212213
e = INTEGER("5")
213214
f = INTEGER("8")
214-
print e*f
215+
print(e*f)
215216

216217

src/exp2python/python/SCL/TypeChecker.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from __future__ import print_function
12
# Copyright (c) 2011, Thomas Paviot (tpaviot@gmail.com)
23
# All rights reserved.
34

@@ -40,7 +41,7 @@ def cast_python_object_to_aggregate(obj, aggregate):
4041
[1.,2.,3.]-> ARRAY(1,3,REAL)"""
4142
aggregate_lower_bound = aggregate.bound_1()
4243
aggregate_upper_bound = aggregate.bound_2()
43-
if type(obj)==list:
44+
if isinstance(obj, list):
4445
for idx in range(aggregate_lower_bound,aggregate_upper_bound+1):
4546
aggregate[idx] = obj[idx-aggregate_lower_bound]
4647
return aggregate
@@ -51,9 +52,9 @@ def check_type(instance, expected_type):
5152
"""
5253
type_match = False #by default, will be set to True if any match
5354
if DEBUG:
54-
print "==="
55-
print "Instance passed: ",instance
56-
print "Expected type: ", expected_type
55+
print("===")
56+
print("Instance passed: ",instance)
57+
print("Expected type: ", expected_type)
5758
# in the case of an enumeration, we have to check if the instance is in the list
5859
if (isinstance(expected_type,ENUMERATION)):
5960
allowed_ids = expected_type.get_enum_ids()
@@ -71,11 +72,11 @@ def check_type(instance, expected_type):
7172
if RAISE_EXCEPTION_IF_TYPE_DOES_NOT_MATCH:
7273
raise TypeError('Argument type must be %s (you passed %s)'%(allowed_types,type(instance)))
7374
else:
74-
print "WARNING: expected '%s' but passed a '%s', casting from python value to EXPRESS type"%(allowed_types, type(instance))
75+
print("WARNING: expected '%s' but passed a '%s', casting from python value to EXPRESS type"%(allowed_types, type(instance)))
7576
return False
7677
elif (isinstance(expected_type, BaseType.Aggregate)):
7778
# first check that they are instance of the same class
78-
if not (type(instance) == type(expected_type)):
79+
if not (isinstance(instance, type(expected_type))):
7980
raise TypeError('Expected %s but passed %s'%(type(expected_type),type(instance)))
8081
# then check that the base type is the same
8182
elif not (instance.get_type() == expected_type.get_type()):
@@ -96,6 +97,6 @@ def check_type(instance, expected_type):
9697
if RAISE_EXCEPTION_IF_TYPE_DOES_NOT_MATCH:
9798
raise TypeError('Argument type must be %s (you passed %s)'%(expected_type,type(instance)))
9899
else:
99-
print "WARNING: expected '%s' but passed a '%s', casting from python value to EXPRESS type"%(expected_type, type(instance))
100+
print("WARNING: expected '%s' but passed a '%s', casting from python value to EXPRESS type"%(expected_type, type(instance)))
100101
return False
101102
return True

src/exp2python/python/SCL/Utils.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3131

3232
''' This module provide string utils'''
33+
from __future__ import print_function
3334

3435
def process_nested_parent_str(attr_str,idx=0):
3536
'''
@@ -61,10 +62,10 @@ def process_nested_parent_str(attr_str,idx=0):
6162
return params,k
6263

6364
if __name__=="__main__":
64-
print process_nested_parent_str2("'A'")[0]
65-
print process_nested_parent_str2("30.0,0.0,5.0")[0]
66-
print process_nested_parent_str2("1,2,(3,4,5),6,7,8")[0]
67-
print process_nested_parent_str2("(#9149,#9166),#9142,.T.")[0]
65+
print(process_nested_parent_str2("'A'")[0])
66+
print(process_nested_parent_str2("30.0,0.0,5.0")[0])
67+
print(process_nested_parent_str2("1,2,(3,4,5),6,7,8")[0])
68+
print(process_nested_parent_str2("(#9149,#9166),#9142,.T.")[0])
6869

6970

7071

src/exp2python/python/SCL/essa_par.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from __future__ import print_function
12
def process_nested_parent_str(attr_str):
23
'''
34
The first letter should be a parenthesis
@@ -63,9 +64,9 @@ def process_nested_parent_str2(attr_str,idx=0):
6364
#print process_nested_parent_str2('1,2,3,4,5,6')
6465
#idx=0
6566
#print process_nested_parent_str2("'A','B','C'")
66-
print process_nested_parent_str2("'A'")[0]
67-
print process_nested_parent_str2("30.0,0.0,5.0")[0]
68-
print process_nested_parent_str2("(Thomas)")[0]
69-
print process_nested_parent_str2("Thomas, Paviot, ouais")[0]
70-
print process_nested_parent_str2("1,2,(3,4,5),6,7,8")[0]
71-
print process_nested_parent_str2("(#9149,#9166),#9142,.T.")[0]
67+
print(process_nested_parent_str2("'A'")[0])
68+
print(process_nested_parent_str2("30.0,0.0,5.0")[0])
69+
print(process_nested_parent_str2("(Thomas)")[0])
70+
print(process_nested_parent_str2("Thomas, Paviot, ouais")[0])
71+
print(process_nested_parent_str2("1,2,(3,4,5),6,7,8")[0])
72+
print(process_nested_parent_str2("(#9149,#9166),#9142,.T.")[0])

0 commit comments

Comments
 (0)