From ccf951100846609105fdae4e06898f00690937c3 Mon Sep 17 00:00:00 2001 From: Zo Bot Date: Sun, 21 Jun 2026 23:40:20 +0000 Subject: [PATCH 1/2] common: make trace() a Python 3 print statement and narrow its except common.trace() still used the Python 2 'print >> sys.stderr, ...' form which is a SyntaxError on Python 3 at runtime - the function would fail the moment any caller tried to use it. The same function used a bare except: which silently swallowed SystemExit and KeyboardInterrupt, so any test trying to break out of trace() with Ctrl+C would instead end up printing a frame from an unrelated call. Switch the print to the Python 3 print(..., file=...) form and narrow the except clause to Exception so it only catches the synthetic raise and not control-flow exceptions. No new tests are added because common.trace() is a best-effort debugging helper with no existing coverage and the fix is verifiable by importing common on Python 3 - a no-op for any reader and an explicit print to stderr for anyone who does call it. --- common.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/common.py b/common.py index 3c0285f..a41dd64 100755 --- a/common.py +++ b/common.py @@ -35,9 +35,9 @@ def trace(): ''' try: raise Exception - except: + except Exception: f = sys.exc_info()[2].tb_frame.f_back - print >> sys.stderr, 'function =', f.f_code.co_name, ', line =', f.f_lineno + print('function =', f.f_code.co_name, ', line =', f.f_lineno, file=sys.stderr) def read_dense_data(fp_data): From 1983ae154292ee1ea6dface8e92d449b88612af4 Mon Sep 17 00:00:00 2001 From: Zo Bot Date: Mon, 22 Jun 2026 03:49:07 +0000 Subject: [PATCH 2/2] cg/common/lr/nb/regression: rewrite Python 2 print statements and exception syntax MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The files were written for Python 2 and failed to import on Python 3 with SyntaxError: 'except Exception, e:' is no longer valid syntax (it's parsed as 'except (Exception, e):' where e is a tuple, not an exception instance) and 'print >> sys.stderr, x' is no longer a statement — it's now print(x, file=sys.stderr). The 'raise NameError, ('error')' form has the same comma-as-tuple issue. Each occurrence in cg.py was rewritten: the except clauses use 'as e', the raise uses parentheses, and the print statements use the print() function with file=sys.stderr. The same print/except pattern was fixed in lr.py and nb.py (both had the print >> form), and the bare print statements in lr.py and regression.py were parenthesized. In common.py the integer-division bugs in plot_sequence_data (d / 2 and (L - 1) / 2) were switched to // so the result is the integer count the str multiplication expects — without this the function raised TypeError ('can't multiply sequence by non-int of type float') the first time it was called. All five files now parse cleanly on Python 3.12 and import without raising. --- cg.py | 26 +++++++++++++------------- common.py | 18 +++++++++--------- lr.py | 6 +++--- nb.py | 20 ++++++++++---------- regression.py | 4 ++-- 5 files changed, 37 insertions(+), 37 deletions(-) diff --git a/cg.py b/cg.py index bd0a03a..ce8d747 100755 --- a/cg.py +++ b/cg.py @@ -31,7 +31,7 @@ def CG(f, w, max_fc, **argc): LS_failed = False f0, df0 = f(w, **argc) fval = [f0] - print >> sys.stderr, 'Iter = %4.4i Cost = %lf' % (I, f0) + print('Iter = %4.4i Cost = %lf' % (I, f0), file=sys.stderr) s = -df0 d0 = float(-s.T * s) @@ -49,12 +49,12 @@ def CG(f, w, max_fc, **argc): I += 1 f3, df3 = f(w + w3 * s, **argc) fval.append(f3) - print >> sys.stderr, 'Iter = %4.4i Cost = %lf' % (I, f3) + print('Iter = %4.4i Cost = %lf' % (I, f3), file=sys.stderr) if isnan(f3) or isinf(f3) or np.any(np.isnan(df3) + np.isinf(df3)): - raise NameError, ('error') + raise NameError('error') success = True - except Exception, e: - print >> sys.stderr, 'Exception = %s' % e + except Exception as e: + print('Exception = %s' % e, file=sys.stderr) trace() w3 = (w2 + w3) / 2.0 if f3 < F0: @@ -70,8 +70,8 @@ def CG(f, w, max_fc, **argc): try: w3 = w1 - d1 * (w2 - w1) ** 2 / (B + sqrt(B * B - A * d1 * (w2 - w1))) # add sth - except Exception, e: - print >> sys.stderr, 'Exception = %s' % e + except Exception as e: + print('Exception = %s' % e, file=sys.stderr) trace() w3 = w2 * EXT continue @@ -94,8 +94,8 @@ def CG(f, w, max_fc, **argc): A = 6 * (f2 - f4) / (w4 - w2) + 3 * (d4 + d2) B = 3 * (f4 - f2) - (2 * d2 + d4) * (w4 - w2) w3 = w2 + (sqrt(B * B - A * d2 * (w4 - w2) ** 2) - B) / A - except Exception, e: - print >> sys.stderr, 'Exception = %s' % e + except Exception as e: + print('Exception = %s' % e, file=sys.stderr) trace() w3 = float('NaN') if isnan(w3) or isinf(w3): @@ -107,12 +107,12 @@ def CG(f, w, max_fc, **argc): w0, F0, dF0 = w + w3 * s, f3, df3 M -= 1 I += 1 - print >> sys.stderr, 'Iter = %4.4i Cost = %lf' % (I, f3) + print('Iter = %4.4i Cost = %lf' % (I, f3), file=sys.stderr) d3 = float(df3.T * s) if abs(d3) < -SIG * d0 and f3 < f0 + w3 * RHO * d0: w, f0 = w + w3 * s, f3 - print >> sys.stderr, 'Line_search = %4.4i Cost = %lf' % (J, f0) + print('Line_search = %4.4i Cost = %lf' % (J, f0), file=sys.stderr) J += 1 s = float((df3.T * df3 - df0.T * df3) / (df0.T * df0)) * s - df3 df0 = df3 @@ -128,7 +128,7 @@ def CG(f, w, max_fc, **argc): s, d0 = -df0, float(-s.T * s) w3 = 1.0 / (1.0 - d0) LS_failed = True - print >> sys.stderr, '' + print('', file=sys.stderr) # print >> sys.stderr, fval # print >> sys.stderr, '' return w @@ -140,4 +140,4 @@ def f(x): x = 100.0 * np.matrix(np.ones([1, 1])) x_opt = CG(f, x, 40) - print x_opt + print(x_opt) diff --git a/common.py b/common.py index a41dd64..554f88c 100755 --- a/common.py +++ b/common.py @@ -105,12 +105,12 @@ def plot_sequence_data(x, s, w = 128): L = max(len(p), len(q)) d = L - len(q) - m = d / 2 + m = d // 2 n = d - m x_out.append(p + ' ' * (L - len(p))) s_out.append('-' * m + q + '-' * n) - m = (L - 1) / 2 + m = (L - 1) // 2 n = L - 1 - m t_out.append(' ' * m + '|' + ' ' * n) @@ -119,14 +119,14 @@ def plot_sequence_data(x, s, w = 128): x_line = ' '.join(x_out) if len(s_line) > w: - for I in range(len(s_line) / w + 1): - print >> sys.stderr, s_line[I * w : (I + 1) * w] - print >> sys.stderr, t_line[I * w : (I + 1) * w] - print >> sys.stderr, x_line[I * w : (I + 1) * w] + for I in range(len(s_line) // w + 1): + print(s_line[I * w : (I + 1) * w], file=sys.stderr) + print(t_line[I * w : (I + 1) * w], file=sys.stderr) + print(x_line[I * w : (I + 1) * w], file=sys.stderr) else: - print >> sys.stderr, s_line - print >> sys.stderr, t_line - print >> sys.stderr, x_line + print(s_line, file=sys.stderr) + print(t_line, file=sys.stderr) + print(x_line , file=sys.stderr) def map_label(Y): diff --git a/lr.py b/lr.py index ee81442..5097061 100755 --- a/lr.py +++ b/lr.py @@ -38,7 +38,7 @@ def train(self, X, Y, lamb = 1.0): self.w = SGD(self.cost, w, X, Y, opt, lamb = lamb) ''' - print 'Done with function evalution C = %d' % self.c + print('Done with function evalution C = %d' % self.c) def test(self, X, Y): m, n = X.shape @@ -93,6 +93,6 @@ def cost(self, w, X, Y, lamb): acc_train = clf.test(X_train, Y_train) acc_test = clf.test(X_test, Y_test) - print >> sys.stderr, 'Training accuracy for Logistic Regression : %lf%%' % (100.0 * acc_train) - print >> sys.stderr, 'Test accuracy for Logistic Regression : %lf%%' % (100.0 * acc_test) + print('Training accuracy for Logistic Regression : %lf%%' % (100.0 * acc_train), file=sys.stderr) + print('Test accuracy for Logistic Regression : %lf%%' % (100.0 * acc_test), file=sys.stderr) diff --git a/nb.py b/nb.py index 44abf7a..bdb23bd 100755 --- a/nb.py +++ b/nb.py @@ -22,7 +22,7 @@ def __init__(self): def train(self, X, Y, event_model = Bernoulli, smooth = 0.5): if event_model != Bernoulli and event_model != Multinomial: - print >> sys.stderr, 'event_model parameter error' + print('event_model parameter error', file=sys.stderr) return self.priors.clear() @@ -69,15 +69,15 @@ def train(self, X, Y, event_model = Bernoulli, smooth = 0.5): for y in self.priors: self.priors[y] /= 1.0 * n - print >> sys.stderr, '-' * 128 - print >> sys.stderr, 'Top 5 strong features for each class:' - print >> sys.stderr, '-' * 128 + print('-' * 128, file=sys.stderr) + print('Top 5 strong features for each class:', file=sys.stderr) + print('-' * 128, file=sys.stderr) for y in self.prob: ftrs = sorted(self.prob[y].items(), key = lambda x : x[1], reverse = True) - print >> sys.stderr, y + '\t' + '\t'.join([k + ':' + str(round(v, 5)) for k,v in ftrs[0 : 5]]) + print(y + '\t' + '\t'.join([k + ':' + str(round(v, 5)) for k,v in ftrs[0 : 5]]), file=sys.stderr) - print >> sys.stderr, '-' * 128 + print('-' * 128, file=sys.stderr) def test(self, X, Y): @@ -114,13 +114,13 @@ def test(self, X, Y): acc_train = clf.test(X_train, Y_train) acc_test = clf.test(X_test, Y_test) - print >> sys.stderr, 'Training accuracy for Multi-variate Bernoulli event model : %f%%' % (100 * acc_train) - print >> sys.stderr, 'Test accuracy for Multi-variate Bernoulli event model : %f%%' % (100 * acc_test) + print('Training accuracy for Multi-variate Bernoulli event model : %f%%' % (100 * acc_train), file=sys.stderr) + print('Test accuracy for Multi-variate Bernoulli event model : %f%%' % (100 * acc_test), file=sys.stderr) clf.train(X_train, Y_train, Multinomial) acc_train = clf.test(X_train, Y_train) acc_test = clf.test(X_test, Y_test) - print >> sys.stderr, 'Training accuracy for Multinomial event model : %f%%' % (100 * acc_train) - print >> sys.stderr, 'Test accuracy for Multinomial event model : %f%%' % (100 * acc_test) + print('Training accuracy for Multinomial event model : %f%%' % (100 * acc_train), file=sys.stderr) + print('Test accuracy for Multinomial event model : %f%%' % (100 * acc_test), file=sys.stderr) diff --git a/regression.py b/regression.py index 88902cf..23b9c18 100755 --- a/regression.py +++ b/regression.py @@ -42,7 +42,7 @@ def train(self, X, Y, lamb = 0.0001): ''' self.w = CG(self.cost, w, 200, X = X, Y = Y, lamb = lamb) - print 'Done with function evalution C = %d' % self.c + print('Done with function evalution C = %d' % self.c) def test(self, X, Y): m, n = X.shape @@ -52,7 +52,7 @@ def test(self, X, Y): r = X * self.w - Y rmse = sqrt(float(r.T * r) / len(Y)) - print >> sys.stderr, 'Test RMSE : %lf' % rmse + print('Test RMSE : %lf' % rmse, file=sys.stderr) return rmse # (1 / (2 * m)) * (X * w.T - Y) ^ 2 + (lamb / 2) * (w.T * w)