Author: jsdelfino
Date: Thu May 13 05:28:54 2010
New Revision: 943806
URL: http://svn.apache.org/viewvc?rev=943806&view=rev
Log:
Add simple implementation of Scheme-like lazy streams.
Added:
tuscany/sca-cpp/trunk/modules/wsgi/stream-test.py
- copied, changed from r943805,
tuscany/sca-cpp/trunk/modules/wsgi/util-test
Modified:
tuscany/sca-cpp/trunk/modules/wsgi/util-test
tuscany/sca-cpp/trunk/modules/wsgi/util.py
Copied: tuscany/sca-cpp/trunk/modules/wsgi/stream-test.py (from r943805,
tuscany/sca-cpp/trunk/modules/wsgi/util-test)
URL:
http://svn.apache.org/viewvc/tuscany/sca-cpp/trunk/modules/wsgi/stream-test.py?p2=tuscany/sca-cpp/trunk/modules/wsgi/stream-test.py&p1=tuscany/sca-cpp/trunk/modules/wsgi/util-test&r1=943805&r2=943806&rev=943806&view=diff
==============================================================================
--- tuscany/sca-cpp/trunk/modules/wsgi/util-test (original)
+++ tuscany/sca-cpp/trunk/modules/wsgi/stream-test.py Thu May 13 05:28:54 2010
@@ -1,5 +1,4 @@
-#!/bin/sh
-
+#!/usr/bin/python
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
@@ -17,19 +16,32 @@
# specific language governing permissions and limitations
# under the License.
-# Run Python util test cases
-here=`readlink -f $0`; here=`dirname $here`
-python_prefix=`cat $here/../python/python.prefix`
-
-$python_prefix/bin/python xml-test.py
-rc=$?
-if [ "$rc" = "0" ]; then
- $python_prefix/bin/python atom-test.py
- rc=$?
-fi
-if [ "$rc" = "0" ]; then
- $python_prefix/bin/python json-test.py
- rc=$?
-fi
+# Test stream functions
+
+import unittest
+from util import *
+
+def testStream():
+
+ s = cons_stream(0, lambda: cons_stream(1, lambda: cons(2, ())))
+ assert len(s) == 3
+ assert car(s) == 0
+ assert cadr(s) == 1
+ assert len(cdr(s)) == 2
+ assert s[0] == 0
+ assert s[1] == 1
+ assert s[2] == 2
+ assert s[:1] == (0, 1)
+ assert s[:5] == (0, 1, 2)
+ assert s[2:5] == (2,)
+ assert s[4:5] == ()
+ assert s[0:] == (0, 1, 2)
+ assert (0, 1, 2) == s[0:]
+
+ return True
+
+if __name__ == "__main__":
+ print "Testing..."
+ testStream()
+ print "OK"
-return $rc
Modified: tuscany/sca-cpp/trunk/modules/wsgi/util-test
URL:
http://svn.apache.org/viewvc/tuscany/sca-cpp/trunk/modules/wsgi/util-test?rev=943806&r1=943805&r2=943806&view=diff
==============================================================================
--- tuscany/sca-cpp/trunk/modules/wsgi/util-test (original)
+++ tuscany/sca-cpp/trunk/modules/wsgi/util-test Thu May 13 05:28:54 2010
@@ -21,9 +21,13 @@
here=`readlink -f $0`; here=`dirname $here`
python_prefix=`cat $here/../python/python.prefix`
-$python_prefix/bin/python xml-test.py
+$python_prefix/bin/python stream-test.py
rc=$?
if [ "$rc" = "0" ]; then
+ $python_prefix/bin/python xml-test.py
+ rc=$?
+fi
+if [ "$rc" = "0" ]; then
$python_prefix/bin/python atom-test.py
rc=$?
fi
Modified: tuscany/sca-cpp/trunk/modules/wsgi/util.py
URL:
http://svn.apache.org/viewvc/tuscany/sca-cpp/trunk/modules/wsgi/util.py?rev=943806&r1=943805&r2=943806&view=diff
==============================================================================
--- tuscany/sca-cpp/trunk/modules/wsgi/util.py (original)
+++ tuscany/sca-cpp/trunk/modules/wsgi/util.py Thu May 13 05:28:54 2010
@@ -16,6 +16,7 @@
# under the License.
# Simple utility functions
+from sys import maxint
# Scheme-like lists
def cons(a, b):
@@ -23,10 +24,16 @@ def cons(a, b):
def car(l):
return l[0]
+
+def first(l):
+ return car(l)
def cdr(l):
return l[1:]
+def rest(l):
+ return cdr(l)
+
def cadr(l):
return car(cdr(l))
@@ -36,12 +43,17 @@ def cddr(l):
def caddr(l):
return car(cddr(l))
+def append(a, b):
+ return a + b
+
def reverse(l):
r = list(l)
r.reverse()
return tuple(r)
def isNil(l):
+ if isinstance(l, streampair):
+ return l.isNil()
return l == ()
def isSymbol(v):
@@ -57,6 +69,56 @@ def isList(v):
def isTaggedList(v, t):
return isList(v) and not isNil(v) and car(v) == t
+
+# Scheme-like streams
+class streampair(object):
+ def __init__(self, car, cdr):
+ self.car = car
+ self.cdr = cdr
+
+ def __repr__(self):
+ return repr(self[0:len(self)])
+
+ def isNil(self):
+ return self.cdr == ()
+
+ def __len__(self):
+ if self.cdr == ():
+ return 0
+ return 1 + len(self.cdr())
+
+ def __getitem__(self, i):
+ if i == 0:
+ return self.car
+ return self.cdr()[i - 1]
+
+ def __getslice__(self, i, j):
+ if isNil(self):
+ return ()
+ if i > 0:
+ if j == maxint:
+ return self.cdr()[i - 1: j]
+ return self.cdr()[i - 1: j - 1]
+ if j == maxint:
+ return self
+ if j == 0:
+ return (self.car,)
+ return (self.car,) + self.cdr()[: j - 1]
+
+ def __eq__(self, other):
+ sl = len(self)
+ ol = len(other)
+ if sl != ol:
+ return False
+ return self[0: sl] == other[0: ol]
+
+ def __ne__(self, other):
+ return not self.__eq__(other)
+
+def cons_stream(car, cdr):
+ return streampair(car, cdr)
+
+
# Scheme-like associations
def assoc(k, l):
if l == ():