Commit 77c95cc2 authored by Tiago Peixoto's avatar Tiago Peixoto

Remove deprecated Python 2 compatibility

parent f758e17e
Pipeline #605 passed with stage
in 430 minutes and 17 seconds
This diff is collapsed.
......@@ -22,7 +22,6 @@
Utility module which includes all the sub-modules in graph_tool
"""
from __future__ import division, absolute_import, print_function
import warnings
from graph_tool import *
......
......@@ -44,14 +44,11 @@ Contents
++++++++
"""
from __future__ import division, absolute_import, print_function
from .. dl_import import dl_import
dl_import("from . import libgraph_tool_centrality")
from .. import _prop, ungroup_vector_property, Vector_size_t
from .. topology import shortest_distance
import sys
import numpy
import numpy.linalg
......@@ -514,8 +511,6 @@ def central_point_dominance(g, betweenness):
Examples
--------
.. doctest:: central_point_dominance
>>> g = gt.collection.data["polblogs"]
>>> g = gt.GraphView(g, vfilt=gt.label_largest_component(g))
>>> vp, ep = gt.betweenness(g)
......
......@@ -41,8 +41,6 @@ Contents
++++++++
"""
from __future__ import division, absolute_import, print_function
from .. dl_import import dl_import
dl_import("from . import libgraph_tool_clustering as _gt")
......@@ -54,7 +52,6 @@ from .. stats import vertex_hist
from collections import defaultdict
from numpy import *
from numpy import random
import sys
__all__ = ["local_clustering", "global_clustering", "extended_clustering",
"motifs", "motif_significance"]
......
......@@ -264,11 +264,6 @@ Contents
"""
from __future__ import division, absolute_import, print_function
import sys
if sys.version_info < (3,):
range = xrange
import os.path
import textwrap
from .. import load_graph
......
......@@ -18,28 +18,10 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from __future__ import division, absolute_import, print_function
import sys
if sys.version_info < (3,):
range = xrange
import os.path
import tempfile
if sys.version_info < (3,):
from urllib2 import urlopen, URLError
import shutil
class TemporaryDirectory(object):
def __init__(self, suffix="", prefix="", dir=None):
self.name = tempfile.mkdtemp(suffix, prefix, dir)
def __enter__(self):
return self.name
def __exit__(self, exc, value, tb):
shutil.rmtree(self.name)
else:
from urllib.request import urlopen, URLError
from tempfile import TemporaryDirectory
from urllib.request import urlopen, URLError
from tempfile import TemporaryDirectory
import tarfile
import warnings
import numpy
......
......@@ -39,8 +39,6 @@ Contents
++++++++
"""
from __future__ import division, absolute_import, print_function
from .. dl_import import dl_import
dl_import("from . import libgraph_tool_correlations")
......
......@@ -21,11 +21,8 @@
Some useful decorators
"""
from __future__ import division, absolute_import, print_function
import inspect
import functools
import sys
import types
################################################################################
......@@ -33,48 +30,7 @@ import types
# Some useful function decorators which will be used
################################################################################
# exec statement in python 2.7 and exec() function in 3.2 are mutually exclusive
if sys.hexversion > 0x03000000:
def exec_function(source, filename, global_map):
exec(compile(source, filename, "exec"), global_map)
else:
eval(compile("""\
def exec_function(source, filename, global_map):
exec compile(source, filename, "exec") in global_map
""","<exec_function>", "exec"))
if sys.hexversion > 0x03000000:
_wraps = functools.wraps
else:
def _wraps(func):
"""This decorator works like the functools.wraps meta-decorator, but
also preserves the function's argument signature. This uses eval, and is
thus a bit of a hack, but there no better way I know of to do this."""
def decorate(f):
argspec = inspect.getfullargspec(func)
___wrap_defaults = defaults = argspec[-1]
if defaults is not None:
def_string = ["___wrap_defaults[%d]" % d for
d in range(len(defaults))]
def_names = argspec[0][-len(defaults):]
else:
def_string = None
def_names = None
args_call = inspect.formatargspec(argspec[0], defaults=def_names)
argspec = inspect.formatargspec(argspec[0], defaults=def_string)
argspec = argspec.lstrip("(").rstrip(")")
wf = "def %s(%s):\n return f%s\n" % \
(func.__name__, argspec, args_call)
if def_string is not None:
for d in def_string:
wf = wf.replace("'%s'" % d, "%s" % d)
for d in def_names:
wf = wf.replace("'%s'" % d, "%s" % d)
exec_function(wf, __file__, locals())
return functools.wraps(func)(locals()[func.__name__])
return decorate
_wraps = functools.wraps
def _attrs(**kwds):
"""Decorator which adds arbitrary attributes to methods"""
......
......@@ -18,8 +18,6 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from __future__ import division, absolute_import, print_function
import sys
import os.path
......
......@@ -66,8 +66,6 @@ Contents
++++++++
"""
from __future__ import division, absolute_import, print_function
from .. import Graph, GraphView, _check_prop_vector, _check_prop_scalar, \
group_vector_property, ungroup_vector_property, infect_vertex_property, \
_prop, _get_rng
......@@ -78,7 +76,6 @@ from .. stats import label_parallel_edges
from .. generation import predecessor_tree, condensation_graph
import numpy.random
from numpy import sqrt
import sys
from .. dl_import import dl_import
dl_import("from . import libgraph_tool_layout")
......
......@@ -18,13 +18,6 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from __future__ import division, absolute_import, print_function
import sys
if sys.version_info < (3,):
range = xrange
else:
unicode = str
import os
import warnings
import numpy
......@@ -237,7 +230,7 @@ def shape_from_prop(shape, enum):
prop.fa %= rg[1] - rg[0] + 1
prop.fa += rg[0]
return prop
if isinstance(shape, (str, unicode)):
if isinstance(shape, str):
return int(getattr(enum, shape))
else:
return shape
......@@ -301,7 +294,7 @@ def surface_from_prop(surface):
surface.value_type())
return prop
if isinstance(surface, (str, unicode)):
if isinstance(surface, str):
return gen_surface(surface)
elif isinstance(surface, cairo.Surface) or surface is None:
return surface
......@@ -409,7 +402,7 @@ def _convert(attr, val, cmap, pmap_default=False, g=None, k=None):
new_val = val
elif isinstance(val, (tuple, np.ndarray)):
new_val = list(val)
elif isinstance(val, (str, unicode)):
elif isinstance(val, str):
new_val = list(color_converter.to_rgba(val))
elif isinstance(val, PropertyMap):
if val.value_type() in ["vector<double>", "vector<long double>"]:
......@@ -708,7 +701,7 @@ def color_contrast(color):
def auto_colors(g, bg, pos, back):
if not isinstance(bg, PropertyMap):
if isinstance(bg, (str, unicode)):
if isinstance(bg, str):
bg = color_converter.to_rgba(bg)
bg = g.new_vertex_property("vector<double>", val=bg)
if not isinstance(pos, PropertyMap):
......@@ -1146,7 +1139,7 @@ def graph_draw(g, pos=None, vprops=None, eprops=None, vorder=None, eorder=None,
x, y, zoom = x, y, 1
if isinstance(output, (str, unicode)):
if isinstance(output, str):
out, auto_fmt = open_file(output, mode="wb")
else:
out = output
......
......@@ -18,13 +18,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from __future__ import division, absolute_import, print_function
import sys
if sys.version_info < (3,):
range = xrange
else:
unicode = str
import os
import os.path
import time
......@@ -32,7 +26,7 @@ import warnings
import ctypes
import ctypes.util
import tempfile
from .. import PropertyMap, group_vector_property, ungroup_vector_property, _c_str
from .. import PropertyMap, group_vector_property, ungroup_vector_property
import numpy
import numpy.random
import copy
......@@ -441,12 +435,12 @@ def graphviz_draw(g, pos=None, size=(15, 15), pin=False, layout=None,
aset(n, "style", "filled")
aset(n, "color", "#2e3436")
# apply color
if isinstance(vcolor, (str, unicode)):
aset(n, "fillcolor", _c_str(vcolor))
if isinstance(vcolor, str):
aset(n, "fillcolor", vcolor)
else:
color = vcolor[v]
if isinstance(color, (str, unicode)):
aset(n, "fillcolor", _c_str(color))
if isinstance(color, str):
aset(n, "fillcolor", color)
else:
color = tuple([int(c * 255.0) for c in vcmap(vnorm(color))])
aset(n, "fillcolor", "#%.2x%.2x%.2x%.2x" % color)
......@@ -492,12 +486,12 @@ def graphviz_draw(g, pos=None, size=(15, 15), pin=False, layout=None,
aset(ge, "dir", "none")
# apply color
if isinstance(ecolor, (str, unicode)):
aset(ge, "color", _c_str(ecolor))
if isinstance(ecolor, str):
aset(ge, "color", ecolor)
else:
color = ecolor[e]
if isinstance(color, (str, unicode)):
aset(ge, "color", _c_str(color))
if isinstance(color, str):
aset(ge, "color", color)
else:
color = tuple([int(c * 255.0) for c in ecmap(enorm(color))])
aset(ge, "color", "#%.2x%.2x%.2x%.2x" % color)
......
......@@ -18,8 +18,6 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from __future__ import division, absolute_import, print_function
import numpy
from .. import GraphView, PropertyMap, VertexPropertyMap, \
......
......@@ -67,8 +67,6 @@ Contents
"""
from __future__ import division, absolute_import, print_function
from .. import _degree, _prop, Graph, GraphView, _get_rng, PropertyMap, \
EdgePropertyMap, VertexPropertyMap, _check_prop_scalar
from .. stats import label_self_loops
......
......@@ -77,8 +77,6 @@ The following network will be used as an example throughout the documentation.
"""
from __future__ import division, absolute_import, print_function
from .. dl_import import dl_import
dl_import("from . import libgraph_tool_flow")
......
......@@ -48,16 +48,11 @@ Contents
++++++++
"""
from __future__ import division, absolute_import, print_function
import sys
if sys.version_info < (3,):
range = xrange
from .. dl_import import dl_import
dl_import("from . import libgraph_tool_generation")
from .. import Graph, GraphView, _check_prop_scalar, _prop, _limit_args, \
_gt_type, _get_rng, _c_str, libcore, Vector_double
_gt_type, _get_rng, libcore, Vector_double
from .. stats import label_parallel_edges, label_self_loops
import inspect
import types
......@@ -805,7 +800,7 @@ def random_rewire(g, model="configuration", n_iter=1, edge_sweep=True,
if not fast:
g.set_fast_edge_removal(True)
pcount = libgraph_tool_generation.random_rewire(g._Graph__graph,
_c_str(model),
model,
n_iter, not edge_sweep,
self_loops, parallel_edges,
configuration, traditional,
......@@ -1763,7 +1758,7 @@ def triangulation(points, type="simple", periodic=False):
g = Graph(directed=False)
pos = g.new_vertex_property("vector<double>")
libgraph_tool_generation.triangulation(g._Graph__graph, points,
_prop("v", g, pos), _c_str(type),
_prop("v", g, pos), type,
periodic)
return g, pos
......
......@@ -18,12 +18,9 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from __future__ import division, absolute_import, print_function
import pickle
import base64
import atexit
import sys
from io import BytesIO
from . import libgraph_tool_core
......@@ -72,8 +69,6 @@ def pickler(stream, obj):
def unpickler(stream):
data = stream.read()
sstream = BytesIO(data)
if sys.version_info < (3,):
return pickle.load(sstream)
return pickle.load(sstream, encoding="bytes")
libgraph_tool_core.set_pickler(pickler)
......
......@@ -153,11 +153,6 @@ Contents
"""
from __future__ import division, absolute_import, print_function
import sys
if sys.version_info < (3,):
range = xrange
__all__ = ["minimize_blockmodel_dl",
"minimize_nested_blockmodel_dl",
"BlockState",
......
......@@ -18,11 +18,6 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from __future__ import division, absolute_import, print_function
import sys
if sys.version_info < (3,):
range = xrange
import numpy
import numpy.random
import bisect
......
......@@ -18,14 +18,9 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from __future__ import division, absolute_import, print_function
import sys
if sys.version_info < (3,):
range = xrange
from .. import _degree, _prop, Graph, GraphView, libcore, _get_rng, \
PropertyMap, VertexPropertyMap, conv_pickle_state, Vector_size_t, \
Vector_double, group_vector_property, perfect_prop_hash
PropertyMap, VertexPropertyMap, Vector_size_t, Vector_double, \
group_vector_property, perfect_prop_hash
from .. generation import condensation_graph, random_rewire, generate_sbm, \
solve_sbm_fugacities, generate_maxent_sbm
from .. stats import label_self_loops, remove_parallel_edges, remove_self_loops
......@@ -669,7 +664,6 @@ class BlockState(object):
return state
def __setstate__(self, state):
conv_pickle_state(state)
self.__init__(**state)
def get_block_state(self, b=None, vweight=False, **kwargs):
......
......@@ -18,13 +18,8 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from __future__ import division, absolute_import, print_function
import sys
if sys.version_info < (3,):
range = xrange
from .. import _degree, _prop, Graph, GraphView, libcore, _get_rng, PropertyMap, \
conv_pickle_state, Vector_size_t, Vector_double, group_vector_property
Vector_size_t, Vector_double, group_vector_property
from .. generation import condensation_graph
from .. stats import label_self_loops
from .. spectral import adjacency
......@@ -128,7 +123,6 @@ class EMBlockState(object):
return state
def __setstate__(self, state):
conv_pickle_state(state)
g, B, vm, em_s, em_t, wr, prs = state
self.__init__(g, B)
g.copy_property(vm, self.vm)
......
......@@ -18,13 +18,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from __future__ import division, absolute_import, print_function
import sys
if sys.version_info < (3,):
range = xrange
from .. import _degree, _prop, Graph, GraphView, libcore, _get_rng, PropertyMap, \
conv_pickle_state
from .. import _degree, _prop, Graph, GraphView, libcore, _get_rng, PropertyMap
import random
from numpy import *
import numpy
......@@ -405,7 +399,6 @@ class LayeredBlockState(OverlapBlockState, BlockState):
return state
def __setstate__(self, state):
conv_pickle_state(state)
self.__init__(**state)
def __copy__(self):
......
......@@ -18,11 +18,6 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from __future__ import division, absolute_import, print_function
import sys
if sys.version_info < (3,):
range = xrange
from .. import Vector_size_t, Vector_double
import numpy
......
......@@ -18,11 +18,6 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from __future__ import division, absolute_import, print_function
import sys
if sys.version_info < (3,):
range = xrange
import numpy
from . util import *
from . mcmc import *
......
......@@ -18,12 +18,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from __future__ import division, absolute_import, print_function
import sys
if sys.version_info < (3,):
range = xrange
from .. import _degree, _prop, Graph, GraphView, conv_pickle_state
from .. import _degree, _prop, Graph, GraphView
from . blockmodel import *
from . blockmodel import _bm_test
from . overlap_blockmodel import *
......@@ -213,10 +208,6 @@ class NestedBlockState(object):
return state
def __setstate__(self, state):
conv_pickle_state(state)
if "kwargs" in state: # backwards compatibility
state["state_args"] = state["kwargs"]
del state["kwargs"]
self.__init__(**state)
def get_bs(self):
......
......@@ -18,13 +18,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from __future__ import division, absolute_import, print_function
import sys
if sys.version_info < (3,):
range = xrange
from .. import _degree, _prop, Graph, GraphView, libcore, _get_rng, PropertyMap, \
conv_pickle_state
from .. import _degree, _prop, Graph, GraphView, libcore, _get_rng, PropertyMap
import random
from numpy import *
......@@ -361,7 +355,6 @@ class OverlapBlockState(BlockState):
return state
def __setstate__(self, state):
conv_pickle_state(state)
self.__init__(**state)
def get_E(self):
......
......@@ -18,11 +18,6 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from __future__ import division, absolute_import, print_function
import sys
if sys.version_info < (3,):
range = xrange
from .. import _degree, _prop, Graph, GraphView, _get_rng, Vector_size_t
from . blockmodel import DictState, get_entropy_args, _bm_test
......
......@@ -18,11 +18,6 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from __future__ import division, absolute_import, print_function
import sys
if sys.version_info < (3,):
range = xrange
from .. import _degree, _prop, Graph, GraphView, _get_rng, Vector_int32_t, \
Vector_size_t
from . blockmodel import DictState, get_entropy_args, _bm_test
......
......@@ -18,11 +18,6 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from __future__ import division, absolute_import, print_function
import sys
if sys.version_info < (3,):
range = xrange
from .. import _degree, _prop, Graph, GraphView, libcore, _get_rng, \
PropertyMap, edge_endpoint_property
......
......@@ -18,11 +18,6 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from __future__ import division, absolute_import, print_function
import sys
if sys.version_info < (3,):
range = xrange
import scipy.special
from numpy import *
......
......@@ -77,11 +77,6 @@ Contents
++++++++
"""
from __future__ import division, absolute_import, print_function
import sys
if sys.version_info < (3,):
range = xrange
from .. dl_import import dl_import
dl_import("from . import libgraph_tool_search")
......
......@@ -39,8 +39,6 @@ Contents
++++++++
"""
from __future__ import division, absolute_import, print_function
from .. import _degree, _prop, Graph, GraphView, _limit_args, Vector_int64_t, \
Vector_double
from .. stats import label_self_loops
......
......@@ -44,15 +44,12 @@ Contents
"""
from __future__ import division, absolute_import, print_function
from .. dl_import import dl_import
dl_import("from . import libgraph_tool_stats")
from .. import _degree, _prop, _get_rng, GraphView, PropertyMap
from numpy import *
import numpy
import sys
__all__ = ["vertex_hist", "edge_hist", "vertex_average", "edge_average",
"label_parallel_edges", "remove_parallel_edges",
......
......@@ -71,8 +71,6 @@ Contents
"""
from __future__ import division, absolute_import, print_function
from .. dl_import import dl_import
dl_import("from . import libgraph_tool_topology")
......@@ -81,7 +79,7 @@ from .. import _prop, Vector_int32_t, Vector_size_t, _check_prop_writable, \
EdgePropertyMap, PropertyMap, GraphView, libcore, _get_rng, _degree, \
perfect_prop_hash, _limit_args
from .. stats import label_self_loops
import random, sys, numpy, collections
import random, numpy, collections
<