diff --git a/dot2tex/__init__.py b/dot2tex/__init__.py index 61ab088..1ed59f7 100644 --- a/dot2tex/__init__.py +++ b/dot2tex/__init__.py @@ -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 diff --git a/dot2tex/base.py b/dot2tex/base.py index 051f473..6fafca3 100644 --- a/dot2tex/base.py +++ b/dot2tex/base.py @@ -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): @@ -1024,7 +1024,7 @@ def process(self): s += "\end{preview}%\n" with open(self.tempfilename, 'w') as f: f.write(self.template.replace('<>', 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() @@ -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) diff --git a/dot2tex/dot2tex.py b/dot2tex/dot2tex.py index 2a28674..1fc76c1 100644 --- a/dot2tex/dot2tex.py +++ b/dot2tex/dot2tex.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- """Convert graphviz graphs to LaTeX-friendly formats Various tools for converting graphs generated by the graphviz library @@ -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 diff --git a/dot2tex/dotparsing.py b/dot2tex/dotparsing.py index c039eb2..8dec0a9 100644 --- a/dot2tex/dotparsing.py +++ b/dot2tex/dotparsing.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- """Graphviz dot language parser. This parser is derived from the the parser distributed with the pydot module. @@ -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): @@ -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 @@ -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 @@ -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): @@ -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.') @@ -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) + @@ -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 @@ -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): @@ -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): @@ -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): diff --git a/dot2tex/pgfformat.py b/dot2tex/pgfformat.py index 83c77a5..c264bb8 100644 --- a/dot2tex/pgfformat.py +++ b/dot2tex/pgfformat.py @@ -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 = '' diff --git a/dot2tex/pstricksformat.py b/dot2tex/pstricksformat.py index 45e9678..37302e1 100644 --- a/dot2tex/pstricksformat.py +++ b/dot2tex/pstricksformat.py @@ -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)) @@ -465,4 +464,4 @@ def start_node(self, node): return "" def end_node(self, node): - return "" \ No newline at end of file + return "" diff --git a/dot2tex/utils.py b/dot2tex/utils.py index 09739c7..e776f2e 100644 --- a/dot2tex/utils.py +++ b/dot2tex/utils.py @@ -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): @@ -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 @@ -124,4 +124,4 @@ def get_all_graph_elements(graph, l=None): if outer: return l else: - l.append(EndOfGraphElement()) \ No newline at end of file + l.append(EndOfGraphElement())