From 2df18b274058b4a7c39c58e102384847f39351e8 Mon Sep 17 00:00:00 2001 From: Hernan Date: Sat, 4 Jun 2022 22:26:07 -0300 Subject: [PATCH] Updated pint example to latest flexparser api --- examples/pint/parse-pint.py | 4 +--- examples/pint/pint_parser/common.py | 6 +++--- examples/pint/pint_parser/context.py | 16 +++++++++------- examples/pint/pint_parser/defaults.py | 2 +- examples/pint/pint_parser/group.py | 2 +- examples/pint/pint_parser/plain.py | 14 +++++--------- examples/pint/pint_parser/system.py | 4 ++-- 7 files changed, 22 insertions(+), 26 deletions(-) diff --git a/examples/pint/parse-pint.py b/examples/pint/parse-pint.py index f53d525..97ace6c 100644 --- a/examples/pint/parse-pint.py +++ b/examples/pint/parse-pint.py @@ -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 diff --git a/examples/pint/pint_parser/common.py b/examples/pint/pint_parser/common.py index 1e170df..3492272 100644 --- a/examples/pint/pint_parser/common.py +++ b/examples/pint/pint_parser/common.py @@ -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("=")] @@ -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()) @@ -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 diff --git a/examples/pint/pint_parser/context.py b/examples/pint/pint_parser/context.py index 9c97d33..d921870 100644 --- a/examples/pint/pint_parser/context.py +++ b/examples/pint/pint_parser/context.py @@ -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: @@ -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) @@ -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) @@ -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: @@ -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: diff --git a/examples/pint/pint_parser/defaults.py b/examples/pint/pint_parser/defaults.py index 92dddad..5d29616 100644 --- a/examples/pint/pint_parser/defaults.py +++ b/examples/pint/pint_parser/defaults.py @@ -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 diff --git a/examples/pint/pint_parser/group.py b/examples/pint/pint_parser/group.py index 6dd8fb7..3cb5dad 100644 --- a/examples/pint/pint_parser/group.py +++ b/examples/pint/pint_parser/group.py @@ -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 diff --git a/examples/pint/pint_parser/plain.py b/examples/pint/pint_parser/plain.py index a0a88ce..b2b6356 100644 --- a/examples/pint/pint_parser/plain.py +++ b/examples/pint/pint_parser/plain.py @@ -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: @@ -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: @@ -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): @@ -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): @@ -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("=") diff --git a/examples/pint/pint_parser/system.py b/examples/pint/pint_parser/system.py index 573815f..1328359 100644 --- a/examples/pint/pint_parser/system.py +++ b/examples/pint/pint_parser/system.py @@ -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(":")] @@ -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