Skip to content

Commit

Permalink
get rid of python2 support
Browse files Browse the repository at this point in the history
  • Loading branch information
bdcht committed Mar 15, 2020
1 parent b0a604a commit 7c69dbf
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 60 deletions.
4 changes: 2 additions & 2 deletions grandalf/graphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class Vertex(vertex_core):
"""

def __init__(self,data=None):
vertex_core.__init__(self)
super().__init__()
# by default, a new vertex belongs to its own component
# but when the vertex is added to a graph, c points to the
# connected component where it belongs.
Expand Down Expand Up @@ -160,7 +160,7 @@ class Edge(edge_core):
"""

def __init__(self,x,y,w=1,data=None,connect=False):
edge_core.__init__(self,x,y)
super().__init__(x,y)
# w is an optional weight associated with the edge.
self.w = w
self.data = data
Expand Down
54 changes: 21 additions & 33 deletions grandalf/layouts.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,6 @@

from grandalf.utils import *

try:
xrange
except NameError:
xrange = range

try:
from itertools import izip
except ImportError:
izip = zip

#------------------------------------------------------------------------------

class VertexViewer(object):
Expand Down Expand Up @@ -114,7 +104,7 @@ class DummyVertex(_sugiyama_vertex_attr):
def __init__(self,r=None,viewclass=VertexViewer):
self.view = viewclass()
self.ctrl = None
_sugiyama_vertex_attr.__init__(self,r,d=1)
super().__init__(r,d=1)

def N(self,dir):
assert dir==+1 or dir==-1
Expand Down Expand Up @@ -304,7 +294,7 @@ def _ordering_reduce_crossings(self):
g = self.layout.grx
N = len(self)
X=0
for i,j in izip(xrange(N-1),xrange(1,N)):
for i,j in zip(range(N-1),range(1,N)):
vi = self[i]
vj = self[j]
ni = [g[v].bar for v in self._neighbors(vi)]
Expand Down Expand Up @@ -338,6 +328,11 @@ class SugiyamaLayout(object):
Attributes:
dirvh (int): the current aligment state
for alignment policy:
dirvh=0 -> dirh=+1, dirv=-1: leftmost upper
dirvh=1 -> dirh=-1, dirv=-1: rightmost upper
dirvh=2 -> dirh=+1, dirv=+1: leftmost lower
dirvh=3 -> dirh=-1, dirv=+1: rightmost lower
order_inter (int): the default number of layer placement iterations
order_attr (str): set attribute name used for layer ordering
xspace (int): horizontal space between vertices in a layer
Expand Down Expand Up @@ -430,12 +425,6 @@ def _edge_inverter(self):
for e in self.g.degenerated_edges:
self.g.add_edge(e)


# internal state for alignment policy:
# dirvh=0 -> dirh=+1, dirv=-1: leftmost upper
# dirvh=1 -> dirh=-1, dirv=-1: rightmost upper
# dirvh=2 -> dirh=+1, dirv=+1: leftmost lower
# dirvh=3 -> dirh=-1, dirv=+1: rightmost lower
@property
def dirvh(self): return self.__dirvh
@property
Expand Down Expand Up @@ -557,7 +546,7 @@ def setdummies(self,e):
ctrl=self.ctrls[e]={}
ctrl[r0]=v0
ctrl[r1]=v1
for r in xrange(r0+1,r1):
for r in range(r0+1,r1):
self.dummyctrl(r,ctrl)

def draw_step(self):
Expand Down Expand Up @@ -607,7 +596,7 @@ def setxy(self):
self.grx[v].X = None
self.grx[v].x = [0.0]*4
curvh = self.dirvh # save current dirvh value
for dirvh in xrange(4):
for dirvh in range(4):
self.dirvh = dirvh
self._coord_vertical_alignment()
self._coord_horizontal_compact()
Expand Down Expand Up @@ -765,9 +754,9 @@ def draw_edges(self):
D = self.ctrls[e]
r0,r1 = self.grx[e.v[0]].rank,self.grx[e.v[1]].rank
if r0<r1:
ranks = xrange(r0+1,r1)
ranks = range(r0+1,r1)
else:
ranks = xrange(r0-1,r1,-1)
ranks = range(r0-1,r1,-1)
l = [D[r].view.xy for r in ranks]
l.insert(0,e.v[0].view.xy)
l.append(e.v[1].view.xy)
Expand Down Expand Up @@ -820,7 +809,7 @@ def draw(self,N=None):
self.draw_edges()

def draw_step(self):
for x in xrange(self._cv_max_iter):
for x in range(self._cv_max_iter):
self.draw(N=1)
self.draw_edges()
yield
Expand All @@ -845,7 +834,7 @@ def part_to_levels(self,alpha,beta):
sorted(ordering, reverse=True)
l = []
self.levels.append(l)
for i in xrange(len(list(ordering))-1):
for i in range(len(list(ordering))-1):
y,v = ordering[i]
l.append(v)
v.level = self.levels.index(l)
Expand Down Expand Up @@ -882,7 +871,7 @@ def _conjugate_gradient_L(self,y,b):
r = b - self.__L_pk(Lii,y)
p = DigcoLayout.linalg.array(r,copy=True)
rr = sum(r*r)
for k in xrange(self._cg_max_iter):
for k in range(self._cg_max_iter):
try:
Lp = self.__L_pk(Lii,p)
alpha = rr/sum(p*Lp)
Expand Down Expand Up @@ -938,7 +927,7 @@ def _cg_Lw(self,Lw,z,b):
r = b - Lw*z
p = r.copy()
rr = scal(r,r)
for k in xrange(self._cg_max_iter):
for k in range(self._cg_max_iter):
if rr<self._cg_tolerance: break
Lp = Lw*p
alpha = rr/scal(p,Lp)
Expand All @@ -965,9 +954,9 @@ def __Lij_w_(self):
self.Dij = self.__Dij_() # we keep D also for L^Z computations
Lij = self.Dij.copy()
n = self.g.order()
for i in xrange(n):
for i in range(n):
d = 0
for j in xrange(n):
for j in range(n):
if j==i: continue
Lij[i,j] = 1.0/self.Dij[i,j]**2
d += Lij[i,j]
Expand All @@ -981,8 +970,8 @@ def __Lij_Z_Z(self,Z):
lzz = Z.copy()*0.0 # lzz has dim Z (n x 2)
liz = DigcoLayout.linalg.matrix([0.0]*n) # liz is a row of L^Z (size n)
# compute lzz = L^Z.Z while assembling L^Z by row (liz):
for i in xrange(n):
iterk_except_i = (k for k in xrange(n) if k!=i)
for i in range(n):
iterk_except_i = (k for k in range(n) if k!=i)
for k in iterk_except_i:
v = Z[i]-Z[k]
liz[0,k] = 1.0/(self.Dij[i,k]*DigcoLayout.linalg.sqrt(v*v.transpose()))
Expand Down Expand Up @@ -1032,6 +1021,5 @@ def _optimize(self,Z,limit=100):


#------------------------------------------------------------------------------
class DwyerLayout(object):
def __init__(self):
raise NotImplementedError
class DwyerLayout(DigcoLayout):
pass
2 changes: 2 additions & 0 deletions grandalf/utils/dot.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
except ImportError:
_has_ply = False

__all__ = ['_has_ply','Dot']

#------------------------------------------------------------------------------
# LALR(1) parser for Graphviz dot file format.
class Dot:
Expand Down
11 changes: 3 additions & 8 deletions grandalf/utils/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@
from math import atan,atan2,degrees,sqrt
from random import SystemRandom

try:
xrange
except NameError:
xrange = range

#------------------------------------------------------------------------------
def intersect2lines(xy1, xy2, xy3, xy4):
(x1,y1) = xy1
Expand Down Expand Up @@ -96,7 +91,7 @@ def median_wh(views):
# a random normalized n-dimension vector orthogonal to (1,1,1,...,1).
def rand_ortho1(n):
r = SystemRandom()
pos = [r.random() for x in xrange(n)]
pos = [r.random() for x in range(n)]
s = sum(pos)
v = array(pos,dtype=float)-(s/float(n))
norm = sqrt(sum(v*v))
Expand Down Expand Up @@ -131,7 +126,7 @@ def setcurve(e,pts,tgs=None):
else:
Q,T = tangents(P,n)
splines=[]
for k in xrange(n-1):
for k in range(n-1):
t = T[k]+T[k+1]
a = 16. - (t.dot(t))
b = 12.*(Q[k].dot(t))
Expand All @@ -154,7 +149,7 @@ def tangents(P,n):
assert n>=2
Q = []
T = []
for k in xrange(0,n-1):
for k in range(0,n-1):
q = P[k+1]-P[k]
t = q/sqrt(q.dot(q))
Q.append(q)
Expand Down
26 changes: 18 additions & 8 deletions grandalf/utils/linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
from math import sqrt
from array import array as _array

constants = (int,float)

def coerce_(types):
if types is None: types = []
if str in types: raise TypeError
if complex in types: raise TypeError
dtype = ('i',int)
if long in types: dtype = ('l',long)
if float in types: dtype = ('d',float)
return dtype

Expand All @@ -29,12 +30,6 @@ def wrapper(self,ij,*args):
return f(self,(I,J),*args)
return wrapper

if sys.version_info < (3,):
constants = (int,long,float)

else:
long = int
constants = (int,long,float)
# minimalistic numpy.array replacement class used as fallback
# when numpy is not found in geometry module
class array(object):
Expand Down Expand Up @@ -142,7 +137,7 @@ def __iter__(self):
for x in self.data: yield x

def __setitem__(self,i,v):
assert isinstance(i,(int,long))
assert isinstance(i,int)
self.data[i] = self.dtype(v)

def __getitem__(self,i):
Expand Down Expand Up @@ -279,3 +274,18 @@ def __iter__(self):
for l in self.data:
for v in l:
yield v

class SimplexMin(object):
def __init__(self,A,b,c):
self.A=A
self.b=b
self.c=c
self.tableau()

def tableau(self):
self.T=[]

def setup(self):
self.enter = []
delf.outer = []

5 changes: 2 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

setup(
name='grandalf',
version='0.6',
version='0.7',

description='Graph and drawing algorithms framework',
long_description=long_descr,
Expand Down Expand Up @@ -59,7 +59,6 @@

# Specify the Python versions you support here. In particular, ensure
# that you indicate whether you support Python 2, Python 3 or both.
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
],

Expand All @@ -78,7 +77,7 @@
setup_requires=['pytest-runner',],
tests_require=['pytest',],

install_requires=['pyparsing','future'],
install_requires=['pyparsing'],

# List additional groups of dependencies here (e.g. development
# dependencies). You can install these using the following syntax,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_layouts.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def create_scenario():
data_to_vertex = {}

vertices = []
for i in xrange(6):
for i in range(6):
data = 'v%s' % (i,)
v = Vertex(data)
data_to_vertex[data] = v
Expand Down
2 changes: 1 addition & 1 deletion tests/test_recurs.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def test_recurs():
# TODO: reimplement the recursive parts of SugiyamaLayout in iterative form.
v = range(1001)
V = [Vertex(x) for x in v]
E = [Edge(V[x],V[x+1]) for x in xrange(1000)]
E = [Edge(V[x],V[x+1]) for x in range(1000)]

gr = graph_core(V,E)
for v in gr.V(): v.view = VertexViewer(10,10)
Expand Down
5 changes: 1 addition & 4 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ def test_linalg_001():
def test_linalg_001():
v1 = array([1,2,3])
assert v1.dim==3
if sys.version_info < (3,):
assert v1.typecode=='i'
else:
assert v1.typecode=='l'
assert v1.typecode=='i'
assert v1.dtype==int
assert len(v1)==3
assert v1[1]==2
Expand Down

0 comments on commit 7c69dbf

Please sign in to comment.