-
Notifications
You must be signed in to change notification settings - Fork 827
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #214 from Textualize/time-no-scalar
Splitting out parsing of durations into new token types, avoiding Scalar
- Loading branch information
Showing
11 changed files
with
251 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,3 +11,4 @@ repos: | |
rev: 21.8b0 | ||
hooks: | ||
- id: black | ||
exclude: ^tests/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,4 @@ typecheck: | |
format: | ||
black src | ||
format-check: | ||
black --check . | ||
black --check src |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,15 +17,13 @@ classifiers = [ | |
"Programming Language :: Python :: 3.10", | ||
] | ||
|
||
|
||
[tool.poetry.dependencies] | ||
python = "^3.7" | ||
rich = "^10.12.0" | ||
#rich = {git = "[email protected]:willmcgugan/rich", rev = "link-id"} | ||
typing-extensions = { version = "^3.10.0", python = "<3.8" } | ||
|
||
[tool.poetry.dev-dependencies] | ||
|
||
pytest = "^6.2.3" | ||
black = "^21.11b1" | ||
mypy = "^0.910" | ||
|
@@ -35,6 +33,9 @@ mkdocstrings = "^0.15.2" | |
mkdocs-material = "^7.1.10" | ||
pre-commit = "^2.13.0" | ||
|
||
[tool.black] | ||
includes = "src" | ||
|
||
[build-system] | ||
requires = ["poetry-core>=1.0.0"] | ||
build-backend = "poetry.core.masonry.api" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import re | ||
|
||
_match_duration = re.compile(r"^(-?\d+\.?\d*)(s|ms)$").match | ||
|
||
|
||
class DurationError(Exception): | ||
""" | ||
Exception indicating a general issue with a CSS duration. | ||
""" | ||
|
||
|
||
class DurationParseError(DurationError): | ||
""" | ||
Indicates a malformed duration string that could not be parsed. | ||
""" | ||
|
||
|
||
def _duration_as_seconds(duration: str) -> float: | ||
""" | ||
Args: | ||
duration (str): A string of the form ``"2s"`` or ``"300ms"``, representing 2 seconds and | ||
300 milliseconds respectively. If no unit is supplied, e.g. ``"2"``, then the duration is | ||
assumed to be in seconds. | ||
Raises: | ||
DurationParseError: If the argument ``duration`` is not a valid duration string. | ||
Returns: | ||
float: The duration in seconds. | ||
""" | ||
match = _match_duration(duration) | ||
|
||
if match: | ||
value, unit_name = match.groups() | ||
value = float(value) | ||
if unit_name == "ms": | ||
duration_secs = value / 1000 | ||
else: | ||
duration_secs = value | ||
else: | ||
try: | ||
duration_secs = float(duration) | ||
except ValueError: | ||
raise DurationParseError( | ||
f"{duration!r} is not a valid duration." | ||
) from ValueError | ||
|
||
return duration_secs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.