[appengine-afterburner] r23 committed - Adding support for with statement.

0 views
Skip to first unread message

appengine-...@googlecode.com

unread,
Mar 15, 2011, 2:46:52 PM3/15/11
to appengine-after...@googlegroups.com
Revision: 23
Author: mike.a...@gmail.com
Date: Tue Mar 15 11:46:09 2011
Log: Adding support for with statement.


http://code.google.com/p/appengine-afterburner/source/detail?r=23

Modified:
/trunk/python/build.sh
/trunk/python/src/afterburner/experimental/quota.py
/trunk/python/src/afterburner/experimental/quota_test.py

=======================================
--- /trunk/python/build.sh Wed Dec 15 17:03:55 2010
+++ /trunk/python/build.sh Tue Mar 15 11:46:09 2011
@@ -23,7 +23,7 @@

export PYTHONPATH="\
$APPENGINE_SDK:\
-$APPENGINE_SDK/lib/django:\
+$APPENGINE_SDK/lib/django_1_2/django:\
$APPENGINE_SDK/lib/fancy_urllib:\
$APPENGINE_SDK/lib/webob:\
$APPENGINE_SDK/lib/yaml/lib:\
@@ -35,7 +35,7 @@
test () {
exit_status=0
for t in $(find "$dir/src" -name "*test.py"); do
- if python $t
+ if python2.5 $t
then
echo "PASSED"
else
@@ -55,9 +55,8 @@
}

doc () {
- cd $dir/doc
- echo `pwd`
- make html
+ rm -rf doc &&
+ sphinx-build -b html src doc
}

case "$1" in
@@ -68,6 +67,6 @@
doc
;;
*)
- echo $"Usage: $0 {test}"
+ echo "Usage: $0 {test}"
exit 1
esac
=======================================
--- /trunk/python/src/afterburner/experimental/quota.py Tue Mar 15 08:16:34
2011
+++ /trunk/python/src/afterburner/experimental/quota.py Tue Mar 15 11:46:09
2011
@@ -14,7 +14,25 @@
# See the License for the specific language governing permissions and
# limitations under the License.

-"""Contains simple quota system for rate-limiting arbitrary operations."""
+"""Simple quota system for rate-limiting arbitrary operations.
+
+Quota is managed by buckets. Each bucket contains a 32-bit int value of
+available quota. Buckets should be refilled manually with 'put' method.
+It is safe to use a single bucket from multiple clients simultaneously.
+
+Usage sample::
+
+ quota_manager = quota.QuotaManager()
+ quota_manager.put("my_bucket", 1000)
+ with quota_manager.consumer("my_bucket", 100) as consumer:
+ consumer.consume(10)
+ if not consumer.check(20):
+ return
+
+
+WARNING: Since memcache storage is not reliable, this quota system is not
+reliable and is best effort only.
+"""

__author__ = 'mike.a...@gmail.com (Mike Aizatsky)'

@@ -28,17 +46,9 @@
# operate only with unsigned values.
_OFFSET = 2**32

+
class QuotaManager(object):
- """Simple quota system manager, backed by memcache storage.
-
- Since memcache storage is not reliable, this quota system is not
reliable and
- best effort only.
-
- Quota is managed by buckets. Each bucket contains a 32-bit int value of
- available quota. Buckets should be refilled manually with 'put' method.
-
- It is safe to use a single bucket from multiple clients simultaneously.
- """
+ """Simple quota system manager, backed by memcache storage."""

def __init__(self, memcache_client=memcache.Client()):
"""Initialize new instance.
@@ -110,6 +120,9 @@
self.memcache_client.set(bucket, amount + _OFFSET,
namespace=_QUOTA_NAMESPACE)

+ def consumer(self, bucket, batch_size):
+ return QuotaConsumer(self, bucket, batch_size)
+

class QuotaConsumer(object):
"""Quota consumer wrapper for efficient quota consuming/reclaiming.
@@ -184,3 +197,11 @@
This method has to be called for quota consistency!
"""
self.quota_manager.put(self.bucket, self.quota)
+
+ def __enter__(self):
+ return self
+
+ def __exit__(self, type, value, traceback):
+ self.dispose()
+
+
=======================================
--- /trunk/python/src/afterburner/experimental/quota_test.py Mon Mar 14
17:50:48 2011
+++ /trunk/python/src/afterburner/experimental/quota_test.py Tue Mar 15
11:46:09 2011
@@ -13,6 +13,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

+from __future__ import with_statement
+
__author__ = 'mike.a...@gmail.com (Mike Aizatsky)'

import unittest
@@ -148,6 +150,16 @@
self.assertTrue(self.consumer.check(75))
self.assertFalse(self.consumer.check(100))

+ def testWithStatement(self):
+ """Test with statement interaction."""
+ self.quota_manager.put("foo", 100)
+
+ with self.quota_manager.consumer("foo", 50) as consumer:
+ self.assertTrue(consumer.consume(1))
+ self.assertEquals(50, self.quota_manager.get("foo"))
+
+ self.assertEquals(99, self.quota_manager.get("foo"))
+

if __name__ == '__main__':
unittest.main()

Reply all
Reply to author
Forward
0 new messages