Gigasecond template (#2135)

* Add gigasecond test template and generate tests.

* Add datetime import.

* gigasecond: add test template

Adds the gigasecond test template. Addresses most of the concerns on the
defunct #2066 submitted by @simonbcodes, however the `strptime` fix
suggested by @cmccandless was brittle, as some of the incoming data had
time data.

We'd _maybe_ want to add a `dateutil` dependency if the canonical data
strays in any way from bog standard ISO 8601 dates, but hopefully if
that happens the `datetime.fromisoformat` calls here will blow up and
let us know.

Closes #1950

* gigasecond: generate with dateutil parse

Adds a dependency to the generator, but shifts the work off the student.
As an added benefit should parse a lot more than fromisoformat would.

Must be merged _after_ #2134.

* Update requirements-generator.txt

Woops!

Co-Authored-By: Corey McCandless <cmccandless@users.noreply.github.com>
This commit is contained in:
Michael Morehouse
2019-11-20 19:57:18 +00:00
committed by Corey McCandless
parent d3d04fb72f
commit ad801810c8
4 changed files with 53 additions and 22 deletions

View File

@@ -37,6 +37,7 @@ from tempfile import NamedTemporaryFile
from textwrap import wrap
from jinja2 import Environment, FileSystemLoader, TemplateNotFound, UndefinedError
from dateutil.parser import parse
VERSION = "0.2.1"
@@ -81,6 +82,26 @@ def wrap_overlong(string, width=70):
return ["{0!r} \\".format(w) for w in wrap(string, width)]
def parse_datetime(string, strip_module=False):
"""
Parse a (hopefully ISO 8601) datestamp to a datetime object and
return its repr for use in a jinja2 template.
If used the template will need to import the datetime module.
import datetime
However if strip_module is True then the template will need to
import the datetime _class_ instead.
from datetime import datetime
"""
result = repr(parse(string))
if strip_module:
return result.replace("datetime.", "", 1)
return result
def get_tested_properties(spec):
"""
Get set of tested properties from spec. Include nested cases.
@@ -258,6 +279,7 @@ def generate(
env.filters["camel_case"] = camel_case
env.filters["wrap_overlong"] = wrap_overlong
env.filters["regex_replace"] = regex_replace
env.filters["parse_datetime"] = parse_datetime
env.tests["error_case"] = error_case
result = True
for exercise in sorted(glob(os.path.join("exercises", exercise_glob))):