Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Replace sage_wraps by decorator library
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasdiez committed Nov 10, 2020
1 parent 2220595 commit b414b8e
Show file tree
Hide file tree
Showing 16 changed files with 333 additions and 622 deletions.
24 changes: 10 additions & 14 deletions src/sage/graphs/tutte_polynomial.py
Expand Up @@ -36,7 +36,7 @@
from sage.misc.lazy_attribute import lazy_attribute
from sage.misc.misc_c import prod
from sage.rings.integer_ring import ZZ
from sage.misc.decorators import sage_wraps
from decorator import decorator

######################
# Graph Modification #
Expand Down Expand Up @@ -482,8 +482,8 @@ def _cache_key(G):
"""
return tuple(G.canonical_label().edges(labels=False, sort=True))


def _cached(func):
@decorator
def _cached(func, G, *args, **kwds):
"""
Wrapper used to cache results of the function `func`
Expand All @@ -497,17 +497,13 @@ def _cached(func):
sage: tutte_polynomial(G)(1,1) #indirect doctest
2000
"""
@sage_wraps(func)
def wrapper(G, *args, **kwds):
cache = kwds.setdefault('cache', {})
key = _cache_key(G)
if key in cache:
return cache[key]
result = func(G, *args, **kwds)
cache[key] = result
return result
wrapper.original_func = func
return wrapper
cache = kwds.setdefault('cache', {})
key = _cache_key(G)
if key in cache:
return cache[key]
result = func(G, *args, **kwds)
cache[key] = result
return result

####################
# Tutte Polynomial #
Expand Down
11 changes: 5 additions & 6 deletions src/sage/interacts/library.py
Expand Up @@ -36,6 +36,7 @@
#*****************************************************************************

from __future__ import absolute_import, division
from decorator import decorater

from sage.all import *
x = SR.var('x')
Expand All @@ -57,7 +58,8 @@
globals()[name] = obj


def library_interact(f):
@decorater
def library_interact(f, *args, **kw):
"""
This is a decorator for using interacts in the Sage library.
Expand All @@ -75,11 +77,8 @@ def library_interact(f):
Interactive function <function f at ...> with 1 widget
n: IntSlider(value=5, description=u'n', max=15, min=-5)
"""
@sage_wraps(f)
def library_wrapper():
# This will display the interact, no need to return anything
interact(f)
return library_wrapper
# This will display the interact, no need to return anything
interact(f)


def html(obj):
Expand Down
17 changes: 6 additions & 11 deletions src/sage/interfaces/magma.py
Expand Up @@ -216,6 +216,7 @@

import re
import sys
from decorator import decorater

from sage.structure.parent import Parent
from .expect import console, Expect, ExpectElement, ExpectFunction, FunctionElement
Expand Down Expand Up @@ -3002,10 +3003,11 @@ def __exit__(self, typ, value, tb):
"""
self.magma.SetVerbose('Groebner', self.groebner_basis_verbose)


def magma_gb_standard_options(func):
@decorater
def magma_gb_standard_options(func, *args, **kwds):
"""
Decorator to force default options for Magma.
Execute function in ``MagmaGBDefaultContext``.
EXAMPLES::
Expand All @@ -3015,13 +3017,6 @@ def magma_gb_standard_options(func):
sage: "mself" in sage_getsource(J._groebner_basis_magma)
True
"""
from sage.misc.decorators import sage_wraps
with MagmaGBDefaultContext():
return func(*args, **kwds)

@sage_wraps(func)
def wrapper(*args, **kwds):
"""
Execute function in ``MagmaGBDefaultContext``.
"""
with MagmaGBDefaultContext():
return func(*args, **kwds)
return wrapper
16 changes: 7 additions & 9 deletions src/sage/interfaces/singular.py
Expand Up @@ -325,6 +325,7 @@
import sys
import pexpect
from time import sleep
from decorator import decorater

from .expect import Expect, ExpectElement, FunctionElement, ExpectFunction

Expand Down Expand Up @@ -1347,7 +1348,8 @@ def __init__(self, parent, type, value, is_name=False):
2
"""
RingElement.__init__(self, parent)
if parent is None: return
if parent is None:
return
if not is_name:
try:
self._name = parent._create(value, type)
Expand Down Expand Up @@ -2744,8 +2746,8 @@ def __exit__(self, typ, value, tb):
except SingularError:
pass


def singular_gb_standard_options(func):
@decorater
def singular_gb_standard_options(func, *args, **kwds):
r"""
Decorator to force a reduced Singular groebner basis.
Expand Down Expand Up @@ -2777,9 +2779,5 @@ def singular_gb_standard_options(func):
This decorator is used automatically internally so the user
does not need to use it manually.
"""
from sage.misc.decorators import sage_wraps
@sage_wraps(func)
def wrapper(*args, **kwds):
with SingularGBDefaultContext():
return func(*args, **kwds)
return wrapper
with SingularGBDefaultContext():
return func(*args, **kwds)
19 changes: 7 additions & 12 deletions src/sage/libs/giac/__init__.py
Expand Up @@ -30,6 +30,8 @@
# https://www.gnu.org/licenses/
# *****************************************************************************

from decorator import decorater

from sage.structure.proof.all import polynomial as proof_polynomial
from sage.rings.polynomial.multi_polynomial_sequence import PolynomialSequence
from .giac import giacsettings, libgiac
Expand Down Expand Up @@ -84,9 +86,11 @@ def __exit__(self, typ, value, tb):
giacsettings.threads = self.threads


def local_giacsettings(func):
@decorater
def local_giacsettings(func, *args, **kwds):
"""
Decorator to preserve Giac's proba_epsilon and threads settings.
Execute function in ``GiacSettingsDefaultContext``.
EXAMPLES::
Expand All @@ -107,17 +111,8 @@ def local_giacsettings(func):
(True, 2)
"""
from sage.misc.decorators import sage_wraps

@sage_wraps(func)
def wrapper(*args, **kwds):
"""
Execute function in ``GiacSettingsDefaultContext``.
"""
with GiacSettingsDefaultContext():
return func(*args, **kwds)

return wrapper
with GiacSettingsDefaultContext():
return func(*args, **kwds)


@local_giacsettings
Expand Down
16 changes: 6 additions & 10 deletions src/sage/libs/singular/standard_options.py
Expand Up @@ -5,6 +5,7 @@
- Martin Albrecht
"""
from decorator import decorator

class LibSingularGBDefaultContext:
def __init__(self):
Expand Down Expand Up @@ -99,9 +100,11 @@ def __exit__(self, typ, value, tb):
"""
self.libsingular_option_context.__exit__(typ,value,tb)

def libsingular_gb_standard_options(func):
@decorator
def libsingular_gb_standard_options(func, *args, **kwds):
"""
Decorator to force a reduced Singular groebner basis.
Execute function in ``LibSingularGBDefaultContext``.
TESTS::
Expand Down Expand Up @@ -131,12 +134,5 @@ def libsingular_gb_standard_options(func):
This decorator is used automatically internally so the user
does not need to use it manually.
"""
from sage.misc.decorators import sage_wraps
@sage_wraps(func)
def wrapper(*args, **kwds):
"""
Execute function in ``LibSingularGBDefaultContext``.
"""
with LibSingularGBDefaultContext():
return func(*args, **kwds)
return wrapper
with LibSingularGBDefaultContext():
return func(*args, **kwds)
2 changes: 1 addition & 1 deletion src/sage/misc/all.py
Expand Up @@ -164,7 +164,7 @@

from .explain_pickle import explain_pickle, unpickle_newobj, unpickle_global, unpickle_build, unpickle_instantiate, unpickle_persistent, unpickle_extension, unpickle_appends

from .decorators import specialize, sage_wraps, infix_operator
from .decorators import specialize, infix_operator

from .unknown import Unknown, UnknownError

Expand Down
3 changes: 0 additions & 3 deletions src/sage/misc/bindable_class.py
Expand Up @@ -154,9 +154,6 @@ def __classget__(cls, instance, owner):
if instance is None:
return cls
return BoundClass(cls, instance)
# We probably do not need to use sage_wraps, since
# sageinspect already supports partial functions
#return sage_wraps(cls)(BoundClass(cls, instance))

class BoundClass(functools.partial):
"""
Expand Down
22 changes: 11 additions & 11 deletions src/sage/misc/cachefunc.pyx
Expand Up @@ -425,7 +425,7 @@ from sage.misc.sageinspect import sage_getfile, sage_getsourcelines, sage_getarg
from inspect import isfunction

from sage.misc.weak_dict cimport CachedWeakValueDictionary
from sage.misc.decorators import decorator_keywords
from decorator import decorator

cdef frozenset special_method_names = frozenset(['__abs__', '__add__',
'__and__', '__call__', '__cmp__', '__coerce__', '__complex__', '__contains__', '__del__',
Expand Down Expand Up @@ -1243,8 +1243,8 @@ cdef class CachedFunction(object):
for ((args,kwargs), val) in P(arglist2):
self.set_cache(val, *args, **kwargs)


cached_function = decorator_keywords(CachedFunction)
def cached_function(func):
return decorate(func, CachedFunction(func))


cdef class WeakCachedFunction(CachedFunction):
Expand Down Expand Up @@ -1433,7 +1433,8 @@ cdef class WeakCachedFunction(CachedFunction):
self.cache = CachedWeakValueDictionary(**kwds)


weak_cached_function = decorator_keywords(WeakCachedFunction)
def weak_cached_function(func):
return decorate(func, WeakCachedFunction(func))

class CachedMethodPickle(object):
"""
Expand Down Expand Up @@ -2995,8 +2996,8 @@ cdef class CachedSpecialMethod(CachedMethod):
D[name] = Caller
return Caller

@decorator_keywords
def cached_method(f, name=None, key=None, do_pickle=None):
@decorator
def cached_method(f, name=None, key=None, do_pickle=None, *args, **kw):
"""
A decorator for cached methods.
Expand Down Expand Up @@ -3090,9 +3091,8 @@ def cached_method(f, name=None, key=None, do_pickle=None):
"""
cdef str fname = name or f.__name__
if fname in special_method_names:
return CachedSpecialMethod(f, name, key=key, do_pickle=do_pickle)
return CachedMethod(f, name, key=key, do_pickle=do_pickle)

return CachedSpecialMethod(f, name, key=key, do_pickle=do_pickle)(*args, **kw)
return CachedMethod(f, name, key=key, do_pickle=do_pickle)(*args, **kw)

cdef class CachedInParentMethod(CachedMethod):
r"""
Expand Down Expand Up @@ -3304,8 +3304,8 @@ cdef class CachedInParentMethod(CachedMethod):
pass
return Caller


cached_in_parent_method = decorator_keywords(CachedInParentMethod)
def cached_in_parent_method(func):
return decorate(func, CachedInParentMethod(func))


class FileCache(object):
Expand Down

0 comments on commit b414b8e

Please sign in to comment.