Skip to content

Commit

Permalink
Forbid whitespace in key-value pairs, allow whitespace to delimit sta…
Browse files Browse the repository at this point in the history
…tes in StatefulSpecies
  • Loading branch information
xnx committed Feb 11, 2022
1 parent beeecd0 commit cb628dd
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 34 deletions.
9 changes: 5 additions & 4 deletions doc/stateful_species.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@ with one or more quantum states of labels, and can be represented by an instance

A ``StatefulSpecies`` object can be instantiated by providing a string consisting of
the formula (which can be parsed into a ``Formula`` object), followed by whitespace
and a semicolon delimited sequence of strings that can be parsed by one of the
``State`` classes described previously.
and a whitespace-delimited sequence of strings that can be parsed by one of the
``State`` classes described previously. For clarity, a semi-colon or comma can also be
used as a delimiter between states.
The state type is deduced from the format of the string.

Examples:

.. code-block:: pycon
>>> from pyvalem.stateful_species import StatefulSpecies
>>> ss1 = StatefulSpecies('HCl v=2;J=0')
>>> ss1 = StatefulSpecies('HCl v=2 J=0')
>>> ss2 = StatefulSpecies('NCO v2+3v1;J=10.5;a(2Σ-g)')
>>> ss3 = StatefulSpecies('Ar+ 1s2.2s2.2p5; 2P_3/2')
>>> ss3 = StatefulSpecies('Ar+ 1s2.2s2.2p5, 2P_3/2')
>>> ss4 = StatefulSpecies('CrH 1sigma2.2sigma1.1pi4.3sigma1; 6SIGMA+')
An HTML representation is accessible through the ``html`` attribute:
Expand Down
7 changes: 3 additions & 4 deletions doc/states.rst
Original file line number Diff line number Diff line change
Expand Up @@ -321,13 +321,12 @@ Examples:

The ``KeyValuePair`` class represents an arbitrary quantum number or symmetry provided
as a (key, value) pair. It is instantiated with a string of the form ``key=value``.
Whitespace within a key-value pair is illegal.
For example:

.. code-block:: pycon
>>> from pyvalem.states import KeyValuePair
>>> kv1 = KeyValuePair('n=1')
>>> kv2 = KeyValuePair('C = 45a#')
>>> kv2 = KeyValuePair('|M|=2')
>>> kv3 = KeyValuePair('sym=anti')
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.3",
version="2.5.4",
description="A package for managing simple chemical species and states",
long_description=long_description,
long_description_content_type="text/x-rst",
Expand Down
3 changes: 2 additions & 1 deletion src/pyvalem/stateful_species.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ def __init__(self, s):

i = s.index(" ")
self.formula = Formula(s[:i])
self.states = state_parser(s[i + 1 :].split(";"))
s = s.replace(";", " ").replace(",", " ")
self.states = state_parser(s[i + 1 :].split())

self._verify_states()

Expand Down
10 changes: 8 additions & 2 deletions src/pyvalem/states/key_value_pair.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,15 @@ def _parse_state(self, state_str):
"""
Parse state_str into a KeyValuePair object.
Whitespace is tolerated in the input, i.e both 'key=value' and
'key = value' are parsed, but no spaces are inserted in the output.
Whitespace is not tolerated in the input, i.e only 'key=value' and not
'key = value' are allowed. No spaces are inserted in the output.
"""

if any(c.isspace() for c in state_str):
raise KeyValuePairError(
"No whitespace allowed in key-value pair: {}".format(state_str)
)

try:
key, value = state_str.split("=")
except ValueError:
Expand Down
20 changes: 3 additions & 17 deletions tests/test_key_value_pairs.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,9 @@ def test_key_value_pair(self):
self.assertEqual(str(kv1), "n=1")
self.assertEqual(kv1.html, "n=1")

kv2 = KeyValuePair("C = 45a#")
self.assertEqual(kv2.key, "C")
self.assertEqual(kv2.value, "45a#")
self.assertEqual(str(kv2), "C=45a#")
self.assertEqual(kv2.html, "C=45a#")

self.assertRaises(KeyValuePairError, KeyValuePair, "*")
self.assertRaises(KeyValuePairError, KeyValuePair, "C = 3")
self.assertRaises(KeyValuePairError, KeyValuePair, "k= 3")
self.assertRaises(KeyValuePairError, KeyValuePair, "k =v")

def test_key_value_pair_equality(self):
kv1 = KeyValuePair("nd=1")
Expand All @@ -33,16 +29,6 @@ def test_key_value_pair_equality(self):
self.assertNotEqual(kv1, kv3)
self.assertNotEqual(kv2, kv4)

def test_key_value_pair_repr(self):
kv1 = KeyValuePair("n=2")
kv2 = KeyValuePair("n =2")
kv3 = KeyValuePair("n= 2")
kv4 = KeyValuePair("n = 2")
kv5 = KeyValuePair("n = 2")
self.assertTrue(
repr(kv1) == repr(kv2) == repr(kv3) == repr(kv4) == repr(kv5) == "n=2"
)

def test_html_escaping(self):
kv1 = KeyValuePair('S="5<n<9"')
self.assertEqual(repr(kv1), 'S="5<n<9"')
Expand Down
10 changes: 5 additions & 5 deletions tests/test_stateful_species.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ def test_stateful_species_parsing(self):
_ = StatefulSpecies("Ar *")
_ = StatefulSpecies("CrH 1sigma2.2sigma1.1pi4.3sigma1; 6SIGMA+")
_ = StatefulSpecies("H(35Cl) J=2")
_ = StatefulSpecies("OH X(2Π_1/2; J=2")
_ = StatefulSpecies("OH X(2Π_1/2, J=2")

_ = StatefulSpecies("HCl v=2;J=0")
_ = StatefulSpecies("HCl v=2 J=0")
_ = StatefulSpecies("C2H3Cl")
_ = StatefulSpecies("C2H2 v2+v1;J=10;X(1SIGMA+u)")
_ = StatefulSpecies("C2H2 v2+v1 J=10;X(1SIGMA+u)")
_ = StatefulSpecies("CO *")

ss1 = StatefulSpecies("NCO v2+3v1;J=10.5;a(2Σ-g)")
Expand Down Expand Up @@ -55,10 +55,10 @@ def test_state_appears_at_most_once(self):
self.assertRaises(StatefulSpeciesError, StatefulSpecies, "HCl v=0;v=1")
self.assertRaises(StatefulSpeciesError, StatefulSpecies, "H 1s1;2s1")
self.assertRaises(StatefulSpeciesError, StatefulSpecies, "H2 1sigma1;2sigma1")
self.assertRaises(StatefulSpeciesError, StatefulSpecies, "CO J=0;J=1")
self.assertRaises(StatefulSpeciesError, StatefulSpecies, "CO J=0; J=1")
self.assertRaises(StatefulSpeciesError, StatefulSpecies, "CO X(1PIu);2Σ-")
self.assertRaises(StatefulSpeciesError, StatefulSpecies, "Ar *;**")
self.assertRaises(StatefulSpeciesError, StatefulSpecies, "Ar 2S;2P_3/2")
self.assertRaises(StatefulSpeciesError, StatefulSpecies, "Ar 2S, 2P_3/2")
# StatefulSpecies('CH3Cl J=2;Ka=1;Kc=2')

def test_atomic_configuration_verification(self):
Expand Down

0 comments on commit cb628dd

Please sign in to comment.