Skip to content

Commit

Permalink
Ensure that n=# key-value pair is before all other key-value pairs in…
Browse files Browse the repository at this point in the history
… canonical ordering
  • Loading branch information
xnx committed May 4, 2022
1 parent 9df3b71 commit 4cc214f
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 3 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

setup(
name="pyvalem",
version="2.5.6",
version="2.5.7",
description="A package for managing simple chemical species and states",
long_description=long_description,
long_description_content_type="text/x-rst",
Expand Down
2 changes: 1 addition & 1 deletion src/pyvalem/stateful_species.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def __repr__(self):
"""Return a canonical text representation of the StatefulSpecies."""
if self.states:
states_sorted = sorted(
self.states, key=lambda state: (STATES[type(state)], repr(state))
self.states, key=lambda state: (STATES[type(state)], state.ordering)
)
return "{} {}".format(
self.formula, ";".join(repr(state) for state in states_sorted)
Expand Down
4 changes: 4 additions & 0 deletions src/pyvalem/states/_base_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,7 @@ def __ne__(self, other):

def __hash__(self):
return hash(self.__repr__())

@property
def ordering(self):
return repr(self)
6 changes: 6 additions & 0 deletions src/pyvalem/states/key_value_pair.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,9 @@ def _parse_state(self, state_str):

def __repr__(self):
return self.state_str

@property
def ordering(self):
if self.key == "n":
return 0
return ord(self.key)
6 changes: 5 additions & 1 deletion tests/test_stateful_species.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,18 @@ def test_stateful_species_repr(self):
ss2 = StatefulSpecies("(235U) l=0;***;n=1")
ss3 = StatefulSpecies("(235U) l=0;n=1;***")
self.assertEqual(repr(ss2), repr(ss3))
self.assertEqual(repr(ss2), "(235U) 3*;l=0;n=1")
self.assertEqual(repr(ss2), "(235U) 3*;n=1;l=0")

for ss_text in ["C+ 4P;2s2.2p1", "C+ 2s2.2p1;4P"]:
self.assertEqual(repr(StatefulSpecies(ss_text)), "C+ 2s2.2p;4P")

for ss_text in ["C+ 2P;2s2.2p1", "C+ 2s2.2p1;2P"]:
self.assertEqual(repr(StatefulSpecies(ss_text)), "C+ 2s2.2p;2P")

def test_stateful_species_key_value_pair_ordering(self):
ss1 = StatefulSpecies("H n=3;l=1")
self.assertEqual(repr(ss1), "H n=3;l=1")


if __name__ == "__main__":
unittest.main()

0 comments on commit 4cc214f

Please sign in to comment.