Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use decorator form of classmethod #500

Merged
merged 1 commit into from
Jul 9, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 18 additions & 36 deletions traits/has_traits.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,7 @@ def __new__(cls, class_name, bases, class_dict):

return klass

@classmethod
def add_listener(cls, listener, class_name=""):
""" Adds a class creation listener.

Expand All @@ -511,15 +512,12 @@ def add_listener(cls, listener, class_name=""):
"""
MetaHasTraits._listeners.setdefault(class_name, []).append(listener)

add_listener = classmethod(add_listener)

@classmethod
def remove_listener(cls, listener, class_name=""):
""" Removes a class creation listener.
"""
MetaHasTraits._listeners[class_name].remove(listener)

remove_listener = classmethod(remove_listener)


def update_traits_class_dict(class_name, bases, class_dict, is_category):
""" Processes all of the traits related data in the class dictionary.
Expand Down Expand Up @@ -1174,6 +1172,7 @@ def _trait_added_changed(self, name):
# Adds/Removes a trait instance creation monitor:
# ---------------------------------------------------------------------------

@classmethod
def trait_monitor(cls, handler, remove=False):
"""Adds or removes the specified *handler* from the list of active
monitors.
Expand Down Expand Up @@ -1204,12 +1203,11 @@ def trait_monitor(cls, handler, remove=False):
if index < 0:
_HasTraits_monitors.append((cls, handler))

trait_monitor = classmethod(trait_monitor)

# ---------------------------------------------------------------------------
# Add a new class trait (i.e. applies to all instances and subclasses):
# ---------------------------------------------------------------------------

@classmethod
def add_class_trait(cls, name, *trait):
""" Adds a named trait attribute to this class.

Expand Down Expand Up @@ -1241,8 +1239,7 @@ def add_class_trait(cls, name, *trait):
for subclass in cls.trait_subclasses(True):
subclass._add_class_trait(name, trait, True)

add_class_trait = classmethod(add_class_trait)

@classmethod
def _add_class_trait(cls, name, trait, is_subclass):
# Get a reference to the class's dictionary and 'prefix' traits:
class_dict = cls.__dict__
Expand Down Expand Up @@ -1309,12 +1306,11 @@ def _add_class_trait(cls, name, trait, is_subclass):
# Finally, add the new trait to the class trait dictionary:
class_traits[name] = trait

_add_class_trait = classmethod(_add_class_trait)

# ---------------------------------------------------------------------------
# Adds a 'category' to the class:
# ---------------------------------------------------------------------------

@classmethod
def add_trait_category(cls, category):
""" Adds a trait category to a class.
"""
Expand All @@ -1334,12 +1330,11 @@ def add_trait_category(cls, category):
if not hasattr(cls, name):
setattr(cls, name, value)

add_trait_category = classmethod(add_trait_category)

# ---------------------------------------------------------------------------
# Adds a 'category' to the class:
# ---------------------------------------------------------------------------

@classmethod
def _add_trait_category(
cls,
base_traits,
Expand Down Expand Up @@ -1404,12 +1399,11 @@ def _add_trait_category(
for name, value in content.items():
base_ve_content.setdefault(name, value)

_add_trait_category = classmethod(_add_trait_category)

# ---------------------------------------------------------------------------
# Sets a trait notification dispatch handler:
# ---------------------------------------------------------------------------

@classmethod
def set_trait_dispatch_handler(cls, name, klass, override=False):
""" Sets a trait notification dispatch handler.
"""
Expand All @@ -1428,12 +1422,11 @@ def set_trait_dispatch_handler(cls, name, klass, override=False):
"%s is not a subclass of TraitChangeNotifyWrapper." % klass
)

set_trait_dispatch_handler = classmethod(set_trait_dispatch_handler)

# ---------------------------------------------------------------------------
# Returns the immediate (or all) subclasses of this class:
# ---------------------------------------------------------------------------

@classmethod
def trait_subclasses(cls, all=False):
""" Returns a list of the immediate (or all) subclasses of this class.

Expand All @@ -1448,17 +1441,14 @@ def trait_subclasses(cls, all=False):
return cls.__subclasses__()
return cls._trait_subclasses([])

trait_subclasses = classmethod(trait_subclasses)

@classmethod
def _trait_subclasses(cls, subclasses):
for subclass in cls.__subclasses__():
if subclass not in subclasses:
subclasses.append(subclass)
subclass._trait_subclasses(subclasses)
return subclasses

_trait_subclasses = classmethod(_trait_subclasses)

# ---------------------------------------------------------------------------
# Returns whether the object implements a specified traits interface:
# ---------------------------------------------------------------------------
Expand Down Expand Up @@ -2085,6 +2075,7 @@ def trait_view(self, name=None, view_element=None):
self,
)

@classmethod
def class_trait_view(cls, name=None, view_element=None):
return cls._trait_view(
name,
Expand All @@ -2095,12 +2086,11 @@ def class_trait_view(cls, name=None, view_element=None):
None,
)

class_trait_view = classmethod(class_trait_view)

# ---------------------------------------------------------------------------
# Gets or sets a ViewElement associated with an object's class:
# ---------------------------------------------------------------------------

@classmethod
def _trait_view(
cls,
name,
Expand Down Expand Up @@ -2173,8 +2163,6 @@ def _trait_view(

return View(trait_selector_f(), buttons=["OK", "Cancel"])

_trait_view = classmethod(_trait_view)

# ---------------------------------------------------------------------------
# Return the default traits view/name:
# ---------------------------------------------------------------------------
Expand All @@ -2188,13 +2176,12 @@ def default_traits_view(self):
# Return the default traits view/name:
# ---------------------------------------------------------------------------

@classmethod
def class_default_traits_view(cls):
""" Returns the name of the default traits view for the class.
"""
return DefaultTraitsView

class_default_traits_view = classmethod(class_default_traits_view)

# ---------------------------------------------------------------------------
# Gets the list of names of ViewElements associated with the object's
# class that are of a specified ViewElement type:
Expand Down Expand Up @@ -2236,6 +2223,7 @@ def trait_view_elements(self):
"""
return self.__class__.class_trait_view_elements()

@classmethod
def class_trait_view_elements(cls):
""" Returns the ViewElements object associated with the class.

Expand All @@ -2244,8 +2232,6 @@ def class_trait_view_elements(cls):
"""
return cls.__dict__[ViewTraits]

class_trait_view_elements = classmethod(class_trait_view_elements)

# ---------------------------------------------------------------------------
# Configure the object's traits:
# ---------------------------------------------------------------------------
Expand Down Expand Up @@ -2370,6 +2356,7 @@ def editable_traits(self):
names.sort()
return names

@classmethod
def class_editable_traits(cls):
"""Returns an alphabetically sorted list of the names of non-event
trait attributes associated with the current class.
Expand All @@ -2378,22 +2365,19 @@ def class_editable_traits(cls):
names.sort()
return names

class_editable_traits = classmethod(class_editable_traits)

def visible_traits(self):
"""Returns an alphabetically sorted list of the names of non-event
trait attributes associated with the current object, that should be GUI visible
"""
return self.trait_names(type=not_event, visible=not_false)

@classmethod
def class_visible_traits(cls):
"""Returns an alphabetically sorted list of the names of non-event
trait attributes associated with the current class, that should be GUI visible
"""
return cls.class_trait_names(type=not_event, visible=not_false)

class_visible_traits = classmethod(class_visible_traits)

# ---------------------------------------------------------------------------
# Pretty print the traits of an object:
# ---------------------------------------------------------------------------
Expand Down Expand Up @@ -3238,6 +3222,7 @@ def traits(self, **metadata):
# Return a dictionary of all traits which match a set of metadata:
# ---------------------------------------------------------------------------

@classmethod
def class_traits(cls, **metadata):
"""Returns a dictionary containing the definitions of all of the trait
attributes of the class that match the set of *metadata* criteria.
Expand Down Expand Up @@ -3288,8 +3273,6 @@ def class_traits(cls, **metadata):

return result

class_traits = classmethod(class_traits)

# ---------------------------------------------------------------------------
# Return a list of all trait names which match a set of metadata:
# ---------------------------------------------------------------------------
Expand All @@ -3310,6 +3293,7 @@ def trait_names(self, **metadata):
"""
return list(self.traits(**metadata).keys())

@classmethod
def class_trait_names(cls, **metadata):
"""Returns a list of the names of all trait attributes whose definitions
match the set of *metadata* criteria specified.
Expand All @@ -3326,8 +3310,6 @@ def class_trait_names(cls, **metadata):
"""
return list(cls.class_traits(**metadata).keys())

class_trait_names = classmethod(class_trait_names)

# ---------------------------------------------------------------------------
# Explicitly sets the value of a cached property:
# ---------------------------------------------------------------------------
Expand Down