Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
""" Replacement rules. """
from __future__ import print_function, division
class Transform(object): """ Immutable mapping that can be used as a generic transformation rule.
Parameters ---------- transform : callable Computes the value corresponding to any key. filter : callable, optional If supplied, specifies which objects are in the mapping.
Examples ========
>>> from sympy.core.rules import Transform >>> from sympy.abc import x
This Transform will return, as a value, one more than the key:
>>> add1 = Transform(lambda x: x + 1) >>> add1[1] 2 >>> add1[x] x + 1
By default, all values are considered to be in the dictionary. If a filter is supplied, only the objects for which it returns True are considered as being in the dictionary:
>>> add1_odd = Transform(lambda x: x + 1, lambda x: x%2 == 1) >>> 2 in add1_odd False >>> add1_odd.get(2, 0) 0 >>> 3 in add1_odd True >>> add1_odd[3] 4 >>> add1_odd.get(3, 0) 4 """
def __init__(self, transform, filter=lambda x: True):
def __contains__(self, item):
def __getitem__(self, key): else: raise KeyError(key)
def get(self, item, default=None): return self[item] else: |