本文整理汇总了Python中sage.misc.function_mangling.ArgumentFixer.fix_to_named方法的典型用法代码示例。如果您正苦于以下问题:Python ArgumentFixer.fix_to_named方法的具体用法?Python ArgumentFixer.fix_to_named怎么用?Python ArgumentFixer.fix_to_named使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sage.misc.function_mangling.ArgumentFixer
的用法示例。
在下文中一共展示了ArgumentFixer.fix_to_named方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: EnumeratedSetFromIterator_method_caller
# 需要导入模块: from sage.misc.function_mangling import ArgumentFixer [as 别名]
# 或者: from sage.misc.function_mangling.ArgumentFixer import fix_to_named [as 别名]
class EnumeratedSetFromIterator_method_caller(Decorator):
r"""
Caller for decorated method in class.
INPUT:
- ``inst`` -- an instance of a class
- ``f`` -- a method of a class of ``inst`` (and not of the instance itself)
- ``name`` -- optional -- either a string (which may contains substitution
rules from argument or a function args,kwds -> string.
- ``options`` -- any option accepted by :class:`EnumeratedSetFromIterator`
"""
def __init__(self, inst, f, name=None, **options):
r"""
Initialize ``self``.
TESTS::
sage: from sage.sets.set_from_iterator import DummyExampleForPicklingTest
sage: d = DummyExampleForPicklingTest()
sage: d.f()
{10, 11, 12, 13, 14, ...}
It is possible to pickle/unpickle the class and the instance::
sage: loads(dumps(DummyExampleForPicklingTest))().f()
{10, 11, 12, 13, 14, ...}
sage: loads(dumps(d)).f()
{10, 11, 12, 13, 14, ...}
But not the enumerated set::
sage: loads(dumps(d.f()))
Traceback (most recent call last):
...
PicklingError: Can't pickle <type 'function'>: attribute lookup __builtin__.function failed
"""
self.inst = inst
self.f = f
self.af = ArgumentFixer(self.f)
if hasattr(f, "__name__"):
self.__name__ = f.__name__
else:
self.__name__ = f.__name__
self.__module__ = f.__module__
self.name = name
self.options = options
def __call__(self,*args,**kwds):
r"""
Returns an instance of :class:`EnumeratedSetFromIterator` with
proper argument.
TESTS::
sage: from sage.sets.set_from_iterator import set_from_method
sage: class A:
... @set_from_method(name = lambda self,n: str(self)*n)
... def f(self,n):
... return xsrange(n)
... def __repr__(self):
... return "A"
sage: a = A()
sage: a.f(3) # indirect doctest
AAA
sage: A.f(a,3) # indirect doctest
AAA
sage: [x for x in a.f(6)] # indirect doctest
[0, 1, 2, 3, 4, 5]
"""
if self.inst is not None:
args = (self.inst,) + args
if self.name:
if isinstance(self.name,str):
aa,kk = self.af.fix_to_named(*args,**kwds)
name = self.name%dict(kk)
else:
name = self.name(*args, **kwds)
return EnumeratedSetFromIterator(self.f, args, kwds, name, **self.options)
return EnumeratedSetFromIterator(self.f, args, kwds, **self.options)
def __get__(self, inst, cls):
r"""
Get a :class:`EnumeratedSetFromIterator_method_caller` bound to a
specific instance of the class of the cached method.
.. NOTE::
:class:`EnumeratedSetFromIterator_method_caller` has a separate
``__get__`` because of the special behavior of category framework
for element classes which are not of extension type (see
:meth:`sage.structure.element.Element.__get__`).
TESTS::
sage: from sage.sets.set_from_iterator import set_from_method
#.........这里部分代码省略.........
示例2: EnumeratedSetFromIterator_function_decorator
# 需要导入模块: from sage.misc.function_mangling import ArgumentFixer [as 别名]
# 或者: from sage.misc.function_mangling.ArgumentFixer import fix_to_named [as 别名]
#.........这里部分代码省略.........
True
An example which mixes together ``set_from_function`` and
``cached_method``::
sage: @cached_function
... @set_from_function(
... name = "Graphs on %(n)d vertices",
... category = FiniteEnumeratedSets(),
... cache = True)
... def Graphs(n): return graphs(n)
sage: Graphs(10)
Graphs on 10 vertices
sage: Graphs(10).unrank(0)
Graph on 10 vertices
sage: Graphs(10) is Graphs(10)
True
The ``cached_function`` must go first::
sage: @set_from_function(
... name = "Graphs on %(n)d vertices",
... category = FiniteEnumeratedSets(),
... cache = True)
... @cached_function
... def Graphs(n): return graphs(n)
sage: Graphs(10)
Graphs on 10 vertices
sage: Graphs(10).unrank(0)
Graph on 10 vertices
sage: Graphs(10) is Graphs(10)
False
"""
def __init__(self, f=None, name=None, **options):
r"""
Initialize ``self``.
TESTS::
sage: from sage.sets.set_from_iterator import set_from_function
sage: F = set_from_function(category=FiniteEnumeratedSets())(xsrange)
sage: TestSuite(F(100)).run()
sage: TestSuite(F(1,5,2)).run()
sage: TestSuite(F(0)).run()
"""
if f is not None:
self.f = f
if hasattr(f, "__name__"):
self.__name__ = f.__name__
else:
self.__name__ = f.__name__
self.__module__ = f.__module__
self.af = ArgumentFixer(f)
if name is not None:
self.name = name
self.options = options
def __call__(self, *args, **kwds):
r"""
Build a new :class:`EnumeratedSet` by calling ``self.f`` with
apropriate argument. If ``f`` is ``None``, then returns a new instance
of :class:`EnumeratedSetFromIterator`.
EXAMPLES::
sage: from sage.sets.set_from_iterator import set_from_function
sage: F = set_from_function(category=FiniteEnumeratedSets())(xsrange)
sage: F(3)
{0, 1, 2}
sage: F(end=7,start=3)
{3, 4, 5, 6}
sage: F(10).cardinality()
10
"""
options = self.options
if hasattr(self, 'f'): # yet initialized
if hasattr(self,'name'):
if isinstance(self.name,str):
if args or kwds:
_,kk = self.af.fix_to_named(*args,**kwds)
name = self.name%dict(kk)
else:
name = self.name
else:
name = self.name(*args,**kwds)
return EnumeratedSetFromIterator(self.f, args, kwds, name=name, **self.options)
return EnumeratedSetFromIterator(self.f, args, kwds, **self.options)
else: # potential global options
if args == ():
assert len(kwds.keys()) == 1
f = kwds.values()[0]
else:
assert len(args) == 1
f = args[0]
return EnumeratedSetFromIterator_function_decorator(
f,
name=getattr(self,'name',None),
**self.options)