gwpy / gwtrigfind

Mirror of official gwtrigfind project
https://git.ligo.org/detchar/tools/gwtrigfind
GNU General Public License v3.0
1 stars 5 forks source link

Compiled re should use a raw string with escape sequences #34

Closed eagoetz closed 1 month ago

eagoetz commented 1 month ago

This PR makes the use of raw strings for compiled regexp. When first running gwtrigfind, I found the warning:

/home/evan.goetz/lscrepos/gwtrigfind-eg/gwtrigfind/core.py:42: SyntaxWarning: invalid escape sequence '\A'
  daily_cbc = re.compile('\Adaily[\s_-]cbc\Z')
/home/evan.goetz/lscrepos/gwtrigfind-eg/gwtrigfind/core.py:43: SyntaxWarning: invalid escape sequence '\A'
  pycbc_live = re.compile('\Apycbc[\s_-]live\Z')
/home/evan.goetz/lscrepos/gwtrigfind-eg/gwtrigfind/core.py:44: SyntaxWarning: invalid escape sequence '\A'
  kleinewelle = re.compile('\A(kw|kleinewelle)\Z', re.I)
/home/evan.goetz/lscrepos/gwtrigfind-eg/gwtrigfind/core.py:45: SyntaxWarning: invalid escape sequence '\A'
  dmt_omega = re.compile('\Admt([\s_-])?omega\Z', re.I)
/home/evan.goetz/lscrepos/gwtrigfind-eg/gwtrigfind/core.py:46: SyntaxWarning: invalid escape sequence '\A'
  omega = re.compile('\Aomega([\s_-])?(online)?\Z', re.I)

Interestingly, running a second time causes the warning to go away, so one doesn't notice this the first time the code is run.

According to re,

If you’re not using a raw string to express the pattern, remember that Python also uses the backslash as an escape sequence in string literals; if the escape sequence isn’t recognized by Python’s parser, the backslash and subsequent character are included in the resulting string. However, if Python would recognize the resulting sequence, the backslash should be repeated twice. This is complicated and hard to understand, so it’s highly recommended that you use raw strings for all but the simplest expressions.

This PR changes the compiled regexp code into raw strings so that this warning is fixed.

duncanmmacleod commented 1 month ago

Migrated to https://git.ligo.org/detchar/tools/gwtrigfind/-/merge_requests/34