Return old behaviours
- Better documented List - Otherwise not binded with cast
This commit is contained in:
1 parent
94461724f6
commit
380b491724
1 file changed
+23
-19
+23
-19
@@ -22,7 +22,8 @@ import gluon.portalocker as portalocker
|
|||||||
__all__ = ['List', 'Storage', 'Settings', 'Messages',
|
__all__ = ['List', 'Storage', 'Settings', 'Messages',
|
||||||
'StorageList', 'load_storage', 'save_storage']
|
'StorageList', 'load_storage', 'save_storage']
|
||||||
|
|
||||||
DEFAULT = lambda: 0
|
def DEFAULT():
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
class Storage(dict):
|
class Storage(dict):
|
||||||
@@ -271,28 +272,38 @@ 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 callable.
|
||||||
instead of `IndexOutOfBounds`.
|
When a(i) is called if i is out of bounds returns None
|
||||||
|
instead of `IndexError`.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __call__(self, i, default=DEFAULT, cast=None, otherwise=None):
|
def __call__(self, i, default=DEFAULT, 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
|
||||||
validity
|
`request.args()` validity.
|
||||||
Args:
|
:params:
|
||||||
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:
|
||||||
- None: results in a 404
|
will be executed when:
|
||||||
- str: redirect to this address
|
- casts fail
|
||||||
- callable: calls the function (nothing is passed)
|
- value not found, dont have default and otherwise is
|
||||||
|
especified
|
||||||
|
can be:
|
||||||
|
- None: results in a 404
|
||||||
|
- str: redirect to this address
|
||||||
|
- 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]
|
n = len(self)
|
||||||
if not value and default is not DEFAULT:
|
if 0 <= i < n or -n <= i < 0:
|
||||||
|
value = self[i]
|
||||||
|
elif default is DEFAULT:
|
||||||
|
value = None
|
||||||
|
else:
|
||||||
value, cast, otherwise = default, False, False
|
value, cast, otherwise = default, False, False
|
||||||
try:
|
try:
|
||||||
if cast:
|
if cast:
|
||||||
@@ -311,13 +322,6 @@ class List(list):
|
|||||||
raise RuntimeError("invalid otherwise")
|
raise RuntimeError("invalid otherwise")
|
||||||
return value
|
return value
|
||||||
|
|
||||||
def __getitem__(self, i):
|
|
||||||
n = len(self)
|
|
||||||
if 0 <= i < n or -n <= i < 0:
|
|
||||||
return super(List, self).__getitem__(i)
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
import doctest
|
import doctest
|
||||||
doctest.testmod()
|
doctest.testmod()
|
||||||
Reference in new issue
Block a user