From 6c1ce8b8202fbb3be965efc3ddb4593b2f6051ec Mon Sep 17 00:00:00 2001 From: Jesse Bannon Date: Wed, 20 Sep 2023 13:45:21 -0700 Subject: [PATCH] organize --- src/ytdl_sub/script/functions/__init__.py | 5 ++ .../string_functions.py} | 45 +---------------- src/ytdl_sub/script/parser.py | 8 ++-- src/ytdl_sub/script/syntax_tree.py | 10 ++-- src/ytdl_sub/script/types.py | 48 +++++++++++++++++++ tests/unit/script/test_parser.py | 8 ++-- 6 files changed, 67 insertions(+), 57 deletions(-) create mode 100644 src/ytdl_sub/script/functions/__init__.py rename src/ytdl_sub/script/{functions.py => functions/string_functions.py} (50%) create mode 100644 src/ytdl_sub/script/types.py diff --git a/src/ytdl_sub/script/functions/__init__.py b/src/ytdl_sub/script/functions/__init__.py new file mode 100644 index 00000000..9b48ab2a --- /dev/null +++ b/src/ytdl_sub/script/functions/__init__.py @@ -0,0 +1,5 @@ +from ytdl_sub.script.functions.string_functions import StringFunctions + + +class Functions(StringFunctions): + pass diff --git a/src/ytdl_sub/script/functions.py b/src/ytdl_sub/script/functions/string_functions.py similarity index 50% rename from src/ytdl_sub/script/functions.py rename to src/ytdl_sub/script/functions/string_functions.py index 4eb64f44..0fbda658 100644 --- a/src/ytdl_sub/script/functions.py +++ b/src/ytdl_sub/script/functions/string_functions.py @@ -1,43 +1,4 @@ -from abc import ABC -from dataclasses import dataclass -from typing import Generic -from typing import TypeVar - -T = TypeVar("T") -NumericT = TypeVar("NumericT", bound=int | float) - - -@dataclass(frozen=True) -class Resolvable(ABC, Generic[T]): - value: T - - def resolve(self) -> str: - return str(self.value) - - -@dataclass(frozen=True) -class Numeric(Resolvable[NumericT], ABC, Generic[NumericT]): - pass - - -@dataclass(frozen=True) -class Integer(Resolvable[int]): - pass - - -@dataclass(frozen=True) -class Float(Resolvable[float]): - pass - - -@dataclass(frozen=True) -class Boolean(Resolvable[bool]): - pass - - -@dataclass(frozen=True) -class String(Resolvable[str]): - pass +from ytdl_sub.script.types import String class StringFunctions: @@ -71,7 +32,3 @@ class StringFunctions: @staticmethod def concat(l_string: String, r_string: String) -> String: return String(f"{l_string}{r_string}") - - -class Functions(StringFunctions): - pass diff --git a/src/ytdl_sub/script/parser.py b/src/ytdl_sub/script/parser.py index dfda3843..1c55ef08 100644 --- a/src/ytdl_sub/script/parser.py +++ b/src/ytdl_sub/script/parser.py @@ -1,14 +1,14 @@ from typing import List from typing import Optional -from ytdl_sub.script.functions import Boolean -from ytdl_sub.script.functions import Float -from ytdl_sub.script.functions import Integer -from ytdl_sub.script.functions import String from ytdl_sub.script.syntax_tree import ArgumentType from ytdl_sub.script.syntax_tree import Function from ytdl_sub.script.syntax_tree import SyntaxTree from ytdl_sub.script.syntax_tree import Variable +from ytdl_sub.script.types import Boolean +from ytdl_sub.script.types import Float +from ytdl_sub.script.types import Integer +from ytdl_sub.script.types import String from ytdl_sub.utils.exceptions import StringFormattingException from ytdl_sub.validators.string_formatter_validators import is_valid_source_variable_name diff --git a/src/ytdl_sub/script/syntax_tree.py b/src/ytdl_sub/script/syntax_tree.py index fe5fd469..4a086dbb 100644 --- a/src/ytdl_sub/script/syntax_tree.py +++ b/src/ytdl_sub/script/syntax_tree.py @@ -8,12 +8,12 @@ from typing import Set from typing import Union from typing import final -from ytdl_sub.script.functions import Boolean -from ytdl_sub.script.functions import Float from ytdl_sub.script.functions import Functions -from ytdl_sub.script.functions import Integer -from ytdl_sub.script.functions import Resolvable -from ytdl_sub.script.functions import String +from ytdl_sub.script.types import Boolean +from ytdl_sub.script.types import Float +from ytdl_sub.script.types import Integer +from ytdl_sub.script.types import Resolvable +from ytdl_sub.script.types import String from ytdl_sub.utils.exceptions import StringFormattingException diff --git a/src/ytdl_sub/script/types.py b/src/ytdl_sub/script/types.py new file mode 100644 index 00000000..5bb41edf --- /dev/null +++ b/src/ytdl_sub/script/types.py @@ -0,0 +1,48 @@ +from abc import ABC +from abc import abstractmethod +from dataclasses import dataclass +from typing import Generic +from typing import TypeVar + +T = TypeVar("T") +NumericT = TypeVar("NumericT", bound=int | float) + + +@dataclass(frozen=True) +class Resolvable(ABC): + @abstractmethod + def resolve(self) -> str: + ... + + +@dataclass(frozen=True) +class ResolvableT(Resolvable, Generic[T]): + value: T + + def resolve(self) -> str: + return str(self.value) + + +@dataclass(frozen=True) +class Numeric(ResolvableT[NumericT], ABC, Generic[NumericT]): + pass + + +@dataclass(frozen=True) +class Integer(ResolvableT[int]): + pass + + +@dataclass(frozen=True) +class Float(ResolvableT[float]): + pass + + +@dataclass(frozen=True) +class Boolean(ResolvableT[bool]): + pass + + +@dataclass(frozen=True) +class String(ResolvableT[str]): + pass diff --git a/tests/unit/script/test_parser.py b/tests/unit/script/test_parser.py index b4fbb504..6c6f010f 100644 --- a/tests/unit/script/test_parser.py +++ b/tests/unit/script/test_parser.py @@ -1,13 +1,13 @@ import pytest -from ytdl_sub.script.functions import Boolean -from ytdl_sub.script.functions import Float -from ytdl_sub.script.functions import Integer -from ytdl_sub.script.functions import String from ytdl_sub.script.parser import parse from ytdl_sub.script.syntax_tree import Function from ytdl_sub.script.syntax_tree import SyntaxTree from ytdl_sub.script.syntax_tree import Variable +from ytdl_sub.script.types import Boolean +from ytdl_sub.script.types import Float +from ytdl_sub.script.types import Integer +from ytdl_sub.script.types import String from ytdl_sub.utils.exceptions import StringFormattingException