#12978: conditionally_defined decorator for methods and other attributes
-------------------------------------------------+-------------------------
Reporter: nthiery | Owner: jason
Type: enhancement | Status: new
Priority: major | Milestone: sage-
Component: misc | wishlist
Keywords: categories, | Resolution:
conditionally_defined | Merged in:
Authors: | Reviewers:
Report Upstream: N/A | Work issues:
Branch: | Commit:
Dependencies: #15056 | Stopgaps:
-------------------------------------------------+-------------------------
Comment (by SimonKing):
I updated the "proof of concept" again. In addition to the previous
example, the implementation now plays well with the category framework.
Namely, in the following example from the doctest, the both the default
and the non-default implementation of a conditionally defined method are
defined in the element classes of categories, and the method which the
implementation depends on is defined in the category, too.
Note that one can now provide either a function or a string to the
decorator. In the latter case, it tests the instance for the presence of
an attribute of the given name.
From the doctests:
{{{
sage: from sage.misc.conditionally_defined import
conditionally_defined
sage: from sage.structure.element import Element
}}}
We define some category with some element method:
{{{
sage: class MyCategory(Category):
....: def super_categories(self):
....: return [Objects()]
....: class ElementMethods:
....: def conditional_method(self, x):
....: print "this works without helper"
....: return -x
}}}
Next, we define a sub-category in which the afore-mentioned element class
is
overloaded, provided that the element has an attribute `"helper"`:
{{{
sage: class MySubCategory(Category):
....: def super_categories(self):
....: return [MyCategory()]
....: class ElementMethods:
....: @conditionally_defined("helper")
....: def conditional_method(self, x):
....: return self.helper(x)^2
}}}
We define a third category that provides an element method called
`"helper"`:
{{{
sage: class MyHelperCategory(Category):
....: def super_categories(self):
....: return [Objects()]
....: class ElementMethods:
....: def helper(self, x):
....: print "category helper"
....: return x*2
}}}
For using a conditionally defined method, it is necessary to either be
able to
assign attributes to the instance, or there must be a public attribute
`__cached_methods` of type `<dict>`. Therefore, we create a Python version
of `sage.structure.element.Element`:
{{{
sage: class MyElement(Element): pass
}}}
We define two parents. Both are objects of our second category, which
conditionally overloads an element method. Only the parent `P2` is also an
object of the "helper category", which provides the prerequisite of
overloading the conditional element method:
{{{
sage: P1 = Parent(category=MySubCategory())
sage: P2 =
Parent(category=Category.join([MySubCategory(),MyHelperCategory()]))
}}}
We show that inheritance of a conditionally defined method from a category
even works if the constructed elements are not instances of the category's
element class:
{{{
sage: e1 = MyElement(P1)
sage: e2 = MyElement(P2)
sage: isinstance(e1, e1.parent().category().element_class)
False
sage: isinstance(e2, e2.parent().category().element_class)
False
}}}
Now, `e`` does not have a "helper" method, and thus its conditional method
relies on an implementation that does not ue the "helper". But `e2` does
have the "helper" method, and its conditional method makes use of it:
{{{
sage: e1.conditional_method(2)
this works without helper
-2
sage: e2.conditional_method(2)
category helper
16
}}}
Nicolas, do you think of further features or use cases? Otherwise, I think
I should finalise the patch by adding docs and tests to every method.
--
Ticket URL: <http://trac.sagemath.org/ticket/12978#comment:19>
Sage <http://www.sagemath.org>
Sage: Creating a Viable Open Source Alternative to Magma, Maple, Mathematica,
and MATLAB
--
You received this message because you are subscribed to the Google Groups
"sage-trac" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sage-trac.
For more options, visit https://groups.google.com/groups/opt_out.