Skip to content

Commit 9c2e1ed

Browse files
authored
Merge pull request #46 from Mathics3/track-element-order-API-change
function get_sort_key() -> element_order property
2 parents 6e76603 + f760afa commit 9c2e1ed

File tree

2 files changed

+43
-48
lines changed

2 files changed

+43
-48
lines changed

.github/workflows/ubuntu.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
runs-on: ubuntu-latest
1212
strategy:
1313
matrix:
14-
python-version: ['3.9', '3.10', '3.11', '3.12', '3.13']
14+
python-version: ['3.10', '3.11', '3.12', '3.13']
1515
steps:
1616
- uses: actions/checkout@v4
1717
- name: Set up Python ${{ matrix.python-version }}

pymathics/graph/base.py

Lines changed: 42 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from mathics.core.element import BaseElement
2222
from mathics.core.evaluation import Evaluation
2323
from mathics.core.expression import Expression
24+
from mathics.core.keycomparable import IMAGE_EXPRESSION_SORT_KEY
2425
from mathics.core.pattern import pattern_objects
2526
from mathics.core.symbols import Symbol, SymbolList, SymbolTrue
2627
from mathics.core.systemsymbols import (
@@ -42,6 +43,7 @@
4243
SymbolUndirectedEdge,
4344
)
4445

46+
GRAPH_EXPRESSION_SORT_KEY = IMAGE_EXPRESSION_SORT_KEY + 1
4547

4648
WL_MARKER_TO_NETWORKX = {
4749
"Circle": "o",
@@ -200,32 +202,28 @@ def _evaluate_atom(self, graph, options, compute):
200202
def __str__(self):
201203
return "-Graph-"
202204

203-
def get_sort_key(self, pattern_sort=False) -> tuple:
205+
@property
206+
def element_order(self) -> tuple:
204207
"""
205-
Returns a particular encoded list (which should be a tuple) that is used
206-
in ``Sort[]`` comparisons and in the ordering that occurs
207-
in an M-Expression which has the ``Orderless`` property.
208-
209-
See the docstring for element.get_sort_key() for more detail.
208+
Return a value which is used in ordering elements
209+
of an expression and Sort[]. The tuple is ultimately compared lexicographically.
210210
"""
211-
212-
if pattern_sort:
213-
return super(_NetworkXBuiltin, self).get_sort_key(True)
214-
else:
215-
# Return a sort_key tuple.
216-
# but with a `2` instead of `1` in the 5th position,
217-
# and adding two extra fields: the length in the 5th position,
218-
# and a hash in the 6th place.
219-
return [
220-
1,
221-
3,
222-
self.class_head_name,
223-
tuple(),
224-
2,
225-
len(self.vertices),
226-
hash(self),
227-
]
228-
return hash(self)
211+
# Return the precedence similar to the key for an `Image[]`
212+
# object, but with a `2` instead of `1` in the 5th position,
213+
#
214+
# Graphs with fewer vertices in a graph should appear before
215+
# nodes with more vertices. This property is captured by the
216+
# 3rd position, of the order tuple. A hash of the object is put
217+
# last for two graphs are structurally the same but different, so that
218+
# the same graph object will appear consecutively in a Sort[].
219+
return (
220+
GRAPH_EXPRESSION_SORT_KEY,
221+
SymbolGraph,
222+
len(self.vertices),
223+
tuple(),
224+
2,
225+
hash(self),
226+
)
229227

230228

231229
class _FullGraphRewrite(Exception):
@@ -397,31 +395,28 @@ def is_mixed_graph(self):
397395
def is_multigraph(self):
398396
return isinstance(self.G, (nx.MultiDiGraph, nx.MultiGraph))
399397

400-
def get_sort_key(self, pattern_sort=False) -> tuple:
398+
@property
399+
def element_order(self) -> tuple:
401400
"""
402-
Returns a particular encoded list (which should be a tuple) that is used
403-
in ``Sort[]`` comparisons and in the ordering that occurs
404-
in an M-Expression which has the ``Orderless`` property.
405-
406-
See the docstring for element.get_sort_key() for more detail.
401+
Return a value which is used in ordering elements
402+
of an expression and Sort[]. The tuple is ultimately compared lexicographically.
407403
"""
408-
409-
if pattern_sort:
410-
return super(Graph, self).get_sort_key(True)
411-
else:
412-
# Return a sort_key tuple.
413-
# but with a `2` instead of `1` in the 5th position,
414-
# and adding two extra fields: the length in the 5th position,
415-
# and a hash in the 6th place.
416-
return [
417-
1,
418-
3,
419-
self.class_head_name,
420-
tuple(),
421-
2,
422-
len(self.vertices),
423-
hash(self),
424-
]
404+
# Return the precedence similar to the key for an `Image[]`
405+
# object, but with a `2` instead of `1` in the 5th position,
406+
#
407+
# Graphs with fewer vertices in a graph should appear before
408+
# nodes with more vertices. This property is captured by the
409+
# 3rd position, of the order tuple. A hash of the object is put
410+
# last for two graphs are structurally the same but different, so that
411+
# the same graph object will appear consecutively in a Sort[].
412+
return (
413+
GRAPH_EXPRESSION_SORT_KEY,
414+
SymbolGraph,
415+
len(self.vertices),
416+
tuple(),
417+
2,
418+
hash(self),
419+
)
425420

426421
def sort_vertices(self, vertices):
427422
return sorted(vertices)

0 commit comments

Comments
 (0)