-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add cut * feat: from stream * test: datetime * doc: performance implications * docs: update readme
- Loading branch information
Showing
7 changed files
with
186 additions
and
6 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
from functools import wraps | ||
from typing import Callable, Generic, Sequence, TypeVar, overload | ||
|
||
from .parser import Parser | ||
from .result import Err, Ok, Result | ||
|
||
In = TypeVar("In", bound=Sequence) | ||
Out = TypeVar("Out") | ||
|
||
|
||
class SoftError(Exception): | ||
inner: Err | ||
|
||
|
||
class Stream(Generic[In]): | ||
inner: In | ||
index: int | ||
|
||
def __init__(self, inner: In, index: int = 0): | ||
self.inner = inner | ||
self.index = index | ||
|
||
def apply(self, parser: Parser[In, Out]) -> Out: | ||
res = parser(self.inner, self.index) | ||
|
||
if isinstance(res, Err): | ||
raise SoftError(res) | ||
|
||
self.index = res.index | ||
|
||
return res.value | ||
|
||
|
||
def _from_stream(func: Callable[[Stream[In]], Out]) -> Parser[In, Out]: | ||
@Parser | ||
@wraps(func) | ||
def fn(stream: In, index: int) -> Result[Out]: | ||
st = Stream(inner=stream, index=index) | ||
try: | ||
out = func(st) | ||
except SoftError as e: | ||
return e.inner | ||
return Ok(out, st.index) | ||
|
||
return fn | ||
|
||
|
||
@overload | ||
def from_stream(func: Callable[[Stream[In]], Out]) -> Parser[In, Out]: ... | ||
@overload | ||
def from_stream( | ||
func: str, | ||
) -> Callable[[Callable[[Stream[In]], Out]], Parser[In, Out]]: ... | ||
|
||
|
||
def from_stream(func: str | Callable[[Stream[In]], Out]): | ||
if isinstance(func, str): | ||
return lambda f: _from_stream(f).desc(func) | ||
|
||
else: | ||
return _from_stream(func) |
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,26 @@ | ||
from datetime import datetime | ||
|
||
import pytest | ||
|
||
from persil import Stream, from_stream, regex, string | ||
|
||
|
||
@from_stream | ||
def datetime_parser(stream: Stream[str]) -> datetime: | ||
year = stream.apply(regex(r"\d{4}").map(int)) | ||
stream.apply(string("/")) | ||
month = stream.apply(regex(r"\d{2}").map(int)) | ||
stream.apply(string("/")) | ||
day = stream.apply(regex(r"\d{2}").map(int)) | ||
return datetime(year, month, day) | ||
|
||
|
||
EXAMPLES = [ | ||
("2024/10/01", datetime(2024, 10, 1)), | ||
] | ||
|
||
|
||
@pytest.mark.parametrize("message,expected", EXAMPLES) | ||
def test_datetime_from_stream(message: str, expected: datetime): | ||
dt = datetime_parser.parse(message) | ||
assert dt == expected |
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,30 @@ | ||
import pytest | ||
from pydantic import BaseModel | ||
|
||
from persil import regex | ||
from persil.stream import Stream, from_stream | ||
|
||
|
||
class Flight(BaseModel): | ||
carrier: str | ||
flight_number: int | ||
|
||
|
||
@from_stream("Flight parser") | ||
def flight_parser(stream: Stream[str]) -> Flight: | ||
carrier = stream.apply(regex(r"[A-Z]{2}")) | ||
flight_number = stream.apply(regex(r"\d{2,4}").map(int)) | ||
|
||
return Flight(carrier=carrier, flight_number=flight_number) | ||
|
||
|
||
EXAMPLES = [ | ||
("AF071", Flight(carrier="AF", flight_number=71)), | ||
("LY180", Flight(carrier="LY", flight_number=180)), | ||
] | ||
|
||
|
||
@pytest.mark.parametrize("message,expected", EXAMPLES) | ||
def test_generate(message: str, expected: Flight): | ||
flight = flight_parser.parse(message) | ||
assert flight == expected |