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

a few minor code simplifications (python3) suggested by ruff #111

Open
wants to merge 1 commit 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: 0 additions & 1 deletion dot2tex/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Convert graphviz graphs to LaTeX-friendly formats

Various tools for converting graphs generated by the graphviz library
Expand Down
6 changes: 3 additions & 3 deletions dot2tex/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def get_drawobj_lblstyle(drawobj, extra_styles=None):
])) or None


class DotConvBase(object):
class DotConvBase:
"""Dot2TeX converter base"""

def __init__(self, options=None):
Expand Down Expand Up @@ -1024,7 +1024,7 @@ def process(self):
s += "\end{preview}%\n"
with open(self.tempfilename, 'w') as f:
f.write(self.template.replace('<<preproccode>>', s))
with open(self.tempfilename, 'r') as f:
with open(self.tempfilename) as f:
s = f.read()
log.debug('Code written to %s\n' % self.tempfilename + s)
self.parse_log_file()
Expand Down Expand Up @@ -1063,7 +1063,7 @@ def parse_log_file(self):
p.kill()
p.wait()

with open(logfilename, 'r') as f:
with open(logfilename) as f:
logdata = f.read()
log.debug('Logfile from LaTeX run: \n' + logdata)
os.chdir(tmpdir)
Expand Down
3 changes: 1 addition & 2 deletions dot2tex/dot2tex.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
"""Convert graphviz graphs to LaTeX-friendly formats

Various tools for converting graphs generated by the graphviz library
Expand Down Expand Up @@ -251,7 +250,7 @@ def print_version_info():


def load_dot_file(filename):
with open(filename, 'r') as f:
with open(filename) as f:
dotdata = f.readlines()
log.info('Data read from %s' % filename)
return dotdata
Expand Down
35 changes: 15 additions & 20 deletions dot2tex/dotparsing.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
"""Graphviz dot language parser.

This parser is derived from the the parser distributed with the pydot module.
Expand Down Expand Up @@ -65,10 +64,7 @@ def needs_quotes(s):
## if not res:
## res = id_re_with_port.match(s)

if not res:
return True

return False
return not res


def quote_if_necessary(s):
Expand All @@ -84,9 +80,8 @@ def quote_if_necessary(s):

def flatten(lst):
for elem in lst:
if type(elem) in (tuple, list):
for i in flatten(elem):
yield i
if isinstance(elem, (tuple, list)):
yield from flatten(elem)
else:
yield elem

Expand Down Expand Up @@ -121,7 +116,7 @@ def nsplit(seq, n=2):
>>> nsplit('aabbcc',n=4)
[('a', 'a', 'b', 'b')]
"""
return [xy for xy in zip(*[iter(seq)] * n)]
return list(zip(*[iter(seq)] * n))


# The following function is from the pydot project
Expand Down Expand Up @@ -286,7 +281,7 @@ def find_graphviz():
SET_GRAPH_ATTR = 'set_graph_attr'


class DotDataParser(object):
class DotDataParser:
"""Container class for parsing Graphviz dot data"""

def __init__(self):
Expand Down Expand Up @@ -426,8 +421,8 @@ def parse_html(s, loc, toks):
closer = '>'
try:
html_text = pyparsing.nestedExpr(opener, closer,
((CharsNotIn(
opener + closer).setParseAction(lambda t: t[0]))
(CharsNotIn(
opener + closer).setParseAction(lambda t: t[0])
)).setParseAction(parse_html)
except:
log.debug('nestedExpr not available.')
Expand All @@ -449,11 +444,11 @@ def parse_html(s, loc, toks):

port_angle = (at + ID).setName("port_angle")

port_location = ((OneOrMore(Group(colon + ID)) |
Group(colon + lparen + ID + comma + ID + rparen))).setName("port_location")
port_location = (OneOrMore(Group(colon + ID)) |
Group(colon + lparen + ID + comma + ID + rparen)).setName("port_location")

port = Combine((Group(port_location + Optional(port_angle)) |
Group(port_angle + Optional(port_location)))).setName("port")
port = Combine(Group(port_location + Optional(port_angle)) |
Group(port_angle + Optional(port_location))).setName("port")

node_id = (ID + Optional(port))
a_list = OneOrMore(ID + Optional(equals + righthand_id) +
Expand Down Expand Up @@ -627,7 +622,7 @@ def parse_dot_data_debug(self, data):
return None


class DotDefaultAttr(object):
class DotDefaultAttr:
def __init__(self, element_type, **kwds):
self.element_type = element_type
self.attr = kwds
Expand All @@ -647,7 +642,7 @@ class DotParsingException(Exception):
"""Base class for dotparsing exceptions."""


class DotNode(object):
class DotNode:
"""Class representing a DOT node"""

def __init__(self, name, **kwds):
Expand Down Expand Up @@ -693,7 +688,7 @@ def __getattr__(self, name):
raise AttributeError


class DotGraph(object):
class DotGraph:
"""Class representing a DOT graph"""

def __init__(self, name='G', strict=True, directed=False, **kwds):
Expand Down Expand Up @@ -1003,7 +998,7 @@ def __str__(self):
'subgraph', self.get_name(), subgraphstr, attrstr, nodestr, edgestr, padding)


class DotEdge(object):
class DotEdge:
"""Class representing a DOT edge"""

def __init__(self, src, dst, directed=False, src_port="", dst_port="", **kwds):
Expand Down
5 changes: 2 additions & 3 deletions dot2tex/pgfformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,9 +343,8 @@ def draw_text(self, drawop, style=None):
def draw_bezier(self, drawop, style=None):
s = ""
c, points = drawop
pp = []
for point in points:
pp.append("(%sbp,%sbp)" % (smart_float(point[0]), smart_float(point[1])))
pp = ["(%sbp,%sbp)" % (smart_float(point[0]), smart_float(point[1]))
for point in points]

pstrs = ["%s .. controls %s and %s " % p for p in nsplit(pp, 3)]
stylestr = ''
Expand Down
7 changes: 3 additions & 4 deletions dot2tex/pstricksformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,8 @@ def draw_polyline(self, drawop, style=None):

def draw_bezier(self, drawop, style=None):
op, points = drawop
pp = []
for point in points:
pp.append("(%sbp,%sbp)" % (smart_float(point[0]), smart_float(point[1])))
pp = ["(%sbp,%sbp)" % (smart_float(point[0]), smart_float(point[1]))
for point in points]

arrowstyle = ""
return " \psbezier{%s}%s\n" % (arrowstyle, "".join(pp))
Expand Down Expand Up @@ -465,4 +464,4 @@ def start_node(self, node):
return ""

def end_node(self, node):
return ""
return ""
6 changes: 3 additions & 3 deletions dot2tex/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def nsplit(seq, n=2):
>>> nsplit('aabbcc',n=4)
[('a', 'a', 'b', 'b')]
"""
return [xy for xy in zip(*[iter(seq)] * n)]
return list(zip(*[iter(seq)] * n))


def chunks(s, cl):
Expand Down Expand Up @@ -101,7 +101,7 @@ def is_multiline_label(drawobject):
return any(x in label for x in [r"\n", r"\l", r"\r"])


class EndOfGraphElement(object):
class EndOfGraphElement:
def __init__(self):
pass

Expand All @@ -124,4 +124,4 @@ def get_all_graph_elements(graph, l=None):
if outer:
return l
else:
l.append(EndOfGraphElement())
l.append(EndOfGraphElement())