Merge pull request #950 from cassiobotaro/master

Fix List behaviour and added new feature
This commit is contained in:
mdipierro
2015-05-03 10:27:46 -05:00
parent 1394942feb
commit 1bb4117cbd
2 changed files with 28 additions and 26 deletions
+24 -26
View File
@@ -269,53 +269,51 @@ class FastStorage(dict):
class List(list): class List(list):
""" """
Like a regular python list but a[i] if i is out of bounds returns None Like a regular python list but a[i] if i is out of bounds returns None
instead of `IndexOutOfBounds` instead of `IndexOutOfBounds`.
""" """
def __call__(self, i, default=DEFAULT, cast=None, otherwise=None): def __call__(self, i, default=None, cast=None, otherwise=None):
"""Allows to use a special syntax for fast-check of `request.args()` """Allows to use a special syntax for fast-check of `request.args()`
validity validity
Args: Args:
i: index i: index
default: use this value if arg not found default: use this value if arg not found
cast: type cast cast: type cast
otherwise: can be: otherwise: can be:
- None: results in a 404 - None: results in a 404
- str: redirect to this address - str: redirect to this address
- callable: calls the function (nothing is passed) - callable: calls the function (nothing is passed)
Example: Example:
You can use:: You can use::
request.args(0,default=0,cast=int,otherwise='http://error_url') request.args(0,default=0,cast=int,otherwise='http://error_url')
request.args(0,default=0,cast=int,otherwise=lambda:...) request.args(0,default=0,cast=int,otherwise=lambda:...)
""" """
value = self[i] or default
try:
if cast:
value = cast(value)
if not value and otherwise:
raise ValueError('Otherwise will raised.')
except (ValueError, TypeError):
from http import HTTP, redirect
if otherwise is None:
raise HTTP(404)
elif isinstance(otherwise, str):
redirect(otherwise)
elif callable(otherwise):
return otherwise()
else:
raise RuntimeError("invalid otherwise")
return value
def __getitem__(self, i):
n = len(self) n = len(self)
if 0 <= i < n or -n <= i < 0: if 0 <= i < n or -n <= i < 0:
value = self[i] return super(List, self).__getitem__(i)
elif default is DEFAULT: return None
value = None
else:
value, cast = default, False
if cast:
try:
value = cast(value)
except (ValueError, TypeError):
from http import HTTP, redirect
if otherwise is None:
raise HTTP(404)
elif isinstance(otherwise, str):
redirect(otherwise)
elif callable(otherwise):
return otherwise()
else:
raise RuntimeError("invalid otherwise")
return value
if __name__ == '__main__': if __name__ == '__main__':
+4
View File
@@ -125,8 +125,12 @@ class TestList(unittest.TestCase):
def test_listcall(self): def test_listcall(self):
a = List((1, 2, 3)) a = List((1, 2, 3))
self.assertEqual(a(1), 2) self.assertEqual(a(1), 2)
self.assertEqual(a[1], 2)
self.assertEqual(a(3), None)
self.assertEqual(a[3], None)
self.assertEqual(a(-1), 3) self.assertEqual(a(-1), 3)
self.assertEqual(a(-5), None) self.assertEqual(a(-5), None)
self.assertEqual(a[-5], None)
self.assertEqual(a(-5, default='x'), 'x') self.assertEqual(a(-5, default='x'), 'x')
self.assertEqual(a(-3, cast=str), '1') self.assertEqual(a(-3, cast=str), '1')
a.append('1234') a.append('1234')