Skip to content

Commit 9bf4b0e

Browse files
authored
Remove explicit Python 2 support (#32)
* Remove explicit Python 2 support The code no longer checks the Python version and no longer uses optparse for older versions. In addition some `__future__` imports are removed. In `smatch-table.py`, timing uses `time.perf_counter()`. Otherwise nothing is really done to update to Python 3 code. Neither smatch.py nor smatch-table.py work with Python2 after this commit, but smatch.py can run if the print() functions are resolved somehow (e.g., by using a logger instead of print()). That said, I deliberately do not put in such accommodations because Python 2 support is a non-goal. Resolves #31 * Add py38 to .appveyor.yml, fix CHANGELOG link
1 parent 8874549 commit 9bf4b0e

7 files changed

+102
-121
lines changed

.appveyor.yml

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ environment:
33
- TOXENV: py35
44
- TOXENV: py36
55
- TOXENV: py37
6+
- TOXENV: py38
67

78
build: off
89

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ python:
33
- '3.5'
44
- '3.6'
55
- '3.7'
6+
- '3.8'
67
install: pip install tox-travis
78
script: tox
89
deploy:

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
## Unreleased
55

6+
* Python 2 support is removed ([#31])
7+
* Python 3.8 support is added
68
* Smatch version is taken from git tag (see [#22][])
79
* Add CI/CD configuration (see PR [#23][])
810
* Better handle deinversion of special roles ([#10][])
@@ -171,4 +173,5 @@ The following are taken from an old `update_log` file:
171173
[#23]: https://github.com/snowblink14/smatch/pull/23
172174
[#25]: https://github.com/snowblink14/smatch/pull/25
173175
[#27]: https://github.com/snowblink14/smatch/pull/27
176+
[#31]: https://github.com/snowblink14/smatch/issues/31
174177

setup.py

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
'Programming Language :: Python :: 3.5',
3232
'Programming Language :: Python :: 3.6',
3333
'Programming Language :: Python :: 3.7',
34+
'Programming Language :: Python :: 3.8',
3435
'Topic :: Scientific/Engineering :: Information Analysis',
3536
'Topic :: Software Development :: Libraries :: Python Modules',
3637
'Topic :: Text Processing :: Linguistic',

smatch-table.py

+36-26
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
#!/usr/bin/env python
22

3-
from __future__ import print_function
4-
53
import sys
64
import os
75
import time
86

97
import amr
108
import smatch
119

12-
1310
ERROR_LOG = sys.stderr
1411

1512
DEBUG_LOG = sys.stderr
@@ -151,21 +148,6 @@ def pprint_table(table):
151148
print("\n")
152149

153150

154-
def build_arg_parser():
155-
"""
156-
Build an argument parser using argparse. Use it when python version is 2.7 or later.
157-
158-
"""
159-
parser = argparse.ArgumentParser(description="Smatch table calculator -- arguments")
160-
parser.add_argument("--fl", type=argparse.FileType('r'), help='AMR ID list file')
161-
parser.add_argument('-f', nargs='+', help='AMR IDs (at least one)')
162-
parser.add_argument("-p", nargs='*', help="User list (can be none)")
163-
parser.add_argument("--fd", default=isi_dir_pre, help="AMR File directory. Default=location on isi machine")
164-
parser.add_argument('-r', type=int, default=4, help='Restart number (Default:4)')
165-
parser.add_argument('-v', action='store_true', help='Verbose output (Default:False)')
166-
return parser
167-
168-
169151
def cb(option, value, parser):
170152
"""
171153
Callback function to handle variable number of arguments in optparse
@@ -262,9 +244,9 @@ def main(arguments):
262244
table[i+1].append(names[i])
263245
for j in range(0, len_name):
264246
if i != j:
265-
start = time.time()
247+
start = time.perf_counter()
266248
table[i+1].append(compute_files(names[i], names[j], ids, args.fd, args.r))
267-
end = time.time()
249+
end = time.perf_counter()
268250
if table[i+1][-1] != -1.0:
269251
acc_time += end-start
270252
else:
@@ -283,22 +265,50 @@ def main(arguments):
283265

284266

285267
if __name__ == "__main__":
286-
whole_start = time.time()
287-
parser = None
288-
args = None
268+
whole_start = time.perf_counter()
269+
289270
import argparse
290-
parser = build_arg_parser()
271+
272+
parser = argparse.ArgumentParser(description="Smatch table calculator")
273+
parser.add_argument(
274+
"--fl",
275+
type=argparse.FileType('r'),
276+
help='AMR ID list file')
277+
parser.add_argument(
278+
'-f',
279+
nargs='+',
280+
help='AMR IDs (at least one)')
281+
parser.add_argument(
282+
"-p",
283+
nargs='*',
284+
help="User list (can be none)")
285+
parser.add_argument(
286+
"--fd",
287+
default=isi_dir_pre,
288+
help="AMR File directory. Default=location on isi machine")
289+
parser.add_argument(
290+
'-r',
291+
type=int,
292+
default=4,
293+
help='Restart number (Default:4)')
294+
parser.add_argument(
295+
'-v',
296+
action='store_true',
297+
help='Verbose output (Default:False)')
298+
291299
args = parser.parse_args()
300+
292301
# Regularize fd, add "/" at the end if needed
293302
if args.fd[-1] != "/":
294303
args.fd += "/"
304+
295305
# acc_time is the smatch calculation time
296306
acc_time = main(args)
297-
whole_end = time.time()
307+
whole_end = time.perf_counter()
298308
# time of the whole running process
299309
whole_time = whole_end - whole_start
310+
300311
# print if needed
301312
# print("Accumulated computation time", acc_time, file=ERROR_LOG)
302313
# print("Total time", whole_time, file=ERROR_LOG)
303314
# print("Percentage", float(acc_time)/float(whole_time), file=ERROR_LOG)
304-

smatch.py

+59-94
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,9 @@
88
99
"""
1010

11-
from __future__ import print_function
12-
from __future__ import division
11+
import random
1312

1413
import amr
15-
import os
16-
import random
1714
import sys
1815

1916
# total number of iteration in smatch computation
@@ -44,64 +41,6 @@
4441
match_triple_dict = {}
4542

4643

47-
def build_arg_parser():
48-
"""
49-
Build an argument parser using argparse. Use it when python version is 2.7 or later.
50-
51-
"""
52-
parser = argparse.ArgumentParser(description="Smatch calculator -- arguments")
53-
parser.add_argument('-f', nargs=2, required=True, type=argparse.FileType('r'),
54-
help='Two files containing AMR pairs. AMRs in each file are separated by a single blank line')
55-
parser.add_argument('-r', type=int, default=4, help='Restart number (Default:4)')
56-
parser.add_argument('--significant', type=int, default=2, help='significant digits to output (default: 2)')
57-
parser.add_argument('-v', action='store_true', help='Verbose output (Default:false)')
58-
parser.add_argument('--vv', action='store_true', help='Very Verbose output (Default:false)')
59-
parser.add_argument('--ms', action='store_true', default=False,
60-
help='Output multiple scores (one AMR pair a score)'
61-
'instead of a single document-level smatch score (Default: false)')
62-
parser.add_argument('--pr', action='store_true', default=False,
63-
help="Output precision and recall as well as the f-score. Default: false")
64-
parser.add_argument('--justinstance', action='store_true', default=False,
65-
help="just pay attention to matching instances")
66-
parser.add_argument('--justattribute', action='store_true', default=False,
67-
help="just pay attention to matching attributes")
68-
parser.add_argument('--justrelation', action='store_true', default=False,
69-
help="just pay attention to matching relations")
70-
71-
return parser
72-
73-
74-
def build_arg_parser2():
75-
"""
76-
Build an argument parser using optparse. Use it when python version is 2.5 or 2.6.
77-
78-
"""
79-
usage_str = "Smatch calculator -- arguments"
80-
parser = optparse.OptionParser(usage=usage_str)
81-
parser.add_option("-f", "--files", nargs=2, dest="f", type="string",
82-
help='Two files containing AMR pairs. AMRs in each file are ' \
83-
'separated by a single blank line. This option is required.')
84-
parser.add_option("-r", "--restart", dest="r", type="int", help='Restart number (Default: 4)')
85-
parser.add_option('--significant', dest="significant", type="int", default=2,
86-
help='significant digits to output (default: 2)')
87-
parser.add_option("-v", "--verbose", action='store_true', dest="v", help='Verbose output (Default:False)')
88-
parser.add_option("--vv", "--veryverbose", action='store_true', dest="vv",
89-
help='Very Verbose output (Default:False)')
90-
parser.add_option("--ms", "--multiple_score", action='store_true', dest="ms",
91-
help='Output multiple scores (one AMR pair a score) instead of ' \
92-
'a single document-level smatch score (Default: False)')
93-
parser.add_option('--pr', "--precision_recall", action='store_true', dest="pr",
94-
help="Output precision and recall as well as the f-score. Default: false")
95-
parser.add_option('--justinstance', action='store_true', default=False,
96-
help="just pay attention to matching instances")
97-
parser.add_option('--justattribute', action='store_true', default=False,
98-
help="just pay attention to matching attributes")
99-
parser.add_option('--justrelation', action='store_true', default=False,
100-
help="just pay attention to matching relations")
101-
parser.set_defaults(r=4, v=False, ms=False, pr=False)
102-
return parser
103-
104-
10544
def get_best_match(instance1, attribute1, relation1,
10645
instance2, attribute2, relation2,
10746
prefix1, prefix2, doinstance=True, doattribute=True, dorelation=True):
@@ -854,36 +793,62 @@ def main(arguments):
854793

855794

856795
if __name__ == "__main__":
857-
parser = None
858-
args = None
859-
# use optparse if python version is 2.5 or 2.6
860-
if sys.version_info[0] == 2 and sys.version_info[1] < 7:
861-
import optparse
862-
863-
if len(sys.argv) == 1:
864-
print("No argument given. Please run smatch.py -h to see the argument description.", file=ERROR_LOG)
865-
exit(1)
866-
parser = build_arg_parser2()
867-
(args, opts) = parser.parse_args()
868-
file_handle = []
869-
if args.f is None:
870-
print("smatch.py requires -f option to indicate two files \
871-
containing AMR as input. Please run smatch.py -h to \
872-
see the argument description.", file=ERROR_LOG)
873-
exit(1)
874-
# assert there are 2 file names following -f.
875-
assert (len(args.f) == 2)
876-
for file_path in args.f:
877-
if not os.path.exists(file_path):
878-
print("Given file", args.f[0], "does not exist", file=ERROR_LOG)
879-
exit(1)
880-
file_handle.append(open(file_path, encoding='utf8'))
881-
# use opened files
882-
args.f = tuple(file_handle)
883-
# use argparse if python version is 2.7 or later
884-
else:
885-
import argparse
886-
887-
parser = build_arg_parser()
888-
args = parser.parse_args()
796+
import argparse
797+
798+
parser = argparse.ArgumentParser(description="Smatch calculator")
799+
parser.add_argument(
800+
'-f',
801+
nargs=2,
802+
required=True,
803+
type=argparse.FileType('r'),
804+
help=('Two files containing AMR pairs. '
805+
'AMRs in each file are separated by a single blank line'))
806+
parser.add_argument(
807+
'-r',
808+
type=int,
809+
default=4,
810+
help='Restart number (Default:4)')
811+
parser.add_argument(
812+
'--significant',
813+
type=int,
814+
default=2,
815+
help='significant digits to output (default: 2)')
816+
parser.add_argument(
817+
'-v',
818+
action='store_true',
819+
help='Verbose output (Default:false)')
820+
parser.add_argument(
821+
'--vv',
822+
action='store_true',
823+
help='Very Verbose output (Default:false)')
824+
parser.add_argument(
825+
'--ms',
826+
action='store_true',
827+
default=False,
828+
help=('Output multiple scores (one AMR pair a score) '
829+
'instead of a single document-level smatch score '
830+
'(Default: false)'))
831+
parser.add_argument(
832+
'--pr',
833+
action='store_true',
834+
default=False,
835+
help=('Output precision and recall as well as the f-score. '
836+
'Default: false'))
837+
parser.add_argument(
838+
'--justinstance',
839+
action='store_true',
840+
default=False,
841+
help="just pay attention to matching instances")
842+
parser.add_argument(
843+
'--justattribute',
844+
action='store_true',
845+
default=False,
846+
help="just pay attention to matching attributes")
847+
parser.add_argument(
848+
'--justrelation',
849+
action='store_true',
850+
default=False,
851+
help="just pay attention to matching relations")
852+
853+
args = parser.parse_args()
889854
main(args)

tox.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[tox]
2-
envlist = py{35,36,37}
2+
envlist = py{35,36,37,38}
33

44
[testenv]
55
commands = python smatch.py -f test_input1.txt test_input2.txt

0 commit comments

Comments
 (0)