Skip to content

Commit

Permalink
Changed abc.Adjustment comparison method;
Browse files Browse the repository at this point in the history
  • Loading branch information
trystyncote committed Jan 11, 2024
1 parent ca519e1 commit 4254090
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 45 deletions.
42 changes: 18 additions & 24 deletions scrivid/_file_objects/adjustments.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ def _increment_value(


class HideAdjustment(Adjustment):
__slots__ = ("_ID", "activation_time")
__slots__ = ("_activation_time", "_ID")

def __init__(self, ID: Hashable, activation_time: int):
self._ID = ID
self.activation_time = activation_time
self._activation_time = activation_time

def __repr__(self) -> str:
id = self._ID
Expand All @@ -47,6 +47,10 @@ def __repr__(self) -> str:
def __hash__(self):
return hash((self._ID, self._activation_time))

@property
def activation_time(self):
return self._activation_time

@property
def id(self):
return self._ID
Expand All @@ -55,18 +59,12 @@ def id(self):
def ID(self):
return self._ID

def _compare(self, other, operation) -> bool:
if not hasattr(other, "activation_time"):
return NotImplemented
else:
return operation(self.activation_time, other.activation_time)

def _enact(self) -> Properties:
return Properties(visibility=VisibilityStatus.HIDE)


class MoveAdjustment(Adjustment):
__slots__ = ("_change", "_ID", "activation_time", "duration")
__slots__ = ("_activation_time", "_change", "_ID", "duration")

def __init__(
self,
Expand All @@ -78,7 +76,7 @@ def __init__(
self._change = change
self.duration = duration
self._ID = ID
self.activation_time = activation_time
self._activation_time = activation_time

def __repr__(self):
id = self._ID
Expand All @@ -88,6 +86,10 @@ def __repr__(self):

return f"{self.__class__.__name__}({id=!r}, {activation_time=!r}, {change=!r}, {duration=!r})"

@property
def activation_time(self):
return self._activation_time

@property
def change(self):
return self._change
Expand All @@ -100,12 +102,6 @@ def id(self):
def ID(self):
return self._ID

def _compare(self, other, operation) -> bool:
if not hasattr(other, "activation_time"):
return NotImplemented
else:
return operation(self.activation_time, other.activation_time)

def _enact(self, length: int) -> Properties:
if self.duration == 1 or self.duration == length:
return self._change
Expand All @@ -126,11 +122,11 @@ def _split_change(self, length: int = 1) -> Properties:


class ShowAdjustment(Adjustment):
__slots__ = ("_ID", "activation_time")
__slots__ = ("_activation_time", "_ID")

def __init__(self, ID: Hashable, activation_time: int):
self._ID = ID
self.activation_time = activation_time
self._activation_time = activation_time

def __repr__(self) -> str:
id = self._ID
Expand All @@ -141,6 +137,10 @@ def __repr__(self) -> str:
def __hash__(self):
return hash((self._ID, self._activation_time))

@property
def activation_time(self):
return self._activation_time

@property
def id(self):
return self._ID
Expand All @@ -149,11 +149,5 @@ def id(self):
def ID(self):
return self._ID

def _compare(self, other, operation) -> bool:
if not hasattr(other, "activation_time"):
return NotImplemented
else:
return operation(self.activation_time, other.activation_time)

def _enact(self) -> Properties:
return Properties(visibility=VisibilityStatus.SHOW)
38 changes: 17 additions & 21 deletions scrivid/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,42 +20,38 @@ class Adjustment(ABC):
activation_time: int

def __eq__(self, other) -> bool:
return self._gatekeep_comparison(other, operator.eq)
return self._comparison(other, operator.eq)

def __ge__(self, other) -> bool:
return self._gatekeep_comparison(other, operator.ge)
return self._comparison(other, operator.ge)

def __gt__(self, other) -> bool:
return self._gatekeep_comparison(other, operator.gt)
return self._comparison(other, operator.gt)

def __le__(self, other) -> bool:
return self._gatekeep_comparison(other, operator.le)
return self._comparison(other, operator.le)

def __lt__(self, other) -> bool:
return self._gatekeep_comparison(other, operator.lt)
return self._comparison(other, operator.lt)

def __ne__(self, other) -> bool:
return self._gatekeep_comparison(other, operator.ne)
return self._comparison(other, operator.ne)

def _gatekeep_comparison(self, other, operation):
def __init_subclass__(cls):
if not isinstance(getattr(cls, "activation_time", None), property):
raise errors.TypeError(
"abc.Adjustment subclass method `activation_time` must be implemented as a property."
)

def _comparison(self, other, operation):
if not isinstance(other, Adjustment):
raise errors.TypeError(f"Expected type Adjustment, got type \'{other.__class__.__name__}\'")
else:
return operation(self.activation_time, other.activation_time)

self_result = self._compare(other, operation)
if self_result is not NotImplemented:
return self_result

other_result = other._compare(self, operation)
if other_result is not NotImplemented:
return other_result

raise errors.TypeError(textwrap.dedent(f"""
Implementations of `._compare` {self.__class__.__name__} and {other.__class__.__name__} were deemed
incompatible. (Both results returned NotImplemented.)
""").replace("\n", ""))

@property
@abstractmethod
def _compare(self, other, operation) -> bool:
def activation_time(self):
raise NotImplementedError

@abstractmethod
Expand Down
97 changes: 97 additions & 0 deletions tests/test_abc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import scrivid

import pytest


def test_defined_adjustment():
class Mock(scrivid.abc.Adjustment):
@property
def activation_time(self):
return None

def _enact(self):
return None

Mock()


def test_defined_qualm():
class Mock(scrivid.abc.Qualm):
def _comparison(self):
return None

def _message(self):
return None

@classmethod
def check(cls):
return None

Mock()


def test_undefined_adjustment_activation_time_property():
class Mock(scrivid.abc.Adjustment):
def _enact(self):
return None

with pytest.raises(TypeError):
Mock()


def test_undefined_adjustment_activation_time_not_property():
with pytest.raises(scrivid.errors.TypeError):
class _(scrivid.abc.Adjustment):
def activation_time(self):
return None

def _enact(self):
return None


def test_undefined_adjustment_enact():
class Mock(scrivid.abc.Adjustment):
@property
def activation_time(self):
return None

with pytest.raises(TypeError):
Mock()


def test_undefined_qualm_check():
class Mock(scrivid.abc.Qualm):
def _comparison(self):
return None

def _message(self):
return None

with pytest.raises(TypeError):
Mock()


def test_undefined_qualm_comparison():
class Mock(scrivid.abc.Qualm):
def _message(self):
return None

@classmethod
def check(cls):
return None

with pytest.raises(TypeError):
Mock()


def test_undefined_qualm_message():
class Mock(scrivid.abc.Qualm):
def _comparison(self):
return None

@classmethod
def check(cls):
return None

with pytest.raises(TypeError):
Mock()

0 comments on commit 4254090

Please sign in to comment.