Skip to content

Commit

Permalink
Merge pull request #629 from wenh06/master
Browse files Browse the repository at this point in the history
Add ReprMixin for more easy enhancement for the __repr__ and __str__ methods of a class
  • Loading branch information
jxmorris12 committed May 25, 2022
2 parents 07abad8 + b8a81fa commit b433fcb
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 33 deletions.
6 changes: 2 additions & 4 deletions textattack/constraints/constraint.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
from abc import ABC, abstractmethod

import textattack
from textattack.shared.utils import default_class_repr
from textattack.shared.utils import ReprMixin


class Constraint(ABC):
class Constraint(ReprMixin, ABC):
"""An abstract class that represents constraints on adversial text
examples. Constraints evaluate whether transformations from a
``AttackedText`` to another ``AttackedText`` meet certain conditions.
Expand Down Expand Up @@ -123,5 +123,3 @@ def extra_repr_keys(self):
line strings are acceptable.
"""
return ["compare_against_original"]

__str__ = __repr__ = default_class_repr
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
"""
from textattack.constraints import PreTransformationConstraint

# from textattack.shared.utils import default_class_repr


class MaxWordIndexModification(PreTransformationConstraint):
"""A constraint disallowing the modification of words which are past some
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

from textattack.constraints import PreTransformationConstraint

# from textattack.shared.utils import default_class_repr


class RepeatModification(PreTransformationConstraint):
"""A constraint disallowing the modification of words which have already
Expand Down
6 changes: 2 additions & 4 deletions textattack/constraints/pre_transformation_constraint.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

from abc import ABC, abstractmethod

from textattack.shared.utils import default_class_repr
from textattack.shared.utils import ReprMixin


class PreTransformationConstraint(ABC):
class PreTransformationConstraint(ReprMixin, ABC):
"""An abstract class that represents constraints which are applied before
the transformation.
Expand Down Expand Up @@ -62,5 +62,3 @@ def extra_repr_keys(self):
line strings are acceptable.
"""
return []

__str__ = __repr__ = default_class_repr
6 changes: 2 additions & 4 deletions textattack/goal_functions/goal_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
GoalFunctionResultStatus,
)
from textattack.shared import validators
from textattack.shared.utils import default_class_repr
from textattack.shared.utils import ReprMixin


class GoalFunction(ABC):
class GoalFunction(ReprMixin, ABC):
"""Evaluates how well a perturbed attacked_text object is achieving a
specified goal.
Expand Down Expand Up @@ -237,5 +237,3 @@ def __setstate__(self, state):
self.__dict__ = state
if self.use_cache:
self._call_model_cache = lru.LRU(state["_call_model_cache"])

__repr__ = __str__ = default_class_repr
9 changes: 2 additions & 7 deletions textattack/search_methods/search_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

from abc import ABC, abstractmethod

from textattack.shared.utils import default_class_repr
from textattack.shared.utils import ReprMixin


class SearchMethod(ABC):
class SearchMethod(ReprMixin, ABC):
"""This is an abstract class that contains main helper functionality for
search methods.
Expand Down Expand Up @@ -65,8 +65,3 @@ def get_victim_model(self):
)
else:
return self.goal_function.model

def extra_repr_keys(self):
return []

__repr__ = __str__ = default_class_repr
13 changes: 13 additions & 0 deletions textattack/shared/utils/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,19 @@ def default_class_repr(self):
return f"{self.__class__.__name__}{extra_str}"


class ReprMixin(object):
"""Mixin for enhanced __repr__ and __str__."""

def __repr__(self):
return default_class_repr(self)

__str__ = __repr__

def extra_repr_keys(self):
"""extra fields to be included in the representation of a class"""
return []


LABEL_COLORS = [
"red",
"green",
Expand Down
4 changes: 1 addition & 3 deletions textattack/shared/word_embeddings.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from textattack.shared import utils


class AbstractWordEmbedding(ABC):
class AbstractWordEmbedding(utils.ReprMixin, ABC):
"""Abstract class representing word embedding used by TextAttack.
This class specifies all the methods that is required to be defined
Expand Down Expand Up @@ -97,8 +97,6 @@ def nearest_neighbours(self, index, topn):
"""
raise NotImplementedError()

__repr__ = __str__ = utils.default_class_repr


class WordEmbedding(AbstractWordEmbedding):
"""Object for loading word embeddings and related distances for TextAttack.
Expand Down
9 changes: 2 additions & 7 deletions textattack/transformations/transformation.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

from abc import ABC, abstractmethod

from textattack.shared.utils import default_class_repr
from textattack.shared.utils import ReprMixin


class Transformation(ABC):
class Transformation(ReprMixin, ABC):
"""An abstract class for transforming a sequence of text to produce a
potential adversarial example."""

Expand Down Expand Up @@ -67,8 +67,3 @@ def _get_transformations(self, current_text, indices_to_modify):
@property
def deterministic(self):
return True

def extra_repr_keys(self):
return []

__repr__ = __str__ = default_class_repr

0 comments on commit b433fcb

Please sign in to comment.