Skip to content

Commit

Permalink
Updated pint example to latest flexparser api
Browse files Browse the repository at this point in the history
  • Loading branch information
hgrecco committed Jun 5, 2022
1 parent e80aa35 commit 2df18b2
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 26 deletions.
4 changes: 1 addition & 3 deletions examples/pint/parse-pint.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ def target(self):
return self.value

@classmethod
def from_string(
cls, s: str, config: common.Config
) -> fp.FromString[ImportDefinition]:
def from_string(cls, s: str) -> fp.FromString[ImportDefinition]:
if s.startswith("@import"):
return ImportDefinition(s[len("@import") :].strip())
return None
Expand Down
6 changes: 3 additions & 3 deletions examples/pint/pint_parser/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class Equality(fp.ParsedStatement):
rhs: str

@classmethod
def from_string(cls, s: str, config: Config) -> fp.FromString[Equality]:
def from_string(cls, s: str) -> fp.FromString[Equality]:
if "=" not in s:
return None
parts = [p.strip() for p in s.split("=")]
Expand All @@ -85,7 +85,7 @@ class Comment(fp.ParsedStatement):
comment: str

@classmethod
def from_string(cls, s: str, config: Config) -> fp.FromString[fp.ParsedStatement]:
def from_string(cls, s: str) -> fp.FromString[fp.ParsedStatement]:
if not s.startswith("#"):
return None
return cls(s[1:].strip())
Expand All @@ -96,7 +96,7 @@ class EndDirectiveBlock(fp.ParsedStatement):
"""An EndDirectiveBlock is simply an "@end" statement."""

@classmethod
def from_string(cls, s: str, config: Config) -> fp.FromString[EndDirectiveBlock]:
def from_string(cls, s: str) -> fp.FromString[EndDirectiveBlock]:
if s == "@end":
return cls()
return None
Expand Down
16 changes: 9 additions & 7 deletions examples/pint/pint_parser/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class _Relation:
equation: str

@classmethod
def _from_string(
def _from_string_and_context_sep(
cls, s: str, config: common.Config, separator: str
) -> fp.FromString[_Relation]:
if separator not in s:
Expand Down Expand Up @@ -76,10 +76,10 @@ def bidirectional(self):
return False

@classmethod
def from_string(
def from_string_and_config(
cls, s: str, config: common.Config
) -> fp.FromString[ForwardRelation]:
return super()._from_string(s, config, "->")
return super()._from_string_and_context_sep(s, config, "->")


@dataclass(frozen=True)
Expand All @@ -96,10 +96,10 @@ def bidirectional(self):
return True

@classmethod
def from_string(
def from_string_and_config(
cls, s: str, config: common.Config
) -> fp.FromString[BidirectionalRelation]:
return super()._from_string(s, config, "<->")
return super()._from_string_and_context_sep(s, config, "<->")


@dataclass(frozen=True)
Expand All @@ -118,7 +118,9 @@ class BeginContext(fp.ParsedStatement):
defaults: Dict[str, numbers.Number]

@classmethod
def from_string(cls, s: str, config: common.Config) -> fp.FromString[BeginContext]:
def from_string_and_config(
cls, s: str, config: common.Config
) -> fp.FromString[BeginContext]:
try:
r = cls._header_re.search(s)
if r is None:
Expand All @@ -136,7 +138,7 @@ def from_string(cls, s: str, config: common.Config) -> fp.FromString[BeginContex
)

if defaults:

# TODO: Use config non_int_type
def to_num(val):
val = complex(val)
if not val.imag:
Expand Down
2 changes: 1 addition & 1 deletion examples/pint/pint_parser/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class BeginDefaults(fp.ParsedStatement):
"""

@classmethod
def from_string(cls, s: str, config: common.Config) -> fp.FromString[BeginDefaults]:
def from_string(cls, s: str) -> fp.FromString[BeginDefaults]:
if s.strip() == "@defaults":
return cls()
return None
Expand Down
2 changes: 1 addition & 1 deletion examples/pint/pint_parser/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class BeginGroup(fp.ParsedStatement):
using_group_names: ty.Tuple[str, ...]

@classmethod
def from_string(cls, s: str, config: common.Config) -> fp.FromString[BeginGroup]:
def from_string(cls, s: str) -> fp.FromString[BeginGroup]:
if not s.startswith("@group"):
return None

Expand Down
14 changes: 5 additions & 9 deletions examples/pint/pint_parser/plain.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class PrefixDefinition(fp.ParsedStatement):
aliases: ty.Tuple[str, ...]

@classmethod
def from_string(
def from_string_and_config(
cls, s: str, config: common.Config
) -> fp.FromString[PrefixDefinition]:
if "=" not in s:
Expand Down Expand Up @@ -98,7 +98,7 @@ class UnitDefinition(fp.ParsedStatement):
is_base: bool

@classmethod
def from_string(
def from_string_and_config(
cls, s: str, config: common.Config
) -> fp.FromString[UnitDefinition]:
if "=" not in s:
Expand Down Expand Up @@ -204,9 +204,7 @@ def is_base(self):
return False

@classmethod
def from_string(
cls, s: str, config: common.Config
) -> fp.FromString[DimensionDefinition]:
def from_string(cls, s: str) -> fp.FromString[DimensionDefinition]:
s = s.strip()

if not (s.startswith("[") and "=" not in s):
Expand Down Expand Up @@ -239,7 +237,7 @@ def is_base(self):
return False

@classmethod
def from_string(
def from_string_and_config(
cls, s: str, config: common.Config
) -> fp.FromString[DerivedDimensionDefinition]:
if not (s.startswith("[") and "=" in s):
Expand Down Expand Up @@ -287,9 +285,7 @@ class AliasDefinition(fp.ParsedStatement):
aliases: ty.Tuple[str, ...]

@classmethod
def from_string(
cls, s: str, config: common.Config
) -> fp.FromString[AliasDefinition]:
def from_string(cls, s: str) -> fp.FromString[AliasDefinition]:
if not s.startswith("@alias "):
return None
name, *aliases = s[len("@alias ") :].split("=")
Expand Down
4 changes: 2 additions & 2 deletions examples/pint/pint_parser/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Rule(fp.ParsedStatement):
old_unit_name: ty.Optional[str] = None

@classmethod
def from_string(cls, s: str, config) -> fp.FromString[Rule]:
def from_string(cls, s: str) -> fp.FromString[Rule]:
if ":" not in s:
return cls(s.strip())
parts = [p.strip() for p in s.split(":")]
Expand All @@ -41,7 +41,7 @@ class BeginSystem(fp.ParsedStatement):
using_group_names: ty.Tuple[str, ...]

@classmethod
def from_string(cls, s: str, config) -> fp.FromString[BeginSystem]:
def from_string(cls, s: str) -> fp.FromString[BeginSystem]:
if not s.startswith("@system"):
return None

Expand Down

0 comments on commit 2df18b2

Please sign in to comment.