Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python 3.6 compatibility. #10

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
build/
*.pyc
.tox
53 changes: 53 additions & 0 deletions CHANGES_FOR_PY36.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
odespy/__init__.py
enable absolute imports , print function
use explicit relative imports
try statement around line that deletes variables from locals

odespy/solvers.py
enable absolute imports , print function
replace all print statements with print function
Replaced many "ValueError, msg" to "ValueError(msg)"
Mixed spaces and tabs: lines 933-937, 962-967, 1508
TypeError, msg to TypeError(msg): 1162
ImportError, msg to ImportError(msg): 2014, 2944
except Exception as e: 2993
"not_valid is not defined error" at 2909
apparently in Python 3 list comprehensions like this in class
body can't access variables defined in class body. This works in py2.7.
Replaced list comprehension with for loop.
in function _format_parameters_table: line 480, convert parameter_names
into list. dict.keys() in PY3 returns dict_list object not list.

odespy/RungeKutta.py
enable absolute imports , print function
replace all print statements with print function
Replaced many "ValueError, msg" to "ValueError(msg)"

odespy/rkc.py
enable absolute imports , print function
Replaced a few "ValueError, msg" to "ValueError(msg)"

odespy/rkc.py
enable absolute imports , print function
replace all print statements with print function

odespy/odepack.py
enable absolute imports , print function
Replaced a few "ValueError, msg" to "ValueError(msg)"
replace all print statements with print function
mixed tabs and space: 1203, 1232, 1235, 1343, 1456, 1574
explicit relative import of _odepack.
remove the global apply() function

odespy/radau5.py
enable absolute imports , print function
print function
mixed tab/space: 283

odespy/problems.py
enable absolute imports , print function
print function

odespy/tests/test_basic.py
enable absolute imports , print function
many places (example line 89, 90): the lambda syntax is invalid in PY3.6
41 changes: 21 additions & 20 deletions odespy/RungeKutta.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from solvers import Solver, Adaptive
from __future__ import absolute_import, print_function
from . solvers import Solver, Adaptive
import numpy as np

def _calculate_order_1_level(coefficients):
Expand Down Expand Up @@ -134,7 +135,7 @@ def middle(x,y,z): # Auxilary function
k = np.zeros((k_len, self.neq), self.dtype) # intern stages

if self.verbose > 0:
print 'advance solution in [%s, %s], h=%g' % (t_n, t_next, h)
print('advance solution in [%s, %s], h=%g' % (t_n, t_next, h))

# Loop until next time point is reached
while (abs(t - t_n) < abs(t_next - t_n)):
Expand All @@ -150,7 +151,7 @@ def middle(x,y,z): # Auxilary function

self.info['rejected'] += 1 # reduced below if accepted
if self.verbose > 0:
print ' u(t=%g)=%g: ' % (t+h, u_new),
print(' u(t=%g)=%g: ' % (t+h, u_new)),

# local error between 2 levels
error = h*np.abs(np.dot(factors_error, k))
Expand All @@ -171,18 +172,18 @@ def middle(x,y,z): # Auxilary function
self.info['rejected'] -= 1

if self.verbose > 0:
print 'accepted, ',
print('accepted, '),
else:
if self.verbose > 0:
print 'rejected, ',
print('rejected, '),

if self.verbose > 0:
print 'err=%s, ' % str(error),
print('err=%s, ' % str(error)),
if hasattr(self, 'u_exact') and callable(self.u_exact):
print 'exact-err=%s, ' % \
(np.asarray(self.u_exact(t+h))-u_new),
print('exact-err=%s, ' %
(np.asarray(self.u_exact(t+h))-u_new)),
if h <= self.min_step:
print 'h=min_step!! ',
print('h=min_step!! '),


# Replace 0 values by 1e-16 since we will divide by error
Expand All @@ -209,7 +210,7 @@ def middle(x,y,z): # Auxilary function
h = min(h, t_next - t_intermediate[-1])

if self.verbose > 0:
print 'new h=%g' % h
print('new h=%g' % h)

if h == 0:
break
Expand Down Expand Up @@ -367,16 +368,16 @@ def validate_data(self):
# Check for dimension of user-defined butcher table.
array_shape = self.butcher_tableau.shape
if len(array_shape) is not 2:
raise ValueError,'''
Illegal input! Your input butcher_tableau should be a 2d-array!'''
raise ValueError('''
Illegal input! Your input butcher_tableau should be a 2d-array!''')
else:
m,n = array_shape
if m not in (n, n + 1):
raise ValueError, '''\
raise ValueError('''\
The dimension of 2d-array <method_yours_array> should be:
1. Either (n, n), --> For 1-level RungeKutta methods
2. Or (n+1, n), --> For 2-levels RungeKutta methods
The shape of your input array is (%d, %d).''' % (m,n)
The shape of your input array is (%d, %d).''' % (m,n))
self._butcher_tableau = self.butcher_tableau

# Check for user-defined order,
Expand All @@ -393,19 +394,19 @@ def validate_data(self):
if array_shape[0] == array_shape[1] + 1:
# 2-level RungeKutta methods
if type(self.method_order) is int:
raise ValueError, error_2level
raise ValueError(error_2level)
try:
order1, order2 = self.method_order
if abs(order1-order2) != 1 or \
order1 < 1 or order2 < 1:
raise ValueError, error_2level
raise ValueError(error_2level)
except:
raise ValueError,error_2level
raise ValueError(error_2level)
else:
# 1-level RungeKutta methods
if type(self.method_order) is not int or \
self.method_order < 1:
raise ValueError,error_1level
raise ValueError(error_1level)
self._method_order = self.method_order

else: # method_order is not specified
Expand All @@ -418,14 +419,14 @@ def validate_data(self):
for i in range(1,array_shape[1] - 1):
if not np.allclose(self.butcher_tableau[i][0],\
sum(self.butcher_tableau[i][1:])):
raise ValueError, '''
raise ValueError('''
Inconsistent data in Butcher_Tableau!
In each lines of stage-coefficients, first number should be
equal to the sum of other numbers.
That is, for a butcher_table with K columns,
a[i][0] == a[i][1] + a[i][2] + ... + a[i][K - 1]
where 1 <= i <= K - 1
Your input for line %d is :%s
''' % (i,str(self.butcher_tableau[i]))
''' % (i,str(self.butcher_tableau[i])))

return True
25 changes: 15 additions & 10 deletions odespy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
supported. A wide range of numerical methods for ODEs are offered:

"""
from __future__ import absolute_import, print_function

# Insert tutorial from ../doc/src/odespy/odespy.rst

Expand Down Expand Up @@ -1254,14 +1255,13 @@ def f(u, t):
The file `logistic10.py <https://github.com/hplgit/odespy/blob/master/doc/src/odespy/src-odespy/logistic10.py>`_ contains a complete program for solving the logistic ODE
with :math:`f(u,t)` implemented in Fortran.
'''

from solvers import *
from RungeKutta import *
from rkc import *
from rkf45 import *
from odepack import *
from radau5 import *
import problems
from . solvers import *
from . RungeKutta import *
from . rkc import *
from . rkf45 import *
from . odepack import *
from . radau5 import *
from . import problems

# Update doc strings with common info
class_, doc_str, classname = None, None, None
Expand All @@ -1282,8 +1282,13 @@ def f(u, t):
__doc__ = __doc__ + typeset_toc(toc) + _tutorial

# Do not pollute namespace
del class_, doc_str, classname, classnames, toc, typeset_toc, \
table_of_parameters, name, obj, inspect
try:
del class_, doc_str, classname, classnames, toc, typeset_toc, \
table_of_parameters, name, obj, inspect
except NameError:
# the var 'name' is used in a list compression, which is leaked into
# locals in PY2 but not in Py3 (at-least py3.6).
pass

if __name__ == '__main__':
from os.path import join
Expand Down
Loading