Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DM-22173: Implement parsing of time strings in user expression #245

Merged
merged 3 commits into from Apr 1, 2020

Conversation

andy-slac
Copy link
Contributor

Time literal was added to a parser in the form T'string', string can
be one of the supported formats for astropy Time class plus optional
format and scale. Some parsing is done in the parser code to determine
string format and convert it to floating number if needed. For
string-based representations (iso, yday, etc.) it relies on astropy
parsing.

Also fixes an issue in QueryBuilder class which had issues with support of
dotted identifiers.

Documentation for parser is updated with description of supported time
literal syntax.

Copy link
Member

@timj timj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

`astropy.time.Time`_ class and parser converts time string into an instance
of that class. For string-based time formats such as ISO the conversion
of a time string to an object is done by the ``Time`` constructor. The syntax
of the string could be anyhting that is suported by ``astropy``, for details
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: anyhting

Time literals
^^^^^^^^^^^^^

Timestaps in a query language are specified using syntax ``T'time-string'``.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: Timestaps

if fmt is not None:
fmt = fmt.lower()
if fmt not in astropy.time.Time.FORMATS:
raise ValueError(f"Time string \"{time_str}\" specifies unknown time format \"{fmt}\"")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We raise early rather than waiting for astropy because we don't like the astropy exception?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do it to make more specific message about format, guessing that format is wrong from astropy exception would be difficult.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. Bad format:

>>> t.Time(123456.43, format="blah")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/timj/work/lsstsw3/miniconda/envs/lsst-scipipe/lib/python3.7/site-packages/astropy/time/core.py", line 429, in __init__
    precision, in_subfmt, out_subfmt)
  File "/Users/timj/work/lsstsw3/miniconda/envs/lsst-scipipe/lib/python3.7/site-packages/astropy/time/core.py", line 484, in _init_from_vals
    precision, in_subfmt, out_subfmt)
  File "/Users/timj/work/lsstsw3/miniconda/envs/lsst-scipipe/lib/python3.7/site-packages/astropy/time/core.py", line 526, in _get_time_fmt
    sorted(self.FORMATS)))
ValueError: Format 'blah' is not one of the allowed formats ['byear', 'byear_str', 'cxcsec', 'datetime', 'datetime64', 'decimalyear', 'fits', 'gps', 'iso', 'isot', 'jd', 'jyear', 'jyear_str', 'mjd', 'plot_date', 'stardate', 'unix', 'yday', 'ymdhms']

but bad scale:

>>> t.Time(123456.43, scale="xc")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/timj/work/lsstsw3/miniconda/envs/lsst-scipipe/lib/python3.7/site-packages/astropy/time/core.py", line 429, in __init__
    precision, in_subfmt, out_subfmt)
  File "/Users/timj/work/lsstsw3/miniconda/envs/lsst-scipipe/lib/python3.7/site-packages/astropy/time/core.py", line 476, in _init_from_vals
    sorted(self.SCALES)))
astropy.time.core.ScaleValueError: Scale 'xc' is not in the allowed scales ['local', 'tai', 'tcb', 'tcg', 'tdb', 'tt', 'ut1', 'utc']

So they have a special exception for bad time scale but not for bad format.

@@ -256,6 +344,15 @@ def p_literal_str(self, p):
"""
p[0] = StringLiteral(p[1])

def p_literal_time(self, p):
""" literal : TIME_LITERAL
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This docstring looks odd but I see it matches the others.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ply uses docstrings to define grammar rules, so it does look different from LSST standard.

"""Test for time literals"""
lexer = ParserLex.make_lexer()

# string can contain anyhting, lexer does not check it
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: anyhting

Copy link
Member

@TallJimbo TallJimbo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only looked at the QueryBuilder changes, but everything looks good there. Thanks for taking care of my bug.

Time literal was added to a parser in the form `T'string'`, string can
be one of the supported formats for astropy Time class plus optional
format and scale. Some parsing is done in the parser code to determine
string format and convert it to floating number if needed. For
string-based representations (iso, yday, etc.) it relies on astropy
parsing.
@andy-slac andy-slac merged commit f3f2412 into master Apr 1, 2020
@timj timj deleted the tickets/DM-22173 branch April 22, 2020 22:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants