Skip to content

Commit a6bf64f

Browse files
committed
Misc 5.0.0 API changes
1 parent a7f7365 commit a6bf64f

File tree

5 files changed

+42
-53
lines changed

5 files changed

+42
-53
lines changed

pymathics/graph/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
In[4]:= CompleteKaryTree[3, VertexLabels->True]
1212
"""
1313

14-
from pymathics.graph.version import __version__
1514
from pymathics.graph.__main__ import * # noqa
16-
from pymathics.graph.tree import * # qoqa
17-
from pymathics.graph.generators import * # noqa
1815
from pymathics.graph.algorithms import * # noqa
16+
from pymathics.graph.generators import * # noqa
17+
from pymathics.graph.tree import * # noqa
18+
from pymathics.graph.version import __version__ # noqa
1919

2020
pymathics_version_data = {
2121
"author": "The Mathics Team",

pymathics/graph/__main__.py

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
from inspect import isgenerator
1313

14-
1514
from mathics.builtin.base import AtomBuiltin, Builtin
1615
from mathics.builtin.box.graphics import GraphicsBox
1716
from mathics.builtin.box.inout import _BoxedString
@@ -269,8 +268,7 @@ def _evaluate_atom(self, graph, options, compute):
269268
def __str__(self):
270269
return "-Graph-"
271270

272-
# FIXME: return type should be a specific kind of Tuple, not a list.
273-
def get_sort_key(self, pattern_sort=False) -> list:
271+
def get_sort_key(self, pattern_sort=False) -> tuple:
274272
"""
275273
Returns a particular encoded list (which should be a tuple) that is used
276274
in ``Sort[]`` comparisons and in the ordering that occurs
@@ -471,6 +469,24 @@ def default_format(self, evaluation, form):
471469
def do_format(self, evaluation, form):
472470
return self
473471

472+
@property
473+
def edges(self):
474+
return self.G.edges
475+
476+
def empty(self):
477+
return len(self.G) == 0
478+
479+
def is_loop_free(self):
480+
return not any(True for _ in nx.nodes_with_selfloops(self.G))
481+
482+
# networkx graphs can't be for mixed
483+
def is_mixed_graph(self):
484+
return False
485+
# return self.edges. ... is_mixed()
486+
487+
def is_multigraph(self):
488+
return isinstance(self.G, (nx.MultiDiGraph, nx.MultiGraph))
489+
474490
def get_sort_key(self, pattern_sort=False) -> tuple:
475491
"""
476492
Returns a particular encoded list (which should be a tuple) that is used
@@ -498,30 +514,6 @@ def get_sort_key(self, pattern_sort=False) -> tuple:
498514
]
499515
return hash(self)
500516

501-
@property
502-
def edges(self):
503-
return self.G.edges
504-
505-
def empty(self):
506-
return len(self.G) == 0
507-
508-
def is_loop_free(self):
509-
return not any(True for _ in nx.nodes_with_selfloops(self.G))
510-
511-
# networkx graphs can't be for mixed
512-
def is_mixed_graph(self):
513-
return False
514-
# return self.edges. ... is_mixed()
515-
516-
def is_multigraph(self):
517-
return isinstance(self.G, (nx.MultiDiGraph, nx.MultiGraph))
518-
519-
def get_sort_key(self, pattern_sort=False):
520-
if pattern_sort:
521-
return super(Graph, self).get_sort_key(True)
522-
else:
523-
return (1, 3, Symbol("Pymathics`Graph"), tuple(), 2, len(self.pixels), hash(self))
524-
525517
@property
526518
def value(self):
527519
return self.G

pymathics/graph/algorithms.py

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,20 @@
55
networkx does all the heavy lifting.
66
"""
77

8-
from mathics.core.expression import Expression, Symbol
8+
from mathics.core.convert.python import from_python
9+
from mathics.core.expression import Expression
10+
from mathics.core.list import ListExpression
11+
from mathics.core.symbols import SymbolFalse
912

1013
from pymathics.graph.__main__ import (
1114
DEFAULT_GRAPH_OPTIONS,
12-
_NetworkXBuiltin,
15+
SymbolDirectedEdge,
16+
SymbolUndirectedEdge,
1317
_create_graph,
18+
_NetworkXBuiltin,
1419
nx,
1520
)
1621

17-
from mathics.core.expression import (
18-
from_python,
19-
)
20-
21-
# FIXME: Add to Mathics Expression
22-
# SymbolFalse = Symbol("System`False")
23-
# SymbolTrue = Symbol("System`True")
24-
2522

2623
class ConnectedComponents(_NetworkXBuiltin):
2724
"""
@@ -152,8 +149,8 @@ def apply(self, graph, expression, evaluation, options):
152149
"%(name)s[graph_, OptionsPattern[%(name)s]]"
153150
graph = self._build_graph(graph, evaluation, options, expression)
154151
if graph:
155-
weight = graph.update_weights(evaluation)
156-
edge_type = "DirectedEdge" if graph.G.is_directed() else "UndirectedEdge"
152+
graph.update_weights(evaluation)
153+
SymbolDirectedEdge if graph.G.is_directed() else SymbolUndirectedEdge
157154
# FIXME: put in edge to Graph conversion function?
158155
edges = [
159156
Expression("UndirectedEdge", u, v)
@@ -187,9 +184,9 @@ def apply(self, graph, expression, evaluation, options):
187184
"%(name)s[graph_, OptionsPattern[%(name)s]]"
188185
graph = self._build_graph(graph, evaluation, options, expression)
189186
if not graph:
190-
return Symbol("System`False")
187+
return SymbolFalse
191188
is_planar, _ = nx.check_planarity(graph.G)
192-
return Symbol("System`True") if is_planar else Symbol("System`False")
189+
return from_python(is_planar)
193190

194191

195192
class WeaklyConnectedComponents(_NetworkXBuiltin):
@@ -214,6 +211,6 @@ def apply(self, graph, expression, evaluation, options):
214211
components = sorted(components, key=lambda c: index[next(iter(c))])
215212

216213
vertices_sorted = graph.vertices.get_sorted()
217-
result = [Expression("List", *vertices_sorted(c)) for c in components]
214+
result = [ListExpression(*vertices_sorted(c)) for c in components]
218215

219-
return Expression("List", *result)
216+
return ListExpression(*result)

pymathics/graph/generators.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,23 @@
55
networkx does all the heavy lifting.
66
"""
77

8+
from typing import Callable, Optional
9+
810
from mathics.builtin.numbers.randomnumbers import RandomEnv
11+
from mathics.core.expression import Expression, Integer, String
912

1013
from pymathics.graph.__main__ import (
1114
Graph,
1215
SymbolUndirectedEdge,
13-
_NetworkXBuiltin,
1416
_convert_networkx_graph,
1517
_graph_from_list,
18+
_NetworkXBuiltin,
1619
_process_graph_options,
1720
has_directed_option,
1821
nx,
1922
)
20-
2123
from pymathics.graph.tree import DEFAULT_TREE_OPTIONS
2224

23-
from mathics.core.expression import Expression, Integer, String
24-
from typing import Callable, Optional
25-
2625
# TODO: Can this code can be DRY'd more?
2726

2827

pymathics/graph/tree.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import networkx as nx
2+
from mathics.core.expression import Atom, Symbol
3+
24
from pymathics.graph.__main__ import (
5+
DEFAULT_GRAPH_OPTIONS,
36
Graph,
47
_graph_from_list,
5-
DEFAULT_GRAPH_OPTIONS,
68
_NetworkXBuiltin,
79
)
8-
from mathics.core.expression import Atom, Symbol
910

1011
DEFAULT_TREE_OPTIONS = {
1112
**DEFAULT_GRAPH_OPTIONS,

0 commit comments

Comments
 (0)