Skip to content

Commit

Permalink
Remove more Python 2 compatibility, relates #1092
Browse files Browse the repository at this point in the history
  • Loading branch information
benmwebb committed Jun 4, 2024
1 parent 78bceea commit 6f8721a
Show file tree
Hide file tree
Showing 10 changed files with 13 additions and 66 deletions.
17 changes: 2 additions & 15 deletions modules/EMageFit/bin/emagefit
Original file line number Diff line number Diff line change
@@ -1,24 +1,11 @@
#!/usr/bin/env python

try:
from configparser import ConfigParser # python3
except ImportError:
from ConfigParser import SafeConfigParser as ConfigParser # python2
from configparser import ConfigParser
import sys
import os
import time
import logging
try:
# Python 3
from importlib.machinery import SourceFileLoader
except ImportError:
# Python 2
class SourceFileLoader(object):
def __init__(self, name, path):
self.name, self.path = name, path
def load_module(self):
import imp
return imp.load_source(self.name, self.path)
from importlib.machinery import SourceFileLoader

log = logging.getLogger("domino_model")

Expand Down
5 changes: 0 additions & 5 deletions modules/EMageFit/pyext/src/buildxlinks.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,8 @@
import IMP.algebra as alg
import IMP.core as core
import logging

import networkx as nx

try:
set = set
except NameError:
from sets import Set as set

log = logging.getLogger("buildxlinks")

Expand Down
13 changes: 3 additions & 10 deletions modules/EMageFit/pyext/src/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,10 @@ def get_experiment_params(fn_params):
@param fn_params configuration file
@return Experiment Class with all the information from the config file
"""
try:
import importlib.machinery
imp = None
except ImportError:
import imp
import importlib.machinery
name, ext = os.path.splitext(fn_params)
if imp is None:
foo = importlib.machinery.SourceFileLoader(name,
fn_params).load_module()
else:
foo = imp.load_source(name, fn_params)
foo = importlib.machinery.SourceFileLoader(name,
fn_params).load_module()
exp = foo.Experiment()
# convert to absolute paths
exp.fn_pdbs = [IMP.get_relative_path(fn_params, fn) for fn in exp.fn_pdbs]
Expand Down
5 changes: 1 addition & 4 deletions modules/isd/data/gen_dict.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#!/usr/bin/env python

try:
import cPickle as pickle
except ImportError:
import pickle
import pickle

di = {'ALA': {'HB': ('HB1', 'HB2', 'HB3'),
'HB%': ('HB1', 'HB2', 'HB3'),
Expand Down
5 changes: 1 addition & 4 deletions modules/isd/pyext/src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,10 +357,7 @@ def Dump(this, filename, gzip=0, mode='w', bin=1):
"""

import os
try:
import cPickle as pickle
except ImportError:
import pickle
import pickle

filename = os.path.expanduser(filename)

Expand Down
3 changes: 0 additions & 3 deletions modules/kernel/pyext/include/IMP_kernel.decorators.i
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,4 @@
bool __bool__() {
return self->get_is_valid();
}
%pythoncode %{
__nonzero__ = __bool__
%}
}
3 changes: 0 additions & 3 deletions modules/kernel/pyext/src/_list_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ def __next__(self):
raise StopIteration
return self.__getfunc(self.__n)

# Python 2
next = __next__


class VarList(object):
"""A list-like object that wraps IMP C++ object accessor methods"""
Expand Down
5 changes: 1 addition & 4 deletions modules/parallel/pyext/src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
import re
import random
import struct
try:
import cPickle as pickle
except ImportError:
import pickle
import pickle
import IMP
from IMP.parallel import workerstate
from IMP.parallel.subproc import _run_background, _Popen4
Expand Down
10 changes: 2 additions & 8 deletions modules/parallel/test/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,8 @@ def test_floats(self):
m.add_worker(IMP.parallel.LocalWorker())
c = m.get_context()

try:
inf = float('inf')
nan = float('nan')
except ValueError:
# Python 2.5 on Windows reports 'invalid literal', so use another
# method to get inf and nan:
inf = 1e300 * 1e300
nan = inf - inf
inf = float('inf')
nan = float('nan')
for f in (inf, nan):
c.add_task(_tasks.SimpleTask(f))
results = list(c.get_results_unordered())
Expand Down
13 changes: 3 additions & 10 deletions modules/test/pyext/src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -990,18 +990,11 @@ def import_python_application(self, app):
"""Import an installed Python application, rather than running it.
This is useful to directly test components of the application.
@return the Python module object."""
try:
import importlib.machinery
imp = None
except ImportError:
import imp
import importlib.machinery
name = os.path.splitext(app)[0]
pathname = os.path.join(os.environ['IMP_BIN_DIR'], app)
if imp is None:
return importlib.machinery.SourceFileLoader(name,
pathname).load_module()
else:
return imp.load_source(name, pathname)
return importlib.machinery.SourceFileLoader(name,
pathname).load_module()

def run_script(self, app, args):
"""Run an application with the given list of arguments.
Expand Down

0 comments on commit 6f8721a

Please sign in to comment.