docs docs docs

This commit is contained in:
Jesse Bannon 2023-11-20 23:12:43 -08:00
parent 8c6c4ebc75
commit 5f049fbfb4
8 changed files with 81 additions and 38 deletions

View file

@ -8,6 +8,9 @@ from ytdl_sub.script.types.resolvable import Resolvable
class ArrayFunctions: class ArrayFunctions:
@staticmethod @staticmethod
def array_extend(*arrays: Array) -> Array: def array_extend(*arrays: Array) -> Array:
"""
Combine multiple Arrays into a single Array.
"""
output: List[Resolvable] = [] output: List[Resolvable] = []
for array in arrays: for array in arrays:
output.extend(array.value) output.extend(array.value)
@ -16,10 +19,16 @@ class ArrayFunctions:
@staticmethod @staticmethod
def array_at(array: Array, idx: Integer) -> Resolvable: def array_at(array: Array, idx: Integer) -> Resolvable:
"""
Return the element in the Array at index ``idx``.
"""
return array.value[idx.value] return array.value[idx.value]
@staticmethod @staticmethod
def array_flatten(array: Array) -> Array: def array_flatten(array: Array) -> Array:
"""
Flatten any nested Arrays into a single-dimensional Array.
"""
output: List[Resolvable] = [] output: List[Resolvable] = []
for elem in array.value: for elem in array.value:
if isinstance(elem, Array): if isinstance(elem, Array):
@ -31,4 +40,7 @@ class ArrayFunctions:
@staticmethod @staticmethod
def array_reverse(array: Array) -> Array: def array_reverse(array: Array) -> Array:
"""
Reverse an Array.
"""
return Array(list(reversed(array.value))) return Array(list(reversed(array.value)))

View file

@ -1,6 +1,8 @@
from ytdl_sub.script.types.resolvable import AnyType from ytdl_sub.script.types.resolvable import AnyType
from ytdl_sub.script.types.resolvable import Boolean from ytdl_sub.script.types.resolvable import Boolean
# pylint: disable=invalid-name
class BooleanFunctions: class BooleanFunctions:
""" """

View file

@ -10,6 +10,10 @@ class ConditionalFunctions:
def if_( def if_(
condition: Boolean, true: AnyTypeReturnableA, false: AnyTypeReturnableB condition: Boolean, true: AnyTypeReturnableA, false: AnyTypeReturnableB
) -> Union[AnyTypeReturnableA, AnyTypeReturnableB]: ) -> Union[AnyTypeReturnableA, AnyTypeReturnableB]:
"""
Conditional ``if`` statement that returns the ``true`` or ``false`` parameter
depending on the ``condition`` value.
"""
if condition.value: if condition.value:
return true return true
return false return false

View file

@ -7,10 +7,16 @@ from ytdl_sub.script.utils.exceptions import UserThrownRuntimeError
class ErrorFunctions: class ErrorFunctions:
@staticmethod @staticmethod
def throw(error_message: String) -> AnyType: def throw(error_message: String) -> AnyType:
"""
Explicitly throw an error with the provided error message.
"""
raise UserThrownRuntimeError(error_message) raise UserThrownRuntimeError(error_message)
@staticmethod @staticmethod
def assert_(condition: Boolean, assert_message: String) -> Boolean: def assert_(condition: Boolean, assert_message: String) -> Boolean:
"""
Explicitly throw an error with the provided assert message if ``condition`` is False.
"""
if not condition.value: if not condition.value:
raise UserThrownRuntimeError(assert_message) raise UserThrownRuntimeError(assert_message)
return condition return condition

View file

@ -1,33 +1,17 @@
from typing import Dict
from typing import Optional from typing import Optional
from ytdl_sub.script.types.array import Array
from ytdl_sub.script.types.map import Map from ytdl_sub.script.types.map import Map
from ytdl_sub.script.types.resolvable import AnyType from ytdl_sub.script.types.resolvable import AnyType
from ytdl_sub.script.types.resolvable import Hashable from ytdl_sub.script.types.resolvable import Hashable
from ytdl_sub.script.types.resolvable import Resolvable
from ytdl_sub.utils.exceptions import StringFormattingException
class MapFunctions: class MapFunctions:
@staticmethod
def map(*key_values: Array) -> Map:
output: Dict[Resolvable, Resolvable] = {}
for key_value in key_values:
if len(key_value.value) != 2:
raise StringFormattingException(
"%map must take Arrays containing pairs of keys and values"
)
output[key_value.value[0]] = key_value.value[1]
return Map(output)
@staticmethod @staticmethod
def map_get(mapping: Map, key: Hashable, default: Optional[AnyType] = None) -> AnyType: def map_get(mapping: Map, key: Hashable, default: Optional[AnyType] = None) -> AnyType:
if key not in mapping.value: """
if default is not None: Return ``key``'s value within the Map. If ``key`` does not exist, and ``default`` is
return default provided, it will return ``default``. Otherwise, will error.
raise StringFormattingException("key not found") """
if default is not None:
return mapping.value.get(key, default=default)
return mapping.value[key] return mapping.value[key]

View file

@ -1,6 +1,5 @@
from typing import Union from typing import Union
from ytdl_sub.script.types.resolvable import AnyType
from ytdl_sub.script.types.resolvable import Boolean from ytdl_sub.script.types.resolvable import Boolean
from ytdl_sub.script.types.resolvable import Float from ytdl_sub.script.types.resolvable import Float
from ytdl_sub.script.types.resolvable import Integer from ytdl_sub.script.types.resolvable import Integer
@ -17,36 +16,63 @@ def _to_numeric(value: int | float) -> Numeric:
class NumericFunctions: class NumericFunctions:
@staticmethod @staticmethod
def float(value: Union[Float, Integer, Boolean, String]) -> Float: def float(value: Union[Float, Integer, Boolean, String]) -> Float:
"""
Cast to Float.
"""
return Float(value=float(value.value)) return Float(value=float(value.value))
@staticmethod @staticmethod
def int(value: Union[Float, Integer, Boolean, String]) -> Integer: def int(value: Union[Float, Integer, Boolean, String]) -> Integer:
"""
Cast to Integer.
"""
return Integer(value=int(value.value)) return Integer(value=int(value.value))
@staticmethod @staticmethod
def add(left: Numeric, right: Numeric) -> Numeric: def add(left: Numeric, right: Numeric) -> Numeric:
"""
``+`` operator. Returns ``left + right``.
"""
return _to_numeric(left.value + right.value) return _to_numeric(left.value + right.value)
@staticmethod @staticmethod
def sub(left: Numeric, right: Numeric) -> Numeric: def sub(left: Numeric, right: Numeric) -> Numeric:
"""
``-`` operator. Returns ``left - right``.
"""
return _to_numeric(left.value - right.value) return _to_numeric(left.value - right.value)
@staticmethod @staticmethod
def mul(left: Numeric, right: Numeric) -> Numeric: def mul(left: Numeric, right: Numeric) -> Numeric:
"""
``*`` operator. Returns ``left * right``.
"""
return _to_numeric(left.value * right.value) return _to_numeric(left.value * right.value)
@staticmethod @staticmethod
def div(left: Numeric, right: Numeric) -> Numeric: def div(left: Numeric, right: Numeric) -> Numeric:
"""
``/`` operator. Returns ``left / right``.
"""
return _to_numeric(left.value / right.value) return _to_numeric(left.value / right.value)
@staticmethod @staticmethod
def mod(value: Numeric, modulo: Numeric) -> Numeric: def mod(left: Numeric, right: Numeric) -> Numeric:
return _to_numeric(value=value.value % modulo.value) """
``%`` operator. Returns ``left % right``.
"""
return _to_numeric(value=left.value % right.value)
@staticmethod @staticmethod
def max(left: Numeric, right: Numeric) -> Numeric: def max(*values: Numeric) -> Numeric:
return _to_numeric(max(left.value, right.value)) """
Returns max of all values.
"""
return _to_numeric(max(val.value for val in values))
@staticmethod @staticmethod
def min(left: Numeric, right: Numeric) -> Numeric: def min(*values: Numeric) -> Numeric:
return _to_numeric(min(left.value, right.value)) """
Returns min of all values.
"""
return _to_numeric(min(val.value for val in values))

View file

@ -8,32 +8,29 @@ from ytdl_sub.script.types.resolvable import String
class StringFunctions: class StringFunctions:
@staticmethod @staticmethod
def string(value: AnyType) -> String: def string(value: AnyType) -> String:
"""
Cast to String.
"""
return String(value=str(value.value)) return String(value=str(value.value))
@staticmethod @staticmethod
def lower(string: String) -> String: def lower(string: String) -> String:
""" """
Returns Lower-case the entire String.
-------
Lower-cased string
""" """
return String(string.value.lower()) return String(string.value.lower())
@staticmethod @staticmethod
def upper(string: String) -> String: def upper(string: String) -> String:
""" """
Returns Upper-case the entire String.
-------
Upper-cased string
""" """
return String(string.value.upper()) return String(string.value.upper())
@staticmethod @staticmethod
def capitalize(string: String) -> String: def capitalize(string: String) -> String:
""" """
Returns Capitalize all words in the String.
-------
Capitalized string
""" """
return String(string.value.capitalize()) return String(string.value.capitalize())
@ -41,6 +38,10 @@ class StringFunctions:
def replace( def replace(
string: String, old: String, new: String, count: Optional[Integer] = None string: String, old: String, new: String, count: Optional[Integer] = None
) -> String: ) -> String:
"""
Replace the ``old`` part of the String with the ``new``. Optionally only replace it
``count`` number of times.
"""
if count: if count:
return String(string.value.replace(old.value, new.value, count.value)) return String(string.value.replace(old.value, new.value, count.value))
@ -48,4 +49,7 @@ class StringFunctions:
@staticmethod @staticmethod
def concat(*args: String) -> String: def concat(*args: String) -> String:
"""
Concatenate multiple Strings into a single String.
"""
return String("".join(*args)) return String("".join(*args))

View file

@ -15,6 +15,11 @@ NumericT = TypeVar("NumericT", bound=int | float)
class NamedType(ABC): class NamedType(ABC):
@classmethod @classmethod
def type_name(cls) -> str: def type_name(cls) -> str:
"""
Returns
-------
The type name to present to users. Defaults to the class name.
"""
return cls.__name__ return cls.__name__