From 271d99098f50038d3130f6072ff3bc64895610ea Mon Sep 17 00:00:00 2001 From: rocky Date: Sun, 17 Aug 2025 18:20:45 -0400 Subject: [PATCH 1/3] function get_sort_key() -> element_order property --- pymathics/graph/base.py | 81 +++++++++++++++++------------------------ 1 file changed, 34 insertions(+), 47 deletions(-) diff --git a/pymathics/graph/base.py b/pymathics/graph/base.py index 8e3bd59..bef4225 100644 --- a/pymathics/graph/base.py +++ b/pymathics/graph/base.py @@ -21,6 +21,7 @@ from mathics.core.element import BaseElement from mathics.core.evaluation import Evaluation from mathics.core.expression import Expression +from mathics.core.keycomparable import IMAGE_EXPRESSION_SORT_KEY from mathics.core.pattern import pattern_objects from mathics.core.symbols import Symbol, SymbolList, SymbolTrue from mathics.core.systemsymbols import ( @@ -42,6 +43,7 @@ SymbolUndirectedEdge, ) +GRAPH_EXPRESSION_SORT_KEY = IMAGE_EXPRESSION_SORT_KEY + 1 WL_MARKER_TO_NETWORKX = { "Circle": "o", @@ -200,32 +202,24 @@ def _evaluate_atom(self, graph, options, compute): def __str__(self): return "-Graph-" - def get_sort_key(self, pattern_sort=False) -> tuple: + @property + def element_order(self) -> tuple: """ - Returns a particular encoded list (which should be a tuple) that is used - in ``Sort[]`` comparisons and in the ordering that occurs - in an M-Expression which has the ``Orderless`` property. - - See the docstring for element.get_sort_key() for more detail. + Return a value which is used in ordering elements + of an expression. The tuple is ultimately compared lexicographically. """ - - if pattern_sort: - return super(_NetworkXBuiltin, self).get_sort_key(True) - else: - # Return a sort_key tuple. - # but with a `2` instead of `1` in the 5th position, - # and adding two extra fields: the length in the 5th position, - # and a hash in the 6th place. - return [ - 1, - 3, - self.class_head_name, - tuple(), - 2, - len(self.vertices), - hash(self), - ] - return hash(self) + # Return the precedence the expression `Image[]`, + # but with a `2` instead of `1` in the 5th position, + # and adding two extra fields: the length in the 3rd position, + # and a hash in the 6th place. + return ( + GRAPH_EXPRESSION_SORT_KEY, + SymbolGraph, + len(self.vertices), + tuple(), + 2, + hash(self), + ) class _FullGraphRewrite(Exception): @@ -397,31 +391,24 @@ def is_mixed_graph(self): def is_multigraph(self): return isinstance(self.G, (nx.MultiDiGraph, nx.MultiGraph)) - def get_sort_key(self, pattern_sort=False) -> tuple: + @property + def element_order(self) -> tuple: """ - Returns a particular encoded list (which should be a tuple) that is used - in ``Sort[]`` comparisons and in the ordering that occurs - in an M-Expression which has the ``Orderless`` property. - - See the docstring for element.get_sort_key() for more detail. + Return a value which is used in ordering elements + of an expression. The tuple is ultimately compared lexicographically. """ - - if pattern_sort: - return super(Graph, self).get_sort_key(True) - else: - # Return a sort_key tuple. - # but with a `2` instead of `1` in the 5th position, - # and adding two extra fields: the length in the 5th position, - # and a hash in the 6th place. - return [ - 1, - 3, - self.class_head_name, - tuple(), - 2, - len(self.vertices), - hash(self), - ] + # Return the precedence the expression `Image[]`, + # but with a `2` instead of `1` in the 5th position, + # and adding two extra fields: the length in the 3rd position, + # and a hash in the 6th place. + return ( + GRAPH_EXPRESSION_SORT_KEY, + SymbolGraph, + len(self.vertices), + tuple(), + 2, + hash(self), + ) def sort_vertices(self, vertices): return sorted(vertices) From cb9b21093b4e9d5cdf99b2db7ef80729abfc9dc8 Mon Sep 17 00:00:00 2001 From: rocky Date: Sun, 17 Aug 2025 18:31:52 -0400 Subject: [PATCH 2/3] Go over element_order comment --- pymathics/graph/base.py | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/pymathics/graph/base.py b/pymathics/graph/base.py index bef4225..9bac809 100644 --- a/pymathics/graph/base.py +++ b/pymathics/graph/base.py @@ -206,12 +206,16 @@ def __str__(self): def element_order(self) -> tuple: """ Return a value which is used in ordering elements - of an expression. The tuple is ultimately compared lexicographically. + of an expression and Sort[]. The tuple is ultimately compared lexicographically. """ - # Return the precedence the expression `Image[]`, - # but with a `2` instead of `1` in the 5th position, - # and adding two extra fields: the length in the 3rd position, - # and a hash in the 6th place. + # Return the precedence similar to the key for an `Image[]` + # object, but with a `2` instead of `1` in the 5th position, + # + # Graphs with fewer vertices in a graph should appear before + # nodes with more vertices. This property is captured by the + # 3rd position, of the order tuple. A hash of the object is put + # last for two graphs are structurally the same but different, so that + # the same graph object will appear consecutively in a Sort[]. return ( GRAPH_EXPRESSION_SORT_KEY, SymbolGraph, @@ -395,12 +399,16 @@ def is_multigraph(self): def element_order(self) -> tuple: """ Return a value which is used in ordering elements - of an expression. The tuple is ultimately compared lexicographically. + of an expression and Sort[]. The tuple is ultimately compared lexicographically. """ - # Return the precedence the expression `Image[]`, - # but with a `2` instead of `1` in the 5th position, - # and adding two extra fields: the length in the 3rd position, - # and a hash in the 6th place. + # Return the precedence similar to the key for an `Image[]` + # object, but with a `2` instead of `1` in the 5th position, + # + # Graphs with fewer vertices in a graph should appear before + # nodes with more vertices. This property is captured by the + # 3rd position, of the order tuple. A hash of the object is put + # last for two graphs are structurally the same but different, so that + # the same graph object will appear consecutively in a Sort[]. return ( GRAPH_EXPRESSION_SORT_KEY, SymbolGraph, From f760afaaab70723a2993916f39d3cd79101a72d1 Mon Sep 17 00:00:00 2001 From: rocky Date: Sun, 17 Aug 2025 18:34:26 -0400 Subject: [PATCH 3/3] Remove 3.9 testing --- .github/workflows/ubuntu.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ubuntu.yml b/.github/workflows/ubuntu.yml index f939b71..b60ba67 100644 --- a/.github/workflows/ubuntu.yml +++ b/.github/workflows/ubuntu.yml @@ -11,7 +11,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - python-version: ['3.9', '3.10', '3.11', '3.12', '3.13'] + python-version: ['3.10', '3.11', '3.12', '3.13'] steps: - uses: actions/checkout@v4 - name: Set up Python ${{ matrix.python-version }}