Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
trac #34079: pycodestyle cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
dcoudert committed Jun 25, 2022
1 parent f8df808 commit b6db584
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 74 deletions.
3 changes: 2 additions & 1 deletion src/sage/graphs/hypergraph_generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
---------------------
"""


class HypergraphGenerators():
r"""
A class consisting of constructors for common hypergraphs.
Expand Down Expand Up @@ -181,7 +182,7 @@ def nauty(self, number_of_sets, number_of_vertices,
nauty_input += " -d" + str(vertex_min_degree) + ":" + str(set_min_size)
nauty_input += " -D" + str(vertex_max_degree) + ":" + str(set_max_size)

nauty_input += " " + str(number_of_vertices) + " " + str(number_of_sets) + " "
nauty_input += " " + str(number_of_vertices) + " " + str(number_of_sets) + " "

sp = subprocess.Popen(shlex.quote(genbgL_path) + " {0}".format(nauty_input), shell=True,
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
Expand Down
9 changes: 5 additions & 4 deletions src/sage/graphs/independent_sets.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ cdef inline int ismaximal(binary_matrix_t g, int n, bitset_t s):

return True


cdef class IndependentSets:
r"""
The set of independent sets of a graph.
Expand Down Expand Up @@ -256,7 +257,7 @@ cdef class IndependentSets:
count += 1

if not self.count_only:
yield [self.vertices[j] for j in range(i + 1) if bitset_in(tmp,j)]
yield [self.vertices[j] for j in range(i + 1) if bitset_in(tmp, j)]
continue

else:
Expand Down Expand Up @@ -380,11 +381,11 @@ cdef class IndependentSets:
try:
bitset_set_first_n(s, 0)

for I in S:
for v in S:
try:
i = self.vertex_to_int[I]
i = self.vertex_to_int[v]
except KeyError:
raise ValueError(str(I) + " is not a vertex of the graph")
raise ValueError(str(v) + " is not a vertex of the graph")

# Adding the new vertex to s
bitset_add(s, i)
Expand Down
89 changes: 51 additions & 38 deletions src/sage/graphs/isgci.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,22 +396,24 @@ class is defined by the exclusion of subgraphs, one can write a generic
from sage.structure.unique_representation import CachedRepresentation, UniqueRepresentation
from sage.misc.unknown import Unknown
from sage.env import GRAPHS_DATA_DIR
from sage.misc.cachefunc import cached_method

import os
import zipfile
from urllib.request import urlopen
from ssl import create_default_context as default_context

#*****************************************************************************
# ****************************************************************************
# Copyright (C) 2011 Nathann Cohen <[email protected]>
#
# Distributed under the terms of the GNU General Public License (GPL)
# http://www.gnu.org/licenses/
#*****************************************************************************
# https://www.gnu.org/licenses/
# ****************************************************************************

_XML_FILE = "isgci_sage.xml"
_SMALLGRAPHS_FILE = "smallgraphs.txt"


class GraphClass(SageObject, CachedRepresentation):
r"""
An instance of this class represents a Graph Class, matching some entry in
Expand All @@ -437,7 +439,7 @@ class GraphClass(SageObject, CachedRepresentation):
"""
def __init__(self, name, gc_id, recognition_function=None):
r"""
Class constructor
Class constructor.
INPUT:
Expand All @@ -460,7 +462,7 @@ class represented by ``gc_id`` ?*

def _repr_(self):
r"""
Returns a short description of the class
Return a short description of the class.
EXAMPLES::
Expand All @@ -471,7 +473,7 @@ def _repr_(self):

def __hash__(self):
r"""
Returns the class' ID hash
Return the class' ID hash.
EXAMPLES::
Expand All @@ -482,7 +484,7 @@ def __hash__(self):

def __le__(self, other):
r"""
<= operator
<= operator.
EXAMPLES::
Expand All @@ -493,22 +495,22 @@ def __le__(self, other):

def __ge__(self, other):
r"""
>= operator
>= operator.
EXAMPLES::
sage: graph_classes.Chordal >= graph_classes.Tree
True
"""
inclusion_digraph = GraphClasses().inclusion_digraph()
if inclusion_digraph.shortest_path(self._gc_id,other._gc_id):
if inclusion_digraph.shortest_path(self._gc_id, other._gc_id):
return True
else:
return Unknown

def __eq__(self, other):
r"""
== operator
== operator.
EXAMPLES::
Expand All @@ -519,7 +521,7 @@ def __eq__(self, other):

def __lt__(self, other):
r"""
>, !=, and < operators
>, !=, and < operators.
EXAMPLES::
Expand All @@ -542,7 +544,7 @@ def __lt__(self, other):

def forbidden_subgraphs(self):
r"""
Returns the list of forbidden induced subgraphs defining the class.
Return the list of forbidden induced subgraphs defining the class.
If the graph class is not defined by a *finite* list of forbidden
induced subgraphs, ``None`` is returned instead.
Expand All @@ -561,15 +563,15 @@ def forbidden_subgraphs(self):
classes = GraphClasses().classes()
gc = classes[self._gc_id]

if gc.get("type",None) != "forbidden":
if gc.get("type", None) != "forbidden":
return None

excluded = gc.get("smallgraph", None)

if not excluded:
return None

if not isinstance(excluded,list):
if not isinstance(excluded, list):
excluded = [excluded]

smallgraphs = GraphClasses().smallgraphs()
Expand All @@ -581,7 +583,7 @@ def forbidden_subgraphs(self):

def __contains__(self, g):
r"""
Tests if ``g`` belongs to the graph class represented by ``self``.
Check if ``g`` belongs to the graph class represented by ``self``.
EXAMPLES::
Expand Down Expand Up @@ -615,7 +617,7 @@ def __contains__(self, g):
excluded = self.forbidden_subgraphs()

if excluded is None:
raise NotImplementedError("No recognition algorithm is available "+
raise NotImplementedError("No recognition algorithm is available "
"for this class.")

for gg in excluded:
Expand All @@ -626,7 +628,7 @@ def __contains__(self, g):

def description(self):
r"""
Prints the information of ISGCI about the current class.
Print the information of ISGCI about the current class.
EXAMPLES::
Expand Down Expand Up @@ -673,16 +675,15 @@ def description(self):
print("\nProblems :")
print("-" * 11)

for pbname,data in sorted(cls["problem"].items()):
for pbname, data in sorted(cls["problem"].items()):
if "complexity" in data:
print("{:30} : {}".format(pbname, data["complexity"]))

from sage.misc.cachefunc import cached_method

class GraphClasses(UniqueRepresentation):
def get_class(self, id):
r"""
Returns the class corresponding to the given id in the ISGCI database.
Return the class corresponding to the given id in the ISGCI database.
INPUT:
Expand Down Expand Up @@ -718,12 +719,14 @@ def get_class(self, id):

return GraphClass(name, id)
else:
raise ValueError("The given class id does not exist in the ISGCI database. Is the db too old ? You can update it with graph_classes.update_db().")
raise ValueError("The given class id does not exist in the ISGCI "
"database. Is the db too old ? You can update it "
"with graph_classes.update_db().")

@cached_method
def classes(self):
r"""
Returns the graph classes, as a dictionary.
Return the graph classes, as a dictionary.
Upon the first call, this loads the database from the local XML
file. Subsequent calls are cached.
Expand All @@ -746,7 +749,7 @@ def classes(self):
@cached_method
def inclusions(self):
r"""
Returns the graph class inclusions
Return the graph class inclusions.
OUTPUT:
Expand All @@ -769,7 +772,7 @@ def inclusions(self):
@cached_method
def smallgraphs(self):
r"""
Returns a dictionary associating a graph to a graph description string.
Return a dictionary associating a graph to a graph description string.
Upon the first call, this loads the database from the local XML files.
Subsequent calls are cached.
Expand All @@ -794,7 +797,7 @@ def smallgraphs(self):
@cached_method
def inclusion_digraph(self):
r"""
Returns the class inclusion digraph
Return the class inclusion digraph.
Upon the first call, this loads the database from the local XML file.
Subsequent calls are cached.
Expand All @@ -804,23 +807,23 @@ def inclusion_digraph(self):
sage: g = graph_classes.inclusion_digraph(); g
Digraph on ... vertices
"""
classes = self.classes()
classes = self.classes()
inclusions = self.inclusions()

from sage.graphs.digraph import DiGraph
inclusion_digraph = DiGraph()
inclusion_digraph.add_vertices(classes.keys())

for edge in inclusions:
if edge.get("confidence","") == "unpublished":
if edge.get("confidence", "") == "unpublished":
continue
inclusion_digraph.add_edge(edge['super'], edge['sub'])

return inclusion_digraph

def _download_db(self):
r"""
Downloads the current version of the ISGCI db
Download the current version of the ISGCI db.
EXAMPLES::
Expand All @@ -842,7 +845,7 @@ def _download_db(self):

def _parse_db(self, directory):
r"""
Parses the ISGCI database and stores its content in ``self``.
Parse the ISGCI database and stores its content in ``self``.
INPUT:
Expand Down Expand Up @@ -873,8 +876,8 @@ def _parse_db(self, directory):
smallgraph_file = open(os.path.join(GRAPHS_DATA_DIR, _SMALLGRAPHS_FILE), 'r')
smallgraphs = {}

for l in smallgraph_file.readlines():
key, string = l.split("\t")
for line in smallgraph_file.readlines():
key, string = line.split("\t")
smallgraphs[key] = Graph(string)

smallgraph_file.close()
Expand Down Expand Up @@ -941,7 +944,7 @@ def _get_ISGCI(self):

# Which copy is the most recent on the disk ?
if (os.path.getmtime(os.path.join(SAGE_DB, _XML_FILE)) >
os.path.getmtime(os.path.join(GRAPHS_DATA_DIR, _XML_FILE))):
os.path.getmtime(os.path.join(GRAPHS_DATA_DIR, _XML_FILE))):

directory = os.path.join(SAGE_DB, _XML_FILE)

Expand Down Expand Up @@ -976,8 +979,8 @@ def show_all(self):
# We want to print the different fields, and this dictionary stores the
# maximal number of characters of each field.
MAX = {
"id" : 0,
"type" : 0,
"id": 0,
"type": 0,
"smallgraph": 0,
"name": 0
}
Expand All @@ -1002,20 +1005,29 @@ def sort_key(x):
MAX[key] = min(length, MAX_LEN)

# Head of the table
print(("{0:"+str(MAX["id"])+"} | {1:"+str(MAX["name"])+"} | {2:"+str(MAX["type"])+"} | {3:"+str(MAX["smallgraph"])+"}").format("id", "name", "type", "smallgraph"))
st = ("{:" + str(MAX["id"]) + "}").format("id")
st += (" | {:" + str(MAX["name"]) + "}").format("name")
st += (" | {:" + str(MAX["type"]) + "}").format("type")
st += (" | {:" + str(MAX["smallgraph"]) + "}").format("smallgraph")
print(st)
print("-" * (sum(MAX.values())+9))

# Entries
for entry in classes_list:
ID = entry.get("id", "")
name = entry.get("name", "")
type = entry.get("type", "")
typ = entry.get("type", "")
smallgraph = entry.get("smallgraph", "")
print(("{0:"+str(MAX["id"])+"} | {1:"+str(MAX["name"])+"} | {2:"+str(MAX["type"])+"} | ").format(ID, name[:MAX_LEN], type[:MAX_LEN])+str(smallgraph)[:MAX_LEN])
st = ("{:" + str(MAX["id"]) + "}").format(ID)
st += (" | {:" + str(MAX["name"]) + "}").format(name[:MAX_LEN])
st += (" | {:" + str(MAX["type"]) + "}").format(typ[:MAX_LEN])
st += " | " + str(smallgraph)[:MAX_LEN]
print(st)


def _XML_to_dict(root):
r"""
Returns the XML data as a dictionary
Return the XML data as a dictionary.
INPUT:
Expand Down Expand Up @@ -1050,6 +1062,7 @@ def _XML_to_dict(root):
return root.text
return ans


graph_classes = GraphClasses()

# Any object added to this list should also appear in the class' documentation, at the top of the file.
Expand Down
Loading

0 comments on commit b6db584

Please sign in to comment.