simplify variable deps

This commit is contained in:
Jesse Bannon 2023-11-21 18:40:49 -08:00
parent d10bd68ba6
commit 0fffc79cfb
5 changed files with 36 additions and 94 deletions

View file

@ -28,25 +28,11 @@ class UnresolvedArray(Array, VariableDependency, FutureResolvable):
@property @property
def variables(self) -> Set[Variable]: def variables(self) -> Set[Variable]:
variables: Set[Variable] = set() return self._variables(*self.value)
for arg in self.value:
if isinstance(arg, Variable):
variables.add(arg)
elif isinstance(arg, VariableDependency):
variables.update(arg.variables)
return variables
@property @property
def function_arguments(self) -> Set[FunctionArgument]: def function_arguments(self) -> Set[FunctionArgument]:
variables: Set[FunctionArgument] = set() return self._function_arguments(*self.value)
for arg in self.value:
if isinstance(arg, FunctionArgument):
variables.add(arg)
elif isinstance(arg, VariableDependency):
variables.update(arg.function_arguments)
return variables
def resolve( def resolve(
self, self,

View file

@ -41,35 +41,11 @@ from ytdl_sub.utils.exceptions import StringFormattingException
class Function(FunctionType, VariableDependency, ABC): class Function(FunctionType, VariableDependency, ABC):
@property @property
def variables(self) -> Set[Variable]: def variables(self) -> Set[Variable]:
""" return self._variables(*self.args)
Returns
-------
All variables used within the function
"""
variables: Set[Variable] = set()
for arg in self.args:
if isinstance(arg, Variable):
variables.add(arg)
elif isinstance(arg, VariableDependency):
variables.update(arg.variables)
return variables
@property @property
def function_arguments(self) -> Set[FunctionArgument]: def function_arguments(self) -> Set[FunctionArgument]:
""" return self._function_arguments(*self.args)
Returns
-------
All function arguments used within the function
"""
function_arguments: Set[FunctionArgument] = set()
for arg in self.args:
if isinstance(arg, FunctionArgument):
function_arguments.add(arg)
elif isinstance(arg, VariableDependency):
function_arguments.update(arg.function_arguments)
return function_arguments
@classmethod @classmethod
def from_name_and_args(cls, name: str, args: List[ArgumentType]) -> "Function": def from_name_and_args(cls, name: str, args: List[ArgumentType]) -> "Function":

View file

@ -1,5 +1,7 @@
import itertools
from dataclasses import dataclass from dataclasses import dataclass
from typing import Dict from typing import Dict
from typing import List
from typing import Set from typing import Set
from ytdl_sub.script.types.resolvable import ArgumentType from ytdl_sub.script.types.resolvable import ArgumentType
@ -27,37 +29,17 @@ class Map(NonHashable):
class UnresolvedMap(Map, VariableDependency, FutureResolvable): class UnresolvedMap(Map, VariableDependency, FutureResolvable):
value: Dict[ArgumentType, ArgumentType] value: Dict[ArgumentType, ArgumentType]
@property
def _as_flat_list(self) -> List[ArgumentType]:
return list(itertools.chain(*self.value.items()))
@property @property
def variables(self) -> Set[Variable]: def variables(self) -> Set[Variable]:
output: Set[Variable] = set() return self._variables(*self._as_flat_list)
for key, value in self.value.items():
if isinstance(key, Variable):
output.add(key)
elif isinstance(key, VariableDependency):
output.update(key.variables)
if isinstance(value, Variable):
output.add(key)
elif isinstance(value, VariableDependency):
output.update(value.variables)
return output
@property @property
def function_arguments(self) -> Set[FunctionArgument]: def function_arguments(self) -> Set[FunctionArgument]:
output: Set[FunctionArgument] = set() return self._function_arguments(*self._as_flat_list)
for key, value in self.value.items():
if isinstance(key, FunctionArgument):
output.add(key)
elif isinstance(key, VariableDependency):
output.update(key.function_arguments)
if isinstance(value, FunctionArgument):
output.add(key)
elif isinstance(value, VariableDependency):
output.update(value.function_arguments)
return output
def resolve( def resolve(
self, self,

View file

@ -19,35 +19,11 @@ class SyntaxTree(VariableDependency):
@property @property
def variables(self) -> Set[Variable]: def variables(self) -> Set[Variable]:
""" return self._variables(*self.ast)
Returns
-------
All variables used within the SyntaxTree
"""
variables: Set[Variable] = set()
for token in self.ast:
if isinstance(token, Variable):
variables.add(token)
elif isinstance(token, VariableDependency):
variables.update(token.variables)
return variables
@property @property
def function_arguments(self) -> Set[FunctionArgument]: def function_arguments(self) -> Set[FunctionArgument]:
""" return self._function_arguments(*self.ast)
Returns
-------
All function arguments used within the SyntaxTree
"""
function_arguments: Set[FunctionArgument] = set()
for token in self.ast:
if isinstance(token, FunctionArgument):
function_arguments.add(token)
elif isinstance(token, VariableDependency):
function_arguments.update(token.function_arguments)
return function_arguments
def resolve( def resolve(
self, self,

View file

@ -15,6 +15,28 @@ from ytdl_sub.utils.exceptions import StringFormattingException
@dataclass(frozen=True) @dataclass(frozen=True)
class VariableDependency(ABC): class VariableDependency(ABC):
@classmethod
def _variables(cls, *args: ArgumentType) -> Set[Variable]:
output: Set[Variable] = set()
for arg in args:
if isinstance(arg, Variable):
output.add(arg)
elif isinstance(arg, VariableDependency):
output.update(arg.variables)
return output
@classmethod
def _function_arguments(cls, *args: ArgumentType) -> Set[FunctionArgument]:
output: Set[FunctionArgument] = set()
for arg in args:
if isinstance(arg, FunctionArgument):
output.add(arg)
elif isinstance(arg, VariableDependency):
output.update(arg.function_arguments)
return output
@property @property
@abstractmethod @abstractmethod
def variables(self) -> Set[Variable]: def variables(self) -> Set[Variable]: