[sympycore] r1222 committed - Implemented SUBSCRIPT walk and is_data_ok methods.

1 view
Skip to first unread message

codesite...@google.com

unread,
Oct 20, 2011, 2:51:11 PM10/20/11
to sympycor...@googlegroups.com
Revision: 1222
Author: pearu.peterson
Date: Thu Oct 20 11:50:14 2011
Log: Implemented SUBSCRIPT walk and is_data_ok methods.
http://code.google.com/p/sympycore/source/detail?r=1222

Modified:
/trunk/sympycore/basealgebra/tests/test_verbatim.py
/trunk/sympycore/heads/functional.py

=======================================
--- /trunk/sympycore/basealgebra/tests/test_verbatim.py Tue Oct 18 10:33:50
2011
+++ /trunk/sympycore/basealgebra/tests/test_verbatim.py Thu Oct 20 11:50:14
2011
@@ -259,3 +259,12 @@
assert str(-(a+b)) in '-(a + b)',str(-(a+b))
assert str(-(-a)) in ['a','--a'],str(-(-a))
assert str(-(+a))=='-a',str(-(+a))
+
+def test_subs():
+ a = Verbatim('a')
+ b = Verbatim('b')
+ c = Verbatim('c')
+
+ assert str(a[b].subs(b,c))=='a[c]'
+ assert str(a[b].subs(a,c))=='c[b]'
+ assert str(a[b,c].subs(b,c))=='a[c, c]'
=======================================
--- /trunk/sympycore/heads/functional.py Sun Mar 15 12:15:52 2009
+++ /trunk/sympycore/heads/functional.py Thu Oct 20 11:50:14 2011
@@ -25,6 +25,7 @@
f = f + p1 + ', '.join(l) + p2
return f,o_p

+
class SubscriptHead(FunctionalHead):
"""
SubscriptHead is a head for n-ary subscript operation,
@@ -34,8 +35,39 @@
op_mth = '__getitem__'
parenthesis = '[]'

+ def is_data_ok(self, cls, data):
+ if type(data) is tuple:
+ if len(data)<2:
+ return '%s data part must be a 2-tuple but got %s-tuple' %
(self, len(data))
+ base, items = data
+ if not isinstance(base, cls):
+ return '%s data 1st item must be %s instance but got %s' %
(self, cls, type(a))
+ if type(items) is not tuple:
+ return '%s data 2nd item must be a tuple but got %s' %
(self, type(items))
+ for i,a in enumerate(items):
+ if not isinstance(a, cls):
+ return '%s data 2nd items %sth item must be %s
instance but got %s' % (self, i, cls, type(a))
+ else:
+ return '%s data part must be a tuple but got %s' % (self,
type(data))
+
def __repr__(self): return 'SUBSCRIPT'

+ def walk(self, func, cls, (base, items), target):
+ new_base = base.head.walk(func, cls, base.data, base)
+ flag = new_base is not base
+ new_items = []
+ for op in items:
+ o = op.head.walk(func, cls, op.data, op)
+ if op is not o:
+ flag = True
+ new_items.append(o)
+ if flag:
+ r = self.new(cls, (new_base, tuple(new_items)))
+ return func(cls, r.head, r.data, r)
+ return func(cls, self, (base, items), target)
+
+
+
class SliceHead(Head):
"""
SliceHead is a head for 3-ary slice operation,
@@ -79,6 +111,7 @@
else: r = '%s:%s:%s' % (start, stop, step)
return r, slice_p

+
def __repr__(self): return 'SLICE'

class LambdaHead(Head):

Reply all
Reply to author
Forward
0 new messages