Skip to content

Commit 271d990

Browse files
committed
function get_sort_key() -> element_order property
1 parent 6e76603 commit 271d990

File tree

1 file changed

+34
-47
lines changed

1 file changed

+34
-47
lines changed

pymathics/graph/base.py

Lines changed: 34 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,24 @@ 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. 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 the expression `Image[]`,
212+
# but with a `2` instead of `1` in the 5th position,
213+
# and adding two extra fields: the length in the 3rd position,
214+
# and a hash in the 6th place.
215+
return (
216+
GRAPH_EXPRESSION_SORT_KEY,
217+
SymbolGraph,
218+
len(self.vertices),
219+
tuple(),
220+
2,
221+
hash(self),
222+
)
229223

230224

231225
class _FullGraphRewrite(Exception):
@@ -397,31 +391,24 @@ def is_mixed_graph(self):
397391
def is_multigraph(self):
398392
return isinstance(self.G, (nx.MultiDiGraph, nx.MultiGraph))
399393

400-
def get_sort_key(self, pattern_sort=False) -> tuple:
394+
@property
395+
def element_order(self) -> tuple:
401396
"""
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.
397+
Return a value which is used in ordering elements
398+
of an expression. The tuple is ultimately compared lexicographically.
407399
"""
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-
]
400+
# Return the precedence the expression `Image[]`,
401+
# but with a `2` instead of `1` in the 5th position,
402+
# and adding two extra fields: the length in the 3rd position,
403+
# and a hash in the 6th place.
404+
return (
405+
GRAPH_EXPRESSION_SORT_KEY,
406+
SymbolGraph,
407+
len(self.vertices),
408+
tuple(),
409+
2,
410+
hash(self),
411+
)
425412

426413
def sort_vertices(self, vertices):
427414
return sorted(vertices)

0 commit comments

Comments
 (0)